request.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //设置域名
  2. const dev_baseUrl = "https://test.hz-hanghui.com:18890/yx-fyzd"; //测试
  3. // const dev_baseUrl = "http://192.168.11.19:9100/yx-fyzd"; //本地
  4. // const pro_baseUrl = "http://192.168.77.19:8081/hanghui-server-platform"; //正式
  5. const pro_baseUrl = "https://tx.hz-hanghui.com:8088/hanghui-server-platform"; //正式
  6. //是否是正式环境
  7. const env = true;
  8. const baseUrl = env ? pro_baseUrl : dev_baseUrl;
  9. //https请求
  10. const request = e => {
  11. let token = my.getStorageSync({
  12. key: 'token'
  13. }).data;
  14. return new Promise(function (resolve, reject) {
  15. my.request({
  16. url: e.url.startsWith('http') ? e.url : (env ?
  17. pro_baseUrl + e.url :
  18. dev_baseUrl + e.url),
  19. data: e.data,
  20. method: e.method,
  21. headers: {
  22. "content-type": e.type,
  23. "Authorization": token ? token.token : '',
  24. },
  25. timeout: e.timeout ? e.timeout : 5000,
  26. success: res => {
  27. if (res.data.code == 200) {
  28. resolve(res.data);
  29. } else {
  30. my.hideLoading();
  31. my.showToast({
  32. content: e.errorTip + (res.data.msg ? res.data.msg : ''),
  33. duration: 3000
  34. });
  35. reject(res.data);
  36. }
  37. },
  38. fail: error => {
  39. my.hideLoading();
  40. my.showToast({
  41. content: e.errorTip + (error.errmsg ? error.errmsg : ''),
  42. duration: 3000,
  43. });
  44. reject(error.errorMessage);
  45. }
  46. });
  47. });
  48. };
  49. // 三要素获取接口
  50. const threeElementsRequest = e => {
  51. return new Promise(function (resolve, reject) {
  52. my.request({
  53. url: e.url,
  54. data: e.data,
  55. method: e.method,
  56. headers: {
  57. "content-type": e.type
  58. },
  59. timeout: 25000,
  60. success: res => {
  61. if (res.data.errcode == 0 || res.data.code == 200) {
  62. resolve(res.data);
  63. } else {
  64. my.hideLoading();
  65. my.showToast({
  66. content: e.errorTip + (res.data.msg ? res.data.msg : ''),
  67. duration: 3000
  68. });
  69. reject(res.data);
  70. }
  71. },
  72. fail: error => {
  73. my.hideLoading();
  74. my.showToast({
  75. content: e.errorTip + (error.errmsg ? error.errmsg : ''),
  76. duration: 3000,
  77. });
  78. reject(error.errorMessage);
  79. }
  80. });
  81. });
  82. };
  83. module.exports = {
  84. env: env,
  85. baseUrl,
  86. dev_baseUrl: dev_baseUrl,
  87. pro_baseUrl: pro_baseUrl,
  88. request: request,
  89. threeElementsRequest: threeElementsRequest,
  90. };