user.js 4.7 KB

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