util.js 3.5 KB

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