httpRequest.js 9.4 KB

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