user.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import Vue from 'vue'
  2. import { getUserInfo, getUsersFace } from '@/network/module/user.api.js'
  3. import { formatDate } from '@/utils/util.js'
  4. import { downloads } from '@/utils/synchronization-persons.js'
  5. import config from '@/common/config.js'
  6. // #ifdef APP-PLUS
  7. const arcfaceModule = uni.requireNativePlugin('ArcfaceModule')
  8. // #endif
  9. const state = {
  10. userInfo: {},
  11. data2Time: '',
  12. photosSync: '',
  13. dateTask: null
  14. };
  15. const mutations = {
  16. SETUSERINFO(state, user) {
  17. state.userInfo = user
  18. },
  19. LOGOUT(state) {
  20. uni.removeStorageSync(config.const.access_token)
  21. state.userInfo = {}
  22. },
  23. SETUSERAVATAR(state, avatar) {
  24. state.userInfo.avatar = avatar
  25. },
  26. SETDATE2TIME(state, data2Time) {
  27. state.data2Time = data2Time || ''
  28. },
  29. SETDATETASK(state, task) {
  30. state.dateTask = task
  31. },
  32. SETPHOTOSSYNC(state, value) {
  33. state.photosSync = value || ''
  34. }
  35. };
  36. const actions = {
  37. goOut({ commit }) {
  38. commit('LOGOUT')
  39. },
  40. // 修改用户头像
  41. setUserAvatar({ commit }, avatar) {
  42. commit('SETUSERAVATAR', avatar)
  43. },
  44. setUserInfo({ commit }, token) {
  45. uni.setStorageSync(config.const.access_token, token)
  46. return new Promise((resolve, reject) => {
  47. getUserInfo().then((res) => {
  48. commit('SETUSERINFO', res.data)
  49. resolve(res.data)
  50. }).catch((error) => {
  51. reject(error)
  52. })
  53. })
  54. },
  55. // 获取所有的用户图片
  56. async getAllUserFace({ commit }) {
  57. // #ifdef APP-PLUS
  58. let personPhotos = []
  59. try{
  60. const result = await getUsersFace()
  61. personPhotos = result.data
  62. // 测试用的图片地址
  63. // personPhotos.push({
  64. // photo: 'http://pic-bucket.ws.126.net/photo/0003/2023-08-17/ICB55PEH00AJ0003NOS.jpg',
  65. // safeThingsUserId: 2
  66. // })
  67. // personPhotos.push({
  68. // photo: 'http://pic-bucket.ws.126.net/photo/0003/2023-08-17/ICB55PEF00AJ0003NOS.jpg',
  69. // safeThingsUserId: 1
  70. // })
  71. }catch(e) {
  72. personPhotos = []
  73. }
  74. if(!personPhotos || personPhotos.length <= 0) {
  75. // 同步信息失败
  76. commit('SETPHOTOSSYNC', null)
  77. return
  78. }
  79. commit('SETPHOTOSSYNC', `0/${personPhotos.length}`)
  80. downloads(personPhotos, (res) => {
  81. // uni.$u.toast(`下载完成:总大小${res.total}, 有效下载数(${res.available}), 需要下载数(${res.downlaods})`)
  82. arcfaceModule.doRegister((result) => {
  83. // uni.$u.toast("注册结果" + JSON.stringify(result))
  84. if(result.code === 200) {
  85. commit('SETPHOTOSSYNC', `${result.data.total}/${result.data.total}`)
  86. } else if(result.code === 300) {
  87. commit('SETPHOTOSSYNC', `${result.data.successCount}/${result.data.total}`)
  88. } else {
  89. uni.$u.toast(result.msg)
  90. }
  91. })
  92. })
  93. // #endif
  94. },
  95. updateHeadStatus({ state, commit }) {
  96. if(state.dateTask) {
  97. return
  98. }
  99. const dateTask = setInterval(() => {
  100. const dateTimeStr = formatDate(new Date(), 9)
  101. commit('SETDATE2TIME', dateTimeStr)
  102. }, 1000)
  103. commit('SETDATETASK', dateTask)
  104. }
  105. };
  106. export default {
  107. state,
  108. mutations,
  109. actions
  110. }