index.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <template>
  2. <view class="layout" :style="[customStyle]">
  3. <slot />
  4. <get-user-info v-model="showInfo" @success="handleGetUserSuccess" />
  5. <get-user-phone v-model="showPhone" />
  6. <tab />
  7. </view>
  8. </template>
  9. <script>
  10. /**
  11. * @param getUserInfo 显示获取用户信息弹窗
  12. * @param getPhone 显示获取手机号弹窗
  13. */
  14. import GetUserInfo from 'components/GetUserInfo'
  15. import GetUserPhone from '../components/GetUserPhone/index'
  16. import { mapState } from 'vuex'
  17. import Tab from './componets/tab'
  18. export default {
  19. name: 'Layout',
  20. components: {
  21. Tab,
  22. GetUserPhone,
  23. GetUserInfo
  24. },
  25. props: {
  26. getUserInfo: {
  27. type: Boolean,
  28. default: false
  29. },
  30. getPhone: {
  31. type: Boolean,
  32. default: false
  33. },
  34. customStyle: {
  35. type: Object,
  36. default() {
  37. return {}
  38. }
  39. }
  40. },
  41. data() {
  42. return {
  43. showInfo: false,
  44. showPhone: false
  45. }
  46. },
  47. computed: {
  48. ...mapState({
  49. token: state => state.user.token,
  50. userInfo: state => state.user.info
  51. }),
  52. isLogin() {
  53. return !!this.token
  54. }
  55. },
  56. watch: {
  57. getUserInfo(val) {
  58. this.showInfo = val
  59. },
  60. getPhone(val) {
  61. this.showPhone = val
  62. }
  63. },
  64. async mounted() {
  65. await this.login()
  66. this.checkUserInfo()
  67. },
  68. methods: {
  69. // 登录
  70. async login() {
  71. if (!this.$api.user.isLogin()) {
  72. await this.$api.user.login().then(res => {
  73. const { token, user } = res.data
  74. this.$store.dispatch('user/token', token)
  75. this.$store.dispatch('user/info', user)
  76. this.checkUserInfo()
  77. this.$jump({
  78. type: 'reload'
  79. })
  80. })
  81. }
  82. },
  83. // 检查用户信息
  84. checkUserInfo() {
  85. if (!this.token) return
  86. if (!this.userInfo || !this.userInfo.nickname) {
  87. this.showInfo = true
  88. } else if (!this.userInfo.phone_num) {
  89. this.showPhone = true
  90. }
  91. },
  92. // 获取用户信息成功
  93. handleGetUserSuccess() {
  94. if (this.userInfo && !this.userInfo.phone_num) {
  95. this.showPhone = true
  96. }
  97. }
  98. }
  99. }
  100. </script>
  101. <style lang="scss" scoped>
  102. // 这里可以定义背景色
  103. .layout{
  104. padding-bottom: 116rpx;
  105. position: relative;
  106. }
  107. </style>