setting.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. deviceStatus: 0,//设备状态
  34. },
  35. themeColor: storageSystem.get("themeColor"),//主题
  36. fingerprintUserList: storageSystem.get("fingerprintUserList"),//指纹登录用户数据
  37. nfcWaiting: "请将手机靠近NFC标签", //nfc读取页面-提示文字-数据存储
  38. webSocket: null,//巡检webSocket
  39. }),
  40. actions: {
  41. /**
  42. * @动态获取屏幕头部高度
  43. */
  44. systemHeightTop() {
  45. let systemInfo = uni.getSystemInfoSync();
  46. this.webViewHeight = systemInfo.safeArea.bottom //高度+安全区域的高度
  47. this.StatusBar = systemInfo.statusBarHeight
  48. this.tabBarHeight = common.isWechatMp() ? "0px" : systemInfo.screenHeight - systemInfo.safeArea.bottom + "px"
  49. this.barHightTop = this.StatusBar ? this.StatusBar + 44 + 'px' : '44px'
  50. //#ifdef MP
  51. let custom = wx.getMenuButtonBoundingClientRect();
  52. let CustomBar = custom.bottom + custom.top - systemInfo.statusBarHeight;
  53. this.barHeight = CustomBar || 0
  54. //#endif
  55. },
  56. /**
  57. * @动态获取用户设备信息
  58. */
  59. baseAppInfo() {
  60. var _this = this
  61. //判断用户是否登录
  62. if (!getToken()) {
  63. return false
  64. }
  65. //判断用户拒绝权限是否超过48小时
  66. if (!common.isExpirationTime()) {
  67. return false
  68. }
  69. // 获取系统信息
  70. const systemInfo = uni.getSystemInfoSync();
  71. _this.deviceList.deviceBrand = systemInfo.deviceBrand;
  72. _this.deviceList.deviceId = systemInfo.deviceId;
  73. _this.deviceList.deviceModel = systemInfo.deviceModel;
  74. _this.deviceList.operateSystem = systemInfo.platform;
  75. // 获取网络状态
  76. uni.getNetworkType({
  77. success: function (res) {
  78. _this.deviceList.networkType = res.networkType;
  79. },
  80. });
  81. // 获取定位信息
  82. uni.getLocation({
  83. type: "gcj02",
  84. geocode: true,
  85. highAccuracyExpireTime: 5000,
  86. success: (res) => {
  87. _this.deviceList.longitude = res.longitude.toString();
  88. _this.deviceList.latitude = res.latitude.toString();
  89. baseAppInfoApi(_this.deviceList).then((res) => { })
  90. uni.setStorageSync("expirationTime", "");
  91. },
  92. fail: (res) => {
  93. if (res.errMsg.indexOf('获取定位权限失败') != -1) {
  94. uni.setStorageSync("expirationTime", Date.parse(new Date()) / 1000);
  95. }
  96. },
  97. });
  98. },
  99. /**
  100. * @消息推送监听
  101. */
  102. pushMessage() {
  103. uni.onPushMessage((res) => {
  104. const platform = uni.getSystemInfoSync().platform;
  105. console.log("收到推送消息:", res); //监听推送消息
  106. if (res.type === "click") {
  107. if (platform == "android") {
  108. } else {
  109. }
  110. } else if (res.type === "receive") {
  111. if (platform == "android") {
  112. } else {
  113. }
  114. }
  115. });
  116. },
  117. /**
  118. * @消息推送监听
  119. */
  120. pushListener() {
  121. //获取应用的CID
  122. uni.getPushClientId({
  123. success: (res) => {
  124. this.pushClientId = res.cid
  125. },
  126. fail(err) {
  127. console.log(err);
  128. },
  129. });
  130. const platform = uni.getSystemInfoSync().platform;
  131. // 接收推送信息 在线
  132. plus.push.addEventListener("receive", (res) => {
  133. console.log(res)
  134. uni.createPushMessage({
  135. title: res.title,
  136. content: res.content,
  137. payload: res.payload
  138. });
  139. });
  140. // 点击推送信息
  141. plus.push.addEventListener("click", (res) => {
  142. console.log(res);
  143. if ("payload" in res) {
  144. if ("infoType" in res.payload) {
  145. if (res.payload.infoType == 5) {
  146. tab.navigateTo(`/pages/business/common/projectMange/record/details?reportId=${res.payload.moduleId}`);
  147. }
  148. }
  149. }
  150. });
  151. },
  152. /**
  153. * @初始化WebSocket
  154. */
  155. initWebSocket() {
  156. var _this = this
  157. const systemInfo = uni.getSystemInfoSync();
  158. _this.webSocket = uni.connectSocket({
  159. // url: `wss://gateway.usky.cn/wss${systemInfo.deviceId},${new Date().getTime()}`,
  160. url: `ws://120.26.164.249:9891/webSocketPatrol/${systemInfo.deviceId},${new Date().getTime()}`,
  161. });
  162. },
  163. /**
  164. * @开启WebSocket
  165. */
  166. openWebSocket() {
  167. var _this = this
  168. if (uni.getStorageSync("serveUrl").indexOf("xf.usky.cn:13212") == -1) {
  169. return false
  170. }
  171. _this.initWebSocket()
  172. uni.onSocketOpen(function (res) {
  173. console.log('WebSocket连接已打开!', res);
  174. });
  175. uni.onSocketMessage(function (res) {
  176. console.log('收到服务器内容:' + res.data);
  177. _this.baseAppInfo();
  178. });
  179. uni.onSocketClose(function (res) {
  180. console.log('WebSocket 已关闭!', res);
  181. _this.closeWebSocket();
  182. _this.initWebSocket()
  183. });
  184. uni.onSocketError(function (res) {
  185. console.log('WebSocket连接打开失败,请检查!');
  186. _this.initWebSocket()
  187. });
  188. },
  189. /**
  190. * @关闭WebSocket
  191. */
  192. closeWebSocket() {
  193. uni.closeSocket();
  194. },
  195. /**
  196. * @点击登录
  197. */
  198. handleToLogin() {
  199. tab.reLaunch("/pages/login");
  200. },
  201. /**
  202. * @点击头像
  203. */
  204. handleToAvatar(type) {
  205. var _this = this
  206. if (type == 1) {
  207. uni.chooseImage({
  208. count: 1, //默认9
  209. sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
  210. sourceType: ["album", "camera"], //从相册选择、摄像头
  211. success: function (res) {
  212. _this.uploadApi(res);
  213. },
  214. });
  215. } else if (type == 2) {
  216. tab.navigateTo("/pages/mine/avatar/index");
  217. }
  218. },
  219. /**
  220. * @upload上传头像
  221. * @api接口请求
  222. */
  223. uploadApi(res) {
  224. const useStore = useStores();
  225. let data = { name: "file", filePath: res.tempFilePaths[0] };
  226. uploadAvatar(data).then((response) => {
  227. useStore.avatar = response.data.url;
  228. updateUserProfile({
  229. avatar: response.data.url,
  230. }).then(() => { });
  231. });
  232. },
  233. /**
  234. * @常见问题
  235. */
  236. handleHelp() {
  237. tab.navigateTo("/pages/mine/help/index");
  238. },
  239. /**
  240. * @我的信息
  241. */
  242. handleToEditInfo() {
  243. tab.navigateTo("/pages/mine/info/index");
  244. },
  245. /**
  246. * @关于我们
  247. */
  248. handleAbout() {
  249. tab.navigateTo("/pages/mine/about/index");
  250. },
  251. /**
  252. * @清理缓存
  253. */
  254. handleCleanTmp() {
  255. // #ifdef H5
  256. uni.showToast({
  257. title: "缓存清理完成",
  258. duration: 2000,
  259. mask: true,
  260. });
  261. // #endif
  262. // #ifdef APP-PLUS
  263. setting.clearCache();//清除应用访问缓存
  264. // #endif
  265. },
  266. /**
  267. * @检查更新
  268. */
  269. handleToUpgrade({ success, error }) {
  270. let platform = uni.getSystemInfoSync().platform; //手机平台
  271. let data = {
  272. _api_key: "fba7440cd37400b6ff46e303896af4df",
  273. appKey: platform === "android" ? "cbd3508235d03365f4253f6aae6b68ab" : "4b858fce6367652f5c0959a0444a4bea",
  274. buildVersion: config.appInfo.version,
  275. };
  276. modal.loading("加载中");
  277. checkUpdates(data).then((res) => {
  278. if (res.code == 0) {
  279. if (res.data.buildHaveNewVersion == true) {
  280. //安卓手机弹窗升级
  281. if (platform === "android") {
  282. success(res)
  283. }
  284. //IOS无法在线升级提示到商店下载
  285. else {
  286. let appleId = 6449016600
  287. plus.runtime.launchApplication({
  288. action: `itms-apps://itunes.apple.com/cn/app/id${appleId}`,
  289. })
  290. }
  291. } else {
  292. modal.msg("您的软件版本已是最新");
  293. }
  294. modal.closeLoading();
  295. }
  296. });
  297. },
  298. /**
  299. * @新消息通知
  300. */
  301. handleToMessage() {
  302. tab.navigateTo("/pages/mine/msg/index");
  303. },
  304. /**
  305. * @账号与安全
  306. */
  307. handleToSecure() {
  308. tab.navigateTo("/pages/mine/secure/index");
  309. },
  310. /**
  311. * @设置
  312. */
  313. handleSetting() {
  314. tab.navigateTo("/pages/mine/setting/index");
  315. },
  316. /**
  317. * @退出登录
  318. */
  319. handleLogout() {
  320. const useStore = useStores();
  321. modal.confirm("确定注销并退出系统吗?").then(() => {
  322. useStore.LogOut().then(() => {
  323. tab.reLaunch("/pages/index");
  324. });
  325. });
  326. },
  327. /**
  328. * @初始化默认主题
  329. */
  330. initThemeColor(themeColor) {
  331. if (!themeColor) {
  332. this.themeColor = constData.themeList[0];
  333. storageSystem.set("themeColor", constData.themeList[0]);
  334. } else {
  335. this.themeColor = themeColor;
  336. storageSystem.set("themeColor", themeColor);
  337. }
  338. },
  339. /**
  340. * @系统主题颜色
  341. */
  342. systemThemeColor(type) {
  343. setTimeout(() => {
  344. if (type.includes(2)) {
  345. uni.setTabBarStyle({
  346. selectedColor: this.themeColor.color,
  347. borderStyle: "white",
  348. });
  349. this.themeColor.tabList.forEach((selectedIconPath, index) => {
  350. uni.setTabBarItem({
  351. index,
  352. selectedIconPath,
  353. });
  354. });
  355. }
  356. if (type.includes(1)) {
  357. uni.setNavigationBarColor({
  358. frontColor: "#ffffff",
  359. backgroundColor: this.themeColor.color,
  360. animation: {
  361. duration: 400,
  362. timingFunc: "easeIn",
  363. },
  364. });
  365. }
  366. }, 0);
  367. },
  368. SET_FINGERPRINT(array) {
  369. this.fingerprintUserList = array;
  370. storageSystem.set("fingerprintUserList", array);
  371. },
  372. SET_STORAGE_OBJECT_KEYS(LIST) {
  373. Object.keys(LIST).forEach(function (key) {
  374. storageSystem.set(key, LIST[key]);
  375. settingStores()[key] = LIST[key];
  376. });
  377. },
  378. },
  379. });
  380. export default settingStores;