building-sign/pages/login/login.vue

146 lines
4.1 KiB
Vue
Raw Normal View History

2022-11-15 18:03:13 +08:00
<template>
<view>
<view class='login-header'>
<image class="infoImg" mode="aspectFill" :src="userInfo.avatarUrl || imgSrc"></image>
<view class="logo-name font34 color-00">{{appletName}}</view>
</view>
<view class="login-footer">
<view class="login-btn color-white font34 bg-blue" type='primary' @tap="bindGetUserInfo"></view>
<view class="agreement-box color-33 font28">点击授权登录表示您已阅读<text @tap="toAgreement" class="agreement color-blue">安全公告</text></view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
userInfo: {}, //用户信息
canIGetUserProfile: false,
imgSrc: '/static/logo.jpg', //默认logo头像
appletName:'工地打卡', //小程序名称
isBindPhone: false, //是否绑定手机号
};
},
onLoad() {
if (uni.getUserProfile) {
this.canIGetUserProfile = true;
}
},
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);
})
}
})
},
// 跳转免责
toAgreement(){
uni.navigateTo({
url:'/pages/login/disclaimers'
})
},
//调起登录授权
bindGetUserInfo(e) {
//新版登录方式
uni.getUserProfile({
desc: '登录',
lang: 'zh_CN',
success: (res) => {
this.userInfo = res.userInfo;
uni.login({
provider: 'weixin',
success: (res)=> {
if (res.code) {
this.updateUserInfo(res.code);
} else {
uni.showToast({
title: '登录失败!',
duration: 2000
});
}
},
});
},
fail: (res) => {}
});
},
//调用登录接口
updateUserInfo(code) {
uni.showToast({
title: '登录中...',
icon:'loading',
duration:10000
})
var params = {
code:code,
nickname: this.userInfo.nickName,//用户昵称
headimgurl: this.userInfo.avatarUrl,//用户头像
country: this.userInfo.country,//用户所在国家
province: this.userInfo.province,//用户所在省份
city: this.userInfo.city,//用户所在城市
gender: this.userInfo.gender,//用户性别
language:this.userInfo.language,//语言
is_active:1
}
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 => {})
}
}
}
</script>
<style scoped>
</style>