util.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. const app = getApp()
  2. function is_loading() {
  3. wx.showLoading({
  4. title: '加载中',
  5. })
  6. }
  7. function over_loading() {
  8. wx.hideLoading()
  9. }
  10. function tips_fail() {
  11. wx.showToast({
  12. title: '网络错误',
  13. icon: 'none',
  14. duration: 2000,
  15. })
  16. }
  17. function postDataByName(data, success_fn) {
  18. wx.setStorageSync('isLoading', 'true')
  19. wx.request({
  20. url: app.globalData.g_url + 'authdata/PostDataByName',
  21. data: data,
  22. method: 'POST',
  23. header: {
  24. 'Content-Type': 'json',
  25. token: app.globalData.g_token,
  26. },
  27. success: function (res) {
  28. //console.log("res",res)
  29. over_loading()
  30. wx.setStorageSync('isLoading', 'false')
  31. if (res.data.code == 200) {
  32. success_fn(res.data)
  33. }
  34. },
  35. fail: function (error) {
  36. console.log(error)
  37. tips_fail()
  38. over_loading()
  39. wx.setStorageSync('isLoading', 'false')
  40. },
  41. })
  42. }
  43. function execDataByConfig(data, success_fn) {
  44. wx.setStorageSync('isLoading', 'true')
  45. is_loading()
  46. wx.request({
  47. url: app.globalData.g_url + 'authdata/ExecDataByConfig',
  48. data: data,
  49. method: 'POST',
  50. header: {
  51. 'Content-Type': 'json',
  52. token: app.globalData.g_token,
  53. },
  54. success: function (res) {
  55. //console.log("res",res)
  56. if (res.data.code == 200) {
  57. success_fn(res.data)
  58. }
  59. over_loading()
  60. wx.setStorageSync('isLoading', 'false')
  61. },
  62. fail: function (error) {
  63. console.log(error)
  64. tips_fail()
  65. over_loading()
  66. wx.setStorageSync('isLoading', 'false')
  67. },
  68. })
  69. }
  70. function getDataByName(data, success_fn) {
  71. wx.setStorageSync('isLoading', 'true')
  72. is_loading()
  73. wx.request({
  74. url: app.globalData.g_url + 'authdata/GetDataByName',
  75. data: data,
  76. method: 'POST',
  77. header: {
  78. 'Content-Type': 'json',
  79. token: app.globalData.g_token,
  80. },
  81. success: function (res) {
  82. //console.log("res",res)
  83. if (res.data.code == 200) {
  84. success_fn(res.data)
  85. }
  86. over_loading()
  87. wx.setStorageSync('isLoading', 'false')
  88. },
  89. fail: function (error) {
  90. console.log(error)
  91. tips_fail()
  92. over_loading()
  93. wx.setStorageSync('isLoading', 'false')
  94. },
  95. })
  96. }
  97. function getDataByNames(data, success_fn) {
  98. wx.setStorageSync('isLoading', 'true')
  99. is_loading()
  100. wx.request({
  101. url: app.globalData.g_url + 'authdata/GetDataByNames',
  102. data: data,
  103. method: 'POST',
  104. header: {
  105. 'Content-Type': 'json',
  106. token: app.globalData.g_token,
  107. },
  108. success: function (res) {
  109. //console.log("res",res)
  110. if (res.data.code == 200) {
  111. success_fn(res.data)
  112. }
  113. over_loading()
  114. wx.setStorageSync('isLoading', 'false')
  115. },
  116. fail: function (error) {
  117. console.log(error)
  118. tips_fail()
  119. over_loading()
  120. wx.setStorageSync('isLoading', 'false')
  121. },
  122. })
  123. }
  124. function ajax(url, data, method, success_fn, error_fn) {
  125. wx.setStorageSync('isLoading', 'true')
  126. is_loading()
  127. wx.request({
  128. url: url,
  129. data: data,
  130. method: method,
  131. header: {
  132. 'Content-Type': 'json',
  133. },
  134. success: function (res) {
  135. //console.log("res",res)
  136. if (res.data.code == 200) {
  137. success_fn(res.data)
  138. } else {
  139. if (error_fn) {
  140. error_fn(res)
  141. }
  142. }
  143. over_loading()
  144. wx.setStorageSync('isLoading', 'false')
  145. },
  146. fail: function (error) {
  147. console.log(error)
  148. tips_fail()
  149. over_loading()
  150. wx.setStorageSync('isLoading', 'false')
  151. },
  152. })
  153. }
  154. // function ajax(url, data, method, success_fn, error_fn) {
  155. // is_loading()
  156. // wx.request({
  157. // url: url,
  158. // data: data,
  159. // method: method,
  160. // header: {
  161. // "Content-Type": "json"
  162. // },
  163. // success: function (res) {
  164. // if (res.data.code == 1 || res.data.code == 0) {
  165. // success_fn(res)
  166. // over_loading()
  167. // } else {
  168. // if (error_fn) {
  169. // error_fn(res)
  170. // }
  171. // over_loading()
  172. // }
  173. // },
  174. // fail: function (error) {
  175. // console.log(error)
  176. // over_loading()
  177. // tips_fail()
  178. // }
  179. // })
  180. // }
  181. function getYesterday() {
  182. var day1 = new Date()
  183. day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000)
  184. var s1 = day1.getFullYear() + '-' + (day1.getMonth() + 1) + '-' + day1.getDate()
  185. return s1
  186. }
  187. function getRealToday() {
  188. var day = new Date()
  189. day.setTime(day.getTime())
  190. var s =
  191. day.getFullYear() +
  192. '-' +
  193. (day.getMonth() + 1).toString().padStart(2, '0') +
  194. '-' +
  195. day.getDate().toString().padStart(2, '0')
  196. return s
  197. }
  198. function getReal7daysToday() {
  199. var day11 = new Date()
  200. day11.setTime(day11.getTime() - 24 * 6 * 60 * 60 * 1000)
  201. var s11 = day11.getFullYear() + '-' + (day11.getMonth() + 1) + '-' + day11.getDate()
  202. return s11
  203. }
  204. function getToday() {
  205. var day2 = new Date()
  206. day2.setTime(day2.getTime())
  207. var s2 =
  208. day2.getFullYear() +
  209. '-' +
  210. String(day2.getMonth() + 1).padStart(2, '0') +
  211. '-' +
  212. String(day2.getDate() + 1).padStart(2, '0')
  213. return s2
  214. }
  215. function get7daysAgo() {
  216. var day4 = new Date()
  217. day4.setTime(day4.getTime() - 24 * 6 * 60 * 60 * 1000)
  218. var s4 =
  219. day4.getFullYear() +
  220. '-' +
  221. String(day4.getMonth() + 1).padStart(2, '0') +
  222. '-' +
  223. String(day4.getDate()).padStart(2, '0')
  224. return s4
  225. }
  226. function get15daysAgo() {
  227. var day5 = new Date()
  228. day5.setTime(day5.getTime() - 24 * 15 * 60 * 60 * 1000)
  229. var s5 = day5.getFullYear() + '-' + (day5.getMonth() + 1) + '-' + day5.getDate()
  230. return s5
  231. }
  232. function get91daysAgo() {
  233. var day6 = new Date()
  234. day6.setTime(day6.getTime() - 24 * 90 * 60 * 60 * 1000)
  235. var s6 = day6.getFullYear() + '-' + (day6.getMonth() + 1) + '-' + day6.getDate()
  236. return s6
  237. }
  238. function getTomorrow() {
  239. var day3 = new Date()
  240. day3.setTime(day3.getTime() + 24 * 60 * 60 * 1000)
  241. var s3 = day3.getFullYear() + '-' + (day3.getMonth() + 1) + '-' + day3.getDate()
  242. return s3
  243. }
  244. module.exports = {
  245. ajax: ajax,
  246. getDataByName: getDataByName,
  247. postDataByName: postDataByName,
  248. execDataByConfig: execDataByConfig,
  249. getYesterday: getYesterday,
  250. getToday: getToday,
  251. getRealToday: getRealToday,
  252. getReal7daysToday: getReal7daysToday,
  253. get7daysAgo: get7daysAgo,
  254. get91daysAgo: get91daysAgo,
  255. get15daysAgo: get15daysAgo,
  256. getTomorrow: getTomorrow,
  257. getDataByNames: getDataByNames,
  258. }