util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. const baseUrl = "http://localhost:8000/"
  2. const ossUrl = "https://sm-sign.oss-cn-shanghai.aliyuncs.com/cert/"
  3. const md5 = require('./md5.js');
  4. const formatTime = date => {
  5. const year = date.getFullYear()
  6. const month = date.getMonth() + 1
  7. const day = date.getDate()
  8. const hour = date.getHours()
  9. const minute = date.getMinutes()
  10. const second = date.getSeconds()
  11. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  12. }
  13. const formatDate = date => {
  14. const year = date.getFullYear()
  15. const month = date.getMonth() + 1
  16. const day = date.getDate()
  17. return [year, month, day].map(formatNumber).join('-')
  18. }
  19. const sec2Date = sec => {
  20. let date = new Date( sec * 1000 );
  21. const year = date.getFullYear()
  22. const month = date.getMonth() + 1
  23. const day = date.getDate()
  24. return [year, month, day].map(formatNumber).join('-')
  25. }
  26. const nextDate = datestr => {
  27. let date = new Date( datestr );
  28. date = new Date(+date + 86400*1000)
  29. const year = date.getFullYear()
  30. const month = date.getMonth() + 1
  31. const day = date.getDate()
  32. return [year, month, day].map(formatNumber).join('-')
  33. }
  34. const formatNumber = n => {
  35. n = n.toString()
  36. return n[1] ? n : '0' + n
  37. }
  38. const formatSeconds = theTime => {
  39. let theTime1 = 0
  40. let theTime2 = 0
  41. if (theTime > 60) {
  42. theTime1 = parseInt(theTime / 60)
  43. theTime = parseInt(theTime % 60)
  44. if (theTime1 > 60) {
  45. theTime2 = parseInt(theTime1 / 60)
  46. theTime1 = parseInt(theTime1 % 60)
  47. }
  48. }
  49. let result = '' + parseInt(theTime) + '秒'
  50. if (theTime1 > 0) {
  51. result = '' + parseInt(theTime1) + '分' + result
  52. }
  53. if (theTime2 > 0) {
  54. result = '' + parseInt(theTime2) + '小时' + result
  55. }
  56. return result
  57. }
  58. const getMultResult = select=>{
  59. let prevList = ["", "A","B","C", "D", "E","F"]
  60. let slist = ''+select
  61. let res = ""
  62. for( let i in slist){
  63. res+=prevList[slist[i]]
  64. console.log(slist[i], prevList[slist[i]] )
  65. }
  66. return res
  67. }
  68. var uploadFile = (tempFilePaths, fun) =>{
  69. let header={}
  70. let murl = `${baseUrl}upload`
  71. let version= "1.0"
  72. let mtime = parseInt(Date.now() / 1000);
  73. // 未登入
  74. let signstr = "hall_" + version + mtime;
  75. let sign = md5.md5(signstr).toLowerCase()
  76. wx.uploadFile({
  77. url: `${murl}?t=${mtime}&v=1&s=${sign}&u=0`,
  78. filePath: tempFilePaths,
  79. name: "avatar",
  80. header:header,
  81. success: function (res) {
  82. if( res.statusCode === 200){
  83. let { code, data, msg} = JSON.parse(res.data)
  84. if( code == 200 ){
  85. fun&&fun(code, data)
  86. }else{
  87. fun&&fun(code, msg)
  88. }
  89. }else{
  90. fun&&fun(404, "请求异常")
  91. }
  92. },
  93. fail: function (res) {
  94. fun&&fun(404, "" );
  95. }
  96. })
  97. }
  98. // 显示繁忙提示
  99. var showBusy = text => wx.showToast({
  100. title: text,
  101. icon: 'loading',
  102. duration: 2000
  103. })
  104. // 显示成功提示
  105. var showSuccess = text => wx.showToast({
  106. title: text,
  107. icon: 'success'
  108. })
  109. var showMsg = text => wx.showToast({
  110. title: text,
  111. image: '/assets/img_close.png',
  112. duration: 1000,
  113. mask: true
  114. })
  115. var showToast = (errCode, okText, text) => {
  116. if( errCode ==200 ){
  117. showSuccess( okText );
  118. }else{
  119. showMsg( text )
  120. }
  121. }
  122. // 显示失败提示
  123. var showModel = (title, content) => {
  124. wx.hideToast();
  125. wx.showModal({
  126. title,
  127. content: JSON.stringify(content),
  128. showCancel: false
  129. })
  130. }
  131. var downloadFile = ( imgSrc, cb )=>{
  132. var save = wx.getFileSystemManager();
  133. var number = Date.now()
  134. save.writeFile({
  135. filePath: wx.env.USER_DATA_PATH + '/pic_' + number + '.png',
  136. data: imgSrc,
  137. encoding: 'base64',
  138. success: res => {
  139. wx.saveImageToPhotosAlbum({
  140. filePath: wx.env.USER_DATA_PATH + '/pic_' + number + '.png',
  141. success: function (res) {
  142. wx.showToast({
  143. title: '保存成功',
  144. })
  145. cb &&cb( true );
  146. },
  147. fail: function (err) {
  148. cb &&cb( false );
  149. }
  150. })
  151. }, fail: err => {
  152. cb &&cb( false );
  153. }
  154. })
  155. }
  156. module.exports = {
  157. baseUrl,
  158. ossUrl,
  159. downloadFile,
  160. formatSeconds: formatSeconds,
  161. formatTime: formatTime,
  162. getMultResult,
  163. uploadFile,
  164. showModel,
  165. showSuccess,
  166. showToast,
  167. showMsg,
  168. sec2Date,
  169. nextDate,
  170. formatDate,
  171. showBusy
  172. }