App.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <script>
  2. import Vue from 'vue'
  3. import {
  4. getUserInfo,
  5. getConfigInfo,
  6. webLogin,
  7. wxConfig,
  8. redirection,
  9. mini_env,
  10. getQueryVariable
  11. } from '@/common/utils/util'
  12. import wx from 'weixin-js-sdk'
  13. import api from '@/common/http/'
  14. var getUserValTimeId = null
  15. import store from '@/store/index.js'
  16. export default {
  17. async onLaunch() {
  18. api
  19. .get('/user/user/log', {})
  20. .then(res => {})
  21. .catch(() => {})
  22. mini_env(bool => {
  23. if (bool) {
  24. api
  25. .post('/user/open/bindOpenId', {})
  26. .then(res => {})
  27. .catch(() => {})
  28. }
  29. })
  30. // #ifdef MP-WEIXIN
  31. // 小程序更新
  32. const updateManager = uni.getUpdateManager()
  33. // 请求完新版本信息
  34. updateManager.onCheckForUpdate(res => {
  35. // console.log(res.hasUpdate);
  36. })
  37. // 新的版本已经下载好
  38. updateManager.onUpdateReady(res => {
  39. uni.showModal({
  40. title: '更新提示',
  41. content: '新版本已经准备好,是否重启应用?',
  42. success(res) {
  43. if (res.confirm) {
  44. // 调用 applyUpdate 应用新版本并重启
  45. updateManager.applyUpdate()
  46. }
  47. }
  48. })
  49. })
  50. // 新的版本下载失败
  51. updateManager.onUpdateFailed(res => {})
  52. // #endif
  53. uni.$on('updateUserInfo', () => {
  54. this.updateUserInfo()
  55. })
  56. uni.getSystemInfo({
  57. success: function (e) {
  58. console.log(e)
  59. Vue.prototype.StatusBar = e.statusBarHeight
  60. // #ifdef MP-WEIXIN
  61. let custom = wx.getMenuButtonBoundingClientRect()
  62. Vue.prototype.Custom = custom
  63. Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight + 4
  64. // #endif
  65. // #ifndef MP-WEIXIN
  66. Vue.prototype.CustomBar = 48
  67. // #endif
  68. //用来判断是否iphoneX类型的全面屏设备
  69. if (e.model.indexOf('iPhone X') == 0) {
  70. Vue.prototype.isIphoneX = 68
  71. } else {
  72. Vue.prototype.isIphoneX = 0
  73. }
  74. }
  75. })
  76. this.$setStorage('realAuthUrl', window.location.href.split('#')[0])
  77. // #ifdef MP-WEIXIN
  78. this.wxLogin()
  79. // #endif
  80. // #ifdef H5
  81. webLogin().then(async res => {
  82. redirection()
  83. const userInfo = await getUserInfo()
  84. console.log(userInfo)
  85. if (!userInfo?.avatar || !userInfo?.nickName) {
  86. let randomNum = new Date().getTime().toString().substr(-6)
  87. let configInfo = await getConfigInfo()
  88. api
  89. .post('/user/userinfo/save', {
  90. userId: store.state.user.userId,
  91. avatarUrl: configInfo.minLogo3,
  92. nickName: `微信用户_${randomNum}`
  93. })
  94. .then(res => {
  95. wxConfig(configInfo, res.data)
  96. })
  97. } else {
  98. let configInfo = await getConfigInfo()
  99. wxConfig(configInfo, {
  100. nickName: store.state.user.name,
  101. userId: store.state.user.userId
  102. })
  103. }
  104. // 如果链接带有serviceId,则绑定用户
  105. if (getQueryVariable('serviceId')) {
  106. api
  107. .post('/user/bind', {
  108. serviceId: getQueryVariable('serviceId'),
  109. userId: store.state.user.userId
  110. })
  111. .then(res => {
  112. console.log('绑定成功:' + res.message)
  113. })
  114. }
  115. })
  116. this.$isResolve()
  117. // #endif
  118. },
  119. onShow: function () {},
  120. onHide: function () {},
  121. methods: {
  122. wxLogin() {
  123. uni.login({
  124. provider: 'weixin',
  125. success: loginRes => {
  126. this.$api
  127. .post('/user/auth', {
  128. code: loginRes.code
  129. })
  130. .then(async res => {
  131. this.$store.commit('user/set_token', res.data.token)
  132. this.$store.commit('user/set_openId', res.data.openId)
  133. this.$store.commit('user/set_name', res.data.nickName)
  134. this.$store.commit('user/set_avatar', res.data.avatar)
  135. this.$store.commit('user/set_userId', res.data.userId)
  136. if (res.data.mobile) {
  137. this.$store.commit('user/set_mobile', res.data.mobile)
  138. }
  139. if (!res.data.avatar || !res.data.nickName) {
  140. await this.saveUserInfo(res.data)
  141. }
  142. this.$isResolve()
  143. })
  144. .catch(res => {
  145. this.$isReject()
  146. })
  147. }
  148. })
  149. },
  150. // 保存用户信息
  151. async saveUserInfo(userInfo) {
  152. let randomNum = new Date().getTime().toString().substr(-6)
  153. let configInfo = await this.$getConfigInfo()
  154. return new Promise((resolve, reject) => {
  155. this.$api
  156. .post('/user/userinfo/save', {
  157. userId: userInfo.userId,
  158. avatarUrl: configInfo.minLogo3,
  159. nickName: `微信用户_${randomNum}`
  160. })
  161. .then(res => {
  162. resolve(res.data)
  163. })
  164. })
  165. },
  166. async updateUserInfo() {}
  167. }
  168. }
  169. </script>
  170. <style lang="scss">
  171. /*每个页面公共css */
  172. @import 'uview-ui/index.scss';
  173. @import 'styles/iconfont.css';
  174. </style>