common.js 3.29 KB
/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content
  })
}

/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function(res) {
        resolve(res)
      }
    })
  })
}

/**
* 参数处理
* @param params 参数
*/
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    var part = encodeURIComponent(propName) + "="
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']'
            var subPart = encodeURIComponent(params) + "="
            result += subPart + encodeURIComponent(value[key]) + "&"
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&"
      }
    }
  }
  return result
}

// 金额转人民币大写
export function formatCurrencyToChinese(num) {
  // 处理空值或非数字
  if (!num || isNaN(Number(num))) return '零元整';

  // 转换为数字并保留2位小数(避免分以下的单位)
  const n = parseFloat(Number(num).toFixed(2));
  if (n === 0) return '零元整';

  const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  const unit = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟'];
  const decUnit = ['角', '分'];
  let str = '';

  // 处理整数部分
  const integer = Math.floor(n);
  // 处理小数部分(角、分)
  const decimal = Math.round((n - integer) * 100);
  const hasDecimal = decimal > 0;

  // 转换整数部分
  if (integer > 0) {
    let intStr = integer.toString();
    for (let i = 0; i < intStr.length; i++) {
      const digitNum = parseInt(intStr[i]);
      const unitIndex = intStr.length - 1 - i;
      if (digitNum !== 0) {
        str += digit[digitNum] + unit[unitIndex];
      } else {
        // 避免连续多个零
        if (intStr[i - 1] !== '0' || (unitIndex % 4 === 0 && unitIndex > 0)) {
          str += digit[0];
        }
      }
    }
    str += '元';
  } else {
    str += '零元';
  }

  // 转换小数部分
  if (hasDecimal) {
    const jiao = Math.floor(decimal / 10);
    const fen = decimal % 10;
    if (jiao > 0) str += digit[jiao] + decUnit[0];
    if (fen > 0) str += digit[fen] + decUnit[1];
  } else {
    str += '整';
  }

  // 处理特殊情况(如"零元整"已在开头处理,此处处理多零情况)
  return str.replace(/零+/g, '零').replace(/零([万亿])/g, '$1').replace(/零元/, '元')
    .replace(/零角零分$/, '整').replace(/零分$/, '整');
}

/**
 * 校验邮箱格式
 * @param email
 * @returns {boolean}
 */
export function isEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return reg.test(email);
}