您的位置:首页 - 文章 - 随笔 - 正文

小程序利用code获取手机号注册

第一步:按钮绑定事件

<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" class="join-btn">微信授权</button>

第二步:

//通过绑定手机号登录
  getPhoneNumber: function (e) { var that = this; console.log(JSON.stringify(e)) console.log(e.detail.encryptedData); console.log(e.detail.iv); if (e.detail.errMsg == "getPhoneNumber:ok") { //同意 //获取code Api:wx.login() WxApiUtils.login().then((codedata) => { console.log("微信login", JSON.stringify(codedata)) //向服务器传参解密获取手机号 decryPhoneNumber( { encryptedData: e.detail.encryptedData, iv: e.detail.iv, code: codedata.code } ).then((res)=>{ console.log("获取手机号成功"+JSON.stringify(res)) that.data.userInfo.organizationCode = getGlobalData('organizationCode'); that.data.userInfo.phone = res.data.phone; console.log(JSON.stringify(that.data.userInfo)) //获取到手机号传入服务器接口 进行注册 register(that.data.userInfo).then(reg => { console.log("注册成功"+JSON.stringify(reg)) }).catch(e=>{}).finally(()=>{}); }).catch((err)=>{ console.log("注册失败") }).finally(()=>{ }); }).catch((err) => { //reject(err); }).finally(() => { wxutil.hideLoading() }); }else{ //拒绝 that.setData({ "registerColor":'#f00', "registerOne":"您拒绝授权将无法加入丽诺尔:", "registerTwo":"请授权您的公开信息(昵称、头像等)" }) }
},

后端代码:

WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(code());

@PostMapping("/decryPhoneNumber") @ApiOperation(value = "解密微信手机号码") public Result decryPhoneNumber(@RequestBody WxPhoneNumberInfoDTO dto) { try { WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(dto.getCode()); log.info("getUserInfo result:" + result.toString()); log.info(String.format("decryPhoneNumber:sessionKey:%s||encryptedData:%s||ivStr%s", new Object[]{result.getSessionKey(), dto.getEncryptedData(), dto.getIv()})); //解密,获取手机号 国家码 String resultInfo = WxMaCryptUtils.decrypt(result.getSessionKey(), dto.getEncryptedData(), dto.getIv()); log.info("decryPhoneNumber result:" + resultInfo); JSONObject obj = JSONObject.parseObject(resultInfo); WxPhoneNumberInfo wxPhoneNumberInfo = new WxPhoneNumberInfo(); wxPhoneNumberInfo.setPhone(obj.getString("phoneNumber")); wxPhoneNumberInfo.setCountryCode(obj.getString("countryCode")); wxPhoneNumberInfo.setPurePhoneNumber(obj.getString("purePhoneNumber")); return ResultGenerator.genSuccessResult(wxPhoneNumberInfo); } catch (Exception e) { return ResultGenerator.genFailResult("获取手机号失败"); } }
 @PostMapping("/register") @ApiOperation(value = "注册") public Result toLogin(@RequestBody UserInfo unserInfo, HttpServletRequest request) { Object ojb= request.getAttribute("WEIXIN_LOGIN_INFO"); //WxUserViewModel( //userId=0, //sessionKey=rvq4r*****tjDIeqMSYw==, //openid=ovx3Q4jB*****I7ekp4Fu_j56I, //unionid= , //organizatinfoCode=6101****005 //) return null;
}
public class WxUserViewModel {	private static final long serialVersionUID = 1L;	private Integer userId;	private String sessionKey;	private String openid;	private String unionid;	private String organizatinfoCode;
}
本文原创,作者:西决,其版权均为品创网络所有。如需转载,请注明出处:https://www.sxpcwlkj.com/%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%88%a9%e7%94%a8code%e8%8e%b7%e5%8f%96%e6%89%8b%e6%9c%ba%e5%8f%b7%e6%b3%a8%e5%86%8c/

发表评论