300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 微信小程序获取手机号 含java后台接口实现

微信小程序获取手机号 含java后台接口实现

时间:2019-03-08 13:16:08

相关推荐

微信小程序获取手机号 含java后台接口实现

前言

前面写了一篇java实现微信小程序加密数据解密_女IT玩家-彬彬的博客-CSDN博客

然后接着写微信小程序如何获取手机号,本文既有小程序的实现代码、也有后台java的实现代码哦!!

小程序实现(我用的uni-app)

参考文档:

获取手机号 | 微信开放文档

1、首先必须先有一个button

<u-button slot="right" type="primary" size="mini" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信手机号注册</u-button>

2、实现方法:getPhoneNumber,这个方法的回调就是我们需要的加密数据

/*** 小程序获取手机号*/getPhoneNumber(e) {console.log(e.detail.errMsg)console.log(e.detail.iv)console.log(e.detail.encryptedData)this.encryptedData = e.detail.encryptedData;this.iv = e.detail.iv;const that = this;wx.checkSession({success () {//session_key 未过期,并且在本生命周期一直有效that.getWXPhone()},fail () {// session_key 已经失效,需要重新执行登录流程const that = thiswx.login({success(res) {// 发送 res.code 到后台换取 openId, sessionKey, unionIdif (res.code) {that.loginCode = res.code;wx.showLoading();if (that.loginCode) {// 执行that.getWXPhone()} else {wx.hideLoading();wx.showModal({showCancel: false,content: '登录失败,请重试'})}} else {wx.showModal({title: '提示',content: '登录失败,请重试。如问题仍无法解决请联系客服。',showCancel: false})}},fail(err) {wx.showModal({title: '提示',content: '登录失败,请重试。如问题仍无法解决请联系客服。',showCancel: false})}})}})},

3、实现方法:getWXPhone ,这个方法用来访问后端接口,解密加密数据,获取手机号

/*** 获取手机号*/getWXPhone() {const that = thisconst url = 'wx/getWXPhone';const params = {code: this.code,encryptedData: this.encryptedData,iv: this.iv}console.log('微信登录传递参数', params)this.$fetch({url, data: params}).then(res => {// console.log('merchantOfWeChatLogin succ', data);console.log(res);wx.hideLoading();if (res.code === 200 && res.success) {console.log(res)} else if (res.code === 80) {that.visibleOfDialog = true} else {that.visibleOfDialog = truewx.showToast({title: res.message,icon: 'none'})}});},

后台接口实现

说明:因为我的小程序前面有微信登录的操作,已经根据code获得过sessionkey,并存在redis上了,所以在获取手机号的时候,先从redis里取sessionkey,如果没有再重新经过code2session方法获取sessionkey,再解密,从而获取用户的手机号。

@ApiOperation(value = "获取微信手机号")@GetMapping(value = "/getWXPhone")public Result getWXPhone(CustomerWxLoginVo customerWxLoginVo, HttpServletRequest req) throws Exception {Result result = new Result<LoginVo>();try {/** 参数属性 */String code = customerWxLoginVo.getCode();String encryptedData = customerWxLoginVo.getEncryptedData();String iv = customerWxLoginVo.getIv();// 校验信息Boolean isExist = redisUtil.hasKey("wx-sessionkey"+code);String sessionkey = null;if (isExist) {sessionkey = (String) redisUtil.get("wx-sessionkey"+code);if (StringUtils.isBlank(sessionkey)) {throw new Exception("获取手机号失败");}} else {// 根据code获取登录密钥JSONObject jsonObject = wxLoginUtil.code2session(appId, appSecret, code);if (jsonObject == null) {return Result.error("获取手机号失败");}if (jsonObject.getIntValue("errcode") != 0) {return Result.error("获取手机号失败");}// 本次登录的会话密钥sessionkey = jsonObject.getString("session_key");if(StringUtils.isBlank(sessionkey)) {return Result.error("获取手机号失败");}redisUtil.set("wx-sessionkey"+code,sessionkey,60*60*24);}JSONObject jsonObject = wxLoginUtil.wXBizDataCrypt(encryptedData, sessionkey, iv);return result.setResult(jsonObject);} catch (Exception e) {result.error500(e.getMessage());}return result;}

wxLoginUtil.code2session 方法实现:

// 微信小程序获取openId, sessionKey, unionIdpublic JSONObject code2session(String appId, String appSecret, String code) throws Exception {String url = String.format("https://api./sns/jscode2session? appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appId, appSecret, code);JSONObject result = JSON.parseObject(HttpUtil.get(url));log.info("code2session的返回结果:" + result);if (!result.containsKey("errcode")) {result.put("errcode", "0");}return result;}

HttpUtil 使用 hutool 的Api:

hutool: 🍬小而全的Java工具类库,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.7.16</version></dependency>

wxLoginUtil.wXBizDataCrypt 方法实现:

java实现微信小程序加密数据解密_女IT玩家-彬彬的博客-CSDN博客

结果

接下的就根据需求自己实现吧!!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。