building-sign/pages/login/login.vue

236 lines
6.6 KiB
Vue
Raw Normal View History

2022-11-15 18:03:13 +08:00
<template>
<view>
<view class='login-header'>
2022-11-22 13:47:31 +08:00
<image class="infoImg" mode="aspectFill" :src="imgSrc"></image>
2022-11-15 18:03:13 +08:00
<view class="logo-name font34 color-00">{{appletName}}</view>
</view>
<view class="login-footer">
2022-11-21 16:03:37 +08:00
<view class="login-btn color-white font34 bg-blue" type='primary' @tap="empowerShow=true" v-if="!isActive"></view>
2022-11-22 13:47:31 +08:00
<view class="login-btn color-white font34 bg-blue" type='primary' @tap="getUserProfileEv('other')" v-else></view>
2022-11-23 16:46:52 +08:00
<view class="agreement-box color-33 font28">点击授权登录表示您已阅读<text @tap="toSafeNotice" class="agreement color-blue">安全告知</text></view>
2022-11-15 18:03:13 +08:00
</view>
2022-11-21 16:03:37 +08:00
<!-- 获取头像&昵称 -->
<view class="pop-up-bg" v-if="empowerShow">
<view class="user-info-box bg-white">
<view class="info">
<view class="cover">
2022-11-21 17:22:31 +08:00
<image :src="imgSrc" mode="aspectFit"></image>
2022-11-21 16:03:37 +08:00
</view>
<view class="name color-99 font24">{{appletName}}</view>
<view class="tips">
<view class="font26">邀请您补全个人信息</view>
<view class="font24 color-blue">(昵称头像)</view>
</view>
</view>
<view class="msg">
<view class="item font26">
<text>头像</text>
<button class="avatar" open-type="chooseAvatar" @chooseavatar="chooseAvatar">
<image :src="userInfo.avatarUrl || logoAvatar" mode="aspectFit"></image>
</button>
</view>
<view class="item font26">
<text>昵称</text>
<input class="nick-name" type="nickname" @blur="nickNameInput" v-model="userInfo.nickName" placeholder="请输入昵称" placeholder-style="color:#999"/>
</view>
</view>
<view class="empower-btns font30">
<view class="btn color-99" @tap="refuseEv"></view>
2022-11-22 13:47:31 +08:00
<view class="btn color-blue" @tap="getUserProfileEv('other')"></view>
2022-11-21 16:03:37 +08:00
</view>
</view>
</view>
2022-11-15 18:03:13 +08:00
</view>
</template>
<script>
export default {
data() {
return {
2022-11-21 16:03:37 +08:00
userInfo: {
nickName:'',
2022-11-23 16:46:52 +08:00
avatarUrl:''
}, //用户信息
2022-11-15 18:03:13 +08:00
canIGetUserProfile: false,
imgSrc: '/static/logo.jpg', //默认logo头像
2022-11-21 16:03:37 +08:00
logoAvatar:'/static/logo-avatar.png', //默认用户头像
2022-11-15 18:03:13 +08:00
appletName:'工地打卡', //小程序名称
isBindPhone: false, //是否绑定手机号
2022-11-21 16:03:37 +08:00
empowerShow:false, //是否显示授权弹窗
isActive:false, //是否已授权
2022-11-15 18:03:13 +08:00
};
},
2022-12-21 16:26:21 +08:00
onLoad() {
2022-11-22 13:47:31 +08:00
2022-11-15 18:03:13 +08:00
},
2022-11-21 16:03:37 +08:00
onShow() {
2022-11-22 13:47:31 +08:00
// 进入登录
this.getUserProfileEv('enter');
2022-11-21 16:03:37 +08:00
},
2022-11-15 18:03:13 +08:00
methods: {
// 设置logo图
setLogo(){
this.$requst.get('/api/v1/index/base-config').then(res=>{
this.imgSrc = this.$http + res.data.logo;
this.appletName = res.data.appletName;
})
},
// 暂不绑定事件
refuse(){
this.isBindPhone=false;
this.$toolAll.tools.showToast('登录成功','success')
uni.reLaunch({url:'/pages/pagehome/pagehome'})
},
// 授权绑定手机号
getphonenumber(e){
let ya = this;
wx.login({
success:(res)=>{
this.$requst.post('/api/v1/user/login',{code:res.code}).then(result => {
if(e.detail.errMsg=="getPhoneNumber:ok"){
this.$requst.post('/api/v1/user/bind-phone',{openid: result.data.openid,session_key:result.data.session_key, iv:e.detail.iv,encryptedData:e.detail.encryptedData}).then(res=>{
console.log('手机号信息:',res);
if(res.code==0){
this.$toolAll.tools.showToast('手机号绑定成功');
this.isBindPhone = true;
} else this.$toolAll.tools.showToast(res.msg);
},error=>{})
} else {
// console.log('取消授权手机号')
}
}).catch(err=>{
console.log(err);
})
}
})
},
// 跳转免责
2022-11-23 16:46:52 +08:00
toSafeNotice(){
2022-11-15 18:03:13 +08:00
uni.navigateTo({
2022-11-23 16:46:52 +08:00
url:'/pagesA/singlePage/singlePage?type=security'
2022-11-15 18:03:13 +08:00
})
2022-11-23 16:46:52 +08:00
},
// 头像上传
uploadImg(url){
uni.showLoading({
title: '上传中'
});
this.$requst.upload('/api/v1/file/upload/image',{path:url}).then(res=>{
if(res.code==0) {
this.userInfo.avatarUrl = this.$hostHttp+res.data.src;
}
uni.hideLoading();
})
2022-11-15 18:03:13 +08:00
},
2022-11-21 16:03:37 +08:00
// 获取头像
2022-11-23 16:46:52 +08:00
chooseAvatar(e){
// 上传头像
this.uploadImg(e.detail.avatarUrl)
},
2022-11-21 16:03:37 +08:00
// 获取昵称
nickNameInput(e){
this.userInfo.nickName = e.detail.value
},
// 拒绝登录
refuseEv(){
this.$toolAll.tools.showToast('您已拒绝授权');
this.empowerShow = false;
},
2022-11-22 13:47:31 +08:00
// 验证登录信息
checkForm(){
2022-11-21 16:03:37 +08:00
if(!this.isActive){
if(this.userInfo.avatarUrl==''){
2022-11-22 13:47:31 +08:00
this.$toolAll.tools.showToast('请选择头像!')
2022-11-21 16:03:37 +08:00
return false;
2022-11-22 13:47:31 +08:00
}else if(this.userInfo.nickName==''){
this.$toolAll.tools.showToast('请输入昵称!')
2022-11-21 16:03:37 +08:00
return false;
2022-11-22 13:47:31 +08:00
}else{
return true;
2022-11-21 16:03:37 +08:00
}
2022-11-22 13:47:31 +08:00
}else{
return true;
2022-11-21 16:03:37 +08:00
}
2022-11-22 13:47:31 +08:00
},
// 允许登录
getUserProfileEv(status) {
2022-11-15 18:03:13 +08:00
//新版登录方式
2022-11-22 13:47:31 +08:00
uni.login({
provider: 'weixin',
2022-12-21 16:26:21 +08:00
success: (res)=> {
2022-11-22 13:47:31 +08:00
if (res.code) {
this.updateUserInfo(res.code,status);
} else {
uni.showToast({
title: '登录失败!',
duration: 2000
});
}
2022-11-15 18:03:13 +08:00
},
});
},
//调用登录接口
2022-11-22 13:47:31 +08:00
updateUserInfo(code,status) {
2022-12-21 16:26:21 +08:00
if(status=='enter'){
this.$requst.post('/api/v1/user/login',{code:code}).then(res => {
2022-11-22 13:47:31 +08:00
if(res.code == 0){
2022-12-21 16:26:21 +08:00
console.log(res,'进入登录信息');
2022-11-22 13:47:31 +08:00
if(res.data.is_active==1){
2022-12-21 16:26:21 +08:00
this.isActive = true;
}
2022-11-22 13:47:31 +08:00
}
2022-12-21 16:26:21 +08:00
},error => {
2022-11-22 13:47:31 +08:00
})
2022-11-15 18:03:13 +08:00
}
2022-12-21 16:26:21 +08:00
if(status=='other'){
2022-11-22 13:47:31 +08:00
if(this.checkForm()){
uni.showToast({
title: '登录中...',
icon:'loading',
duration:10000
})
var params = {
code:code,
nickname: this.userInfo.nickName,//用户昵称
headimgurl: this.userInfo.avatarUrl,//用户头像
is_active:1
2022-11-15 18:03:13 +08:00
}
2022-11-22 13:47:31 +08:00
this.$requst.post('/api/v1/user/login',params).then(res => {
if(res.code == 0){
uni.setStorageSync('userId',res.data.account_id)
uni.setStorageSync('token',res.data.token)//缓存token
uni.setStorageSync('openid',res.data.openid)//缓存openid
uni.setStorageSync('expire',res.data.expire)//缓存失效时间(时间戳格式)
uni.setStorageSync('phone_active',res.data.phone_active)//是否授权手机号
uni.setStorageSync('is_active',res.data.is_active)//是否授权头像和昵称
if(uni.getStorageSync('page-path-options')) {
uni.reLaunch({ // 重新进入当前页面
url:uni.getStorageSync('page-path-options')
})
} else {
uni.reLaunch({
url:'/pages/pagehome/pagehome'
})
}
}
},error => {})
2022-11-15 18:03:13 +08:00
}
2022-11-22 13:47:31 +08:00
}
2022-11-15 18:03:13 +08:00
}
}
}
</script>
<style scoped>
</style>