index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <view v-if="isLoad" class="login" :style="appThemeStyle">
  3. <MpWeixin v-if="isMpWeixinAuth" @success="onGetUserInfoSuccess" />
  4. <Main v-else :isParty="isParty" :partyData="partyData" :isMpWeixinMobile="isMpWeixinMobile" />
  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. setting: {},
  23. // 是否显示微信小程序授权登录
  24. isMpWeixinAuth: false,
  25. // 是否显示微信小程序端 一键授权手机号
  26. isMpWeixinMobile: false,
  27. // 是否存在第三方用户信息
  28. isParty: false,
  29. // 第三方用户信息数据
  30. partyData: {}
  31. }
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. async onLoad(options) {
  37. // 获取注册设置
  38. await this.getRegisterSetting()
  39. // 设置当前是否显示第三方授权登录
  40. await this.setShowUserInfo()
  41. // 数据加载完成
  42. this.isLoad = true
  43. },
  44. methods: {
  45. // 获取注册设置 [后台-客户端-注册设置]
  46. async getRegisterSetting() {
  47. // this.setting = await SettingModel.item(SettingKeyEnum.REGISTER.value, false)
  48. },
  49. /**
  50. * 设置当前是否显示第三方授权登录
  51. * - 条件1: 只有对应的客户端显示获取用户信息按钮, 例如微信小程序、微信公众号
  52. * - 条件2: 注册设置是否已开启该选项
  53. */
  54. async setShowUserInfo() {
  55. const app = this
  56. // 判断当前客户端是微信小程序
  57. const isMpWeixin = app.platform === 'MP-WEIXIN'
  58. // 判断是否显示第三方授权登录
  59. app.isMpWeixinAuth = isMpWeixin && Boolean(app.setting.isOauthMpweixin)
  60. app.isMpWeixinMobile = isMpWeixin && Boolean(app.setting.isOauthMobileMpweixin)
  61. },
  62. // 获取到用户信息的回调函数
  63. onGetUserInfoSuccess(result) {
  64. // 记录第三方用户信息数据
  65. this.partyData = result
  66. // 显示注册页面
  67. this.onShowRegister()
  68. },
  69. // 显示注册页面
  70. onShowRegister() {
  71. // 是否显示微信小程序授权登录
  72. if (this.partyData.oauth === 'MP-WEIXIN') {
  73. this.isMpWeixinAuth = false
  74. }
  75. // 已获取到了第三方用户信息
  76. this.isParty = true
  77. }
  78. }
  79. }
  80. </script>
  81. <style lang="scss" scoped></style>