user.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { v4 as uuidv4 } from "uuid";
  2. import IMSDK from "openim-uniapp-polyfill";
  3. import { businessGetUserInfo } from "@/pages_im/api/login";
  4. import { getSubDepartment } from "@/pages_im/api/orgnizaition";
  5. import { filterEmptyValue, getPurePath } from "@/pages_im/util/common";
  6. import { getUserInDepartment } from "@/pages_im/api/orgnizaition";
  7. const state = {
  8. selfInfo: {},
  9. organizationData: {},
  10. authData: {},
  11. cacheMap: {},
  12. isSyncing: false,
  13. reinstall: false,
  14. progress: 0,
  15. rootFontSize: uni.getStorageSync("RootFontSize") || "18px",
  16. };
  17. const mutations = {
  18. SET_SELF_INFO(state, info) {
  19. state.selfInfo = {
  20. ...info,
  21. };
  22. },
  23. SET_ORGANIZATION_DATA(state, data) {
  24. state.organizationData = {
  25. ...data,
  26. };
  27. },
  28. SET_AUTH_DATA(state, data) {
  29. state.authData = {
  30. ...data,
  31. };
  32. },
  33. INIT_CACHE_MAP(state, data) {
  34. state.cacheMap = {
  35. ...data
  36. }
  37. },
  38. UPDATE_CACHE_MAP(state, data) {
  39. const tmp = {...state.cacheMap}
  40. tmp[data.key] = {...data}
  41. state.cacheMap = tmp
  42. uni.setStorageSync("CacheMap", tmp);
  43. },
  44. DELETE_CACHE_MAP(state, key) {
  45. const tmp = {...state.cacheMap}
  46. delete tmp[key]
  47. state.cacheMap = tmp
  48. uni.setStorageSync("CacheMap", tmp);
  49. },
  50. SET_IS_SYNCING(state, data) {
  51. state.isSyncing = data;
  52. },
  53. SET_REINSTALL(state, data) {
  54. state.reinstall = data;
  55. },
  56. SET_PROGRESS(state, data) {
  57. state.progress = data;
  58. },
  59. SET_ROOT_FONT_SIZE(state, data) {
  60. state.rootFontSize = data;
  61. },
  62. };
  63. const actions = {
  64. async getSelfInfo({ commit }) {
  65. try {
  66. const { data } = await IMSDK.asyncApi(
  67. IMSDK.IMMethods.GetSelfUserInfo,
  68. uuidv4(),
  69. );
  70. const { users } = await businessGetUserInfo(data.userID);
  71. const businessData = users[0] ?? {};
  72. filterEmptyValue(businessData);
  73. const { users:members } = await getUserInDepartment(data.userID)
  74. commit("SET_SELF_INFO", {
  75. ...data,
  76. ...businessData,
  77. members: members[0]?.members || [],
  78. });
  79. const res = await getSubDepartment();
  80. console.log("getSubDepartment", res);
  81. commit("SET_ORGANIZATION_DATA", res);
  82. } catch (e) {
  83. console.log(e);
  84. //uni.$u.toast("获取个人信息失败");
  85. }
  86. },
  87. async updateBusinessInfo({ commit, state }) {
  88. try {
  89. const { users } = await businessGetUserInfo(state.selfInfo.userID);
  90. const businessData = users[0] ?? {};
  91. filterEmptyValue(businessData);
  92. commit("SET_SELF_INFO", {
  93. ...state.selfInfo,
  94. ...businessData,
  95. });
  96. } catch (e) {
  97. console.log(e);
  98. }
  99. },
  100. initCache({ commit}) {
  101. const cacheMap = uni.getStorageSync("CacheMap") || {};
  102. commit("INIT_CACHE_MAP", cacheMap);
  103. },
  104. addCacheTask({ commit, state }, { key, url}) {
  105. const task = state.cacheMap[key] || {};
  106. if (task.state){
  107. return;
  108. }
  109. task.state = "pending";
  110. commit("UPDATE_CACHE_MAP", {
  111. key,
  112. ...task
  113. });
  114. const failedCallback = () => {
  115. uni.showToast({
  116. title: "下载失败",
  117. icon: 'none'
  118. })
  119. commit("DELETE_CACHE_MAP", key);
  120. }
  121. uni.downloadFile({
  122. url,
  123. success: (res) => {
  124. if (res.statusCode === 200) {
  125. uni.saveFile({
  126. tempFilePath: res.tempFilePath,
  127. success: ({ savedFilePath }) => {
  128. task.state = "success";
  129. task.path = savedFilePath;
  130. commit("UPDATE_CACHE_MAP", {
  131. key,
  132. ...task
  133. });
  134. uni.showToast({
  135. title: `文件已保存到${getPurePath(savedFilePath)}`,
  136. icon: 'none'
  137. })
  138. },
  139. fail: function (err) {
  140. failedCallback()
  141. },
  142. });
  143. } else {
  144. failedCallback()
  145. }
  146. },
  147. fail: () => {
  148. failedCallback()
  149. },
  150. });
  151. },
  152. addLocalPathToCache({ commit }, { key, path }) {
  153. commit("UPDATE_CACHE_MAP", {
  154. key,
  155. state: "success",
  156. path
  157. });
  158. },
  159. deleteCacheData({ commit}, key) {
  160. commit("DELETE_CACHE_MAP", key);
  161. }
  162. };
  163. export default {
  164. namespaced: true,
  165. state,
  166. mutations,
  167. actions,
  168. };