前言

服务端微信小程序登录流程.

1.微信小程序获取token


    @Value("${wechat.mini.appid}")
    private String wechatAppId;
    @Value("${wechat.mini.secret}")
    private String wechatSecret;

    private String getWechatToken () {
        log.info("微信小程序获取token");
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wechatAppId +"&secret=" + wechatSecret;
        try {
            String rest = HttpRequestUtil.get(url);
            WeChatToken weChatToken = JSON.parseObject(rest, WeChatToken.class);
            if (weChatToken.getErrcode() == null || weChatToken.getErrcode().equals("0")) {
                return weChatToken.getAccess_token();
            }
            log.error("微信小程序获取token失败" + weChatToken.getErrmsg());
            return null;
        } catch (Exception e) {
            log.error("微信小程序获取token失败" + e.getMessage());
            return null;
        }
    }

2.获取手机号

    // 前端通过code换取用户手机号
    // 每个code只能使用一次,code的有效期为5分钟
    @GetMapping("/getPhoneNumber")
    public Result<Authority> getPhoneNumber(@RequestParam("code") String code) {
        log.info("微信小程序获取手机号:" + code);
        String wechatToken = getWechatToken();
        if (StringUtils.isEmpty(wechatToken)) {
            return Result.error("服务异常,请重试");
        }
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" +  wechatToken;
        try {
            HashMap<String, String> map = new HashMap<>();
            map.put("code", code);
            String rest = HttpRequestUtil.post(url, JSON.toJSONString(map));

            WeChatSession weChatSession = JSON.parseObject(rest, WeChatSession.class);
            if (weChatSession.getErrcode() == null || weChatSession.getErrcode().equals("0")) {
                String phone_info = weChatSession.getPhone_info();
                WeChatPhoneInfo phoneInfo = JSON.parseObject(phone_info, WeChatPhoneInfo.class);
                return Result.success(phoneInfo);
            }
            return Result.error(weChatSession.getErrmsg());
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error("服务异常,请重试");
        }
    }
@Data
public class WeChatSession {
    String openid;
    String session_key;
    String unionid;
    String errcode;
    String errmsg;
    String phone_info;

}

3.小程序登录授权

// 前端通过code获取微信session信息
    @GetMapping("/code2Session")
    public Result<String> code2Session(@RequestParam("code") String code) {

        log.info("微信小程序登录:" + code);
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wechatAppId + "&secret=" + wechatSecret + "&js_code=" + code + "&grant_type=authorization_code";
        try {
            String rest = HttpRequestUtil.get(url);
            WeChatSession weChatSession = JSON.parseObject(rest, WeChatSession.class);
            if (weChatSession.getErrcode() == null || weChatSession.getErrcode().equals("0")) {
                return Result.success(weChatSession);
            }
            return Result.error(weChatSession.getErrmsg());
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error("服务异常,请重试");
        }
    }

4.微信小程序用户登录

    // 校验用户是否注册,是否绑定
    // 未注册用户先注册
    // 未绑定用户
    @PostMapping("/login")
    public Result<Authority> login(@RequestParam(name = "appId") String appId, @RequestParam(name = "openId") String openId, @RequestParam(name = "mobile") String mobile) {
        return loginFacadeService.loginByOpenId(appId, openId, mobile);
    }

5.首次登录小程序的用户进行绑定

// 首次登录小程序的用户记录openId,下次使用openId直接登陆
    @PostMapping("/bind")
    public Result<Authority> bind(@RequestBody @Valid WeChatBind weChatBind) {
        return loginFacadeService.bindOpenId(weChatBind.getAppId(), weChatBind.getMobile(), weChatBind.getPswd(), weChatBind.getOpenId());
    }

更多推荐