多租户商城-商户小程序端
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.

98 lines
2.1 KiB

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