httpRequest.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. }else if (result.data.code !=0&&result.data.msg!='未进入公司') {
  168. uni.showToast({
  169. title: result.data.msg||'查询失败',
  170. icon: 'none'
  171. })
  172. }
  173. succ.call(self, result.data)
  174. },
  175. fail: function(e) {
  176. error.call(self, e)
  177. }
  178. })
  179. })
  180. } else {
  181. return new Promise((succ, error) => {
  182. uni.request({
  183. url: url,
  184. data: data,
  185. method: "POST",
  186. header: {
  187. "content-type": header,
  188. },
  189. success: function(result) {
  190. succ.call(self, result.data)
  191. },
  192. fail: function(e) {
  193. error.call(self, e)
  194. }
  195. })
  196. })
  197. }
  198. },
  199. getT: function(url, data, header) {
  200. header = header || "application/x-www-form-urlencoded";
  201. url = this.config("APIHOST1") + url;
  202. let token = uni.getStorageSync("token");
  203. if (token) {
  204. return new Promise((succ, error) => {
  205. uni.request({
  206. url: url,
  207. data: data,
  208. method: "GET",
  209. header: {
  210. "content-type": header,
  211. "token": token
  212. },
  213. success: function(result) {
  214. if (result.data.code == 401) {
  215. uni.removeStorageSync("token")
  216. uni.removeStorageSync("userId")
  217. uni.removeStorageSync("phone")
  218. uni.removeStorageSync("openid")
  219. uni.removeStorageSync("userName")
  220. uni.removeStorageSync("relation")
  221. uni.removeStorageSync("relation_id")
  222. uni.removeStorageSync("isInvitation")
  223. uni.removeStorageSync("zhiFuBao")
  224. uni.removeStorageSync("zhiFuBaoName")
  225. uni.showToast({
  226. title: '请先登录,以便更好使用!',
  227. icon: 'none'
  228. })
  229. }else if (result.data.code !=0&&result.data.msg!='未进入公司') {
  230. uni.showToast({
  231. title: result.data.msg||'查询失败',
  232. icon: 'none'
  233. })
  234. }
  235. succ.call(self, result.data)
  236. },
  237. fail: function(e) {
  238. error.call(self, e)
  239. }
  240. })
  241. })
  242. } else {
  243. return new Promise((succ, error) => {
  244. uni.request({
  245. url: url,
  246. data: data,
  247. method: "GET",
  248. header: {
  249. "content-type": header
  250. },
  251. success: function(result) {
  252. if (result.data.code !=0&&result.data.msg!='未进入公司'){
  253. uni.showToast({
  254. title: result.data.msg||'查询失败',
  255. icon: 'none'
  256. })
  257. }
  258. succ.call(self, result.data)
  259. },
  260. fail: function(e) {
  261. error.call(self, e)
  262. }
  263. })
  264. })
  265. }
  266. },
  267. get: function(url, data, header) {
  268. header = header || "application/x-www-form-urlencoded";
  269. url = this.config("APIHOST") + url;
  270. let token = uni.getStorageSync("token");
  271. return new Promise((succ, error) => {
  272. uni.request({
  273. url: url,
  274. data: data,
  275. method: "GET",
  276. header: {
  277. "content-type": header,
  278. "token": token
  279. },
  280. success: function(result) {
  281. if (result.data.code == 401) {
  282. uni.removeStorageSync("token")
  283. uni.removeStorageSync("userId")
  284. uni.removeStorageSync("phone")
  285. uni.removeStorageSync("openid")
  286. uni.removeStorageSync("userName")
  287. uni.removeStorageSync("relation")
  288. uni.removeStorageSync("relation_id")
  289. uni.removeStorageSync("isInvitation")
  290. uni.removeStorageSync("zhiFuBao")
  291. uni.removeStorageSync("zhiFuBaoName")
  292. uni.showToast({
  293. title: '请先登录,以便更好使用!',
  294. icon: 'none'
  295. })
  296. }else if (result.data.code !=0&&result.data.msg!='未进入公司') {
  297. uni.showToast({
  298. title: result.data.msg||'查询失败',
  299. icon: 'none'
  300. })
  301. }
  302. succ.call(self, result.data)
  303. },
  304. fail: function(e) {
  305. error.call(self, e)
  306. }
  307. })
  308. })
  309. },
  310. getMsg: function(url, data, header) {
  311. header = header || "application/x-www-form-urlencoded";
  312. url = this.config("APIHOST2") + url;
  313. let token = uni.getStorageSync("token");
  314. return new Promise((succ, error) => {
  315. uni.request({
  316. url: url,
  317. data: data,
  318. method: "GET",
  319. header: {
  320. "content-type": header,
  321. "token": token
  322. },
  323. success: function(result) {
  324. if (result.data.code == 401) {
  325. uni.removeStorageSync("token")
  326. uni.removeStorageSync("userId")
  327. uni.removeStorageSync("phone")
  328. uni.removeStorageSync("openid")
  329. uni.removeStorageSync("userName")
  330. uni.removeStorageSync("relation")
  331. uni.removeStorageSync("relation_id")
  332. uni.removeStorageSync("isInvitation")
  333. uni.removeStorageSync("zhiFuBao")
  334. uni.removeStorageSync("zhiFuBaoName")
  335. uni.showToast({
  336. title: '请先登录,以便更好使用!',
  337. icon: 'none'
  338. })
  339. }
  340. succ.call(self, result.data)
  341. },
  342. fail: function(e) {
  343. error.call(self, e)
  344. }
  345. })
  346. })
  347. }
  348. }