httpRequest.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import configdata from './config'
  2. import cache from './cache'
  3. module.exports = {
  4. config: function(name) {
  5. var info = null;
  6. if (name) {
  7. var name2 = name.split("."); //字符分割
  8. if (name2.length > 1) {
  9. info = configdata[name2[0]][name2[1]] || null;
  10. } else {
  11. info = configdata[name] || null;
  12. }
  13. if (info == null) {
  14. let web_config = cache.get("web_config");
  15. if (web_config) {
  16. if (name2.length > 1) {
  17. info = web_config[name2[0]][name2[1]] || null;
  18. } else {
  19. info = web_config[name] || null;
  20. }
  21. }
  22. }
  23. }
  24. return info;
  25. },
  26. post: function(url, data, header) {
  27. header = header || "application/x-www-form-urlencoded";
  28. url = this.config("APIHOST") + url;
  29. let token = uni.getStorageSync("token");
  30. return new Promise((succ, error) => {
  31. uni.request({
  32. url: url,
  33. data: data,
  34. method: "POST",
  35. header: {
  36. "content-type": header,
  37. "token": token
  38. },
  39. success: function(result) {
  40. if (result.data.code == 401) {
  41. // uni.clearStorage();
  42. uni.removeStorageSync("token")
  43. uni.removeStorageSync("userId")
  44. uni.removeStorageSync("phone")
  45. uni.removeStorageSync("openid")
  46. uni.removeStorageSync("userName")
  47. uni.removeStorageSync("relation")
  48. uni.removeStorageSync("relation_id")
  49. uni.removeStorageSync("isInvitation")
  50. uni.removeStorageSync("zhiFuBao")
  51. uni.removeStorageSync("zhiFuBaoName")
  52. uni.showToast({
  53. title: '用户信息失效,请重新登录!',
  54. icon: 'none'
  55. })
  56. setTimeout(() => {
  57. // 选择合适的路由方法(根据需求切换)
  58. // 1. 跳转到登录页,关闭当前页面(推荐,避免回退到失效页面)
  59. uni.redirectTo({
  60. url: '/pages/public/login' // 替换为你的登录页路径
  61. });
  62. // 2. 如果需要关闭所有页面,强制跳转到登录页(比如退出登录场景)
  63. // uni.reLaunch({
  64. // url: '/pages/public/login'
  65. // });
  66. // 3. 普通跳转(保留当前页面栈,不推荐此场景)
  67. // uni.navigateTo({
  68. // url: '/pages/public/login'
  69. // });
  70. }, 500); // 延迟时间必须 ≥
  71. }
  72. succ.call(self, result.data)
  73. },
  74. fail: function(e) {
  75. error.call(self, e)
  76. }
  77. })
  78. })
  79. },
  80. postT: function(url, data, header) {
  81. header = header || "application/x-www-form-urlencoded";
  82. url = this.config("APIHOST1") + url;
  83. let token = uni.getStorageSync("token");
  84. if (token) {
  85. return new Promise((succ, error) => {
  86. uni.request({
  87. url: url,
  88. data: data,
  89. method: "POST",
  90. header: {
  91. "content-type": header,
  92. "token": token
  93. },
  94. success: function(result) {
  95. if (result.data.code == 401) {
  96. uni.removeStorageSync("token")
  97. uni.removeStorageSync("userId")
  98. uni.removeStorageSync("phone")
  99. uni.removeStorageSync("openid")
  100. uni.removeStorageSync("userName")
  101. uni.removeStorageSync("relation")
  102. uni.removeStorageSync("relation_id")
  103. uni.removeStorageSync("isInvitation")
  104. uni.removeStorageSync("zhiFuBao")
  105. uni.removeStorageSync("zhiFuBaoName")
  106. uni.showToast({
  107. title: '用户信息失效,请重新登录!',
  108. icon: 'none'
  109. })
  110. }
  111. succ.call(self, result.data)
  112. },
  113. fail: function(e) {
  114. error.call(self, e)
  115. }
  116. })
  117. })
  118. } else {
  119. return new Promise((succ, error) => {
  120. uni.request({
  121. url: url,
  122. data: data,
  123. method: "POST",
  124. header: {
  125. "content-type": header,
  126. },
  127. success: function(result) {
  128. succ.call(self, result.data)
  129. },
  130. fail: function(e) {
  131. error.call(self, e)
  132. }
  133. })
  134. })
  135. }
  136. },
  137. postJson: function(url, data, header) {
  138. header = header || "application/json";
  139. url = this.config("APIHOST1") + url;
  140. let token = uni.getStorageSync("token");
  141. if (token) {
  142. return new Promise((succ, error) => {
  143. uni.request({
  144. url: url,
  145. data: data,
  146. method: "POST",
  147. header: {
  148. "content-type": header,
  149. "token": token
  150. },
  151. success: function(result) {
  152. if (result.data.code == 401) {
  153. uni.removeStorageSync("token")
  154. uni.removeStorageSync("userId")
  155. uni.removeStorageSync("phone")
  156. uni.removeStorageSync("openid")
  157. uni.removeStorageSync("userName")
  158. uni.removeStorageSync("relation")
  159. uni.removeStorageSync("relation_id")
  160. uni.removeStorageSync("isInvitation")
  161. uni.removeStorageSync("zhiFuBao")
  162. uni.removeStorageSync("zhiFuBaoName")
  163. uni.showToast({
  164. title: '用户信息失效,请重新登录!',
  165. icon: 'none'
  166. })
  167. }
  168. succ.call(self, result.data)
  169. },
  170. fail: function(e) {
  171. error.call(self, e)
  172. }
  173. })
  174. })
  175. } else {
  176. return new Promise((succ, error) => {
  177. uni.request({
  178. url: url,
  179. data: data,
  180. method: "POST",
  181. header: {
  182. "content-type": header,
  183. },
  184. success: function(result) {
  185. succ.call(self, result.data)
  186. },
  187. fail: function(e) {
  188. error.call(self, e)
  189. }
  190. })
  191. })
  192. }
  193. },
  194. getT: function(url, data, header) {
  195. header = header || "application/x-www-form-urlencoded";
  196. url = this.config("APIHOST1") + url;
  197. let token = uni.getStorageSync("token");
  198. if (token) {
  199. return new Promise((succ, error) => {
  200. uni.request({
  201. url: url,
  202. data: data,
  203. method: "GET",
  204. header: {
  205. "content-type": header,
  206. "token": token
  207. },
  208. success: function(result) {
  209. if (result.data.code == 401) {
  210. uni.removeStorageSync("token")
  211. uni.removeStorageSync("userId")
  212. uni.removeStorageSync("phone")
  213. uni.removeStorageSync("openid")
  214. uni.removeStorageSync("userName")
  215. uni.removeStorageSync("relation")
  216. uni.removeStorageSync("relation_id")
  217. uni.removeStorageSync("isInvitation")
  218. uni.removeStorageSync("zhiFuBao")
  219. uni.removeStorageSync("zhiFuBaoName")
  220. uni.showToast({
  221. title: '用户信息失效,请重新登录!',
  222. icon: 'none'
  223. })
  224. }
  225. succ.call(self, result.data)
  226. },
  227. fail: function(e) {
  228. error.call(self, e)
  229. }
  230. })
  231. })
  232. } else {
  233. return new Promise((succ, error) => {
  234. uni.request({
  235. url: url,
  236. data: data,
  237. method: "GET",
  238. header: {
  239. "content-type": header
  240. },
  241. success: function(result) {
  242. succ.call(self, result.data)
  243. },
  244. fail: function(e) {
  245. error.call(self, e)
  246. }
  247. })
  248. })
  249. }
  250. },
  251. get: function(url, data, header) {
  252. header = header || "application/x-www-form-urlencoded";
  253. url = this.config("APIHOST") + url;
  254. let token = uni.getStorageSync("token");
  255. return new Promise((succ, error) => {
  256. uni.request({
  257. url: url,
  258. data: data,
  259. method: "GET",
  260. header: {
  261. "content-type": header,
  262. "token": token
  263. },
  264. success: function(result) {
  265. if (result.data.code == 401) {
  266. uni.removeStorageSync("token")
  267. uni.removeStorageSync("userId")
  268. uni.removeStorageSync("phone")
  269. uni.removeStorageSync("openid")
  270. uni.removeStorageSync("userName")
  271. uni.removeStorageSync("relation")
  272. uni.removeStorageSync("relation_id")
  273. uni.removeStorageSync("isInvitation")
  274. uni.removeStorageSync("zhiFuBao")
  275. uni.removeStorageSync("zhiFuBaoName")
  276. uni.showToast({
  277. title: '用户信息失效,请重新登录!',
  278. icon: 'none'
  279. })
  280. }
  281. succ.call(self, result.data)
  282. },
  283. fail: function(e) {
  284. error.call(self, e)
  285. }
  286. })
  287. })
  288. },
  289. getMsg: function(url, data, header) {
  290. header = header || "application/x-www-form-urlencoded";
  291. url = this.config("APIHOST2") + url;
  292. let token = uni.getStorageSync("token");
  293. return new Promise((succ, error) => {
  294. uni.request({
  295. url: url,
  296. data: data,
  297. method: "GET",
  298. header: {
  299. "content-type": header,
  300. "token": token
  301. },
  302. success: function(result) {
  303. if (result.data.code == 401) {
  304. uni.removeStorageSync("token")
  305. uni.removeStorageSync("userId")
  306. uni.removeStorageSync("phone")
  307. uni.removeStorageSync("openid")
  308. uni.removeStorageSync("userName")
  309. uni.removeStorageSync("relation")
  310. uni.removeStorageSync("relation_id")
  311. uni.removeStorageSync("isInvitation")
  312. uni.removeStorageSync("zhiFuBao")
  313. uni.removeStorageSync("zhiFuBaoName")
  314. uni.showToast({
  315. title: '用户信息失效,请重新登录!',
  316. icon: 'none'
  317. })
  318. }
  319. succ.call(self, result.data)
  320. },
  321. fail: function(e) {
  322. error.call(self, e)
  323. }
  324. })
  325. })
  326. }
  327. }