common.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. import PinYin from "./pinyin";
  2. // 延迟引入 store,避免循环依赖和初始化阻塞
  3. let store;
  4. const getStore = () => {
  5. if (!store) {
  6. store = require('@/store').default;
  7. }
  8. return store;
  9. };
  10. import excel from "@/pages_im/static/images/file_message/file_excel.png";
  11. import ppt from "@/pages_im/static/images/file_message/file_ppt.png";
  12. import word from "@/pages_im/static/images/file_message/file_word.png";
  13. import zip from "@/pages_im/static/images/file_message/file_zip.png";
  14. import pdf from "@/pages_im/static/images/file_message/file_pdf.png";
  15. import unknown from "@/pages_im/static/images/file_message/file_unknown.png";
  16. export const html2Text = (html) => {
  17. if (!html) {
  18. return "";
  19. }
  20. return html
  21. .replace(/<\/p><p>/g, "\n")
  22. .replace(/\&nbsp;/g, " ")
  23. .replace(/<p>/g, "")
  24. .replace(/<\/p>/g, "")
  25. .replace(/<br>/g, "")
  26. .trim();
  27. };
  28. export const parseBr = (content) => {
  29. if (!content) {
  30. return "";
  31. }
  32. return content.replace(/\n/g, "\\n").trim();
  33. };
  34. export const formatInputHtml = (html) => {
  35. let atUsersInfo = [];
  36. let text = html2Text(html);
  37. const imgReg = new RegExp("(i?)(<img)([^>]+>)", "gmi");
  38. const customDataReg = /data-custom=".+"/;
  39. text = text.replace(imgReg, (img) => {
  40. if (img.includes('class="at_el"')) {
  41. const atInfoArr = img
  42. .match(customDataReg)[0]
  43. .slice(13, -1)
  44. .split("&amp;");
  45. atUsersInfo.push({
  46. atUserID: atInfoArr[0].slice(7),
  47. groupNickname: atInfoArr[1].slice(15),
  48. });
  49. return `@${atInfoArr[0].slice(7)} `;
  50. }
  51. if (img.includes('class="emoji_el"')) {
  52. return img.match(customDataReg)[0].slice(23, -1);
  53. }
  54. return "";
  55. });
  56. return {
  57. text,
  58. atUsersInfo,
  59. };
  60. };
  61. export const getEl = (el) => {
  62. return new Promise((resolve) => {
  63. const query = uni.createSelectorQuery().in(this);
  64. query
  65. .select(el)
  66. .boundingClientRect((data) => {
  67. // 存在data,且存在宽和高,视为渲染完毕
  68. resolve(data);
  69. })
  70. .exec();
  71. });
  72. };
  73. export const getDbDir = () => {
  74. return new Promise((resolve, reject) => {
  75. plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, (fs) => {
  76. fs.root.getDirectory(
  77. "user",
  78. {
  79. create: true,
  80. },
  81. (entry) => {
  82. console.log("qxj fullPath:"+entry.fullPath); // 打印完整路径
  83. resolve(entry.fullPath);
  84. },
  85. (error) => {
  86. reject(error);
  87. },
  88. );
  89. });
  90. });
  91. };
  92. export const formatChooseData = (data, key = "nickname") => {
  93. const ucfirst = (l1) => {
  94. if (l1.length > 0) {
  95. var first = l1.substr(0, 1).toUpperCase();
  96. var spare = l1.substr(1, l1.length);
  97. return first + spare;
  98. }
  99. };
  100. const arraySearch = (l1, l2) => {
  101. for (var name in PinYin) {
  102. if (PinYin[name].indexOf(l1) != -1) {
  103. return ucfirst(name);
  104. break;
  105. }
  106. }
  107. return false;
  108. };
  109. const codefans = (l1) => {
  110. l1 = l1 ?? "unkown";
  111. var l2 = l1.length;
  112. var I1 = "";
  113. var reg = new RegExp("[a-zA-Z0-9- ]");
  114. for (var i = 0; i < l2; i++) {
  115. var val = l1.substr(i, 1);
  116. var name = arraySearch(val, PinYin);
  117. if (reg.test(val)) {
  118. I1 += val;
  119. } else if (name !== false) {
  120. I1 += name;
  121. }
  122. }
  123. I1 = I1.replace(/ /g, "-");
  124. while (I1.indexOf("--") > 0) {
  125. I1 = I1.replace("--", "-");
  126. }
  127. return I1;
  128. };
  129. var arr = [],
  130. firstName;
  131. for (var i = 0; i < data.length; i++) {
  132. firstName = data[i].initial = codefans(data[i][key]).substr(0, 1);
  133. arr.push(firstName.toUpperCase());
  134. }
  135. var arrlist = [];
  136. for (i = 0; i < arr.length; i++) {
  137. if (arrlist.indexOf(arr[i]) == -1) {
  138. arrlist.push(arr[i]);
  139. }
  140. }
  141. var dataSort = [];
  142. for (var i = 0; i < arrlist.length; i++) {
  143. dataSort[i] = {
  144. initial: arrlist[i],
  145. };
  146. dataSort[i].data = [];
  147. for (var j = 0; j < data.length; j++) {
  148. if (data[j].initial.toUpperCase() == dataSort[i].initial) {
  149. dataSort[i].data.push(data[j]);
  150. }
  151. }
  152. }
  153. for (var i = 0; i < dataSort.length - 1; i++) {
  154. for (var j = 1; j < dataSort.length - i; j++) {
  155. if (dataSort[j - 1].initial > dataSort[j].initial) {
  156. var a = dataSort[j];
  157. dataSort[j] = dataSort[j - 1];
  158. dataSort[j - 1] = a;
  159. }
  160. }
  161. }
  162. const NomalInitial = "QWERTYUIOPLKJHGFDSAZXCVBNM".split("");
  163. const special = {
  164. initial: "#",
  165. data: [],
  166. };
  167. const newFilterData = dataSort.filter((d) => {
  168. if (!NomalInitial.includes(d.initial)) {
  169. special.data = [...special.data, ...d.data];
  170. } else {
  171. return d;
  172. }
  173. });
  174. if (special.data.length > 0) {
  175. newFilterData.push(special);
  176. }
  177. const indexList = newFilterData.map((item) => item.initial);
  178. const dataList = newFilterData.map((item) => item.data);
  179. return {
  180. indexList,
  181. dataList,
  182. };
  183. };
  184. export const getPurePath = (path) => {
  185. const prefix = "file://";
  186. const relativeRrefix = "_doc/";
  187. if (path.includes(prefix)) {
  188. path = path.replace(prefix, "");
  189. }
  190. if (path.includes(relativeRrefix)) {
  191. path = plus.io.convertLocalFileSystemURL(path);
  192. }
  193. return path;
  194. };
  195. export const filterEmptyValue = (obj) => {
  196. for (let key in obj) {
  197. if (obj[key] === "") {
  198. delete obj[key];
  199. }
  200. }
  201. };
  202. export const toastWithCallback = (message, callBack, duration = 2000) => {
  203. uni.$u.toast(message);
  204. setTimeout(callBack, duration);
  205. };
  206. export const checkLoginError = (error) => {
  207. if (!error?.errCode) {
  208. return "操作失败";
  209. }
  210. switch (error.errCode) {
  211. case 20001:
  212. return "密码错误";
  213. case 20002:
  214. return "账号不存在";
  215. case 20003:
  216. return "手机号已经注册";
  217. case 20004:
  218. return "账号已注册";
  219. case 20005:
  220. return "操作过于频繁,请稍后再试";
  221. case 20006:
  222. return "验证码错误";
  223. case 20007:
  224. return "验证码过期";
  225. case 20008:
  226. return "验证码错误次数超过限制,请稍后再试";
  227. case 20009:
  228. return "验证码已被使用";
  229. case 20010:
  230. return "邀请码已被使用";
  231. case 20011:
  232. return "邀请码不存在";
  233. case 20012:
  234. return "操作限制";
  235. case 20014:
  236. return "账号已注册";
  237. default:
  238. return "操作失败";
  239. }
  240. };
  241. export class IgexinTool {
  242. isAndorid = plus.os.name == "Android";
  243. PushManager;
  244. context;
  245. Instance;
  246. GeTuiSdk;
  247. constructor() {
  248. if (this.isAndorid) {
  249. this.PushManager = plus.android.importClass("com.igexin.sdk.PushManager");
  250. this.context = plus.android.runtimeMainActivity().getContext();
  251. this.Instance = this.PushManager.getInstance();
  252. } else {
  253. this.GeTuiSdk = plus.ios.importClass("GeTuiSdk");
  254. }
  255. }
  256. bindAlias(alias) {
  257. console.log("qxj bindAlias::"+alias+" context:"+this.context);
  258. if (this.isAndorid) {
  259. this.Instance.bindAlias(this.context, alias);
  260. } else {
  261. this.GeTuiSdk.bindAliasandSequenceNum(alias, alias);
  262. }
  263. }
  264. unbindAlias(alias) {
  265. console.log("unbindAlias", alias);
  266. if (this.isAndorid) {
  267. this.Instance.unBindAlias(this.context, alias, true);
  268. } else {
  269. this.GeTuiSdk.unbindAliasandSequenceNumandIsSelf(alias, alias, true);
  270. }
  271. }
  272. getVersion() {
  273. if (this.isAndorid) {
  274. return this.Instance.getVersion(this.context);
  275. } else {
  276. return this.GeTuiSdk.version;
  277. }
  278. }
  279. turnOnPush() {
  280. console.log("turnOnPush:::");
  281. if (this.isAndorid) {
  282. this.Instance.turnOnPush(this.context);
  283. } else {
  284. this.GeTuiSdk.setPushModeForOff(false);
  285. }
  286. plus.push.setAutoNotification(true);
  287. setTimeout(() => plus.push.getClientInfo(), 2000);
  288. }
  289. turnOffPush() {
  290. if (this.isAndorid) {
  291. this.Instance.turnOffPush(this.context);
  292. } else {
  293. this.GeTuiSdk.setPushModeForOff(true);
  294. }
  295. }
  296. }
  297. // #ifdef APP-PLUS
  298. export const Igexin = new IgexinTool();
  299. // #endif
  300. export const copyFileToDoc = (from, to = "background") => {
  301. return new Promise((resolve, reject) => {
  302. plus.io.resolveLocalFileSystemURL(
  303. from,
  304. (entry) => {
  305. plus.io.requestFileSystem(
  306. plus.io.PRIVATE_DOC,
  307. (fs) => {
  308. fs.root.getDirectory(
  309. to,
  310. {
  311. create: true,
  312. exclusive: false,
  313. },
  314. (dirEntry) => {
  315. entry.copyTo(
  316. dirEntry,
  317. null,
  318. (movedEntry) => {
  319. resolve(movedEntry.fullPath);
  320. },
  321. (err) => reject(err),
  322. );
  323. },
  324. (err) => reject(err),
  325. );
  326. },
  327. (err) => reject(err),
  328. );
  329. },
  330. (err) => reject(err),
  331. );
  332. });
  333. };
  334. export const getFileType = (name) => {
  335. const idx = name.lastIndexOf(".");
  336. return name.slice(idx + 1);
  337. };
  338. export const getFileIcon = (fileName) => {
  339. const fileType = getFileType(fileName);
  340. const wordType = ["doc", "docx", "docm", "dot"];
  341. const pdfType = ["pdf"];
  342. const pptType = ["pptx", "pptm", "ppt"];
  343. const excelType = ["xlsx", "xlsm", "xlsb", "xltx"];
  344. const zipType = ["zip", "rar", "tar", "gz"];
  345. if (wordType.includes(fileType)) {
  346. return word;
  347. }
  348. if (pdfType.includes(fileType)) {
  349. return pdf;
  350. }
  351. if (pptType.includes(fileType)) {
  352. return ppt;
  353. }
  354. if (excelType.includes(fileType)) {
  355. return excel;
  356. }
  357. if (zipType.includes(fileType)) {
  358. return zip;
  359. }
  360. return unknown;
  361. };
  362. export const checkFileIsExist = ({ key, path }) => {
  363. return new Promise((resolve) => {
  364. if (!path) {
  365. resolve("");
  366. return;
  367. }
  368. plus.io.resolveLocalFileSystemURL(
  369. path,
  370. (res) => {
  371. resolve(path);
  372. },
  373. (err) => {
  374. console.log(err);
  375. getStore().dispatch("user/deleteCacheData", key);
  376. resolve("");
  377. },
  378. );
  379. });
  380. }
  381. export const isDoctorAction = (userId) => {
  382. let isDoctorAct=false;
  383. if(userId!=undefined && (userId!="" || userId.length>0)){
  384. if(userId.indexOf('D')!==-1){
  385. isDoctorAct=true;
  386. }
  387. if(userId.indexOf('C')!==-1){
  388. }
  389. }
  390. return isDoctorAct;
  391. }