文档

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

微信扫一扫,分享到朋友圈

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

第一步:按钮绑定事件

<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;
}
展开阅读全文
干一件事情,用心干就完了
上一篇

记录一个linux系统安装多个tomcat

你也可能喜欢

发表评论

您的电子邮件地址不会被公开。 必填项已用 * 标注

提示:点击验证后方可评论!

插入图片

分类目录

微信扫一扫

微信扫一扫