优惠券小程序(猎户)

util.js 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const rootDocment = 'https://yhq-xcx.wenxingshuju.com' // 请求地址 - 生产环境
  2. // const rootDocment = 'http://39.106.149.78:10201' // 请求地址 - 测试环境
  3. const req = (url, method, data, selfHeader) => {
  4. const dataes = {
  5. 'user_id': uni.getStorageSync('user_id'),
  6. 'ttl': uni.getStorageSync('ttl'),
  7. 'sign': uni.getStorageSync('sign'),
  8. }
  9. const header = {
  10. 'sessionKey': uni.getStorageSync('session_key') ? uni.getStorageSync('session_key') : false
  11. }
  12. return new Promise((resolve, reject) => {
  13. uni.request({
  14. url:`${rootDocment}${url}`,
  15. data: data ? Object.assign(dataes, data) : dataes,
  16. method: method,
  17. header: selfHeader ? Object.assign(header, selfHeader) : header,
  18. success: function (res) {
  19. console.log('util::url => ', `${rootDocment}${url}`)
  20. console.log('util::success res => ', res)
  21. resolve(res)
  22. },
  23. fail: function (err) {
  24. console.log('util::url => ', `${rootDocment}${url}`)
  25. console.log('util::fail err => ', err)
  26. reject(err)
  27. }
  28. })
  29. })
  30. }
  31. const get = (url, data, header = {}) => {
  32. return req(url, 'GET', data, {
  33. ...header
  34. })
  35. }
  36. const post = (url, data, header = {}) => {
  37. return req(url, 'POST', data, {
  38. 'Content-Type': 'application/x-www-form-urlencoded',
  39. ...header,
  40. })
  41. }
  42. const postJSON = (url, data, header = {}) => {
  43. return req(url, 'POST', data, {
  44. 'Content-Type': 'application/json',
  45. ...header,
  46. })
  47. }
  48. export {
  49. req,
  50. rootDocment,
  51. get,
  52. post,
  53. postJSON,
  54. }