123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <view class="MainPage">
  3. <slot></slot>
  4. <view class="PhoneAuthPopup" v-show="ShowPhoneAuthPopup">
  5. <view class="centerLabel">
  6. <text>授权手机号</text>
  7. <text>申请使用您的手机号</text>
  8. <view class="flex-h">
  9. <text @tap="ShowPhoneAuthPopup = false; $emit('UserInfoChange')">拒绝</text>
  10. <view class="flex-item"></view>
  11. <text>允许</text>
  12. <button open-type="getPhoneNumber" @getphonenumber="GetUserPhone">获取授权</button>
  13. </view>
  14. </view>
  15. </view>
  16. <view class="UserIconAuthPopup" v-show="ShowUserIconAuthPopup">
  17. <view class="centerLabel">
  18. <text>授权头像</text>
  19. <text>申请使用您的头像</text>
  20. <view class="flex-h">
  21. <text @tap="ShowUserIconAuthPopup = false; $emit('UserInfoChange')">拒绝</text>
  22. <view class="flex-item"></view>
  23. <text>允许</text>
  24. <button open-type="getUserInfo" @getuserinfo="GetUserIcon">获取授权</button>
  25. </view>
  26. </view>
  27. </view>
  28. <view class="StudentIdPopup" v-show="ShowStudentIdPopup">
  29. <view class="centerLabel">
  30. <text>完善信息</text>
  31. <text>去完善您的个人信息</text>
  32. <view>
  33. <navigator url="/pages/Mine/MyInfo/index">去完善</navigator>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import Taro from '@tarojs/taro'
  41. import { createNamespacedHelpers } from 'vuex'
  42. const { mapState: mapUserState, mapActions: mapUserActions, mapMutations: mapUserMutations } = createNamespacedHelpers('user')
  43. export default {
  44. name: 'MainPage',
  45. props: {
  46. Data: {
  47. default: null,
  48. type: Object
  49. }
  50. },
  51. data () {
  52. return {
  53. ShowPhoneAuthPopup: false,
  54. ShowUserIconAuthPopup: false,
  55. ShowStudentIdPopup: false,
  56. WxInfoData: {
  57. encryptedData: null,
  58. iv: null,
  59. rawData: null,
  60. signature: null
  61. }
  62. }
  63. },
  64. computed: {
  65. ...mapUserState({
  66. UserInfo: x => x.UserInfo // 用户信息
  67. })
  68. },
  69. components: {
  70. },
  71. created () {
  72. this.Init()
  73. },
  74. methods: {
  75. ...mapUserActions([
  76. 'WxGetPhoneAuth',
  77. 'WxLogin',
  78. 'PutUserInfo'
  79. ]),
  80. ...mapUserMutations([
  81. 'EditUserInfo'
  82. ]),
  83. Init () {
  84. const _that = this
  85. let CurrentPageRoute = Taro.getCurrentPages()[Taro.getCurrentPages().length - 1].route
  86. if (this.UserInfo === null) {
  87. wx.login({
  88. success (res) {
  89. _that.WxLogin({ queryData: { code: res.code } }).then((res) => {
  90. wx.setStorageSync('token', res.data.data.token)
  91. wx.setStorageSync('tokentime', Date.now())
  92. if (CurrentPageRoute !== 'pages/Index/index') { // 非首页
  93. // if (CurrentPageRoute !== '') { // 非首页
  94. if (!_that.UserInfo.avatar) { // 未授权头像
  95. _that.ShowUserIconAuthPopup = true
  96. } else if (!_that.UserInfo.phone) { // 未授权手机号
  97. _that.ShowPhoneAuthPopup = true
  98. } else {
  99. _that.$emit('UserInfoChange')
  100. }
  101. } else {
  102. _that.$emit('UserInfoChange')
  103. }
  104. })
  105. }
  106. })
  107. } else {
  108. if (CurrentPageRoute !== 'pages/Index/index') { // 非首页
  109. // if (CurrentPageRoute !== '') { // 非首页
  110. if (!_that.UserInfo.avatar) { // 未授权头像
  111. _that.ShowUserIconAuthPopup = true
  112. } else if (!_that.UserInfo.phone) { // 未授权手机号
  113. _that.ShowPhoneAuthPopup = true
  114. } else {
  115. _that.$emit('UserInfoChange')
  116. }
  117. } else {
  118. _that.$emit('UserInfoChange')
  119. }
  120. }
  121. },
  122. GetUserIcon (e) {
  123. if (e.detail.userInfo.avatarUrl) {
  124. this.PutUserInfo({ data: { data: { avatar: e.detail.userInfo.avatarUrl, personId: this.UserInfo.personId } } }).then(() => {
  125. this.EditUserInfo({ name: 'avatar', value: e.detail.userInfo.avatarUrl })
  126. this.ShowUserIconAuthPopup = false
  127. if (!this.UserInfo.phone) {
  128. this.ShowPhoneAuthPopup = true
  129. }
  130. })
  131. } else {
  132. if (!this.UserInfo.phone) {
  133. this.ShowPhoneAuthPopup = true
  134. }
  135. }
  136. },
  137. GetUserPhone (e) {
  138. console.log('------------->', e)
  139. for (let key in this.WxInfoData) {
  140. this.WxInfoData[key] = e.detail[key]
  141. }
  142. this.WxGetPhoneAuth({ data: { data: { ...this.WxInfoData, sessionKey: this.UserInfo.sessionKey } } }).then((res) => {
  143. if (res.data.data.phone) {
  144. this.PutUserInfo({ data: { data: { phone: res.data.data.phone, personId: this.UserInfo.personId } } }).then(() => {
  145. this.EditUserInfo({ name: 'phone', value: res.data.data.phone })
  146. this.ShowPhoneAuthPopup = false
  147. this.$emit('UserInfoChange')
  148. })
  149. } else {
  150. wx.showToast({
  151. title: '获取手机号失败',
  152. icon: 'none',
  153. duration: 2000
  154. })
  155. this.ShowPhoneAuthPopup = false
  156. this.$emit('UserInfoChange')
  157. }
  158. })
  159. }
  160. }
  161. }
  162. </script>
  163. <style lang="scss">
  164. @import "page.scss";
  165. </style>