util.js 4.3 KB

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