util.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import config from './config'
  2. const formatTime = (time, format = 'yyyy-mm-dd HH:MM') => {
  3. let date;
  4. if(time.toString().length == 13){
  5. date = new Date(time);
  6. }else{
  7. date = new Date(time * 1000);
  8. }
  9. let year = date.getFullYear()
  10. let month = formatNumber(date.getMonth() + 1)
  11. let day = formatNumber(date.getDate())
  12. let hour = formatNumber(date.getHours())
  13. let minute = formatNumber(date.getMinutes())
  14. let second = formatNumber(date.getSeconds())
  15. format = format.replace('yyyy',year)
  16. format = format.replace('mm',month)
  17. format = format.replace('dd',day)
  18. format = format.replace('HH',hour)
  19. format = format.replace('MM',minute)
  20. format = format.replace('ss',second)
  21. return format
  22. }
  23. const formatNumber = n => {
  24. n = n.toString()
  25. return n[1] ? n : `0${n}`
  26. }
  27. const cdn = (url) => {
  28. if (!url) {
  29. return ''
  30. }
  31. if (typeof url === 'string') {
  32. if ((/^(http|https):\/\/.+/.test(url))) {
  33. return url
  34. } else{
  35. return config.cdn + url
  36. }
  37. } else {
  38. let arr = []
  39. for (let i = 0; i < url.length; i++) {
  40. if ((/^(http|https):\/\/.+/.test(url[i]))) {
  41. arr.push(url[i])
  42. } else {
  43. arr.push(config.cdn + url[i])
  44. }
  45. }
  46. return arr
  47. }
  48. }
  49. const skip = (url, events = {}, success)=>{
  50. wx.navigateTo({
  51. url,
  52. events,
  53. success
  54. })
  55. }
  56. const authSkip = (url, events = {})=>{
  57. if(wx.getStorageSync('token')){
  58. wx.navigateTo({
  59. url,
  60. events
  61. })
  62. }else{
  63. wx.navigateTo({
  64. url: '/service/login/login',
  65. events
  66. })
  67. }
  68. }
  69. const back = (delta = 1) => {
  70. wx.navigateBack({
  71. delta: delta
  72. })
  73. }
  74. const toast = (msg, time = 2000) =>{
  75. wx.showToast({
  76. title: msg,
  77. icon: 'none',
  78. duration: time
  79. })
  80. }
  81. const previewImage = (arr,index=0)=>{
  82. wx.previewImage({
  83. urls: cdn(arr),
  84. current: cdn(arr)[index]
  85. })
  86. }
  87. module.exports = {
  88. formatTime : formatTime,
  89. cdn : cdn,
  90. skip : skip,
  91. authSkip: authSkip,
  92. back : back,
  93. toast : toast,
  94. previewImage: previewImage
  95. }