Develop wechat applet from scratch (4): after wechat applet binds system account and authorizes login

Keywords: Java SpringBoot Apache

1. Background development environment:

Language: java

Framework: springboot

2. Code example:

package com.zc.wechat.web;

import com.zc.common.api.util.Result;
import com.zc.wechat.model.Token;
import com.zc.wechat.model.app.Jscode2sessionResult;
import com.zc.wechat.service.WechatAppService;
import com.zc.wechat.service.WechatServerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Controller
@RequestMapping("/wechat")
public class WechatController {

    @Value("${wechat.appid}")
    private String appid;

    @Autowired
    private WechatServerService wechatServerService;
    @Autowired
    private WechatAppService wechatAppService;

    /**
     * Get the token of the login system
     *
     * @return
     */
    @RequestMapping(value = "/user/getToken", method = {RequestMethod.GET})
    @ResponseBody
    public Result<Token> getToken(@RequestParam(value = "code") String code) throws IOException {
        final String appId = appid;
        Jscode2sessionResult openidResult = wechatAppService.getJscode2session(appId, code, "authorization_code");
        Result<Token> result = new Result();
        Token token = wechatServerService.getTokenByOpenid(openidResult.getOpenid());
        result.setResult(token);
        return result;
    }

    @RequestMapping(value = "/user/bind", method = {RequestMethod.POST})
    @ResponseBody
    public Result<Token> bindUser(@RequestParam(value = "userName") String userName,
                                  @RequestParam(value = "password") String password,
                                  @RequestParam(value = "state", required = false) String state,
                                  @RequestParam(value = "code") String code) {
        Result<Token> result = new Result();
        if(!wechatServerService.checkUser(userName, password)){
            result.setStatus(Result.STATUS_FAIL);
            return result;
        }
        final String appId = appid;
        Jscode2sessionResult openidResult = wechatAppService.getJscode2session(appId, code, "authorization_code");
        String openid= openidResult.getOpenid();
        if(StringUtils.isEmpty(openid)){
            result.setStatus(Result.STATUS_FAIL);
            return result;
        }
        if(!wechatServerService.bindUser(userName, openid)){
            result.setStatus(Result.STATUS_FAIL);
            return result;
        }
        result.setResult(wechatServerService.getTokenByOpenid(openid));
        return result;
    }
}

Note: the bindUser method obtains the parameters from the small program side, where userName and password are the user name and password of their own system, and code is the code of the wechat applet mentioned in the previous post. The openid is obtained through the wechat API interface and bound with their own system. The getToken method is to get the user token of auth2. After the user enters the applet, he will go to get the token. If not, he will jump to the binding page. The user's other requests must be accompanied by a token, so that the login user can be judged.

Posted by chetanrakesh on Wed, 15 Apr 2020 10:46:26 -0700