useConfigStore.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {defineStore} from "pinia";
  2. import storage from "@/utils/storage";
  3. import platform from "@/utils/platform";
  4. import {getSystemDeviceInfo} from "@/utils/getSystemDeviceInfo";
  5. import {FileStorage} from "@/utils/fileStorage";
  6. import {confirmModal, showToast} from "@/utils/app";
  7. import * as dayjs from "@/utils/dayjs"
  8. import {logs} from "@/utils/util";
  9. import {calcStrHash} from "@/utils/machine/function";
  10. const configUrl = '/storage/emulated/0/.TXZNMac.txt'
  11. const fileStorage = new FileStorage()
  12. export const useConfigStore = defineStore('config', {
  13. state: () => ({
  14. mac: null,
  15. mType: 7, // 7 BMI(默认);8 肺活量;9 视力;
  16. mName: '',
  17. platform,
  18. }),
  19. getters: {},
  20. actions: {
  21. _init() {
  22. // this.platform = ()
  23. this.getMachineType()
  24. this.mType = 7
  25. },
  26. setMachineType(item) {
  27. storage.setKey('machineType', {
  28. name: item.name,
  29. type: item.type,
  30. })
  31. },
  32. getMachineType() {
  33. let info = storage.getKey('machineType')
  34. console.log('getMachineType', info)
  35. if (info) {
  36. this.mType = info.type
  37. this.mName = info.name
  38. }
  39. },
  40. async getMac() {
  41. return new Promise(async (resolve, reject) => {
  42. let config = await this.getConfig()
  43. console.log('config', config)
  44. let mac = config.mac ?? await this.createMac()
  45. console.log('config2', mac)
  46. if (mac) {
  47. resolve(mac)
  48. } else {
  49. reject(false)
  50. }
  51. })
  52. },
  53. async createMac() {
  54. let {imei} = await getSystemDeviceInfo()
  55. try {
  56. if (!imei) {
  57. // #ifdef APP-PLUS
  58. await confirmModal('无法获取设备imei,请联系管理员!', {
  59. showCancel: false,
  60. })
  61. plus.runtime.quit()
  62. // #endif
  63. // #ifdef H5
  64. showToast('无法获取设备imei,请联系管理员!')
  65. return false
  66. // #endif
  67. }
  68. } catch (e) {
  69. console.error('createMac 获取设备码失败')
  70. return false
  71. }
  72. let timestamp = dayjs().valueOf()
  73. let data = await this.getConfig()
  74. let a = calcStrHash(imei, 5)
  75. let b = calcStrHash(timestamp, 4)
  76. data.mac = `BMI${a}${b}`
  77. data.timestamp = timestamp
  78. data.imei = imei
  79. await this.getConfig(data)
  80. this.mac = data.mac
  81. return data.mac
  82. },
  83. async getConfig(data = false) {
  84. if (data) {
  85. fileStorage.storage(configUrl, (data || {}))
  86. return data
  87. }
  88. data = {}
  89. try {
  90. let info = await fileStorage.read(configUrl)
  91. if (info) data = info
  92. } catch (e) {
  93. fileStorage.storage(configUrl, {})
  94. }
  95. return data
  96. },
  97. }
  98. })