Develop WeChat applet from scratch (4): WeChat applet binds the system account and authorizes login to the backend

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;

    /**
     * 获取登录系统的token
     *
     * @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;
    }
}

Description: The bindUser method obtains the parameters passed from the applet, where userName and password are the user name and password of its own system, code is the code of the WeChat applet mentioned in the previous post, and the openid is obtained through the WeChat API interface. Bind to its own system. The getToken method is to get the user token of auth2. After the user enters the applet, he will get the token. If not, jump to the binding page. The user's other requests must bring the token, so that the logged-in user can be judged.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325207152&siteId=291194637