ygoa.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { getUserInfo,getLoginInfo } from '@/utils/auth'
  2. import { useUserStore } from '@/store/user';
  3. import $modal from '@/plugins/modal.js'
  4. const userStore=useUserStore();
  5. export function convertToChineseCurrency(amount) {
  6. if (amount === "0" || amount === 0) {
  7. return "零元整";
  8. }
  9. const CN_NUMS = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
  10. const CN_INT = ["", "拾", "佰", "千"];
  11. const CN_UNIT = ["", "万", "亿", "兆"];
  12. const CN_DECIMAL = ["角", "分", "厘", "毫"];
  13. // 将金额转换为字符串并去掉多余的零
  14. let str = amount.toString().replace(/[^0-9.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./, '').replace('$#$', '.');
  15. let [integerPart, decimalPart] = str.split('.');
  16. // 处理整数部分
  17. let integerLen = integerPart.length;
  18. let integerResult = '';
  19. let zeroCount = 0; // 记录连续的零
  20. for (let i = 0; i < integerLen; i++) {
  21. let num = integerPart.charAt(i);
  22. let unitPos = integerLen - i - 1;
  23. let unitIndex = unitPos % 4;
  24. // 当前位为零时不进行处理
  25. if (num === '0') {
  26. zeroCount++;
  27. } else {
  28. if (zeroCount > 0) {
  29. integerResult += CN_NUMS[0];
  30. }
  31. zeroCount = 0;
  32. integerResult += CN_NUMS[parseInt(num)] + CN_INT[unitIndex];
  33. }
  34. if (unitIndex === 0) {
  35. integerResult += CN_UNIT[Math.floor(unitPos / 4)];
  36. }
  37. }
  38. // 处理小数部分
  39. let decimalResult = '';
  40. if (decimalPart) {
  41. let decimalLen = decimalPart.length;
  42. // 最多显示4位小数
  43. if (decimalLen > 4) decimalLen = 4
  44. integerResult += '元'
  45. for (let i = 0; i < decimalLen; i++) {
  46. let num = decimalPart.charAt(i);
  47. if (num !== '0') {
  48. decimalResult += CN_NUMS[parseInt(num)] + CN_DECIMAL[i];
  49. }
  50. }
  51. }
  52. if (!decimalResult) {
  53. decimalResult = '整';
  54. }
  55. return integerResult + decimalResult;
  56. }
  57. export function clearCache() {
  58. $modal.confirm('是否确定要清理缓存?').then(res=>{
  59. const userInfo = getUserInfo();
  60. const loginInfo=getLoginInfo();
  61. uni.clearStorageSync();
  62. uni.setStorageSync('userInfo', userInfo)
  63. uni.setStorageSync('loginInfo', loginInfo)
  64. userStore.user = userInfo
  65. userStore.useId = userInfo.useId
  66. // 提示用户缓存清理成功
  67. uni.showToast({
  68. title: '缓存清理成功',
  69. icon: 'success',
  70. duration: 2000
  71. });
  72. }).catch(()=>{})
  73. }