util.js 3.5 KB

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