common.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // import { CryptoJS } from './crypto-js.min.js';
  2. import { CryptoJS } from './crypto-js.min.js';
  3. let loading = false;
  4. let count = 0;
  5. /** 请求弹窗开启 */
  6. export const showLoading = function (title = '') {
  7. if (!loading) {
  8. uni.showLoading({
  9. title: title || '',
  10. mask: true,
  11. });
  12. loading = true;
  13. }
  14. count += 1;
  15. };
  16. /** 请求弹窗关闭 */
  17. export const hideLoading = function () {
  18. let timer = setTimeout(() => {
  19. // 在下一次tick中如果没有新的loading进来(count === 1) 则关闭loading (消除多接口同步或者异步的loading闪屏)
  20. if (count === 1 && loading) {
  21. uni.hideLoading();
  22. loading = false;
  23. }
  24. count -= 1;
  25. clearTimeout(timer);
  26. timer = null;
  27. }, 0);
  28. };
  29. /** 提示消息 */
  30. export const showToast = function (msg: string, fn = () => {}, duration = 1500) {
  31. let timer = null;
  32. uni.showToast({
  33. title: msg,
  34. mask: true,
  35. icon: 'none',
  36. duration: duration,
  37. });
  38. if (fn) {
  39. timer = setTimeout(() => {
  40. fn();
  41. timer = null;
  42. }, duration);
  43. }
  44. };
  45. /** 提示信息 */
  46. export const showModal = function (content: string, callBack = () => {}, data: any = {}) {
  47. uni.showModal({
  48. content: content,
  49. title: data.title || '',
  50. confirmText: data.confirmText || '确定',
  51. confirmColor: data.confirmColor || '',
  52. cancelColor: data.cancelColor || '',
  53. cancelText: data.cancelText || '',
  54. showCancel: data.cancelText ? true : false,
  55. success: (res) => {
  56. if (res.confirm && callBack instanceof Function) {
  57. callBack();
  58. }
  59. if (!res.confirm && data.callBack instanceof Function) {
  60. data.callBack();
  61. }
  62. },
  63. });
  64. };
  65. /** 是否为空 */
  66. export const isEmpty = function (obj) {
  67. if (obj == undefined || obj == null || obj == '') {
  68. return true;
  69. } else if (obj instanceof Array) {
  70. return obj.length <= 0;
  71. } else if (obj instanceof Object) {
  72. return Object.keys(obj).length <= 0;
  73. }
  74. return false;
  75. };
  76. /** 是否不为空 */
  77. export const isNotEmpty = function (obj) {
  78. return !isEmpty(obj);
  79. };
  80. /** des加密 */
  81. export const desEncrypt = function (message, key) {
  82. let keyHex = CryptoJS.enc.Utf8.parse(key);
  83. let encrypted = CryptoJS.DES.encrypt(message, keyHex, {
  84. mode: CryptoJS.mode.ECB,
  85. padding: CryptoJS.pad.Pkcs7,
  86. });
  87. return encrypted.toString();
  88. };
  89. /** des ECB解密str:秘钥串;key:秘钥;exportType:输出方式; */
  90. export const desDecrypt = function (str, key, exportType) {
  91. let keyHex = CryptoJS.enc.Utf8.parse(key);
  92. let decrypted = CryptoJS.DES.decrypt(
  93. exportType == 'hex'
  94. ? {
  95. ciphertext: CryptoJS.enc.Hex.parse(str),
  96. }
  97. : str,
  98. keyHex,
  99. {
  100. mode: CryptoJS.mode.ECB,
  101. padding: CryptoJS.pad.Pkcs7,
  102. }
  103. );
  104. return decrypted.toString(CryptoJS.enc.Utf8);
  105. };
  106. /** 数组转化成对象,方便使用 */
  107. export const turnToMap = (list) => {
  108. let map = {};
  109. list.map((item) => {
  110. map[item.routePath] = JSON.parse(item.infoJson || '{}');
  111. });
  112. return map;
  113. };