猎豆优选小程序

util.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. let BASE_URL = 'https://ld.726p.com' // 请求地址 - 生产环境
  2. // let BASE_URL = 'https://ld-test.wenxingshuju.com' // ceshi
  3. // let BASE_URL = 'http://60.205.169.65:8006' // 请求地址 - 测试环境
  4. // #ifdef H5
  5. BASE_URL = '' //线上模式
  6. // BASE_URL = '/api' // 开发模式
  7. // #endif
  8. const LOGIN_ERR_CODES = [ // 登录状态失效错误码
  9. '2008',
  10. '2005',
  11. '4001',
  12. '4002',
  13. ]
  14. const handleToLogin = () => {
  15. uni.clearStorageSync()
  16. uni.reLaunch({
  17. url: '/pages/subPackages/login/loginPhone'
  18. });
  19. }
  20. const req = (url, method, data, selfHeader, isHideLoading = false) => {
  21. const dataes = {
  22. // 'ttl': uni.getStorageSync('ttl'),
  23. }
  24. const header = {
  25. 'source': uni.getStorageSync('channel_source') || '',
  26. 'version': uni.getStorageSync('channel_version') || '',
  27. // #ifdef H5
  28. 'source': '3002',
  29. 'version': 'v1.0',
  30. // #endif
  31. 'token': uni.getStorageSync('token') || '',
  32. }
  33. return new Promise((resolve, reject) => {
  34. // S 联调日志
  35. try {
  36. console.log('$$req url => ', url)
  37. console.log('$$req method => ', method)
  38. console.log('$$req params => ', data ? Object.assign(dataes, data) : dataes)
  39. } catch (error) {
  40. console.log(error)
  41. }
  42. // E 联调日志
  43. if (!isHideLoading) { // 默认显示loading
  44. uni.showLoading({ title: '加载中...', mask: true })
  45. }
  46. uni.request({
  47. url:`${BASE_URL}${url}`,
  48. data: data ? Object.assign(dataes, data) : dataes,
  49. method: method,
  50. header: selfHeader ? Object.assign(header, selfHeader) : header,
  51. success: function (res) {
  52. uni.hideLoading()
  53. if (LOGIN_ERR_CODES.includes(String(res.data.errno))) {
  54. handleToLogin()
  55. }
  56. resolve(res)
  57. },
  58. fail: function (err) {
  59. uni.hideLoading()
  60. reject(err)
  61. }
  62. })
  63. })
  64. }
  65. const get = (url, data, header = {}, isHideLoading = false) => {
  66. return req(url, 'GET', data, { ...header }, isHideLoading)
  67. }
  68. const post = (url, data, header = {}, isHideLoading = false) => {
  69. return req(url, 'POST', data, {
  70. 'Content-Type': 'application/x-www-form-urlencoded',
  71. ...header,
  72. }, isHideLoading)
  73. }
  74. const postJSON = (url, data, header = {}, isHideLoading = false) => {
  75. return req(url, 'POST', data, {
  76. 'Content-Type': 'application/json',
  77. ...header,
  78. }, isHideLoading)
  79. }
  80. // 获取随机参数 h5使用
  81. const getUuid = () => {
  82. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  83. const r = (Math.random() * 16) | 0,
  84. v = c == 'x' ? r : (r & 0x3) | 0x8
  85. return v.toString(16)
  86. })
  87. }
  88. export {
  89. req,
  90. BASE_URL,
  91. get,
  92. post,
  93. postJSON,
  94. getUuid,
  95. }