新接口getUserProfileFn内置login,如果必须要login返回的参数要隔离开 vue3书写要对按钮配置属性
<button @click="logintou" data-eventsync="true" class="main-login-bottom">授权登录</button>
——————————————————
const app = getApp()
export let loginFn = () => {
return new Promise((resolve, reject) => {
uni.login({
force: true,
success(res) {
console.log(`login 调用成功`, res)
resolve(res)
},
fail(res) {
console.log(`login 调用失败`)
reject(res)
}
})
})
}
export let getUserProfileFn = () => {
return new Promise((resolve, reject) => {
uni.getUserProfile({
desc: '获取用户信息',
withCredentials: false,
force: true,
success: (res) => {
console.log('getUserProfile 调用成功:', res)
resolve(res)
},
fail(res) {
console.log('getUserProfile 调用失败', res)
reject(res)
}
})
})
}
export let getCookieFn = (loginRes, {
nickName,
avatarUrl,
gender
}) => {
app.globalData.api.index
.getList({
bodyData: {
m: 'Tiktok',
c: 'Public',
a: 'douyinLogin',
mchid: 1
},
data: {
anonymous_code: loginRes.anonymousCode,
code: loginRes.code,
nickname: nickName,
headimg: avatarUrl,
sex: gender
}
})
.then((fan) => {
app.globalData.token = fan.data.data.cookie
app.globalData.info.encid = fan.data.data.encid
uni.setStorageSync('info', app.globalData.info)
uni.setStorageSync('yjtoken', fan.data.data.cookie)
loginShow.value = false
})
}
export let logintou = async () => {
try {
const userProfileRes = await getUserProfileFn()
app.globalData.info = userProfileRes.userInfo
const loginRes = await loginFn()
await getCookieFn(loginRes, userProfileRes.userInfo)
} catch (error) {
// 处理错误
}
}