You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.2 KiB

3 months ago
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. import Auth from '../../libs/wechat';
  11. import store from "../index";
  12. import { tokenIsExistApi,loginConfigApi,getTheme } from '@/api/api.js';
  13. import {
  14. getOrderPayConfig
  15. } from '../../api/order.js';
  16. import {
  17. getUserInfo,
  18. computeUser
  19. } from "../../api/user.js";
  20. import {
  21. LOGIN_STATUS,
  22. UID,
  23. PLATFORM,
  24. BOTTOM_NAVIGATION_ISCUSTOM
  25. } from '../../config/cache';
  26. import Cache from '../../utils/cache';
  27. import {
  28. USER_INFO
  29. } from '../../config/cache';
  30. let cartArr = [{
  31. name: "微信支付",
  32. icon: "icon-weixinzhifu1",
  33. value: 'weixin',
  34. title: '微信快捷支付',
  35. payStatus: 1,
  36. },
  37. {
  38. name: "余额支付",
  39. icon: "icon-yuezhifu",
  40. value: 'yue',
  41. title: '可用余额:',
  42. payStatus: 1,
  43. userBalance: ''
  44. }
  45. ];
  46. const state = {
  47. token: Cache.get(LOGIN_STATUS) || '',
  48. backgroundColor: "#fff",
  49. userInfo: Cache.get(USER_INFO) ? JSON.parse(Cache.get(USER_INFO)) : null,
  50. uid: Cache.get(UID) || null,
  51. homeActive: false,
  52. chatUrl: Cache.get('chatUrl') || '',
  53. systemPlatform: Cache.get(PLATFORM) ? Cache.get(PLATFORM) : '',
  54. productType: Cache.get('productType') || '',
  55. bottomNavigationIsCustom: Cache.get('BOTTOM_NAVIGATION_ISCUSTOM') ? Cache.get('BOTTOM_NAVIGATION_ISCUSTOM') : false, //是否使用自定义导航
  56. };
  57. const mutations = {
  58. LOGIN(state, opt) {
  59. state.token = opt.token;
  60. Cache.set(LOGIN_STATUS, opt.token);
  61. },
  62. SETUID(state, val) {
  63. state.uid = val;
  64. Cache.set(UID, val);
  65. },
  66. UPDATE_LOGIN(state, token) {
  67. state.token = token;
  68. },
  69. LOGOUT(state) {
  70. state.token = undefined;
  71. state.uid = undefined
  72. Cache.clear(LOGIN_STATUS);
  73. Cache.clear(UID);
  74. Cache.clear(USER_INFO);
  75. },
  76. BACKGROUND_COLOR(state, color) {
  77. state.color = color;
  78. document.body.style.backgroundColor = color;
  79. },
  80. UPDATE_USERINFO(state, userInfo) {
  81. state.userInfo = userInfo;
  82. Cache.set(USER_INFO, userInfo);
  83. },
  84. OPEN_HOME(state) {
  85. state.homeActive = true;
  86. },
  87. CLOSE_HOME(state) {
  88. state.homeActive = false;
  89. },
  90. SET_CHATURL(state, chatUrl) {
  91. state.chatUrl = chatUrl;
  92. },
  93. SYSTEM_PLATFORM(state, systemPlatform) {
  94. state.systemPlatform = systemPlatform;
  95. Cache.set(PLATFORM, systemPlatform);
  96. },
  97. //更新useInfo数据
  98. changInfo(state, payload) {
  99. state.userInfo[payload.amount1] = payload.amount2;
  100. Cache.set(USER_INFO, state.userInfo);
  101. },
  102. //商品类型,用于区分一般商品
  103. PRODUCT_TYPE(state, productType) {
  104. state.productType = productType;
  105. Cache.set('productType', productType);
  106. },
  107. /** 是否使用自定义导航 **/
  108. BottomNavigationIsCustom: (state, bottomNavigationIsCustom) => {
  109. state.bottomNavigationIsCustom = bottomNavigationIsCustom
  110. Cache.set(BOTTOM_NAVIGATION_ISCUSTOM, bottomNavigationIsCustom);
  111. },
  112. //清除所有本地缓存
  113. clearStorage(state) {
  114. uni.clearStorageSync();
  115. },
  116. };
  117. const actions = {
  118. USERINFO({
  119. state,
  120. commit
  121. }, force) {
  122. return new Promise(reslove => {
  123. getUserInfo().then(res => {
  124. commit("UPDATE_USERINFO", res.data);
  125. reslove(res.data);
  126. });
  127. }).catch(() => {
  128. commit("LOGOUT");
  129. });
  130. },
  131. getPayConfig({
  132. state,
  133. commit
  134. }, force) {
  135. return new Promise(reslove => {
  136. getOrderPayConfig().then(res => {
  137. let data = res.data;
  138. cartArr[0].payStatus = data.payWechatOpen ? 1 : 0;
  139. cartArr[1].payStatus = data.yuePayStatus ? 1 : 0;
  140. cartArr[1].userBalance = data.userBalance ? data.userBalance : 0;
  141. // #ifdef H5
  142. // if (Auth.isWeixin()) {
  143. // cartArr[2].payStatus = 0;
  144. // } else {
  145. // cartArr[2].payStatus = data.aliPayStatus ? 1 : 0;
  146. // }
  147. // #endif
  148. // #ifdef APP-PLUS
  149. // cartArr[2].payStatus = data.aliPayStatus ? 1 : 0;
  150. // #endif
  151. let cartArrs = cartArr.filter(e => e.payStatus === 1);
  152. reslove({
  153. userBalance: data.userBalance,
  154. payConfig: cartArrs
  155. });
  156. })
  157. }).catch(err => {
  158. return util.Tips({
  159. title: err
  160. });
  161. });
  162. },
  163. /**
  164. * 校验token是否有效,true为有效false为无效
  165. */
  166. GetTokenIsExist({
  167. state,
  168. commit
  169. }, force) {
  170. return new Promise(reslove => {
  171. tokenIsExistApi().then(res => {
  172. if (!res.data) {
  173. store.commit('UPDATE_LOGIN', '');
  174. store.commit('UPDATE_USERINFO', {});
  175. store.commit('SETUID', '');
  176. }
  177. reslove(res.data);
  178. });
  179. }).catch(err => {
  180. return util.Tips({
  181. title: err
  182. });
  183. });
  184. },
  185. //获取diy颜色配置
  186. GetThemeConfig({
  187. state,
  188. commit
  189. }) {
  190. return new Promise(reslove => {
  191. getTheme().then(resP => {
  192. Cache.set('theme', `theme${Number(resP.data.value)}`);
  193. // #ifdef H5
  194. window.document.documentElement.setAttribute('data-theme', state.globalData.theme);
  195. // #endif
  196. })
  197. }).catch(err => {
  198. return util.Tips({
  199. title: err
  200. });
  201. });
  202. },
  203. };
  204. export default {
  205. state,
  206. mutations,
  207. actions
  208. };