微信小程序后端开发流程
发布时间:2020-12-14 19:05:18 所属栏目:资源 来源:网络整理
导读:1、前端调用 wx.login 返回了code,然后调用wx.getUserInfo获取到用户的昵称 头像 2、服务端根据code去微信获取openid, 接口地址:?developers.weixin.qq.com/miniprogram… 微信小程序后端接口开发 controller层 public class OauthController { @Autowire
1、前端调用 wx.login 返回了code,然后调用wx.getUserInfo获取到用户的昵称 头像 2、服务端根据code去微信获取openid, 接口地址:?developers.weixin.qq.com/miniprogram… 微信小程序后端接口开发controller层 public class OauthController {
@Autowired
private WeChatService weChatService;
/**
* 微信授权用js_code换取openId
* @param code
* @return
*/
@GetMapping("/code2Session")
public BaseResponse code2Session(String code) {
log.info("code2Session,code={}",code);
if (StringUtil.isEmpty(code)) {
return BaseResponse.buildFail("参数异常");
}
Code2SessionResponse res = weChatService.code2Session(code);
log.info(,res);
if (!res.isSuccess()) {
return BaseResponse.buildFail(res.getErrCode(),res.getErrMsg());
}
return BaseResponse.buildSuccess(res);
}
/**
* 解密获取手机号
* @param request
* @param response
* @param param
* @return
*/
public BaseResponse decryptGetPhone(HttpServletRequest request,HttpServletResponse response,@RequestBody OauthParam param) {
if (!StringUtil.isEmpty(param.getOpenId())) {//微信授权登录
String sessionKey = weChatService.getSessionKey(param.getOpenId());
if (StringUtil.isEmpty(sessionKey)) {
"会话不存在");
}
Sha1Utils sha = new Sha1Utils();
// 获取用户信息
log.debug("微信登陆 sessionKey = {}",sessionKey);
String userInfoStr = sha.decryptWXAppletInfo(sessionKey,param.getEncryptedData(),param.getIv());
if (StringUtil.isEmpty(userInfoStr)) {
"无法获取用户信息");
}
JSONObject json = JSONObject.parSEObject(userInfoStr);
//绑定微信的手机号
String tel = json.getString("purePhoneNumber");
Assert.isTrue(!StringUtils.isEmpty(tel),"无法获取用户手机号");
BaseResponse baseResponse=new BaseResponse();
baseResponse.setResultInfo(tel);
baseResponse.setState(0);
return baseResponse;
}
}
}
复制代码
接口实现类
|