app.js 4.8 KB

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