58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import { login } from '../../utils/auth'
|
|
|
|
const app = getApp<{ userInfo: any; isLoggedIn: boolean }>()
|
|
|
|
Page({
|
|
data: {
|
|
loading: false,
|
|
},
|
|
|
|
onLoad() {
|
|
if (app.globalData?.isLoggedIn) {
|
|
wx.switchTab({ url: '/pages/feed/feed' })
|
|
}
|
|
},
|
|
|
|
async onLogin() {
|
|
if (this.data.loading) return
|
|
this.setData({ loading: true })
|
|
|
|
try {
|
|
wx.showLoading({ title: '登录中...', mask: true })
|
|
|
|
// 先获取用户授权
|
|
await new Promise<void>((resolve, reject) => {
|
|
wx.getUserProfile({
|
|
desc: '用于展示您的汪圈个人主页',
|
|
success: () => resolve(),
|
|
fail: reject,
|
|
})
|
|
})
|
|
|
|
const userInfo = await login()
|
|
app.globalData.userInfo = userInfo
|
|
app.globalData.isLoggedIn = true
|
|
|
|
wx.hideLoading()
|
|
wx.showToast({ title: '欢迎加入汪圈!', icon: 'success' })
|
|
setTimeout(() => wx.switchTab({ url: '/pages/feed/feed' }), 800)
|
|
} catch (e: any) {
|
|
wx.hideLoading()
|
|
if (e?.errMsg?.includes('deny') || e?.errMsg?.includes('cancel')) {
|
|
wx.showToast({ title: '需要授权才能登录', icon: 'none' })
|
|
} else {
|
|
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
|
|
}
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
onPrivacy() {
|
|
wx.navigateTo({ url: '/pages/webview/webview?url=YOUR_PRIVACY_URL' })
|
|
},
|
|
|
|
onTerms() {
|
|
wx.navigateTo({ url: '/pages/webview/webview?url=YOUR_TERMS_URL' })
|
|
},
|
|
})
|