app.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import {
  2. getInfoByToken,
  3. authLogin,
  4. getConfig
  5. } from '@/api/app';
  6. import {
  7. USER_INFO,
  8. TOKEN,
  9. CONFIG
  10. } from '@/config/cachekey';
  11. import Cache from '@/utils/cache'
  12. import wechath5 from '@/utils/wechath5'
  13. import {
  14. baseURL,
  15. basePath
  16. } from '@/config/app'
  17. const state = {
  18. config: Cache.get(CONFIG) || {
  19. center_setting: {},
  20. index_setting: {},
  21. navigation_menu: [],
  22. navigation_setting: {},
  23. share: {}
  24. },
  25. userInfo: Cache.get('USERINFO') || {},
  26. token: Cache.get(TOKEN) || null,
  27. sysInfo: {},
  28. };
  29. const mutations = {
  30. login(state, opt) {
  31. console.log('登录成功')
  32. console.log(opt)
  33. state.token = opt.token;
  34. Cache.set(TOKEN, opt.token, 2 * 60 * 60); //保存2小时
  35. this.dispatch('getUser')
  36. },
  37. logout(state) {
  38. state.token = undefined;
  39. state.userInfo = undefined;
  40. Cache.remove(TOKEN);
  41. Cache.remove('USERINFO');
  42. Cache.remove('USERINFOLIST');
  43. },
  44. setUserInfo(state, userInfo) {
  45. state.userInfo = userInfo;
  46. Cache.set('USERINFO', userInfo, 2 * 60 * 60);
  47. },
  48. setConfig(state, data) {
  49. state.config = Object.assign(state.config, data)
  50. Cache.set(CONFIG, state.config);
  51. },
  52. setSystemInfo(state, data) {
  53. state.sysInfo = data
  54. }
  55. };
  56. const actions = {
  57. getUser({
  58. state,
  59. commit
  60. }) {
  61. return new Promise(resolve => {
  62. getInfoByToken().then(res => {
  63. //token 过期,重新登录
  64. if (res.code == 2) {
  65. uni.navigateTo({
  66. url: '/pages/login/login'
  67. })
  68. return false;
  69. }
  70. if (res.code == 1) {
  71. //个人用户,拒绝访问
  72. if (res.data.type == 1) {
  73. uni.showToast({
  74. icon: 'none',
  75. title: '仅对企业开放',
  76. complete: () => {
  77. uni.navigateTo({
  78. url: '/pages/login/login'
  79. })
  80. }
  81. })
  82. return false;
  83. }
  84. //企业用户
  85. if (res.data.type == 2) {
  86. authLogin(res.data).then(auth => {
  87. commit('setUserInfo', auth.data)
  88. });
  89. }
  90. }
  91. resolve()
  92. })
  93. })
  94. },
  95. getConfig({
  96. state,
  97. commit
  98. }) {
  99. return new Promise(resolve => {
  100. getConfig({}).then(res => {
  101. if (res.status == 1) {
  102. commit('setConfig', res.data)
  103. }
  104. resolve()
  105. })
  106. })
  107. },
  108. getSystemInfo({
  109. state,
  110. commit
  111. }) {
  112. uni.getSystemInfo({
  113. success: res => {
  114. let {
  115. statusBarHeight,
  116. platform,
  117. } = res;
  118. let navHeight;
  119. if (platform == 'ios' || platform == 'devtools') {
  120. navHeight = statusBarHeight + 44;
  121. } else {
  122. navHeight = statusBarHeight + 48;
  123. }
  124. commit('setSystemInfo', {
  125. ...res,
  126. navHeight,
  127. })
  128. },
  129. fail(err) {
  130. console.log(err);
  131. }
  132. });
  133. },
  134. wxShare({
  135. state
  136. }, opt) {
  137. // #ifdef H5
  138. const shareInfo = state.config.share
  139. const inviteCode = state.userInfo.distribution_code
  140. const href = window.location.href
  141. const sym = href.includes('?') ? '&' : '?'
  142. const option = {
  143. shareTitle: shareInfo.h5_share_title,
  144. shareLink: inviteCode ? `${href}${sym}invite_code=${inviteCode}` : href,
  145. shareImage: shareInfo.h5_share_image,
  146. shareDesc: shareInfo.h5_share_intro
  147. }
  148. wechath5.share(Object.assign(option, opt))
  149. // #endif
  150. }
  151. };
  152. export default {
  153. state,
  154. mutations,
  155. actions
  156. };