httpRequest.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. return new Promise((succ, error) => {
  30. uni.request({
  31. url: url,
  32. data: data,
  33. method: "POST",
  34. header: {
  35. "content-type": header
  36. },
  37. success: function (result) {
  38. if(result.data.code == 401) {
  39. uni.removeStorageSync("token")
  40. uni.removeStorageSync("userId")
  41. uni.removeStorageSync("phone")
  42. uni.removeStorageSync("openid")
  43. uni.removeStorageSync("userName")
  44. uni.removeStorageSync("relation")
  45. uni.removeStorageSync("relation_id")
  46. uni.removeStorageSync("isInvitation")
  47. uni.removeStorageSync("zhiFuBao")
  48. uni.removeStorageSync("zhiFuBaoName")
  49. // uni.showToast({
  50. // title: '用户信息失效,请重新登录!',
  51. // icon: 'none'
  52. // })
  53. }
  54. succ.call(self, result.data)
  55. },
  56. fail: function (e) {
  57. error.call(self, e)
  58. }
  59. })
  60. })
  61. },
  62. postT: function (url, data, header) {
  63. header = header || "application/x-www-form-urlencoded";
  64. url = this.config("APIHOST1") + url;
  65. // let token = uni.getStorageSync("token");
  66. let token =uni.getStorageSync("token");
  67. if (token) {
  68. return new Promise((succ, error) => {
  69. uni.request({
  70. url: url,
  71. data: data,
  72. method: "POST",
  73. header: {
  74. "content-type": header,
  75. "token": token
  76. },
  77. success: function (result) {
  78. console.error(result);
  79. if(result.data.code == 401 && token) {
  80. uni.removeStorageSync("token")
  81. uni.removeStorageSync("userId")
  82. uni.removeStorageSync("phone")
  83. uni.removeStorageSync("openid")
  84. uni.removeStorageSync("userName")
  85. uni.removeStorageSync("relation")
  86. uni.removeStorageSync("relation_id")
  87. uni.removeStorageSync("isInvitation")
  88. uni.removeStorageSync("zhiFuBao")
  89. uni.removeStorageSync("zhiFuBaoName")
  90. uni.showToast({
  91. title: '用户信息失效,请重新登录!',
  92. icon: 'none'
  93. })
  94. }
  95. succ.call(self, result.data)
  96. },
  97. fail: function (e) {
  98. error.call(self, e)
  99. }
  100. })
  101. })
  102. } else {
  103. return new Promise((succ, error) => {
  104. uni.request({
  105. url: url,
  106. data: data,
  107. method: "POST",
  108. header: {
  109. "content-type": header,
  110. },
  111. success: function (result) {
  112. if(result.data.code == 401) {
  113. uni.removeStorageSync("token")
  114. uni.removeStorageSync("userId")
  115. uni.removeStorageSync("phone")
  116. uni.removeStorageSync("openid")
  117. uni.removeStorageSync("userName")
  118. uni.removeStorageSync("relation")
  119. uni.removeStorageSync("relation_id")
  120. uni.removeStorageSync("isInvitation")
  121. uni.removeStorageSync("zhiFuBao")
  122. uni.removeStorageSync("zhiFuBaoName")
  123. // uni.showToast({
  124. // title: '用户信息失效,请重新登录!',
  125. // icon: 'none'
  126. // })
  127. }
  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. let token = uni.getStorageSync("token");
  142. if (token) {
  143. return new Promise((succ, error) => {
  144. uni.request({
  145. url: url,
  146. data: data,
  147. method: "POST",
  148. header: {
  149. "content-type": header,
  150. "token": token
  151. },
  152. success: function (result) {
  153. // console.error(result);
  154. if(result.data.code == 401 && token) {
  155. uni.removeStorageSync("token")
  156. uni.removeStorageSync("userId")
  157. uni.removeStorageSync("phone")
  158. uni.removeStorageSync("openid")
  159. uni.removeStorageSync("userName")
  160. uni.removeStorageSync("relation")
  161. uni.removeStorageSync("relation_id")
  162. uni.removeStorageSync("isInvitation")
  163. uni.removeStorageSync("zhiFuBao")
  164. uni.removeStorageSync("zhiFuBaoName")
  165. uni.showToast({
  166. title: '用户信息失效,请重新登录!',
  167. icon: 'none'
  168. })
  169. }
  170. succ.call(self, result.data)
  171. },
  172. fail: function (e) {
  173. error.call(self, e)
  174. }
  175. })
  176. })
  177. } else {
  178. return new Promise((succ, error) => {
  179. uni.request({
  180. url: url,
  181. data: data,
  182. method: "POST",
  183. header: {
  184. "content-type": header,
  185. },
  186. success: function (result) {
  187. succ.call(self, result.data)
  188. },
  189. fail: function (e) {
  190. error.call(self, e)
  191. }
  192. })
  193. })
  194. }
  195. },
  196. getT: function (url, data, header) {
  197. header = header || "application/x-www-form-urlencoded";
  198. url = this.config("APIHOST1") + url;
  199. let token = uni.getStorageSync("token");
  200. if (token) {
  201. return new Promise((succ, error) => {
  202. uni.request({
  203. url: url,
  204. data: data,
  205. method: "GET",
  206. header: {
  207. "content-type": header,
  208. "token": token
  209. },
  210. success: function (result) {
  211. if(result.data.code == 401 && token) {
  212. uni.removeStorageSync("token")
  213. uni.removeStorageSync("userId")
  214. uni.removeStorageSync("phone")
  215. uni.removeStorageSync("openid")
  216. uni.removeStorageSync("userName")
  217. uni.removeStorageSync("relation")
  218. uni.removeStorageSync("relation_id")
  219. uni.removeStorageSync("isInvitation")
  220. uni.removeStorageSync("zhiFuBao")
  221. uni.removeStorageSync("zhiFuBaoName")
  222. uni.showToast({
  223. title: '用户信息失效,请重新登录!',
  224. icon: 'none'
  225. })
  226. }
  227. succ.call(self, result.data)
  228. },
  229. fail: function (e) {
  230. error.call(self, e)
  231. }
  232. })
  233. })
  234. } else {
  235. return new Promise((succ, error) => {
  236. uni.request({
  237. url: url,
  238. data: data,
  239. method: "GET",
  240. header: {
  241. "content-type": header
  242. },
  243. success: function (result) {
  244. if(result.data.code == 401) {
  245. uni.removeStorageSync("token")
  246. uni.removeStorageSync("userId")
  247. uni.removeStorageSync("phone")
  248. uni.removeStorageSync("openid")
  249. uni.removeStorageSync("userName")
  250. uni.removeStorageSync("relation")
  251. uni.removeStorageSync("relation_id")
  252. uni.removeStorageSync("isInvitation")
  253. uni.removeStorageSync("zhiFuBao")
  254. uni.removeStorageSync("zhiFuBaoName")
  255. // uni.showToast({
  256. // title: '用户信息失效,请重新登录!',
  257. // icon: 'none'
  258. // })
  259. }
  260. succ.call(self, result.data)
  261. },
  262. fail: function (e) {
  263. error.call(self, e)
  264. }
  265. })
  266. })
  267. }
  268. },
  269. posts: function (url, data, header) {
  270. header = header || "application/x-www-form-urlencoded";
  271. return new Promise((succ, error) => {
  272. uni.request({
  273. url: url,
  274. data: data,
  275. method: "POST",
  276. header: {
  277. "content-type": header
  278. },
  279. success: function (result) {
  280. if(result.data.code == 401) {
  281. uni.removeStorageSync("token")
  282. uni.removeStorageSync("userId")
  283. uni.removeStorageSync("phone")
  284. uni.removeStorageSync("openid")
  285. uni.removeStorageSync("userName")
  286. uni.removeStorageSync("relation")
  287. uni.removeStorageSync("relation_id")
  288. uni.removeStorageSync("isInvitation")
  289. uni.removeStorageSync("zhiFuBao")
  290. uni.removeStorageSync("zhiFuBaoName")
  291. // uni.showToast({
  292. // title: '用户信息失效,请重新登录!',
  293. // icon: 'none'
  294. // })
  295. }
  296. succ.call(self, result.data)
  297. },
  298. fail: function (e) {
  299. error.call(self, e)
  300. }
  301. })
  302. })
  303. },
  304. postF: function (url, data, header) {
  305. header = header || "application/json";
  306. url = this.config("APIHOST") + url;
  307. return new Promise((succ, error) => {
  308. uni.request({
  309. url: url,
  310. data: data,
  311. method: "POST",
  312. header: {
  313. "content-type": header
  314. },
  315. success: function (result) {
  316. if(result.data.code == 401) {
  317. uni.removeStorageSync("token")
  318. uni.removeStorageSync("userId")
  319. uni.removeStorageSync("phone")
  320. uni.removeStorageSync("openid")
  321. uni.removeStorageSync("userName")
  322. uni.removeStorageSync("relation")
  323. uni.removeStorageSync("relation_id")
  324. uni.removeStorageSync("isInvitation")
  325. uni.removeStorageSync("zhiFuBao")
  326. uni.removeStorageSync("zhiFuBaoName")
  327. // uni.showToast({
  328. // title: '用户信息失效,请重新登录!',
  329. // icon: 'none'
  330. // })
  331. }
  332. succ.call(self, result.data)
  333. },
  334. fail: function (e) {
  335. error.call(self, e)
  336. }
  337. })
  338. })
  339. },
  340. postFs: function (url, data, header) {
  341. header = header || "application/json";
  342. return new Promise((succ, error) => {
  343. uni.request({
  344. url: url,
  345. data: data,
  346. method: "POST",
  347. header: {
  348. "content-type": header
  349. },
  350. success: function (result) {
  351. if(result.data.code == 401) {
  352. uni.removeStorageSync("token")
  353. uni.removeStorageSync("userId")
  354. uni.removeStorageSync("phone")
  355. uni.removeStorageSync("openid")
  356. uni.removeStorageSync("userName")
  357. uni.removeStorageSync("relation")
  358. uni.removeStorageSync("relation_id")
  359. uni.removeStorageSync("isInvitation")
  360. uni.removeStorageSync("zhiFuBao")
  361. uni.removeStorageSync("zhiFuBaoName")
  362. // uni.showToast({
  363. // title: '用户信息失效,请重新登录!',
  364. // icon: 'none'
  365. // })
  366. }
  367. succ.call(self, result.data)
  368. },
  369. fail: function (e) {
  370. error.call(self, e)
  371. }
  372. })
  373. })
  374. },
  375. get: function (url, data, header) {
  376. header = header || "application/x-www-form-urlencoded";
  377. url = this.config("APIHOST") + url;
  378. return new Promise((succ, error) => {
  379. uni.request({
  380. url: url,
  381. data: data,
  382. method: "GET",
  383. header: {
  384. "content-type": header
  385. },
  386. success: function (result) {
  387. if(result.data.code == 401) {
  388. uni.removeStorageSync("token")
  389. uni.removeStorageSync("userId")
  390. uni.removeStorageSync("phone")
  391. uni.removeStorageSync("openid")
  392. uni.removeStorageSync("userName")
  393. uni.removeStorageSync("relation")
  394. uni.removeStorageSync("relation_id")
  395. uni.removeStorageSync("isInvitation")
  396. uni.removeStorageSync("zhiFuBao")
  397. uni.removeStorageSync("zhiFuBaoName")
  398. // uni.showToast({
  399. // title: '用户信息失效,请重新登录!',
  400. // icon: 'none'
  401. // })
  402. }
  403. succ.call(self, result.data)
  404. },
  405. fail: function (e) {
  406. error.call(self, e)
  407. }
  408. })
  409. })
  410. },
  411. getJd: function (url, data, header) {
  412. header = header || "application/x-www-form-urlencoded";
  413. return new Promise((succ, error) => {
  414. uni.request({
  415. url: url,
  416. data: data,
  417. method: "GET",
  418. header: {
  419. "content-type": header
  420. },
  421. success: function (result) {
  422. if(result.data.code == 401) {
  423. uni.removeStorageSync("token")
  424. uni.removeStorageSync("userId")
  425. uni.removeStorageSync("phone")
  426. uni.removeStorageSync("openid")
  427. uni.removeStorageSync("userName")
  428. uni.removeStorageSync("relation")
  429. uni.removeStorageSync("relation_id")
  430. uni.removeStorageSync("isInvitation")
  431. uni.removeStorageSync("zhiFuBao")
  432. uni.removeStorageSync("zhiFuBaoName")
  433. // uni.showToast({
  434. // title: '用户信息失效,请重新登录!',
  435. // icon: 'none'
  436. // })
  437. }
  438. succ.call(self, result.data)
  439. },
  440. fail: function (e) {
  441. error.call(self, e)
  442. }
  443. })
  444. })
  445. },
  446. get1: function (url, data, header) {
  447. header = header || "application/json";
  448. url = this.config("APIHOST") + url;
  449. return new Promise((succ, error) => {
  450. uni.request({
  451. url: url,
  452. data: data,
  453. method: "GET",
  454. success: function (result) {
  455. if(result.data.code == 401) {
  456. uni.removeStorageSync("token")
  457. uni.removeStorageSync("userId")
  458. uni.removeStorageSync("phone")
  459. uni.removeStorageSync("openid")
  460. uni.removeStorageSync("userName")
  461. uni.removeStorageSync("relation")
  462. uni.removeStorageSync("relation_id")
  463. uni.removeStorageSync("isInvitation")
  464. uni.removeStorageSync("zhiFuBao")
  465. uni.removeStorageSync("zhiFuBaoName")
  466. // uni.showToast({
  467. // title: '用户信息失效,请重新登录!',
  468. // icon: 'none'
  469. // })
  470. }
  471. succ.call(self, result.data)
  472. },
  473. fail: function (e) {
  474. error.call(self, e)
  475. }
  476. })
  477. })
  478. }
  479. }