Sanakey
2021-07-09 3fc241d02fb8b671289de3d9d80bf848349c04d4
utils/util.ts
@@ -1,23 +1,28 @@
const formatTime = (dateString: Date) => {
  let date = new Date(dateString);
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return (
    [year, month, day].map(formatNumber).join('-') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
import * as dayjs from "dayjs";
const formatTime = (dateString: Date,format: string ='YYYY-MM-DD HH:mm') => {
  return dayjs(dateString).format(format);
}
const formatNumber = (n: number) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}
// const formatTime2 = (dateString: Date) => {
//   let date = new Date(dateString);
//   const year = date.getFullYear()
//   const month = date.getMonth() + 1
//   const day = date.getDate()
//   const hour = date.getHours()
//   const minute = date.getMinutes()
//   const second = date.getSeconds()
//
//   return (
//     [year, month, day].map(formatNumber).join('-') +
//     ' ' +
//     [hour, minute, second].map(formatNumber).join(':')
//   )
// }
//
// const formatNumber = (n: number) => {
//   const s = n.toString()
//   return s[1] ? s : '0' + s
// }
const to = (promise:Promise<any>)=>{
  return promise.then((res:any)=>{
@@ -62,7 +67,7 @@
  });
}
const debounce = (fn:()=>{},delay:number = 500)=>{
const debounce = (fn:()=>{},delay:number = 200)=>{
  let timer:any = null;
  return function () {
    if (timer) clearTimeout(timer);
@@ -176,6 +181,18 @@
  return new Proxy(target, handler)
}
const scrollToError = (selector:string, errorMsg:string, duration:number = 3000)=>{
  wx.pageScrollTo({
    selector,
    duration: 300
  })
  wx.showToast({
    title: errorMsg,
    icon: 'none',
    duration
  })
}
export default{
  formatTime,
  to,
@@ -187,5 +204,6 @@
  getLocation,
  getAuthorizeInfo,
  setShopName,
  watchObj
  watchObj,
  scrollToError
}