当前位置:网站首页>微信小程序获取用户手机号码
微信小程序获取用户手机号码
2022-08-03 11:05:00 【詹Sir(开源字节)】
我们在做小程序开发的过程中,经常会涉及到用户认证的问题,最普遍的就是获取用户的手机号码一键登录
获取用户手机号码 分为以下几步:
1、第一点击页面获取授权code
2、第二获取小程序token
3、第三根据token与code获取手机号码
4、执行登录认证逻辑
接下来我们来实现以上四步
首先先看前端页面获取code与手机号码:
<button open-type="getPhoneNumber" @getphonenumber="weChatLogin" class="clearBtn"> <view class="loginType"> <view class="item"> <view class="icon"><u-icon size="60" name="weixin-fill" color="rgb(83,194,64)"></u-icon></view> 微信 </view> </view> </button> weChatLogin(e){ uni.showLoading({title:"登录中....",mask:true}) let code= e.detail.code; let url = "/api/miniWxApi/getPhoneNum?code="+code; this.$u.get(url).then(res => { let phoneNum = res.phoneNum let weChatUrl = "/api/weChatLogin"; this.$u.post(weChatUrl,{ username: phoneNum, code: code }).then(data => { uni.hideLoading(); // 登录成功初始化token与用户信息 this.$u.vuex('vuex_token', data.token); this.$u.vuex('vuex_user', data.loginUser); uni.switchTab({ url: '/pages/index/index' }) }); }); },
接下来我们看后端获取token与认证逻辑:
/** * 获取用户手机号码 */ @GetMapping("/getPhoneNum") public AjaxResult getPhoneNum(String code){ AjaxResult ajax = AjaxResult.success(); // Object token = getAccessToken(); Object token = WxUtil.obtainAccessToken(APPID, SECRET); String phoneNum = WxUtil.getPhoneNum(token,code); ajax.put("phoneNum",phoneNum); return ajax; } /** * 获取AccessToken */ @GetMapping("/getAccessToken") public AjaxResult getAccessToken(){ AjaxResult ajax = AjaxResult.success(); Object token = redisCache.getCacheObject(accessTokenKey); if(StringUtils.isNull(token)){ token = WxUtil.obtainAccessToken(APPID, SECRET); redisCache.setCacheObject(accessTokenKey,token,30,TimeUnit.MINUTES); } ajax.put(accessTokenKey,token); return ajax; } /** * 微信小程序注册/登录 */ @PostMapping("/weChatLogin") public AjaxResult weChatLogin(HttpServletRequest request,@RequestBody LoginBody loginBody) { AjaxResult ajax = AjaxResult.success(); String msg = "登录成功"; if (StringUtils.isEmpty(loginBody.getUsername())){ msg = "用户名不能为空"; return error(msg); } // 判断是否为新用户 SysUser sysUser = userService.selectUserByUserName(loginBody.getUsername()); // 不是新用户,创建用户 if(sysUser == null){ sysUser = new SysUser(); sysUser.setUserName(loginBody.getUsername()); sysUser.setNickName(loginBody.getUsername()); sysUser.setPassword(loginBody.getUsername()); sysUser.setPhonenumber(loginBody.getUsername()); sysUser.setPassword(SecurityUtils.encryptPassword(loginBody.getUsername())); sysUser.setDeptId(110L); Long[] postIds = new Long[1]; postIds[0] = 8L; Long[] roleIds = new Long[1]; roleIds[0] = 5L; sysUser.setPostIds(postIds); sysUser.setRoleIds(roleIds); // 保存完用户后,还需要设置用户的角色,部门与岗位 userService.insertUser(sysUser); } // 生成token LoginUser loginUser = new LoginUser(sysUser,null); String token = tokenService.createToken(loginUser); ajax.put(Constants.TOKEN, token); ajax.put("loginUser", loginUser); return ajax; }
如上步骤即可实现uniapp微信小程序登录,开源字节坚持写优雅的代码,以上代码可在开源字节低代码平台中免费获取
如若转载,请注明出处:开源字节 https://sourcebyte.cn/article/209.html
边栏推荐
- 程序员架构修炼之道:如何设计出可持续演进的系统架构?
- 巴比特 | 元宇宙每日必读:玩家离场,平台关停,数字藏品市场正逐渐降温,行业的未来究竟在哪里?...
- C#/VB.NET 从PDF中提取表格
- 实现2d人物在跳跃的同时左右移动
- CADEditorX ActiveX 14.1.X
- 面试官:工作两年了,这么简单的算法题你都不会?
- Classical Architecture and Memory Classification of Embedded Software Components
- Objective - C code analysis of the deep and shallow copy
- How to retrieve IDC research reports?
- QT with OpenGL(HDR)
猜你喜欢
随机推荐
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二
[华为云在线课程][SQL语法入门][学习笔记]
MATLAB programming and application 2.7 Structural data and unit data
Binary search tree (search binary tree) simulation implementation (there is a recursive version)
Activiti产生的背景和作用
微信多开批处理(自动获取安装路径)
Matplotlib
QT with OpenGL(HDR)
Who is more popular for hybrid products, depending on technology or market?
CADEditorX ActiveX 14.1.X
【无标题】函数,对象,方法的区别
Skills required to be a good architect: How to draw a system architecture that everyone will love?What's the secret?Come and open this article to see it!...
MATLAB Programming and Applications 2.6 Strings
redis基础知识总结——数据类型(字符串,列表,集合,哈希,集合)
Summary of redis basics - data types (strings, lists, sets, hashes, sets)
云原生 Dev0ps 实践
记住用户名案例(js)
Machine Learning Overview
成为优秀架构师必备技能:怎样才能画出让所有人赞不绝口的系统架构图?秘诀是什么?快来打开这篇文章看看吧!...
鸿蒙第三次