多租户商城-商户小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.2 KiB

2 years ago
2 years ago
2 years ago
  1. import {showLoading,hideLoading} from "./plugIn/globalLoading";
  2. const request = (url, data, method = 'GET') => {
  3. return new Promise((resolve, reject) => {
  4. let header = {
  5. "Content-Type": "application/json"
  6. }
  7. const res = uni.getStorageSync('storage_key');
  8. const token = res.token
  9. if (token) {
  10. header['Authorization'] = token
  11. }
  12. var project = "";
  13. // #ifdef H5
  14. var fullUrl = window.location.search;
  15. if (fullUrl.indexOf("?") != -1) {
  16. fullUrl = fullUrl.substring(1);
  17. var paramStrs = fullUrl.split("&");
  18. for(let i=0;i<paramStrs.length;i++){
  19. const newStrs = paramStrs[i].split("=");
  20. if(newStrs[0] === 'project'){
  21. project = newStrs[1]
  22. break;
  23. }
  24. }
  25. }
  26. // #endif
  27. if(!project){
  28. project = uni.getStorageSync('project');
  29. }
  30. header['project'] = project
  31. var locale = uni.getLocale() || 'zh'
  32. if(locale == 'zh-Hans'){
  33. locale = 'zh'
  34. }
  35. header['language'] = locale
  36. showLoading()
  37. uni.request({
  38. url: url,
  39. data: data,
  40. method: method,
  41. header: header,
  42. success: res => {
  43. hideLoading()
  44. if (res.statusCode == 200) {
  45. if (res.data.code === "200" || res.data.code === "") {
  46. resolve(res.data)
  47. } else if (res.data.code === "20004" || res.data.code === "20005") {
  48. uni.removeStorageSync("storage_key")
  49. uni.navigateTo({
  50. url: '/pages_category_page2/userModule/login'
  51. })
  52. } else {
  53. uni.showToast({
  54. title: res.data.message,
  55. icon: "none"
  56. })
  57. }
  58. } else {
  59. reject(res)
  60. }
  61. },
  62. fail: res => {
  63. hideLoading()
  64. reject(res)
  65. },
  66. })
  67. });
  68. }
  69. //不带token接口请求,首页
  70. const request1 = (url, data, method = 'GET') => {
  71. return new Promise((resolve, reject) => {
  72. let header = {
  73. 'Content-Type': 'application/json',
  74. 'tenant': 'MDAwMA==',
  75. }
  76. showLoading()
  77. uni.request({
  78. url: url,
  79. data: data,
  80. method: method,
  81. header: header,
  82. success: res => {
  83. hideLoading()
  84. if (res.data.code === "200" || data.code === "") {
  85. resolve(res.data)
  86. } else {
  87. reject(res)
  88. }
  89. },
  90. fail: res => {
  91. hideLoading()
  92. reject(res)
  93. }
  94. })
  95. });
  96. }
  97. module.exports = {
  98. request: request,
  99. request1: request1
  100. }