util.js 4.3 KB

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