| | |
| | | <image class="edit-icon" src="/static/image/mine/edit.png" mode="aspectFit" @tap="editPassword"></image> |
| | | </view> |
| | | </view> |
| | | <view class="list-item"> |
| | | <view class="list-item" @tap="swapWechat"> |
| | | <view class="list-left"> |
| | | <image class="list-icon" src="/static/image/mine/weixinIcon.png" mode="aspectFit"></image> |
| | | <text class="list-title">绑定微信</text> |
| | |
| | | <text class="wechat-name">{{ profile.wechatBinding.nickname || '已绑定微信' }}</text> |
| | | </view> |
| | | <text class="wechat-unbound" v-else>去绑定</text> |
| | | <image class="swap-icon" src="/static/image/mine/refish.png" mode="aspectFit" @tap="swapWechat"></image> |
| | | <image class="swap-icon" src="/static/image/mine/refish.png" mode="aspectFit" @tap.stop="swapWechat"></image> |
| | | </view> |
| | | </view> |
| | | </view> |
| | |
| | | }) |
| | | } catch (error) { |
| | | uni.showToast({ |
| | | title: '微信绑定失败', |
| | | title: resolveWechatBindErrorMessage(error), |
| | | icon: 'none', |
| | | }) |
| | | } finally { |
| | |
| | | */ |
| | | function requestWechatAuthorization() { |
| | | return new Promise<WechatLoginResult>((resolve, reject) => { |
| | | // #ifdef APP-PLUS |
| | | plus.oauth.getServices((services) => { |
| | | const weixinService = services.find((service) => service.id === 'weixin') |
| | | if (!weixinService) { |
| | | reject({ |
| | | code: -3, |
| | | errMsg: '当前环境不支持微信登录', |
| | | }) |
| | | return |
| | | } |
| | | if (!weixinService.nativeClient) { |
| | | reject({ |
| | | code: -8, |
| | | errMsg: '请先安装微信客户端', |
| | | }) |
| | | return |
| | | } |
| | | requestWechatAuthorizeByService(weixinService, resolve, reject) |
| | | }, (error: any) => { |
| | | reject({ |
| | | code: error?.code, |
| | | errMsg: error?.message || '获取微信登录服务失败', |
| | | }) |
| | | }) |
| | | return |
| | | // #endif |
| | | |
| | | uni.login({ |
| | | provider: 'weixin', |
| | | success: (res) => { |
| | |
| | | reject(error) |
| | | }, |
| | | }) |
| | | }) |
| | | } |
| | | |
| | | /** |
| | | * 通过微信原生服务执行授权;如存在旧授权态则先退出再重新拉起。 |
| | | */ |
| | | function requestWechatAuthorizeByService( |
| | | weixinService: any, |
| | | resolve: (value: WechatLoginResult) => void, |
| | | reject: (reason?: any) => void |
| | | ) { |
| | | const executeAuthorize = () => { |
| | | weixinService.authorize((event: any) => { |
| | | const code = normalizeOptionalText(event?.code) |
| | | const authResult = weixinService.authResult || {} |
| | | const accessToken = normalizeOptionalText(authResult.access_token || authResult.accessToken) |
| | | const openid = normalizeOptionalText(authResult.openid || authResult.openId) |
| | | if (!code && !(accessToken && openid)) { |
| | | reject({ |
| | | code: -99, |
| | | errMsg: '微信授权未返回有效凭证', |
| | | }) |
| | | return |
| | | } |
| | | resolve({ |
| | | code, |
| | | authResult: { |
| | | access_token: authResult.access_token, |
| | | accessToken: authResult.accessToken, |
| | | openid: authResult.openid, |
| | | openId: authResult.openId, |
| | | unionid: authResult.unionid, |
| | | unionId: authResult.unionId, |
| | | }, |
| | | }) |
| | | }, (error: any) => { |
| | | reject({ |
| | | code: error?.code, |
| | | errMsg: error?.message || '微信授权失败', |
| | | }) |
| | | }, { |
| | | scope: 'snsapi_userinfo', |
| | | state: 'bind_wechat', |
| | | }) |
| | | } |
| | | |
| | | const authResult = weixinService?.authResult || {} |
| | | const hasAuthorized = !!normalizeOptionalText(authResult.access_token || authResult.accessToken || authResult.openid || authResult.openId) |
| | | if (!hasAuthorized || typeof weixinService.logout !== 'function') { |
| | | executeAuthorize() |
| | | return |
| | | } |
| | | weixinService.logout(() => { |
| | | executeAuthorize() |
| | | }, () => { |
| | | executeAuthorize() |
| | | }) |
| | | } |
| | | |
| | |
| | | }) |
| | | return null |
| | | } |
| | | |
| | | /** |
| | | * 将微信绑定错误归一化为可直接提示给用户的文案。 |
| | | */ |
| | | function resolveWechatBindErrorMessage(error: any) { |
| | | const errorMessage = normalizeOptionalText(error?.errMsg || error?.message) |
| | | if (errorMessage) { |
| | | return errorMessage |
| | | } |
| | | return '微信绑定失败' |
| | | } |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |