feat: 登录的时候上传头像和微信名

This commit is contained in:
zc 2025-12-09 14:08:45 +08:00
parent 0baa476aee
commit c25e4ed02c

View File

@ -40,10 +40,19 @@
</view>
</view>
<view class="auth-title">申请获取以下权限</view>
<view class="auth-subtitle">获得你的公开信息昵称头像等</view>
<view class="auth-subtitle">获得你的公开信息昵称头像手机号等</view>
<!-- #ifdef MP-WEIXIN -->
<view class="login-btn">
<button class="button" open-type="getPhoneNumber" @getphonenumber="handleLoginWithPhone" @click="clickPhoneNumber" :class="{ disabled }">
一键登录
</button>
</view>
<!-- #endif -->
<!-- #ifndef MP-WEIXIN -->
<view class="login-btn">
<view class="button" :class="{ disabled }" @click="handleLogin()">一键登录</view>
</view>
<!-- #endif -->
<view class="no-login-btn">
<view class="button" @click="handleCancel()">暂不登录</view>
</view>
@ -53,10 +62,9 @@
<script>
import store from '@/store'
import * as LoginApi from '@/api/login'
import * as UploadApi from '@/api/upload'
import AvatarImage from '@/components/avatar-image'
import * as Verify from '@/utils/verify'
import * as OrderApi from '@/api/order'
export default {
components: {
@ -74,17 +82,66 @@
tempFile: undefined,
//
form: {
avatarId: null,
nickName: ''
},
//
phoneEncryptedData: '',
phoneIv: '',
// (code)
// openid
code: ''
}
},
methods: {
//
//
handleLogin() {
this.onAuthSuccess({})
},
// :
// : getphonenumbercode
// getphonenumbercode,encryptedData
async clickPhoneNumber() {
// #ifdef MP-WEIXIN
if (!this.disabled) {
this.code = await this.getCode()
}
// #endif
},
//
async handleLoginWithPhone({ detail }) {
const app = this
// #ifdef MP-WEIXIN
//
if (detail.errMsg === 'getPhoneNumber:ok') {
//
app.phoneEncryptedData = detail.encryptedData
app.phoneIv = detail.iv
console.log('获取手机号成功', {
hasEncryptedData: !!detail.encryptedData,
hasIv: !!detail.iv,
hasCode: !!app.code
})
//
let userInfo = {
phoneEncryptedData: detail.encryptedData,
phoneIv: detail.iv,
nickName: '',
avatarUrl: ''
}
//
app.onAuthSuccess(userInfo)
} else {
//
console.log('用户拒绝授权手机号', detail)
app.$toast('为了更好的体验,请授权手机号再登录')
}
// #endif
},
//
// :
// 1.codeuserInfo
@ -92,22 +149,51 @@
// 3.,
async onAuthSuccess(userInfo) {
const app = this
// codecode
//
const code = await app.getCode()
store.dispatch('LoginMpWx', { code })
if (!app.code) {
app.code = await app.getCode()
}
//
const loginData = {
code: app.code,
phoneEncryptedData: userInfo.phoneEncryptedData || '',
phoneIv: userInfo.phoneIv || '',
nickName: userInfo.nickName || '',
avatarUrl: userInfo.avatarUrl || ''
}
console.log('登录数据:', {
hasCode: !!loginData.code,
hasPhoneNumber: !!(loginData.phoneEncryptedData && loginData.phoneIv),
nickName: loginData.nickName,
hasAvatar: !!(loginData.avatarUrl)
})
//
app.disabled = true
store.dispatch('LoginMpWx', loginData)
.then(result => {
//
app.$toast('登录成功')
// :
uni.$emit('syncRefresh', true)
//
setTimeout(() => app.onNavigateBack(), 2000)
app.disabled = false
if (!result.data.buyer.avatarUrl) {
app.isPersonal = true
} else {
app.$toast('登录成功')
uni.$emit('syncRefresh', true)
setTimeout(() => app.onNavigateBack(), 2000)
}
})
.catch(err => {
const resultData = err.result.data
const resultData = err.result?.data || {}
//
if (resultData.isBindMobile) {
app.onEmitSuccess(userInfo)
} else {
//
app.disabled = false
app.code = '' // code
}
})
},
@ -145,8 +231,64 @@
onChooseAvatar({ detail }) {
// #ifdef MP-WEIXIN
const app = this
app.avatarUrl = detail.avatarUrl
app.tempFile = { path: app.avatarUrl }
console.log('onChooseAvatar', detail)
// chooseAvatar 访
//
const tempFilePath = detail.avatarUrl
//
app.avatarUrl = tempFilePath
//
// uni.saveFile
uni.saveFile({
tempFilePath: tempFilePath,
success: (res) => {
// 使
const savedFilePath = res.savedFilePath
console.log('头像文件保存成功', savedFilePath)
app.avatarUrl = savedFilePath
app.tempFile = { path: savedFilePath }
},
fail: (err) => {
console.error('头像文件保存失败', err)
// 使
// 访使
try {
const fs = wx.getFileSystemManager()
// 使
const userDataPath = wx.env && wx.env.USER_DATA_PATH
if (userDataPath) {
const timestamp = Date.now()
const random = Math.random().toString(36).substring(2, 8)
const destPath = `${userDataPath}/avatar_${timestamp}_${random}.jpg`
fs.copyFile({
srcPath: tempFilePath,
destPath: destPath,
success: (copyRes) => {
console.log('头像文件复制成功', destPath)
app.avatarUrl = destPath
app.tempFile = { path: destPath }
},
fail: (copyErr) => {
console.error('头像文件复制也失败', copyErr)
// 使
app.tempFile = { path: tempFilePath }
}
})
} else {
// 使
app.tempFile = { path: tempFilePath }
}
} catch (e) {
console.error('文件处理异常', e)
// 使
app.tempFile = { path: tempFilePath }
}
}
})
// #endif
},
@ -168,9 +310,9 @@
//
uploadFile() {
const app = this
return UploadApi.image([app.tempFile], false)
.then(fileIds => {
app.form.avatarId = fileIds[0]
return OrderApi.apiUploadFile([app.tempFile])
.then(res => {
app.form.avatarUrl = res[0].data[0].url
app.tempFile = null
return true
})
@ -199,7 +341,7 @@
return
}
//
app.onAuthSuccess({ nickName: app.form.nickName, avatarId: app.form.avatarId })
app.onAuthSuccess({ nickName: app.form.nickName, avatarUrl: app.form.avatarUrl })
},
// oauth
@ -350,11 +492,19 @@
display: flex;
justify-content: center;
align-items: center;
border: none;
padding: 0;
line-height: 86rpx;
//
&.disabled {
opacity: 0.6;
}
// button
&::after {
border: none;
}
}
}