setting.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import { defineStore } from "pinia";
  2. import { useStores } from "@/store/modules/index";
  3. import { storage, storageSystem } from "@/utils/storage";
  4. import { getToken, setToken, removeToken } from "@/utils/auth";
  5. // 接口引用
  6. import { uploadAvatar, updateUserProfile } from "@/api/system/user";
  7. import { baseAppInfoApi } from "@/api/common/index";
  8. import { checkUpdates } from "@/api/system/setting";
  9. // 组件引用
  10. import config from "@/config";
  11. import tab from "@/plugins/tab.plugins.js";
  12. import modal from "@/plugins/modal.plugins.js";
  13. import common from "@/plugins/common.plugins.js";
  14. import setting from "@/plugins/setting.plugins.js";
  15. import constData from "@/plugins/constData.plugins.js";
  16. const settingStores = defineStore("storage-setting", {
  17. state: () => ({
  18. pushClientId: undefined,//应用消息推送cid
  19. currentSize: "",//APP缓存
  20. barHeight: 0,//微信小程序顶部安全距离
  21. webViewHeight: "",//webView整体高度-铺满
  22. StatusBar: 0,//APP顶部安全距离
  23. tabBarHeight: "",//APP底部安全距离
  24. barHightTop: "",//APP头部计算距离
  25. deviceList: {
  26. networkType: null, //网络类型
  27. deviceBrand: null, //设备品牌
  28. deviceId: null, //设备编号
  29. deviceModel: null, //设备型号
  30. userName: null, //用户账号
  31. longitude: "", //经度
  32. latitude: "", //纬度
  33. },
  34. themeColor: storageSystem.get("themeColor"),//主题
  35. fingerprintUserList: storageSystem.get("fingerprintUserList"),//指纹登录用户数据
  36. nfcWaiting: "请将手机靠近NFC标签", //nfc读取页面-提示文字-数据存储
  37. }),
  38. actions: {
  39. /**
  40. * @系统主题颜色
  41. */
  42. systemThemeColor(type) {
  43. setTimeout(() => {
  44. if (type.includes(2)) {
  45. uni.setTabBarStyle({
  46. selectedColor: this.themeColor.color,
  47. borderStyle: "white",
  48. });
  49. this.themeColor.tabList.forEach((selectedIconPath, index) => {
  50. uni.setTabBarItem({
  51. index,
  52. selectedIconPath,
  53. });
  54. });
  55. }
  56. if (type.includes(1)) {
  57. uni.setNavigationBarColor({
  58. frontColor: "#ffffff",
  59. backgroundColor: this.themeColor.color,
  60. animation: {
  61. duration: 400,
  62. timingFunc: "easeIn",
  63. },
  64. });
  65. }
  66. }, 0);
  67. },
  68. /**
  69. * @动态获取屏幕头部高度
  70. */
  71. systemHeightTop() {
  72. let systemInfo = uni.getSystemInfoSync();
  73. this.webViewHeight = systemInfo.safeArea.bottom //高度+安全区域的高度
  74. this.StatusBar = systemInfo.statusBarHeight
  75. this.tabBarHeight = common.isWechatMp() ? "0px" : systemInfo.screenHeight - systemInfo.safeArea.bottom + "px"
  76. this.barHightTop = this.StatusBar ? this.StatusBar + 44 + 'px' : '44px'
  77. //#ifdef MP
  78. let custom = wx.getMenuButtonBoundingClientRect();
  79. let CustomBar = custom.bottom + custom.top - systemInfo.statusBarHeight;
  80. this.barHeight = CustomBar || 0
  81. //#endif
  82. },
  83. /**
  84. * @动态获取用户设备信息
  85. */
  86. baseAppInfo() {
  87. var _this = this
  88. //判断用户是否登录
  89. if (!getToken()) {
  90. return false
  91. }
  92. //判断用户拒绝权限是否超过48小时
  93. if (!common.isExpirationTime()) {
  94. return false
  95. }
  96. // 获取系统信息
  97. const systemInfo = uni.getSystemInfoSync();
  98. _this.deviceList.deviceBrand = systemInfo.deviceBrand;
  99. _this.deviceList.deviceId = systemInfo.deviceId;
  100. _this.deviceList.deviceModel = systemInfo.deviceModel;
  101. _this.deviceList.operateSystem = systemInfo.platform;
  102. // 获取网络状态
  103. uni.getNetworkType({
  104. success: function (res) {
  105. _this.deviceList.networkType = res.networkType;
  106. },
  107. });
  108. // 获取定位信息
  109. uni.getLocation({
  110. type: "gcj02",
  111. geocode: true,
  112. highAccuracyExpireTime: 5000,
  113. success: (res) => {
  114. _this.deviceList.longitude = res.longitude.toString();
  115. _this.deviceList.latitude = res.latitude.toString();
  116. baseAppInfoApi(_this.deviceList).then((res) => { })
  117. uni.setStorageSync("expirationTime", undefined);
  118. },
  119. fail: (res) => {
  120. uni.setStorageSync("expirationTime", Date.parse(new Date()) / 1000);
  121. },
  122. });
  123. },
  124. /**
  125. * @消息推送监听
  126. */
  127. pushMessage() {
  128. uni.onPushMessage((res) => {
  129. const platform = uni.getSystemInfoSync().platform;
  130. console.log("收到推送消息:", res); //监听推送消息
  131. if (res.type === "click") {
  132. if (platform == "android") {
  133. } else {
  134. }
  135. } else if (res.type === "receive") {
  136. if (platform == "android") {
  137. } else {
  138. }
  139. }
  140. });
  141. },
  142. /**
  143. * @消息推送监听
  144. */
  145. pushListener() {
  146. //获取应用的CID
  147. uni.getPushClientId({
  148. success: (res) => {
  149. this.pushClientId = res.cid
  150. },
  151. fail(err) {
  152. console.log(err);
  153. },
  154. });
  155. const platform = uni.getSystemInfoSync().platform;
  156. // 接收推送信息 在线
  157. plus.push.addEventListener("receive", (res) => {
  158. uni.createPushMessage({
  159. title: res.title,
  160. content: res.content,
  161. });
  162. });
  163. // 点击推送信息
  164. plus.push.addEventListener("click", (res) => {
  165. console.log(res);
  166. if (res.aps) {
  167. if ("url" in res.payload) {
  168. } else {
  169. }
  170. console.log(res.payload);
  171. }
  172. });
  173. },
  174. /**
  175. * @点击登录
  176. */
  177. handleToLogin() {
  178. tab.reLaunch("/pages/login");
  179. },
  180. /**
  181. * @点击头像
  182. */
  183. handleToAvatar(type) {
  184. if (type == 1) {
  185. uni.chooseImage({
  186. count: 1, //默认9
  187. sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
  188. sourceType: ["album", "camera"], //从相册选择、摄像头
  189. success: function (res) {
  190. this.uploadApi(res);
  191. },
  192. });
  193. } else if (type == 2) {
  194. tab.navigateTo("/pages/mine/avatar/index");
  195. }
  196. },
  197. /**
  198. * @upload上传头像
  199. * @api接口请求
  200. */
  201. uploadApi(res) {
  202. const useStore = useStores();
  203. let data = { name: "file", filePath: res.tempFilePaths[0] };
  204. uploadAvatar(data).then((response) => {
  205. useStore.avatar = response.data.url;
  206. updateUserProfile({
  207. avatar: response.data.url,
  208. }).then(() => { });
  209. });
  210. },
  211. /**
  212. * @常见问题
  213. */
  214. handleHelp() {
  215. tab.navigateTo("/pages/mine/help/index");
  216. },
  217. /**
  218. * @我的信息
  219. */
  220. handleToEditInfo() {
  221. tab.navigateTo("/pages/mine/info/index");
  222. },
  223. /**
  224. * @关于我们
  225. */
  226. handleAbout() {
  227. tab.navigateTo("/pages/mine/about/index");
  228. },
  229. /**
  230. * @清理缓存
  231. */
  232. handleCleanTmp() {
  233. // #ifdef H5
  234. uni.showToast({
  235. title: "缓存清理完成",
  236. duration: 2000,
  237. mask: true,
  238. });
  239. // #endif
  240. // #ifdef APP-PLUS
  241. setting.clearCache();//清除应用访问缓存
  242. // #endif
  243. },
  244. /**
  245. * @检查更新
  246. */
  247. handleToUpgrade({ success, error }) {
  248. let data = {
  249. _api_key: "fba7440cd37400b6ff46e303896af4df",
  250. appKey: "cbd3508235d03365f4253f6aae6b68ab",
  251. buildVersion: config.appInfo.version,
  252. };
  253. modal.loading("加载中");
  254. checkUpdates(data).then((res) => {
  255. if (res.code == 0) {
  256. let platform = uni.getSystemInfoSync().platform; //手机平台
  257. if (res.data.buildHaveNewVersion == true) {
  258. //安卓手机弹窗升级
  259. if (platform === "android") {
  260. success(res)
  261. }
  262. //IOS无法在线升级提示到商店下载
  263. else {
  264. uni.showModal({
  265. title: "发现新版本 " + res.data.buildVersion,
  266. content: "请到App store进行升级",
  267. showCancel: false,
  268. });
  269. }
  270. } else {
  271. modal.msg("您的软件版本已是最新");
  272. }
  273. modal.closeLoading();
  274. }
  275. });
  276. },
  277. /**
  278. * @新消息通知
  279. */
  280. handleToMessage() {
  281. tab.navigateTo("/pages/mine/msg/index");
  282. },
  283. /**
  284. * @账号与安全
  285. */
  286. handleToSecure() {
  287. tab.navigateTo("/pages/mine/secure/index");
  288. },
  289. /**
  290. * @设置
  291. */
  292. handleSetting() {
  293. tab.navigateTo("/pages/mine/setting/index");
  294. },
  295. /**
  296. * @退出登录
  297. */
  298. handleLogout() {
  299. const useStore = useStores();
  300. modal.confirm("确定注销并退出系统吗?").then(() => {
  301. useStore.LogOut().then(() => {
  302. tab.reLaunch("/pages/index");
  303. });
  304. });
  305. },
  306. /**
  307. * @初始化默认主题
  308. */
  309. initThemeColor(themeColor) {
  310. if (!themeColor) {
  311. this.themeColor = constData.themeList[0];
  312. storageSystem.set("themeColor", constData.themeList[0]);
  313. } else {
  314. this.themeColor = themeColor;
  315. storageSystem.set("themeColor", themeColor);
  316. }
  317. },
  318. SET_FINGERPRINT(array) {
  319. this.fingerprintUserList = array;
  320. storageSystem.set("fingerprintUserList", array);
  321. },
  322. SET_STORAGE_OBJECT_KEYS(LIST) {
  323. Object.keys(LIST).forEach(function (key) {
  324. storageSystem.set(key, LIST[key]);
  325. settingStores()[key] = LIST[key];
  326. });
  327. },
  328. },
  329. });
  330. export default settingStores;