system.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { defineStore } from "pinia";
  2. import verConfig from "@/ver.config";
  3. import { GTE_BANNER, RATE_LIST, OTHER_CONFIG, CONTENT_WINNOTICE, CONTENT_COUPON, CONTENT_BOXES } from "@/api";
  4. import { getStorage, setStorage } from "@/utils";
  5. import { nextTick } from "vue"
  6. import { openUrl } from "@/utils"
  7. import { setLan } from "@/locale"
  8. import { useShopStore } from "./shop"
  9. const state = () => ({
  10. banner: [],
  11. rateList: [],
  12. langeuageList: [],
  13. countryCodesList: [],
  14. currency: getStorage(verConfig.symbol).code || verConfig.currency,
  15. lang: getStorage(verConfig.langName) || verConfig.lang,
  16. symbol: getStorage(verConfig.symbol) || {},
  17. winnotice: {},
  18. coupon: {},
  19. count: 0,
  20. networkStatus: true,
  21. isConnected: true,
  22. isFirstConnectNetwork: true,
  23. isHomeFirstConnect: false,
  24. appinfo: {
  25. update: 0
  26. }
  27. })
  28. export const useSystemStore = defineStore('system', {
  29. state,
  30. getters: {
  31. getBanner() {
  32. // if (!this.banner.length) {
  33. // this.setBanner()
  34. // }
  35. return this.banner || []
  36. },
  37. getRateList() {
  38. return this.rateList;
  39. },
  40. getLangeuage() {
  41. return this.langeuageList
  42. },
  43. getCountryCodes() {
  44. return this.countryCodesList
  45. },
  46. getCurrency() {
  47. return this.currency
  48. },
  49. getLang() {
  50. return this.lang
  51. },
  52. getSymbol() {
  53. return this.symbol
  54. },
  55. getWinnotice() {
  56. return this.winnotice
  57. },
  58. getCoupon() {
  59. return this.coupon
  60. },
  61. getAppinfo() {
  62. return this.appinfo
  63. },
  64. getCount() {
  65. return this.count
  66. }
  67. },
  68. actions: {
  69. // 检查网络状态
  70. checkNetworkStatus() {
  71. uni.getNetworkType({
  72. success: (result) => {
  73. const { networkType, errMsg } = result
  74. if (networkType != 'none') {
  75. if (this.isFirstConnectNetwork) {
  76. this.setFirstConnectStatus(false)
  77. }
  78. this.setNetworkStatus(true)
  79. }
  80. else {
  81. this.setNetworkStatus(false)
  82. }
  83. }
  84. })
  85. },
  86. // 设置网络和连接状态
  87. setNetworkStatus(status) {
  88. this.networkStatus = status
  89. this.isConnected = status
  90. },
  91. setFirstConnectStatus(flag) {
  92. this.isFirstConnectNetwork = flag
  93. },
  94. setIsHomeFirstConnect(flag) {
  95. this.isHomeFirstConnect = flag
  96. },
  97. setBanner() {
  98. return new Promise(async (resolve, reject) => {
  99. try {
  100. const res = await GTE_BANNER();
  101. this.banner = res.data || []
  102. } catch (error) { }
  103. })
  104. },
  105. setLan(_lan_) {
  106. this.lang = _lan_
  107. },
  108. setCurrency(currency) {
  109. this.currency = currency;
  110. this.setSymbol();
  111. },
  112. setAppinfo(info) {
  113. this.appinfo = info
  114. },
  115. async setRateList() {
  116. try {
  117. const res = await RATE_LIST();
  118. this.rateList = res.data.currencys;
  119. this.langeuageList = res.data.langs;
  120. this.countryCodesList = res.data.countryCodes
  121. nextTick(() => {
  122. const lang = res.data.language
  123. this.initLanguage(lang)
  124. !getStorage(verConfig.symbol) && this.setSymbol();
  125. })
  126. } catch (error) { }
  127. },
  128. initLanguage(lang) {
  129. const useShop = useShopStore();
  130. const savedLang = getStorage('userLanguage');
  131. if (!savedLang) {
  132. setStorage(verConfig.langName, lang);
  133. this.setLan(lang)
  134. setLan(lang, () => {
  135. useShop.setHotLink();
  136. });
  137. setStorage('userLanguage', lang);
  138. }
  139. },
  140. setSymbol() {
  141. let findItem = this.rateList.find((item) => item.code == this.currency);
  142. setStorage(verConfig.symbol, findItem);
  143. this.currency = findItem.code;
  144. this.symbol = findItem
  145. },
  146. async service() {
  147. try {
  148. const res = await OTHER_CONFIG('system');
  149. if (!res.data.service_url) return;
  150. openUrl(res.data.service_url);
  151. } catch (error) { }
  152. },
  153. async setWinnotice() {
  154. try {
  155. const res = await CONTENT_WINNOTICE();
  156. this.winnotice = res.data && res.data[0]
  157. } catch (error) { }
  158. },
  159. async setCoupon(params = {}, other = {}) {
  160. try {
  161. const res = await CONTENT_COUPON(params, other);
  162. this.coupon = res.data && res.data[0]
  163. } catch (error) { }
  164. },
  165. async setBoxes(params = {}) {
  166. try {
  167. const res = await CONTENT_BOXES(params);
  168. this.count = res.data && res.data.count || 0
  169. } catch (error) { }
  170. },
  171. }
  172. })