index.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <div>
  3. <MpWeixin v-if="isShowUserInfo" @success="onGetUserInfoSuccess" />
  4. <Main v-else :isParty="isExistUserInfo" :partyData="partyData" />
  5. </div>
  6. </template>
  7. <script>
  8. import Main from './components/main'
  9. import MpWeixin from './components/mp-weixin'
  10. export default {
  11. components: {
  12. Main,
  13. MpWeixin
  14. },
  15. data() {
  16. return {
  17. // 是否显示获取用户信息组件
  18. isShowUserInfo: false,
  19. // 是否已获取到了用户信息
  20. isExistUserInfo: false,
  21. // 第三方用户信息数据
  22. partyData: {}
  23. }
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad(options) {
  29. const app = this
  30. // 只有微信小程序才显示获取用户信息按钮
  31. // #ifdef MP-WEIXIN
  32. app.isShowUserInfo = wx.canIUse('getUserProfile')
  33. // #endif
  34. },
  35. methods: {
  36. // 获取到用户信息的回调函数
  37. onGetUserInfoSuccess({ oauth, code, userInfo }) {
  38. // 记录第三方用户信息数据
  39. this.partyData = { oauth, code, userInfo }
  40. // 显示注册页面
  41. this.onShowRegister()
  42. },
  43. // 显示注册页面
  44. onShowRegister() {
  45. // 是否显示获取用户信息组件
  46. this.isShowUserInfo = false
  47. // 是否已获取到了用户信息
  48. this.isExistUserInfo = true
  49. }
  50. }
  51. }
  52. </script>
  53. <style lang="scss" scoped>
  54. </style>