app.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {
  2. getUser
  3. } from '@/api/user'
  4. import {
  5. getCartNum
  6. } from '@/api/store';
  7. import {
  8. USER_INFO,
  9. TOKEN,
  10. CONFIG
  11. } from '@/config/cachekey';
  12. import Cache from '@/utils/cache'
  13. import wechath5 from '@/utils/wechath5'
  14. import {
  15. isWeixinClient
  16. } from '@/utils/tools'
  17. import {
  18. baseURL,
  19. basePath
  20. } from '@/config/app'
  21. const state = {
  22. config: Cache.get(CONFIG) || {
  23. center_setting: {},
  24. index_setting: {},
  25. navigation_menu: [],
  26. navigation_setting: {},
  27. share: {}
  28. },
  29. userInfo: {
  30. user_money: 0,
  31. user_integral: 0,
  32. coupon: 0
  33. },
  34. token: Cache.get(TOKEN) || null,
  35. cartNum: "",
  36. sysInfo: {}
  37. };
  38. const mutations = {
  39. login(state, opt) {
  40. state.token = opt.token;
  41. Cache.set(TOKEN, opt.token, 59 * 24 * 60 * 60);
  42. this.dispatch('getUser')
  43. },
  44. logout(state) {
  45. state.token = undefined;
  46. state.userInfo = {
  47. user_money: 0,
  48. user_integral: 0,
  49. coupon: 0
  50. }
  51. Cache.remove(TOKEN);
  52. },
  53. setCartNum(state, num) {
  54. state.cartNum = num
  55. },
  56. setUserInfo(state, user) {
  57. state.userInfo = user
  58. },
  59. setConfig(state, data) {
  60. state.config = Object.assign(state.config, data)
  61. Cache.set(CONFIG, state.config);
  62. },
  63. setSystemInfo(state, data) {
  64. state.sysInfo = data
  65. }
  66. };
  67. const actions = {
  68. getCartNum({
  69. state,
  70. commit
  71. }) {
  72. return new Promise(resolve => {
  73. if (!state.token) return uni.removeTabBarBadge({
  74. index: 3
  75. })
  76. getCartNum().then(res => {
  77. if (res.code == 1) {
  78. commit('setCartNum', res.data.num)
  79. if (!res.data.num) return uni.removeTabBarBadge({
  80. index: 3
  81. })
  82. uni.setTabBarBadge({
  83. index: 3,
  84. text: String(res.data.num)
  85. })
  86. resolve()
  87. }
  88. })
  89. })
  90. },
  91. getUser({
  92. state,
  93. commit
  94. }) {
  95. return new Promise(resolve => {
  96. getUser().then(res => {
  97. if (res.code == 1) {
  98. commit('setUserInfo', res.data)
  99. }
  100. resolve()
  101. })
  102. })
  103. },
  104. getSystemInfo({
  105. state,
  106. commit
  107. }) {
  108. uni.getSystemInfo({
  109. success: res => {
  110. let {
  111. statusBarHeight,
  112. platform,
  113. } = res;
  114. let navHeight;
  115. if (platform == 'ios' || platform == 'devtools') {
  116. navHeight = statusBarHeight + 44;
  117. } else {
  118. navHeight = statusBarHeight + 48;
  119. }
  120. commit('setSystemInfo', {
  121. ...res,
  122. navHeight,
  123. })
  124. console.log(state)
  125. },
  126. fail(err) {
  127. console.log(err);
  128. }
  129. });
  130. },
  131. wxShare({
  132. state
  133. }, opt) {
  134. // #ifdef H5
  135. const shareInfo = state.config.share
  136. const inviteCode = state.userInfo.distribution_code
  137. const href = window.location.href
  138. const sym = href.includes('?') ? '&' : '?'
  139. const option = {
  140. shareTitle: shareInfo.h5_share_title,
  141. shareLink: inviteCode ? `${href}${sym}invite_code=${inviteCode}` : href,
  142. shareImage: shareInfo.h5_share_image,
  143. shareDesc: shareInfo.h5_share_intro
  144. }
  145. wechath5.share(Object.assign(option, opt))
  146. // #endif
  147. }
  148. };
  149. export default {
  150. state,
  151. mutations,
  152. actions
  153. };