httpRequest.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. }else if (result.data.code !=0&&result.data.msg!='未进入公司') {
  225. // uni.showToast({
  226. // title: result.data.msg||'查询失败!',
  227. // icon: 'none'
  228. // })
  229. }
  230. succ.call(self, result.data)
  231. },
  232. fail: function(e) {
  233. error.call(self, e)
  234. }
  235. })
  236. })
  237. } else {
  238. return new Promise((succ, error) => {
  239. uni.request({
  240. url: url,
  241. data: data,
  242. method: "GET",
  243. header: {
  244. "content-type": header
  245. },
  246. success: function(result) {
  247. if (result.data.code !=0&&result.data.msg!='未进入公司'){
  248. // uni.showToast({
  249. // title: result.data.msg||'查询失败.',
  250. // icon: 'none'
  251. // })
  252. }
  253. succ.call(self, result.data)
  254. },
  255. fail: function(e) {
  256. error.call(self, e)
  257. }
  258. })
  259. })
  260. }
  261. },
  262. get: function(url, data, header) {
  263. header = header || "application/x-www-form-urlencoded";
  264. url = this.config("APIHOST") + url;
  265. let token = uni.getStorageSync("token");
  266. return new Promise((succ, error) => {
  267. uni.request({
  268. url: url,
  269. data: data,
  270. method: "GET",
  271. header: {
  272. "content-type": header,
  273. "token": token
  274. },
  275. success: function(result) {
  276. if (result.data.code == 401) {
  277. uni.removeStorageSync("token")
  278. uni.removeStorageSync("userId")
  279. uni.removeStorageSync("phone")
  280. uni.removeStorageSync("openid")
  281. uni.removeStorageSync("userName")
  282. uni.removeStorageSync("relation")
  283. uni.removeStorageSync("relation_id")
  284. uni.removeStorageSync("isInvitation")
  285. uni.removeStorageSync("zhiFuBao")
  286. uni.removeStorageSync("zhiFuBaoName")
  287. uni.showToast({
  288. title: '请先登录,以便更好使用!',
  289. icon: 'none'
  290. })
  291. }else if (result.data.code !=0&&result.data.msg!='未进入公司') {
  292. // uni.showToast({
  293. // title: result.data.msg||'查询失败?',
  294. // icon: 'none'
  295. // })
  296. }
  297. succ.call(self, result.data)
  298. },
  299. fail: function(e) {
  300. error.call(self, e)
  301. }
  302. })
  303. })
  304. },
  305. getMsg: function(url, data, header) {
  306. header = header || "application/x-www-form-urlencoded";
  307. url = this.config("APIHOST2") + url;
  308. let token = uni.getStorageSync("token");
  309. return new Promise((succ, error) => {
  310. uni.request({
  311. url: url,
  312. data: data,
  313. method: "GET",
  314. header: {
  315. "content-type": header,
  316. "token": token
  317. },
  318. success: function(result) {
  319. if (result.data.code == 401) {
  320. uni.removeStorageSync("token")
  321. uni.removeStorageSync("userId")
  322. uni.removeStorageSync("phone")
  323. uni.removeStorageSync("openid")
  324. uni.removeStorageSync("userName")
  325. uni.removeStorageSync("relation")
  326. uni.removeStorageSync("relation_id")
  327. uni.removeStorageSync("isInvitation")
  328. uni.removeStorageSync("zhiFuBao")
  329. uni.removeStorageSync("zhiFuBaoName")
  330. uni.showToast({
  331. title: '请先登录,以便更好使用!',
  332. icon: 'none'
  333. })
  334. }
  335. succ.call(self, result.data)
  336. },
  337. fail: function(e) {
  338. error.call(self, e)
  339. }
  340. })
  341. })
  342. }
  343. }