<template>
<view style="position:relative;">
<!-- <img style="width: 100%; height: 1120rpx;" src="/pagesA/static/background/background.png" /> -->
<image style="padding-left:170rpx;padding-top:180rpx;width:580rpx;height:560rpx;" src="/static/logo.png">
<view style="position: fixed;
top: 0;
left: 0;
right: 0%;
bottom: 0;
background-color: #21D4FD;
background-image: linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
z-index: -1;"></view>
<button style="position:absolute; width:456rpx;left:77px; top:340px; border-radius: 20px;" lang="zh_CN"
@click="getPhoneNumber">微信用户快捷登录</button>
<span style="position:absolute; width:456rpx;left:57px; top:390px;color: white;"
@click="handleLogin">用户名密码登录</span>
</view>
</template>
<script>
import {
wexinLogin
} from "@/api/login";
export default {
data() {
return {
tableNo: '',
}
},
onLoad: function(e) {
const url = decodeURIComponent(e.q);
console.log("二维码链接:" + url);
e = this.GetWxMiniProgramUrlParam(url);
this.tableNo = e.tableNo
console.log("设备号:" + this.tableNo);
},
methods: {
GetWxMiniProgramUrlParam(url) {
let theRequest = {};
if (url.indexOf("#") != -1) {
const str = url.split("#")[1];
const strs = str.split("&");
for (let i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
}
} else if (url.indexOf("?") != -1) {
const str = url.split("?")[1];
const strs = str.split("&");
for (let i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
}
}
return theRequest;
},
getPhoneNumber() {
var that = this;
uni.showLoading({
title: '加载中',
});
uni.getUserInfo({
provider: 'weixin',
success: function(info) {
console.log("用户信息:" + info.userInfo)
that.nickName = info.userInfo.nickName
that.avatarUrl = info.userInfo.avatarUrl
uni.login({
"provider": "weixin",
"onlyAuthorize": true,
success: function(event) {
const {
code
} = event
console.log("code" + event.code)
var params = {
tableNo: that.tableNo,
wxCode: event.code,
userInfo: info.userInfo
}
wexinLogin(params).then(res => {
if (res.code == 200) {
uni.showToast({
title: '登录成功',
icon: 'success',
mask: true,
});
uni.removeStorageSync('userInfo');
uni.removeStorageSync('token');
uni.removeStorageSync();
uni.setStorageSync('App-Token', res.data.token)
uni.setStorageSync('data', res.data)
console.log('用户数据', res.data)
uni.switchTab({
url: "/pages/index"
})
}
})
},
fail: function(err) {
}
})
}
})
},
handleLogin() {
uni.navigateTo({
url: '/pages/login'
})
}
}
}
</script>
<style>
page {
background-color: #ffffff;
}
.header {
margin: 90rpx 0 90rpx 50rpx;
border-bottom: 1px solid #ccc;
text-align: center;
width: 650rpx;
height: 300rpx;
line-height: 450rpx;
}
.header image {
width: 200rpx;
height: 200rpx;
}
.content {
margin-left: 50rpx;
margin-bottom: 90rpx;
}
.content text {
display: block;
color: #9d9d9d;
margin-top: 40rpx;
}
.bottom {
border-radius: 80rpx;
margin: 70rpx 50rpx;
font-size: 35rpx;
}
<style>
private Map<String, String> wxAuth(String wxCode) {
HashMap<String, String> hashMap = new HashMap<>();
if (wxCode == null) {
return null;
}
String sessionKey = null;
String openId = null;
try {
WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(wxCode);
sessionKey = result.getSessionKey();
openId = result.getOpenid();
} catch (Exception e) {
e.printStackTrace();
} finally {
WxMaConfigHolder.remove();
}
if (sessionKey == null || openId == null) {
return null;
}
hashMap.put("openId", openId);
hashMap.put("sessionKey", sessionKey);
return hashMap;
}
@PostMapping("/loginByWeixin")
public AjaxResult loginByWeixin(@RequestBody WxLoginInfo wxLoginInfo) {
AjaxResult ajax = AjaxResult.success();
String wxCode = wxLoginInfo.getWxCode();
UserInfo userInfo = wxLoginInfo.getUserInfo();
Map<String, String> wxAuth = wxAuth(wxCode);
if (wxAuth == null || userInfo == null) {
return AjaxResult.error("授权信息获取失败");
}
String openId = wxAuth.get("openId");
String sessionKey = wxAuth.get("sessionKey");
SysUser user = userService.selectUserByOpenId(openId);
String token = null;
if (user == null) {
user = new SysUser();
StringJoiner stj = new StringJoiner("", "YH", "");
stj.add(sequenceFactory.generateOrderNum("JMT", 5));
user.setUserCode(stj.toString());
user.setUserName(sessionKey);
user.setPassword(SecurityUtils.encryptPassword(openId));
user.setOpenId(openId);
user.setAvatar(userInfo.getAvatarUrl());
user.setSessionKey(sessionKey);
user.setTableNo(wxLoginInfo.getTableNo());
user.setLoginStatus("未授权");
userService.insertUser(user);
token = loginService.login(user.getUserName(), user.getOpenId());
} else {
userService.updateTableNoByOpenId(openId, wxLoginInfo.getTableNo());
userService.updateLoginStatusByOpenId(openId, "已授权");
token = loginService.login(user.getUserName(), openId);
user.setSessionKey(sessionKey);
}
ajax.put("token", token);
ajax.put("userInfo", user);
return AjaxResult.success(ajax);
}