util.js 854 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // 工具函数库
  2. import config from './config'
  3. // http get工具函数 获取数据
  4. export function get (url, data) {
  5. return request(url, 'GET', data)
  6. }
  7. export function post (url, data) {
  8. return request(url, 'POST', data)
  9. }
  10. function request (url, method, data, header = {}) {
  11. return new Promise((resolve, reject) => {
  12. wx.request({
  13. data,
  14. method,
  15. header,
  16. url: config.host + url,
  17. success: function (res) {
  18. if (res.data.code === 0) {
  19. resolve(res.data.data)
  20. } else {
  21. showModal('失败', res.data.data.msg)
  22. reject(res.data)
  23. }
  24. }
  25. })
  26. })
  27. }
  28. export function showModal (title, content) {
  29. wx.showModal({
  30. title,
  31. content,
  32. showCancel: false
  33. })
  34. }
  35. export function showSuccess (text) {
  36. wx.showToast({
  37. title: text,
  38. icon: 'success'
  39. })
  40. }