util.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. const year = date.getFullYear()
  10. const month = formatNumber(date.getMonth() + 1)
  11. const day = formatNumber(date.getDate())
  12. const hour = formatNumber(date.getHours())
  13. const minute = formatNumber(date.getMinutes())
  14. const 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 = {})=>{
  50. wx.navigateTo({
  51. url,
  52. events
  53. })
  54. }
  55. const authSkip = (url, events = {})=>{
  56. if(wx.getStorageSync('token')){
  57. wx.navigateTo({
  58. url,
  59. events
  60. })
  61. }else{
  62. wx.navigateTo({
  63. url: '/service/login/login',
  64. events
  65. })
  66. }
  67. }
  68. const back = (delta = 1) => {
  69. wx.navigateBack({
  70. delta: delta
  71. })
  72. }
  73. const toast = (msg, time = 2000) =>{
  74. wx.showToast({
  75. title: msg,
  76. icon: 'none',
  77. duration: time
  78. })
  79. }
  80. function setWatcher(page) {
  81. let data = page.data;
  82. let watch = page.watch;
  83. Object.keys(watch).forEach(v => {
  84. let key = v.split('.');
  85. let nowData = data;
  86. for (let i = 0; i < key.length - 1; i++) {
  87. nowData = nowData[key[i]];
  88. }
  89. let lastKey = key[key.length - 1];
  90. let watchFun = watch[v].handler || watch[v];
  91. let deep = watch[v].deep;
  92. observe(nowData, lastKey, watchFun, deep, page);
  93. })
  94. }
  95. function observe(obj, key, watchFun, deep, page) {
  96. var val = obj[key];
  97. if (deep && val != null && typeof val === 'object') {
  98. Object.keys(val).forEach(childKey => {
  99. observe(val, childKey, watchFun, deep, page);
  100. })
  101. }
  102. Object.defineProperty(obj, key, {
  103. configurable: true,
  104. enumerable: true,
  105. set: function(newVal) {
  106. watchFun.call(page, newVal, val);
  107. val = newVal;
  108. if (deep) {
  109. observe(obj, key, watchFun, deep, page);
  110. }
  111. },
  112. get: function() {
  113. return val;
  114. }
  115. })
  116. }
  117. const previewImage = (arr,index=0)=>{
  118. wx.previewImage({
  119. urls: cdn(arr),
  120. current: cdn(arr)[index]
  121. })
  122. }
  123. module.exports = {
  124. formatTime : formatTime,
  125. cdn : cdn,
  126. skip : skip,
  127. authSkip: authSkip,
  128. back : back,
  129. toast : toast,
  130. setWatcher: setWatcher,
  131. previewImage: previewImage
  132. }