helper.js 3.02 KB
export async function wechatShareSetting(axios, store, shareConfig = {
    title: `借条100`, // 分享标题
    link: `https://www.51liuliang.cc/netiou`, 
    imgUrl: 'https://www.51liuliang.cc/netiou/logo1.png',
    desc: '',
    success: function () {},
    cancel: function () {}
}) {
  const u = navigator.userAgent
  const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
  let {data}  = await axios.post(`/wechatTicket`, {
    userId: store.state.user.id,
    href: isiOS ? store.state.firstUrl : window.location.href
  })
  if (data.success) {
    window.wx.config({
      debug: false, 
      appId: data.config.appId, // 必填,公众号的唯一标识
      timestamp: data.config.timestamp, // 必填,生成签名的时间戳
      nonceStr: data.config.nonceStr, // 必填,生成签名的随机串
      signature: data.config.signature,// 必填,签名
      jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填,需要使用的JS接口列表
    })
    window.wx.ready(() => {
      window.wx.onMenuShareTimeline(shareConfig)
      window.wx.onMenuShareAppMessage(shareConfig)
    })
    window.wx.error(function(res){
      // alert(res)
    })
  }
}

export function testIOSVersion() {
  const u = navigator.userAgent.toLowerCase()
  const ver = u.match(/cpu iphone os (.*?) like mac os/)
  if (!ver) {
    return true
  } else {
    return (parseFloat(ver[1].replace(/_/g,".")) < 10 ? false : true)
  }
}

/**
 * 获取YYYY-MM-DD格式的字符串
 * @param {string} source 日期字符串
 */
export function dateParse(source) {
  let date = source ? new Date(source) : new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  month = month < 10 ? '0'+ month : month
  day = day < 10 ? '0'+ day : day      
  return `${year}-${month}-${day}`
}

/**
 * 利息计算(不含违约金)
 * @param {float} amount 借款金额
 * @param {float} rate 利率
 * @param {string} start 借款起始日期 YYYY-MM-DD
 * @param {string} end 借款结束日期 YYYY-MM-DD
 */
export function interest(amount, rate, start, end) {
    if (!start || !end) return 0 
    start = new Date(start).getTime()
    end = new Date(end).getTime()
    let gap =  Math.floor((end - start) / (1000 * 60 * 60 * 24))
    gap = gap > 0 ? gap : 0 
  return Math.round(amount * rate / 365 * gap * 100) / 100
}

/**
 * 利息计算(含违约金)
 * @param {float} amount 借款金额
 * @param {float} rate 利率
 * @param {string} start 借款起始日期 YYYY-MM-DD
 * @param {string} end 借款结束日期 YYYY-MM-DD
 */
export function interestWithDamage(amount, rate, start, end) {
  if (!start || !end) return 0 
  let now = new Date().getTime()
  start = new Date(start).getTime()
  end = new Date(end).getTime()
  let gap =  (end - start) / (1000 * 60 * 60 * 24)
  gap = gap > 0 ? gap : 0 
  let overdue
  if (now <= end) {
    overdue = 0
  } else {
    overdue =  Math.floor((now - end) / (1000 * 60 * 60 * 24))
  }
return Math.round((amount * rate / 365 * gap + amount * 0.24 / 365 * overdue) * 100) / 100
}