app.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.showModal({
  66. // title: '提示',
  67. // content: '请使用企业身份登录',
  68. // mask: false,
  69. // success: function(res) {
  70. // if (res.confirm) {
  71. // uni.navigateTo({
  72. // url: '/pages/login/login'
  73. // })
  74. // }
  75. // }
  76. // })
  77. return false;
  78. }
  79. if (res.code == 1) {
  80. //个人用户,拒绝访问
  81. if (res.data.type == 1) {
  82. uni.showToast({
  83. icon: 'none',
  84. title: '仅对企业开放',
  85. complete: () => {
  86. uni.navigateTo({
  87. url: '/pages/login/login'
  88. })
  89. }
  90. })
  91. return false;
  92. }
  93. //企业用户
  94. if (res.data.type == 2) {
  95. authLogin(res.data).then(auth => {
  96. commit('setUserInfo', auth.data)
  97. });
  98. }
  99. }
  100. resolve()
  101. })
  102. })
  103. },
  104. getConfig({
  105. state,
  106. commit
  107. }) {
  108. return new Promise(resolve => {
  109. getConfig({}).then(res => {
  110. if (res.status == 1) {
  111. commit('setConfig', res.data)
  112. }
  113. resolve()
  114. })
  115. })
  116. },
  117. getSystemInfo({
  118. state,
  119. commit
  120. }) {
  121. uni.getSystemInfo({
  122. success: res => {
  123. let {
  124. statusBarHeight,
  125. platform,
  126. } = res;
  127. let navHeight;
  128. if (platform == 'ios' || platform == 'devtools') {
  129. navHeight = statusBarHeight + 44;
  130. } else {
  131. navHeight = statusBarHeight + 48;
  132. }
  133. commit('setSystemInfo', {
  134. ...res,
  135. navHeight,
  136. })
  137. },
  138. fail(err) {
  139. console.log(err);
  140. }
  141. });
  142. },
  143. wxShare({
  144. state
  145. }, opt) {
  146. // #ifdef H5
  147. const shareInfo = state.config.share
  148. const inviteCode = state.userInfo.distribution_code
  149. const href = window.location.href
  150. const sym = href.includes('?') ? '&' : '?'
  151. const option = {
  152. shareTitle: shareInfo.h5_share_title,
  153. shareLink: inviteCode ? `${href}${sym}invite_code=${inviteCode}` : href,
  154. shareImage: shareInfo.h5_share_image,
  155. shareDesc: shareInfo.h5_share_intro
  156. }
  157. wechath5.share(Object.assign(option, opt))
  158. // #endif
  159. }
  160. };
  161. export default {
  162. state,
  163. mutations,
  164. actions
  165. };