從零開始開發微信小程序(四):微信小程序綁定系統帳號並受權登陸以後臺端

1. 後臺開發環境:java

    語言:javaweb

    框架:springboot spring

 2. 代碼示例:apache

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;
    }
}

說明:bindUser方法獲取從小程序端傳過來的參數,其中userName和password是自身系統的用戶名密碼,code是上一篇帖子裏提到的微信小程序的code,經過微信的API接口拿到openid,和自身的系統進行綁定。getToken方法是獲取auth2的用戶token,之後用戶進入小程序後,去拿token,沒有的話跳轉到綁定頁面。用戶的其餘請求都要帶上token,這樣就能判斷登陸用戶了。小程序

相關文章
相關標籤/搜索