common.js 9.7 KB

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