common.js 11 KB

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