user.js 4.3 KB

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