httpRequest.js 14 KB

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