util.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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||0;
  69. let token=authDB&&authDB.token||''
  70. console.log( "authDB", authDB)
  71. if( user_id && token ){
  72. header['x-token'] = token
  73. header['x-user-id']= user_id
  74. }
  75. let body = JSON.stringify(data);
  76. wx.request({
  77. method: "POST",
  78. url: `${baseUrl}${method}`,
  79. data: body,
  80. header:header,
  81. success: function(res) {
  82. if( res.statusCode === 200){
  83. let { code, data, msg} = res.data
  84. if( code == 0 ){
  85. fun&&fun(code, data)
  86. }else{
  87. if( data && data.reload ){
  88. wx.navigateTo({
  89. url: '/pages/login/login',
  90. })
  91. return
  92. }
  93. showModel("请求异常", msg)
  94. fun&&fun(code, msg)
  95. }
  96. }else{
  97. fun&&fun(404, "请求异常")
  98. }
  99. },
  100. fail: function(res) {
  101. showModel("请求异常", "网络异常")
  102. fun&&fun(404, "请求异常")
  103. }
  104. })
  105. }
  106. var uploadFile = (tempFilePaths, fun) =>{
  107. let authDB = wx.getStorageSync("@yunyuanqu");
  108. let user_id = authDB&&authDB.ID||0;
  109. let token=authDB&&authDB.token||''
  110. let header={}
  111. if( user_id && token ){
  112. header['x-token'] = token
  113. header['x-user-id']= user_id
  114. }
  115. wx.uploadFile({
  116. url: `${baseUrl}/wx/upload`,
  117. filePath: tempFilePaths,
  118. name: "file",
  119. header:header,
  120. success: function (res) {
  121. if( res.statusCode === 200){
  122. let { code, data, msg} = JSON.parse(res.data)
  123. console.log( code, data, msg )
  124. if( code == 0 ){
  125. fun&&fun(code, data)
  126. }else{
  127. fun&&fun(code, msg)
  128. }
  129. }else{
  130. fun&&fun(404, "请求异常")
  131. }
  132. },
  133. fail: function (res) {
  134. fun&&fun(404, "" );
  135. }
  136. })
  137. }
  138. var uploadFile = (tempFilePaths, fun, sign) =>{
  139. let authDB = wx.getStorageSync("@yunyuanqu");
  140. let user_id = authDB&&authDB.ID||0;
  141. let token=authDB&&authDB.token||''
  142. let header={}
  143. if( user_id && token ){
  144. header['x-token'] = token
  145. header['x-user-id']= user_id
  146. }
  147. let murl = `${baseUrl}/wx/upload`
  148. if( sign ) murl = murl+"Sign"
  149. wx.uploadFile({
  150. url: murl,
  151. filePath: tempFilePaths,
  152. name: "file",
  153. header:header,
  154. success: function (res) {
  155. if( res.statusCode === 200){
  156. let { code, data, msg} = JSON.parse(res.data)
  157. console.log( code, data, msg )
  158. if( code == 0 ){
  159. fun&&fun(code, data)
  160. }else{
  161. fun&&fun(code, msg)
  162. }
  163. }else{
  164. fun&&fun(404, "请求异常")
  165. }
  166. },
  167. fail: function (res) {
  168. fun&&fun(404, "" );
  169. }
  170. })
  171. }
  172. module.exports = {
  173. formatTime,
  174. curDate,
  175. showBusy,
  176. showSuccess,
  177. showMsg,
  178. showModel,
  179. filterTime,
  180. http,
  181. uploadFile,
  182. showToast,
  183. wiki,
  184. baseUrl,
  185. location
  186. }