setting.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { defineStore } from "pinia";
  2. import storage from "@/utils/storage";
  3. import constant from "@/utils/constant";
  4. import modal from "@/plugins/modal.plugins.js";
  5. import tab from "@/plugins/tab.plugins.js";
  6. import setting from "@/plugins/setting.plugins.js";
  7. import { useStores } from "@/store/modules/index";
  8. import { uploadAvatar, updateUserProfile } from "@/api/system/user";
  9. const settingStore = defineStore("setting", {
  10. state: () => ({
  11. currentSize: "",//APP缓存
  12. themeColor: storage.get(constant.themeColor),//主题
  13. barHeight: 0,//微信小程序顶部安全距离
  14. StatusBar: 0,//app顶部安全距离
  15. tabBarHeight: 0,//app底部安全距离
  16. barHightTop: "",//app头部计算距离
  17. }),
  18. persist: {
  19. // 自定义数据持久化方式
  20. // key: 'store-key', 指定key进行存储,此时非key的值不会持久化,刷新就会丢失
  21. storage: window ? window.localStorage : uni.setStorageSync(), // 指定换成地址
  22. // paths: ['nested.data'],// 指定需要持久化的state的路径名称
  23. beforeRestore: (context) => {
  24. console.log("Before" + context);
  25. },
  26. afterRestore: (context) => {
  27. console.log("After" + context);
  28. },
  29. },
  30. actions: {
  31. /**
  32. * @系统主题颜色
  33. */
  34. systemThemeColor(type) {
  35. if (type.includes(2)) {
  36. uni.setTabBarStyle({
  37. selectedColor: this.themeColor.color,
  38. borderStyle: "white",
  39. });
  40. this.themeColor.tabList.forEach((selectedIconPath, index) => {
  41. uni.setTabBarItem({
  42. index,
  43. selectedIconPath,
  44. });
  45. });
  46. }
  47. if (type.includes(1)) {
  48. uni.setNavigationBarColor({
  49. frontColor: "#ffffff",
  50. backgroundColor: this.themeColor.color,
  51. animation: {
  52. duration: 400,
  53. timingFunc: "easeIn",
  54. },
  55. });
  56. }
  57. },
  58. /**
  59. * @动态获取屏幕头部高度
  60. */
  61. systemHeightTop() {
  62. let systemInfo = uni.getSystemInfoSync();
  63. var statusBarHeight = systemInfo.statusBarHeight
  64. this.StatusBar = statusBarHeight
  65. this.tabBarHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom
  66. this.barHightTop = this.StatusBar ? this.StatusBar + 44 + 'px' : '44px'
  67. //#ifdef MP
  68. let custom = wx.getMenuButtonBoundingClientRect();
  69. let CustomBar = custom.bottom + custom.top - systemInfo.statusBarHeight;
  70. this.barHeight = CustomBar || 0
  71. //#endif
  72. },
  73. /**
  74. * @常见问题
  75. */
  76. handleHelp() {
  77. tab.navigateTo("/pages/mine/help/index");
  78. },
  79. /**
  80. * @我的信息
  81. */
  82. handleToEditInfo() {
  83. tab.navigateTo("/pages/mine/info/index");
  84. },
  85. /**
  86. * @关于我们
  87. */
  88. handleAbout() {
  89. tab.navigateTo("/pages/mine/about/index");
  90. },
  91. /**
  92. * @清理缓存
  93. */
  94. handleCleanTmp() {
  95. // #ifdef H5
  96. modal.showToast("H5暂不支持此功能");
  97. // #endif
  98. // #ifdef APP-PLUS
  99. setting.clearCache();
  100. // #endif
  101. },
  102. /**
  103. * @检查更新
  104. */
  105. handleToUpgrade() {
  106. modal.showToast("模块建设中~");
  107. },
  108. /**
  109. * @账号与安全
  110. */
  111. handleToSecure() {
  112. tab.navigateTo("/pages/mine/secure/index");
  113. },
  114. /**
  115. * @设置
  116. */
  117. handleSetting() {
  118. tab.navigateTo("/pages/mine/setting/index");
  119. },
  120. /**
  121. * @退出登录
  122. */
  123. handleLogout() {
  124. const useStore = useStores();
  125. modal.confirm("确定注销并退出系统吗?").then(() => {
  126. useStore.LogOut().then(() => {
  127. tab.reLaunch("/pages/index");
  128. });
  129. });
  130. },
  131. /**
  132. * @点击头像
  133. */
  134. handleToAvatar(type) {
  135. if (type == 1) {
  136. uni.chooseImage({
  137. count: 1, //默认9
  138. sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
  139. sourceType: ["album", "camera"], //从相册选择、摄像头
  140. success: function (res) {
  141. this.uploadApi(res);
  142. },
  143. });
  144. } else if (type == 2) {
  145. tab.navigateTo("/pages/mine/avatar/index");
  146. }
  147. },
  148. /**
  149. * @upload上传头像
  150. * @api接口请求
  151. */
  152. uploadApi(res) {
  153. const useStore = useStores();
  154. let data = { name: "file", filePath: res.tempFilePaths[0] };
  155. uploadAvatar(data).then((response) => {
  156. useStore.$state.avatar = response.data.url;
  157. updateUserProfile({
  158. avatar: response.data.url,
  159. }).then(() => { });
  160. });
  161. },
  162. SET_THEMECOLOR(themeColor) {
  163. // storage.get(constant.name)
  164. this.themeColor = themeColor;
  165. storage.set(constant.themeColor, themeColor);
  166. },
  167. },
  168. });
  169. export default settingStore;