ygoa.js 2.5 KB

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