emis-sapp/components/buns-api/login/mp-login.vue
2025-04-16 00:40:23 +08:00

184 lines
4.8 KiB
Vue

<template>
<view style="position: relative;height: 400rpx;">
<tpx-layout>
<template #body>
<u-row custom-style="margin-top: 30rpx;">
<view class="mr-40">
<button class="btn-open avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
<u-image v-if="submitLogin.avatarUrl" :src="submitLogin.avatarUrl" width="100" height="100" mode="aspectFill"/>
<view v-else class="avatar-empty">头像</view>
</button>
</view>
<u-input v-model="submitLogin.nickName" type="nickname" placeholder="昵称"
font-size="30"
placeholder-style=""
></u-input>
</u-row>
<u-row custom-style="margin-top: 30rpx;">
<u-input v-model="submitLogin.phoneNumber" placeholder="手机号"
readonly="true"
font-size="30"
placeholder-style=""
>
<template #suffix>
<button class="btn-open clr-wht font-28" open-type="getPhoneNumber"
@getphonenumber="getPhoneNumber" style=""
>
<text class="clr-prm">获取手机号</text>
</button>
</template>
</u-input>
</u-row>
</template>
<template #footer>
<u-button v-if="!pageLoading" @click="handleUpdateUserinfo" type="primary" custom-style="height:66rpx;font-size:32rpx;border-radius: 16rpx;">登录</u-button>
</template>
</tpx-layout>
</view>
</template>
<script>
import { uniPromiseApi, showLoading, hideLoading } from "@/common/untool"
import { USERKEY } from "@/store/actionKey"
import { checkParams } from "@/common/validate"
import * as apiService from "@/common/api"
import { getCommonReqData } from "@/common/manageServer"
import { doRequest } from "@/common/upload"
import { getSessionUserInfo, getSessionXToken } from "@/session"
export default {
data() {
return {
pageLoading: false,
submitLogin: {
code: '',
nickName: '',
avatarUrl: '',
phoneNumber: '',
},
submitValidConfig: {
nickName: { required: true, requiredMsg: '昵称不能为空' },
// avatarUrl: { required: true, requiredMsg: '头像不能为空' },
phoneNumber: { required: true, requiredMsg: '手机号不能为空' },
},
token: '',
}
},
computed: {
},
created() {
try{
// this.initData()
}catch(e){
console.error("created -- initData ==========", e)
}
},
methods: {
commodHeaders(){
return {
...getCommonReqData().header,
}
},
async initData(){
// 防止多次触发
if(this.pageLoading) {
return
}
this.pageLoading = true
let isLogined = !!getSessionXToken()
if(!isLogined) {
try{
await this.autoLogin()
this.loginSucceed()
}catch(err){
console.error("isLogined ==== err", err)
}
}
this.pageLoading = false
},
async onChooseAvatar(res) {
let tmpFilePath = res.detail.avatarUrl
let upRes = await doRequest(tmpFilePath, { header: this.commodHeaders() })
this.submitLogin.avatarUrl = upRes.url
},
async autoLogin(){
let token = ''
showLoading()
try{
let lg = await uniPromiseApi('login', {})
let code = lg.res.code
this.submitLogin.code = code
let res = await apiService.loginByMP({ ...this.submitLogin, _loading: true })
token = res.token
this.token = token
}catch(e){
console.error("autoLogin error=====", e)
}
hideLoading()
return token
},
async getPhoneNumber(e){
let bPhoneRes = await apiService.bindPhone({ ...(e?.detail || {}), _loading: true }, this.commodHeaders())
this.submitLogin.phoneNumber = bPhoneRes.phone
},
handleNicknamereview(e){
console.log(e)
},
async handleUpdateUserinfo() {
await checkParams(this.submitLogin, this.submitValidConfig)
// 更新用户信息
await this.autoLogin()
this.$emit("userinfoUpdateBack")
},
async loginSucceed(){
let token = this.token
await this.$store.dispatch(USERKEY.UPDATETOKEN, { token })
this.$emit("succeedBack")
},
async handleCheckUserInfo(){
const res = await apiService.getInfoByApp({ _loading: true })
let userInfo = res?.user || {}
Object.assign(this.submitLogin , {
nickName: userInfo.nickName,
avatarUrl: userInfo.avatar,
phoneNumber: userInfo.phonenumber,
})
try{
await checkParams(this.submitLogin, this.submitValidConfig, false)
}catch(err){
return false
console.error("auto login checkParams err=====", err)
}
return true
}
}
}
</script>
<style scoped lang="scss">
.lgn-page {
position: relative;
min-height: 100%;
}
.yunduo{
position: absolute;
bottom: 0rpx;
left: 0;
right: 0;
}
.avatar-empty{
width: 100rpx;
height: 100rpx;
line-height: 100rpx;
background-color: #fff;
text-align: center;
border-radius: 10rpx;
font-size: 28rpx;
color: $uni-color-primary;
}
</style>