user.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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") || "14px",
  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(IMSDK.IMMethods.GetSelfUserInfo,uuidv4());
  67. // const { users } = await businessGetUserInfo(data.userID);
  68. // let businessData = users[0] ?? {};
  69. // filterEmptyValue(businessData);
  70. // const { users:members } = await getUserInDepartment(data.userID)
  71. var userId=uni.getStorageSync('userId');
  72. var avatar=uni.getStorageSync('avatar');
  73. var nickName=uni.getStorageSync('nickName');
  74. var uid="U"+userId;
  75. let businessData={userID:uid,nickname:nickName,faceURL:avatar};
  76. commit("SET_SELF_INFO", {
  77. ...data,
  78. ...businessData,
  79. //members: members[0]?.members || []
  80. members: []
  81. });
  82. // const res = await getSubDepartment();
  83. // commit("SET_ORGANIZATION_DATA", res);
  84. } catch (e) {
  85. console.log(e);
  86. //uni.$u.toast("获取个人信息失败");
  87. }
  88. },
  89. async updateBusinessInfo({ commit, state }) {
  90. try {
  91. const { users } = await businessGetUserInfo(state.selfInfo.userID);
  92. const businessData = users[0] ?? {};
  93. filterEmptyValue(businessData);
  94. commit("SET_SELF_INFO", {
  95. ...state.selfInfo,
  96. ...businessData,
  97. });
  98. } catch (e) {
  99. console.log(e);
  100. }
  101. },
  102. initCache({ commit}) {
  103. const cacheMap = uni.getStorageSync("CacheMap") || {};
  104. commit("INIT_CACHE_MAP", cacheMap);
  105. },
  106. addCacheTask({ commit, state }, { key, url}) {
  107. const task = state.cacheMap[key] || {};
  108. if (task.state){
  109. return;
  110. }
  111. task.state = "pending";
  112. commit("UPDATE_CACHE_MAP", {
  113. key,
  114. ...task
  115. });
  116. const failedCallback = () => {
  117. uni.showToast({
  118. title: "下载失败",
  119. icon: 'none'
  120. })
  121. commit("DELETE_CACHE_MAP", key);
  122. }
  123. uni.downloadFile({
  124. url,
  125. success: (res) => {
  126. if (res.statusCode === 200) {
  127. uni.saveFile({
  128. tempFilePath: res.tempFilePath,
  129. success: ({ savedFilePath }) => {
  130. task.state = "success";
  131. task.path = savedFilePath;
  132. commit("UPDATE_CACHE_MAP", {
  133. key,
  134. ...task
  135. });
  136. uni.showToast({
  137. title: `文件已保存到${getPurePath(savedFilePath)}`,
  138. icon: 'none'
  139. })
  140. },
  141. fail: function (err) {
  142. failedCallback()
  143. },
  144. });
  145. } else {
  146. failedCallback()
  147. }
  148. },
  149. fail: () => {
  150. failedCallback()
  151. },
  152. });
  153. },
  154. addLocalPathToCache({ commit }, { key, path }) {
  155. commit("UPDATE_CACHE_MAP", {
  156. key,
  157. state: "success",
  158. path
  159. });
  160. },
  161. deleteCacheData({ commit}, key) {
  162. commit("DELETE_CACHE_MAP", key);
  163. }
  164. };
  165. export default {
  166. namespaced: true,
  167. state,
  168. mutations,
  169. actions,
  170. };