user.js 4.8 KB

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