springboot框架 后台获取微信小程序用户的openid

小程序端app.js中:

// 登录
    wx.login({
      success: res => {        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          wx.request({
            url: 'http://localhost:84/user/login',
            method: 'POST',           
             data: {
              code: res.code
            },            
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(res) {
              console.log("openid:"+res.data.openid);             
              if (res.data.openid != "" || res.data.openid!=null){                // 登录成功
                wx.setStorageSync("openid", res.data.openid);//将用户id保存到缓存中
                wx.setStorageSync("session_key", res.data.session_key);//将session_key保存到缓存中
              }else{                // 登录失败
                // TODO 跳转到错误页面,要求用户重试


                return false;
              }
            }
          })
        } else {
          console.log('获取用户登录态失败!' + res.errMsg)
        }
      }
    })


java端:

@RestController
public class LoginController  extends BaseController {
    @Autowired
    private SysUserMapper sysUserMapper;
    @PostMapping("/user/login")
    @ResponseBody
    public String login(@RequestHeader("token") String token,@RequestParam("code") String code){
        String appID = "wx8786*******41b1c";
        String appSecret = "7ab8d23ec**********3070ecf71afcd";
        String loginname = JwtUtil.getUsername(token);

        Map<String,Object> map = new HashMap<String,Object>();
        String result = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session?appid=" + appID + "&secret="
                        + appSecret + "&js_code=" + code+ "&grant_type=authorization_code"
                ,map);
        ObjectMapper mapper = new ObjectMapper();
        OpenIdJson openIdJson = null;
        try {
            openIdJson = mapper.readValue(result, OpenIdJson.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(result.toString());
        System.out.println(openIdJson.getOpenid());
        SysUser sysUser = new SysUser();
        sysUser.setOpenid(openIdJson.getOpenid());
        sysUser.setLoginName(loginname);
        sysUserMapper.updateUserByUsername(sysUser);
        return result;

    }
}

上面的HttpUtil.get()方法引入了工具包

<!-- 调用Api工具类 -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>4.1.0</version>
</dependency>

OpenIdJson的实体类:

public class OpenIdJson {
    private String openid;
    private String session_key;

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public String getSession_key() {
        return session_key;
    }

    public void setSession_key(String session_key) {
        this.session_key = session_key;
    }
}


本文由站长原创或收集,不代表本站立场,如若转载,请注明出处:http://www.swzhinan.com/post/286.html

本文 暂无 评论

回复给

欢迎点评

联系我们

站长QQ:384827360

站长邮件:384827360@qq.com

工作时间:周一至周五,9:30-15:30,节假日休息

QR code