setting.js 14 KB

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