当前位置:网站首页>微信小程序获取用户手机号码
微信小程序获取用户手机号码
2022-08-03 10:56:00 【InfoQ】

<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'
})
});
});
},
/**
* 获取用户手机号码
*/
@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;
}
边栏推荐
- Why is the new earth blurred, in-depth analysis of white balls, viewing pictures, and downloading problems
- MATLAB programming and application 2.7 Structural data and unit data
- 509. 斐波那契数
- ERC20通证标准是什么?
- 【多线程的相关内容】
- 混动产品谁更吃香,看技术还是看市场?
- 图新地球为什么很模糊,白球、看图、下载问题深度剖析
- Dva.js 新手入门指南
- 孙宇晨式“溢价逻辑”:不局限眼前,为全人类的“星辰大海”大胆下注
- This article takes you to understand the principle of CDN technology
猜你喜欢
![[LeetCode—Question 2 Sum of Two Numbers Detailed Code Explanation ] The source code is attached, which can be copied directly](/img/19/a3f58d5a1150d99571205a7e2f7345.png)
[LeetCode—Question 2 Sum of Two Numbers Detailed Code Explanation ] The source code is attached, which can be copied directly

创建C UDR时,指定的HANDLESNULLS的作用是什么?

谷歌实用插件分享
![[Bubble sort and odd-even sorting]](/img/89/d63afe1900a05b2a5615fcc3c09ccb.png)
[Bubble sort and odd-even sorting]

Win10/11 删除文件资源管理器左侧栏目文件夹

"Global Digital Economy Conference" landed in N World, Rongyun provides communication cloud service support

科普大佬说 | 黑客帝国与6G有什么关系?

出色的移动端用户验证

numpy

二叉搜索树(搜索二叉树)模拟实现(有递归版本)
随机推荐
The way of programmer architecture practice: how to design a sustainable evolution system architecture?
Simple implementation of a high-performance clone of Redis using .NET (1)
Who is more popular for hybrid products, depending on technology or market?
[LeetCode—Question 2 Sum of Two Numbers Detailed Code Explanation ] The source code is attached, which can be copied directly
Web Server 设置缓存响应字段的一些推荐方案
for in 和 for of的区别
Cross-chain bridge protocol Nomad suffers hacker attack, losing more than $150 million
Summary of redis basics - data types (strings, lists, sets, hashes, sets)
Machine Learning (Chapter 1) - Feature Engineering
成为优秀架构师必备技能:怎样才能画出让所有人赞不绝口的系统架构图?秘诀是什么?快来打开这篇文章看看吧!...
Advanced use of MySQL database
被审稿人吐槽没有novelty!深度学习方向怎么找创新点?
[Explanation of JDBC and inner classes]
numpy
servlet生命周期详解--【结合源码】
MATLAB程序设计与应用 2.6 字符串
【输出一个整数的的每一位,由高到低输出。使用递归和不使用递归】
从餐桌到太空,孙宇晨的“星辰大海”
【无标题】函数,对象,方法的区别
Machines need tokens more than people