httpRequest.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. }
  57. succ.call(self, result.data)
  58. },
  59. fail: function(e) {
  60. error.call(self, e)
  61. }
  62. })
  63. })
  64. },
  65. postT: function(url, data, header) {
  66. header = header || "application/x-www-form-urlencoded";
  67. url = this.config("APIHOST1") + url;
  68. let token = uni.getStorageSync("token");
  69. if (token) {
  70. return new Promise((succ, error) => {
  71. uni.request({
  72. url: url,
  73. data: data,
  74. method: "POST",
  75. header: {
  76. "content-type": header,
  77. "token": token
  78. },
  79. success: function(result) {
  80. if (result.data.code == 401) {
  81. uni.removeStorageSync("token")
  82. uni.removeStorageSync("userId")
  83. uni.removeStorageSync("phone")
  84. uni.removeStorageSync("openid")
  85. uni.removeStorageSync("userName")
  86. uni.removeStorageSync("relation")
  87. uni.removeStorageSync("relation_id")
  88. uni.removeStorageSync("isInvitation")
  89. uni.removeStorageSync("zhiFuBao")
  90. uni.removeStorageSync("zhiFuBaoName")
  91. uni.showToast({
  92. title: '用户信息失效,请重新登录!',
  93. icon: 'none'
  94. })
  95. }
  96. succ.call(self, result.data)
  97. },
  98. fail: function(e) {
  99. error.call(self, e)
  100. }
  101. })
  102. })
  103. } else {
  104. return new Promise((succ, error) => {
  105. uni.request({
  106. url: url,
  107. data: data,
  108. method: "POST",
  109. header: {
  110. "content-type": header,
  111. },
  112. success: function(result) {
  113. succ.call(self, result.data)
  114. },
  115. fail: function(e) {
  116. error.call(self, e)
  117. }
  118. })
  119. })
  120. }
  121. },
  122. postJson: function(url, data, header) {
  123. header = header || "application/json";
  124. url = this.config("APIHOST1") + url;
  125. let token = uni.getStorageSync("token");
  126. if (token) {
  127. return new Promise((succ, error) => {
  128. uni.request({
  129. url: url,
  130. data: data,
  131. method: "POST",
  132. header: {
  133. "content-type": header,
  134. "token": token
  135. },
  136. success: function(result) {
  137. if (result.data.code == 401) {
  138. uni.removeStorageSync("token")
  139. uni.removeStorageSync("userId")
  140. uni.removeStorageSync("phone")
  141. uni.removeStorageSync("openid")
  142. uni.removeStorageSync("userName")
  143. uni.removeStorageSync("relation")
  144. uni.removeStorageSync("relation_id")
  145. uni.removeStorageSync("isInvitation")
  146. uni.removeStorageSync("zhiFuBao")
  147. uni.removeStorageSync("zhiFuBaoName")
  148. uni.showToast({
  149. title: '用户信息失效,请重新登录!',
  150. icon: 'none'
  151. })
  152. }
  153. succ.call(self, result.data)
  154. },
  155. fail: function(e) {
  156. error.call(self, e)
  157. }
  158. })
  159. })
  160. } else {
  161. return new Promise((succ, error) => {
  162. uni.request({
  163. url: url,
  164. data: data,
  165. method: "POST",
  166. header: {
  167. "content-type": header,
  168. },
  169. success: function(result) {
  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. if (token) {
  184. return new Promise((succ, error) => {
  185. uni.request({
  186. url: url,
  187. data: data,
  188. method: "GET",
  189. header: {
  190. "content-type": header,
  191. "token": token
  192. },
  193. success: function(result) {
  194. if (result.data.code == 401) {
  195. uni.removeStorageSync("token")
  196. uni.removeStorageSync("userId")
  197. uni.removeStorageSync("phone")
  198. uni.removeStorageSync("openid")
  199. uni.removeStorageSync("userName")
  200. uni.removeStorageSync("relation")
  201. uni.removeStorageSync("relation_id")
  202. uni.removeStorageSync("isInvitation")
  203. uni.removeStorageSync("zhiFuBao")
  204. uni.removeStorageSync("zhiFuBaoName")
  205. uni.showToast({
  206. title: '用户信息失效,请重新登录!',
  207. icon: 'none'
  208. })
  209. }
  210. succ.call(self, result.data)
  211. },
  212. fail: function(e) {
  213. error.call(self, e)
  214. }
  215. })
  216. })
  217. } else {
  218. return new Promise((succ, error) => {
  219. uni.request({
  220. url: url,
  221. data: data,
  222. method: "GET",
  223. header: {
  224. "content-type": header
  225. },
  226. success: function(result) {
  227. succ.call(self, result.data)
  228. },
  229. fail: function(e) {
  230. error.call(self, e)
  231. }
  232. })
  233. })
  234. }
  235. },
  236. get: function(url, data, header) {
  237. header = header || "application/x-www-form-urlencoded";
  238. url = this.config("APIHOST") + url;
  239. let token = uni.getStorageSync("token");
  240. return new Promise((succ, error) => {
  241. uni.request({
  242. url: url,
  243. data: data,
  244. method: "GET",
  245. header: {
  246. "content-type": header,
  247. "token": token
  248. },
  249. success: function(result) {
  250. if (result.data.code == 401) {
  251. uni.removeStorageSync("token")
  252. uni.removeStorageSync("userId")
  253. uni.removeStorageSync("phone")
  254. uni.removeStorageSync("openid")
  255. uni.removeStorageSync("userName")
  256. uni.removeStorageSync("relation")
  257. uni.removeStorageSync("relation_id")
  258. uni.removeStorageSync("isInvitation")
  259. uni.removeStorageSync("zhiFuBao")
  260. uni.removeStorageSync("zhiFuBaoName")
  261. uni.showToast({
  262. title: '用户信息失效,请重新登录!',
  263. icon: 'none'
  264. })
  265. }
  266. succ.call(self, result.data)
  267. },
  268. fail: function(e) {
  269. error.call(self, e)
  270. }
  271. })
  272. })
  273. },
  274. getMsg: function(url, data, header) {
  275. header = header || "application/x-www-form-urlencoded";
  276. url = this.config("APIHOST2") + url;
  277. let token = uni.getStorageSync("token");
  278. return new Promise((succ, error) => {
  279. uni.request({
  280. url: url,
  281. data: data,
  282. method: "GET",
  283. header: {
  284. "content-type": header,
  285. "token": token
  286. },
  287. success: function(result) {
  288. if (result.data.code == 401) {
  289. uni.removeStorageSync("token")
  290. uni.removeStorageSync("userId")
  291. uni.removeStorageSync("phone")
  292. uni.removeStorageSync("openid")
  293. uni.removeStorageSync("userName")
  294. uni.removeStorageSync("relation")
  295. uni.removeStorageSync("relation_id")
  296. uni.removeStorageSync("isInvitation")
  297. uni.removeStorageSync("zhiFuBao")
  298. uni.removeStorageSync("zhiFuBaoName")
  299. uni.showToast({
  300. title: '用户信息失效,请重新登录!',
  301. icon: 'none'
  302. })
  303. }
  304. succ.call(self, result.data)
  305. },
  306. fail: function(e) {
  307. error.call(self, e)
  308. }
  309. })
  310. })
  311. }
  312. }