index.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view v-if="isLoad" class="login">
  3. <MpWeixin v-if="isMpWeixinAuth" @success="onGetUserInfoSuccess" />
  4. <Main v-else :isParty="isParty" :partyData="partyData" />
  5. </view>
  6. </template>
  7. <script>
  8. import Main from './components/main'
  9. import MpWeixin from './components/mp-weixin'
  10. import SettingKeyEnum from '@/common/enum/setting/Key'
  11. import SettingModel from '@/common/model/Setting'
  12. export default {
  13. components: {
  14. Main,
  15. MpWeixin
  16. },
  17. data() {
  18. return {
  19. // 数据加载完成 [防止在微信小程序端onLoad和view渲染同步进行]
  20. isLoad: false,
  21. // 是否显示微信小程序授权登录
  22. isMpWeixinAuth: false,
  23. // 是否存在第三方用户信息
  24. isParty: false,
  25. // 第三方用户信息数据
  26. partyData: {}
  27. }
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. async onLoad(options) {
  33. console.log('login onLoad')
  34. // 设置当前是否显示微信小程序授权登录
  35. await this.setShowUserInfo()
  36. // 数据加载完成
  37. this.isLoad = true
  38. console.log('isLoad', true)
  39. },
  40. methods: {
  41. /**
  42. * 设置当前是否显示微信小程序授权登录
  43. * - 条件1: 只有微信小程序才显示获取用户信息按钮
  44. * - 条件2: 后台设置是否已开启该选项 [后台-客户端-注册设置]
  45. */
  46. async setShowUserInfo() {
  47. console.log('setShowUserInfo start')
  48. const app = this
  49. // 判断当前客户端是微信小程序, 并且支持getUserProfile接口
  50. const isMpWeixin = app.$platform === 'MP-WEIXIN' && wx.canIUse('getUserProfile')
  51. // 获取后台设置
  52. // 优化建议: 可以将isCache改为true启用缓存
  53. await SettingModel.item(SettingKeyEnum.REGISTER.value, false)
  54. .then(setting => {
  55. app.isMpWeixinAuth = isMpWeixin && setting.isOauthMpweixin
  56. console.log('setShowUserInfo complete')
  57. })
  58. },
  59. // 获取到用户信息的回调函数
  60. onGetUserInfoSuccess({ oauth, code, userInfo }) {
  61. // 记录第三方用户信息数据
  62. this.partyData = { oauth, code, userInfo }
  63. // 显示注册页面
  64. this.onShowRegister()
  65. },
  66. // 显示注册页面
  67. onShowRegister() {
  68. // 是否显示微信小程序授权登录
  69. this.isMpWeixinAuth = false
  70. // 已获取到了第三方用户信息
  71. this.isParty = true
  72. }
  73. }
  74. }
  75. </script>
  76. <style lang="scss" scoped>
  77. </style>