app.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <div class="layout" v-if="isMounted">
  3. <div class="header">
  4. <div class="navbar flex justify-between items-center">
  5. <div class="logo flex items-center" @click="backHome">
  6. <img
  7. src="https://static.caimei365.com/www/authentic/h5/icon-logo.png"
  8. />
  9. <span>认证通</span>
  10. </div>
  11. <div class="user-info">
  12. <template v-if="accessToken">
  13. <span v-text="userInfo.mobile"></span>
  14. <span class="underline logout" @click="logout">退出登录</span>
  15. </template>
  16. <template v-else>
  17. <div
  18. class="login pr-3 pl-3 border rounded-sm border-white leading-6"
  19. @click="onLogin"
  20. >
  21. 登录
  22. </div>
  23. </template>
  24. </div>
  25. </div>
  26. </div>
  27. <div class="content">
  28. <nuxt />
  29. </div>
  30. <div class="footer flex justify-center items-center">
  31. - 由采美网提供技术支持 -
  32. </div>
  33. <SimpleLogin></SimpleLogin>
  34. </div>
  35. </template>
  36. <script>
  37. import { mapGetters } from 'vuex'
  38. import { isWeChat } from '~/utils/validator'
  39. import { toAuthorization } from '~/utils'
  40. export default {
  41. computed: {
  42. ...mapGetters([
  43. 'userInfo',
  44. 'accessToken',
  45. 'authUserId',
  46. 'appId',
  47. 'accountType',
  48. 'routePrefix',
  49. ]),
  50. },
  51. head() {
  52. return {
  53. link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
  54. }
  55. },
  56. data() {
  57. return {
  58. isMounted: false,
  59. }
  60. },
  61. mounted() {
  62. this.init()
  63. console.log(1)
  64. },
  65. beforeDestroy() {
  66. window.removeEventListener('resize', () => {})
  67. },
  68. methods: {
  69. init() {
  70. this.responseWidth()
  71. this.initPageData()
  72. },
  73. // 初始化数据页面公共数据
  74. initPageData() {
  75. // 获取供应商id
  76. const authUserId = parseInt(this.$route.params.template)
  77. const routePrefix = `/${authUserId}/app`
  78. // 保存页面路由前缀
  79. this.$store.commit('app/SET_ROUTE_PREFIX', routePrefix)
  80. // 保存用户AppId
  81. this.$store.commit('user/SET_AUTH_USER_ID', authUserId)
  82. // 获取用户信息
  83. let userInfo = this.$getStorage(routePrefix, 'userInfo')
  84. if (userInfo && userInfo.authUserId === authUserId) {
  85. this.$store.commit('user/SET_USER_INFO', userInfo)
  86. }
  87. // 初始化供应商信息
  88. this.fetchSupplierInfo()
  89. },
  90. // 获取供应商信息
  91. async fetchSupplierInfo() {
  92. try {
  93. const res = await this.$http.api.fetchSupplierInfo({
  94. authUserId: this.authUserId,
  95. })
  96. this.$store.commit('supplier/SET_SUPPLIER_INFO', res.data)
  97. this.$store.commit('user/SET_APPID', res.data.appId)
  98. // 如果appId存在
  99. if (res.data.appId) {
  100. this.checkAccountType(res.data.appId)
  101. }
  102. } catch (error) {
  103. console.log(error)
  104. } finally {
  105. this.isMounted = true
  106. // 清除缓存
  107. this.refreshCacheData()
  108. }
  109. },
  110. // 校验公众号类型
  111. async checkAccountType(appId) {
  112. try {
  113. // 1订阅号,2服务号
  114. const res = await this.$http.api.checkAccountType({ appId })
  115. this.$store.commit('user/SET_ACCOUNT_TYPE', res.data)
  116. } catch (error) {
  117. console.log(error)
  118. }
  119. },
  120. onLogin() {
  121. // 在微信浏览器中使用微信授权登录
  122. if (isWeChat() && this.appId && this.accountType === 2) {
  123. const payload = {
  124. authUserId: this.authUserId,
  125. routePrefix: this.routePrefix,
  126. }
  127. return toAuthorization(this.appId, payload)
  128. }
  129. this.$store.commit('app/SHOW_LOGIN')
  130. },
  131. // 退出登录
  132. logout() {
  133. this.$store.dispatch('user/logout')
  134. console.log(this.routePrefix)
  135. this.$removeStorage(this.routePrefix, 'userInfo')
  136. this.backHome()
  137. },
  138. // 回到首页
  139. backHome() {
  140. if (this.$route.path === this.routePrefix) return
  141. this.$router.replace(this.routePrefix)
  142. },
  143. // 响应页面宽度变化
  144. responseWidth() {
  145. this.$store.commit('app/SET_SCREEN', window.innerWidth)
  146. window.addEventListener('resize', (e) => {
  147. this.$store.commit('app/SET_SCREEN', e.target.innerWidth)
  148. })
  149. },
  150. // 数据初始化刷新浏览器
  151. refreshCacheData() {
  152. this.$removeStorage(this.routePrefix, 'club_list_data')
  153. },
  154. },
  155. }
  156. </script>
  157. <style scoped lang="scss">
  158. // PC端
  159. @media screen and (min-width: 768px) {
  160. .layout {
  161. padding-top: 80px;
  162. user-select: none;
  163. .header {
  164. position: fixed;
  165. top: 0;
  166. left: 0;
  167. z-index: 999;
  168. width: 100%;
  169. height: 80px;
  170. box-sizing: border-box;
  171. background: linear-gradient(90deg, #101010 0%, #404040 100%);
  172. .navbar {
  173. width: 1200px;
  174. margin: 0 auto;
  175. height: 100%;
  176. }
  177. .logo {
  178. cursor: pointer;
  179. img {
  180. display: block;
  181. width: 44px;
  182. height: 44px;
  183. }
  184. span {
  185. font-size: 24px;
  186. color: #fff;
  187. }
  188. }
  189. .user-info {
  190. color: #fff;
  191. font-size: 16px;
  192. .login,
  193. .logout {
  194. cursor: pointer;
  195. }
  196. }
  197. }
  198. .content {
  199. min-height: calc(100vh - 80px - 80px);
  200. background-color: #f7f7f7;
  201. overflow: hidden;
  202. }
  203. .footer {
  204. height: 80px;
  205. background-color: #2c3038;
  206. color: #fff;
  207. font-size: 14px;
  208. }
  209. }
  210. }
  211. // 移动端
  212. @media screen and (max-width: 768px) {
  213. .layout {
  214. padding-top: 12.8vw;
  215. .header {
  216. position: fixed;
  217. top: 0;
  218. left: 0;
  219. z-index: 999;
  220. width: 100%;
  221. padding: 0 2.4vw;
  222. height: 12.8vw;
  223. box-sizing: border-box;
  224. background: linear-gradient(90deg, #101010 0%, #404040 100%);
  225. .navbar {
  226. height: 100%;
  227. }
  228. .logo {
  229. img {
  230. display: block;
  231. width: 8vw;
  232. height: 8vw;
  233. }
  234. span {
  235. font-size: 4vw;
  236. color: #fff;
  237. }
  238. }
  239. .user-info {
  240. color: #fff;
  241. font-size: 3vw;
  242. }
  243. }
  244. .content {
  245. min-height: calc(100vh - 12.8vw - 12.4vw);
  246. }
  247. .footer {
  248. height: 12.4vw;
  249. background-color: #2c3038;
  250. color: #fff;
  251. font-size: 3vw;
  252. }
  253. }
  254. }
  255. </style>