util.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // const baseUrl = "http://192.168.3.2:8000/"
  2. const baseUrl = "https://edu.ndjsxh.cn:8443/"
  3. const ossUrl = "https://sm-sign.oss-cn-shanghai.aliyuncs.com/cert/"
  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 sign = "hall_" + version + mtime;
  54. wx.uploadFile({
  55. url: `${murl}?t=${mtime}&v=1&s=${sign}`,
  56. filePath: tempFilePaths,
  57. name: "avatar",
  58. header:header,
  59. success: function (res) {
  60. if( res.statusCode === 200){
  61. let { code, data, msg} = JSON.parse(res.data)
  62. if( code == 200 ){
  63. fun&&fun(code, data)
  64. }else{
  65. fun&&fun(code, msg)
  66. }
  67. }else{
  68. fun&&fun(404, "请求异常")
  69. }
  70. },
  71. fail: function (res) {
  72. fun&&fun(404, "" );
  73. }
  74. })
  75. }
  76. // 显示繁忙提示
  77. var showBusy = text => wx.showToast({
  78. title: text,
  79. icon: 'loading',
  80. duration: 2000
  81. })
  82. // 显示成功提示
  83. var showSuccess = text => wx.showToast({
  84. title: text,
  85. icon: 'success'
  86. })
  87. var showMsg = text => wx.showToast({
  88. title: text,
  89. image: '/assets/img_close.png',
  90. duration: 1000,
  91. mask: true
  92. })
  93. var showToast = (errCode, okText, text) => {
  94. if( errCode ==200 ){
  95. showSuccess( okText );
  96. }else{
  97. showMsg( text )
  98. }
  99. }
  100. // 显示失败提示
  101. var showModel = (title, content) => {
  102. wx.hideToast();
  103. wx.showModal({
  104. title,
  105. content: JSON.stringify(content),
  106. showCancel: false
  107. })
  108. }
  109. var downloadFile = ( imgSrc, cb )=>{
  110. var save = wx.getFileSystemManager();
  111. var number = Date.now()
  112. save.writeFile({
  113. filePath: wx.env.USER_DATA_PATH + '/pic_' + number + '.png',
  114. data: imgSrc,
  115. encoding: 'base64',
  116. success: res => {
  117. wx.saveImageToPhotosAlbum({
  118. filePath: wx.env.USER_DATA_PATH + '/pic_' + number + '.png',
  119. success: function (res) {
  120. wx.showToast({
  121. title: '保存成功',
  122. })
  123. cb &&cb( true );
  124. },
  125. fail: function (err) {
  126. cb &&cb( false );
  127. }
  128. })
  129. }, fail: err => {
  130. cb &&cb( false );
  131. }
  132. })
  133. }
  134. module.exports = {
  135. baseUrl,
  136. ossUrl,
  137. downloadFile,
  138. formatSeconds: formatSeconds,
  139. formatTime: formatTime,
  140. getMultResult,
  141. uploadFile,
  142. showModel,
  143. showSuccess,
  144. showToast,
  145. showMsg,
  146. showBusy
  147. }