index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. await SettingModel.item(SettingKeyEnum.REGISTER.value, false)
  53. .then(setting => {
  54. app.isMpWeixinAuth = isMpWeixin && setting.isOauthMpweixin
  55. console.log('setShowUserInfo complete')
  56. })
  57. },
  58. // 获取到用户信息的回调函数
  59. onGetUserInfoSuccess({ oauth, code, userInfo }) {
  60. // 记录第三方用户信息数据
  61. this.partyData = { oauth, code, userInfo }
  62. // 显示注册页面
  63. this.onShowRegister()
  64. },
  65. // 显示注册页面
  66. onShowRegister() {
  67. // 是否显示微信小程序授权登录
  68. this.isMpWeixinAuth = false
  69. // 已获取到了第三方用户信息
  70. this.isParty = true
  71. }
  72. }
  73. }
  74. </script>
  75. <style lang="scss" scoped>
  76. </style>