util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. var env = 'development';
  2. const md5 = require('./md5.js');
  3. var wiki = require('./wiki.js');
  4. var location = require('./location.js');
  5. const baseUrl = "https://edu.ndjsxh.cn:8443/api"
  6. // const baseUrl = "http://localhost:8888"
  7. const formatTime = date => {
  8. const year = date.getFullYear()
  9. const month = date.getMonth() + 1
  10. const day = date.getDate()
  11. const hour = date.getHours()
  12. const minute = date.getMinutes()
  13. const second = date.getSeconds()
  14. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  15. }
  16. const filterTime = ( str) => {
  17. if(!str) return "---";
  18. return str.substr(0,19).replace("T", " ");
  19. }
  20. const curDate = () =>{
  21. var nowDate = new Date();
  22. var year = nowDate.getFullYear();
  23. var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
  24. var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
  25. return year + "-" + month + "-" + day;
  26. }
  27. const formatNumber = n => {
  28. n = n.toString()
  29. return n[1] ? n : '0' + n
  30. }
  31. // 显示繁忙提示
  32. var showBusy = text => wx.showToast({
  33. title: text,
  34. icon: 'loading',
  35. duration: 2000
  36. })
  37. // 显示成功提示
  38. var showSuccess = text => wx.showToast({
  39. title: text,
  40. icon: 'success'
  41. })
  42. var showMsg = text => wx.showToast({
  43. title: text,
  44. image: '/img/img_close.png',
  45. duration: 1000,
  46. mask: true
  47. })
  48. var showToast = (errCode, okText, text) => {
  49. if( errCode ==200 ){
  50. showSuccess( okText );
  51. }else{
  52. showMsg( text )
  53. }
  54. }
  55. // 显示失败提示
  56. var showModel = (title, content) => {
  57. wx.hideToast();
  58. wx.showModal({
  59. title,
  60. content: JSON.stringify(content),
  61. showCancel: false
  62. })
  63. }
  64. // 请求
  65. var http = (method, data, fun) => {
  66. let header = { 'content-type': 'application/json'}
  67. let authDB = wx.getStorageSync("@yunyuanqu");
  68. let user_id = authDB&&authDB.id||authDB.ID||0;
  69. let token=authDB&&authDB.token||''
  70. if( user_id && token ){
  71. header['x-token'] = token
  72. header['x-user-id']= user_id
  73. }
  74. let body = JSON.stringify(data);
  75. wx.request({
  76. method: "POST",
  77. url: `${baseUrl}${method}`,
  78. data: body,
  79. header:header,
  80. success: function(res) {
  81. if( res.statusCode === 200){
  82. let { code, data, msg} = res.data
  83. if( code == 0 ){
  84. fun&&fun(code, data)
  85. }else{
  86. if( data && data.reload ){
  87. wx.navigateTo({
  88. url: '/pages/login/login',
  89. })
  90. return
  91. }
  92. showModel("请求异常", msg)
  93. fun&&fun(code, msg)
  94. }
  95. }else{
  96. fun&&fun(404, "请求异常")
  97. }
  98. },
  99. fail: function(res) {
  100. showModel("请求异常", "网络异常")
  101. fun&&fun(404, "请求异常")
  102. }
  103. })
  104. }
  105. var uploadFile = (tempFilePaths, fun, sign) =>{
  106. let authDB = wx.getStorageSync("@yunyuanqu");
  107. let user_id = authDB&&authDB.id||authDB.ID||0;
  108. let token=authDB&&authDB.token||''
  109. let header={}
  110. if( user_id && token ){
  111. header['x-token'] = token
  112. header['x-user-id']= user_id
  113. }
  114. let murl = `${baseUrl}/wx/upload`
  115. if( sign ) murl = murl+"Sign"
  116. wx.uploadFile({
  117. url: murl,
  118. filePath: tempFilePaths,
  119. name: "file",
  120. header:header,
  121. success: function (res) {
  122. if( res.statusCode === 200){
  123. let { code, data, msg} = JSON.parse(res.data)
  124. console.log( code, data, msg )
  125. if( code == 0 ){
  126. fun&&fun(code, data)
  127. }else{
  128. fun&&fun(code, msg)
  129. }
  130. }else{
  131. fun&&fun(404, "请求异常")
  132. }
  133. },
  134. fail: function (res) {
  135. fun&&fun(404, "" );
  136. }
  137. })
  138. }
  139. module.exports = {
  140. formatTime,
  141. curDate,
  142. showBusy,
  143. showSuccess,
  144. showMsg,
  145. showModel,
  146. filterTime,
  147. http,
  148. uploadFile,
  149. showToast,
  150. wiki,
  151. baseUrl,
  152. location
  153. }