httpRequest.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import configdata from './config'
  2. import cache from './cache'
  3. import queue from './queue'
  4. let domainReadyPromise = null
  5. ensureDomainReady()
  6. // 确保已经从后端拿到动态域名配置(只会真实请求一次)
  7. function ensureDomainReady() {
  8. // 1)如果本地已经有 web_config,直接返回
  9. const webConfig = cache.get('web_config')
  10. if (webConfig && webConfig.APIHOST) {
  11. return Promise.resolve()
  12. }
  13. // 2)已经在请求中了,复用同一个 Promise
  14. if (domainReadyPromise) return domainReadyPromise
  15. // 3)第一次:去后端拉取域名配置
  16. domainReadyPromise = new Promise((resolve) => {
  17. uni.request({
  18. url: 'https://www.bosszan.com/sqx_fast/app/dict/list',
  19. data: {
  20. type: '路由'
  21. },
  22. method: 'GET',
  23. success: (res) => {
  24. if (res.data && res.data.code === 0 && res.data.data) {
  25. const cfg = res.data.data
  26. if (cfg?.length && cfg[0].code) {
  27. let webConfigData = cfg[0].code == 'test' ? configdata.test : configdata.production
  28. cache.put('web_config', webConfigData)
  29. }
  30. }
  31. },
  32. complete: () => {
  33. // 无论成功失败,都结束,失败时后面会走 config.js 兜底
  34. resolve()
  35. }
  36. })
  37. })
  38. return domainReadyPromise
  39. }
  40. module.exports = {
  41. config: function(name) {
  42. let info = null
  43. if (!name) return info
  44. const name2 = name.split('.')
  45. const webConfig = cache.get('web_config')
  46. // 1. 先看后端下发的动态配置
  47. if (webConfig) {
  48. if (name2.length > 1) {
  49. info = webConfig[name2[0]] && webConfig[name2[0]][name2[1]]
  50. } else {
  51. info = webConfig[name]
  52. }
  53. }
  54. // 2. 动态没有,再用静态 config.js 兜底
  55. if (info == null) {
  56. if (name2.length > 1) {
  57. info = configdata[name2[0]] && configdata[name2[0]][name2[1]]
  58. } else {
  59. info = configdata[name]
  60. }
  61. }
  62. return info == null ? null : info
  63. },
  64. // config: function(name) {
  65. // var info = null;
  66. // if (name) {
  67. // var name2 = name.split("."); //字符分割
  68. // if (name2.length > 1) {
  69. // info = configdata[name2[0]][name2[1]] || null;
  70. // } else {
  71. // info = configdata[name] || null;
  72. // }
  73. // if (info == null) {
  74. // let web_config = cache.get("web_config");
  75. // console.log('web_config', web_config)
  76. // if (web_config) {
  77. // if (name2.length > 1) {
  78. // info = web_config[name2[0]][name2[1]] || null;
  79. // } else {
  80. // info = web_config[name] || null;
  81. // }
  82. // }
  83. // }
  84. // }
  85. // return info;
  86. // },
  87. post: function(url, data, header) {
  88. header = header || "application/x-www-form-urlencoded";
  89. return ensureDomainReady().then(() => {
  90. const host = this.config('APIHOST')
  91. const fullUrl = host + url
  92. let token = uni.getStorageSync("token");
  93. return new Promise((succ, error) => {
  94. uni.request({
  95. url: fullUrl,
  96. data: data,
  97. method: "POST",
  98. header: {
  99. "content-type": header,
  100. "token": token
  101. },
  102. success: function(result) {
  103. if (result.data.code == 401) {
  104. // uni.clearStorage();
  105. uni.removeStorageSync("token")
  106. uni.removeStorageSync("userId")
  107. uni.removeStorageSync("phone")
  108. uni.removeStorageSync("openid")
  109. uni.removeStorageSync("userName")
  110. uni.removeStorageSync("relation")
  111. uni.removeStorageSync("relation_id")
  112. uni.removeStorageSync("isInvitation")
  113. uni.removeStorageSync("zhiFuBao")
  114. uni.removeStorageSync("zhiFuBaoName")
  115. uni.showToast({
  116. title: '请先登录,以便更好使用!',
  117. icon: 'none'
  118. })
  119. setTimeout(() => {
  120. // 选择合适的路由方法(根据需求切换)
  121. // 1. 跳转到登录页,关闭当前页面(推荐,避免回退到失效页面)
  122. queue.toLogin()
  123. // uni.redirectTo({
  124. // url: '/pages/public/login' // 替换为你的登录页路径
  125. // });
  126. // 2. 如果需要关闭所有页面,强制跳转到登录页(比如退出登录场景)
  127. // uni.reLaunch({
  128. // url: '/pages/public/login'
  129. // });
  130. // 3. 普通跳转(保留当前页面栈,不推荐此场景)
  131. // uni.navigateTo({
  132. // url: '/pages/public/login'
  133. // });
  134. }, 500); // 延迟时间必须 ≥
  135. }
  136. succ.call(self, result.data)
  137. },
  138. fail: function(e) {
  139. error.call(self, e)
  140. }
  141. })
  142. })
  143. })
  144. },
  145. postT: function(url, data, header, timeout) {
  146. header = header || "application/x-www-form-urlencoded";
  147. return ensureDomainReady().then(() => {
  148. const host = this.config('APIHOST1')
  149. const fullUrl = host + url
  150. let token = uni.getStorageSync("token");
  151. console.log('timeout', timeout)
  152. if (token) {
  153. return new Promise((succ, error) => {
  154. uni.request({
  155. url: fullUrl,
  156. data: data,
  157. method: "POST",
  158. timeout: timeout ? timeout : 60000,
  159. header: {
  160. "content-type": header,
  161. "token": token
  162. },
  163. success: function(result) {
  164. if (result.data.code == 401) {
  165. uni.removeStorageSync("token")
  166. uni.removeStorageSync("userId")
  167. uni.removeStorageSync("phone")
  168. uni.removeStorageSync("openid")
  169. uni.removeStorageSync("userName")
  170. uni.removeStorageSync("relation")
  171. uni.removeStorageSync("relation_id")
  172. uni.removeStorageSync("isInvitation")
  173. uni.removeStorageSync("zhiFuBao")
  174. uni.removeStorageSync("zhiFuBaoName")
  175. uni.showToast({
  176. title: '请先登录,以便更好使用!',
  177. icon: 'none'
  178. })
  179. }
  180. succ.call(self, result.data)
  181. },
  182. fail: function(e) {
  183. error.call(self, e)
  184. }
  185. })
  186. })
  187. } else {
  188. return new Promise((succ, error) => {
  189. uni.request({
  190. url: fullUrl,
  191. data: data,
  192. method: "POST",
  193. header: {
  194. "content-type": header,
  195. },
  196. success: function(result) {
  197. succ.call(self, result.data)
  198. },
  199. fail: function(e) {
  200. error.call(self, e)
  201. }
  202. })
  203. })
  204. }
  205. })
  206. },
  207. postJson: function(url, data, header) {
  208. header = header || "application/json";
  209. return ensureDomainReady().then(() => {
  210. const host = this.config('APIHOST1')
  211. const fullUrl = host + url
  212. let token = uni.getStorageSync("token");
  213. if (token) {
  214. return new Promise((succ, error) => {
  215. uni.request({
  216. url: fullUrl,
  217. data: data,
  218. method: "POST",
  219. header: {
  220. "content-type": header,
  221. "token": token
  222. },
  223. success: function(result) {
  224. if (result.data.code == 401) {
  225. uni.removeStorageSync("token")
  226. uni.removeStorageSync("userId")
  227. uni.removeStorageSync("phone")
  228. uni.removeStorageSync("openid")
  229. uni.removeStorageSync("userName")
  230. uni.removeStorageSync("relation")
  231. uni.removeStorageSync("relation_id")
  232. uni.removeStorageSync("isInvitation")
  233. uni.removeStorageSync("zhiFuBao")
  234. uni.removeStorageSync("zhiFuBaoName")
  235. uni.showToast({
  236. title: '请先登录,以便更好使用!',
  237. icon: 'none'
  238. })
  239. } else if (result.data.code != 0 && result.data.msg != '未进入公司') {
  240. uni.showToast({
  241. title: result.data.msg || '查询失败',
  242. icon: 'none'
  243. })
  244. }
  245. succ.call(self, result.data)
  246. },
  247. fail: function(e) {
  248. error.call(self, e)
  249. }
  250. })
  251. })
  252. } else {
  253. return new Promise((succ, error) => {
  254. uni.request({
  255. url: fullUrl,
  256. data: data,
  257. method: "POST",
  258. header: {
  259. "content-type": header,
  260. },
  261. success: function(result) {
  262. succ.call(self, result.data)
  263. },
  264. fail: function(e) {
  265. error.call(self, e)
  266. }
  267. })
  268. })
  269. }
  270. });
  271. },
  272. getT: function(url, data, header) {
  273. header = header || "application/x-www-form-urlencoded";
  274. return ensureDomainReady().then(() => {
  275. const host = this.config('APIHOST1')
  276. const fullUrl = host + url
  277. let token = uni.getStorageSync("token");
  278. if (token) {
  279. return new Promise((succ, error) => {
  280. uni.request({
  281. url: fullUrl,
  282. data: data,
  283. method: "GET",
  284. header: {
  285. "content-type": header,
  286. "token": token
  287. },
  288. success: function(result) {
  289. if (result.data.code == 401) {
  290. uni.removeStorageSync("token")
  291. uni.removeStorageSync("userId")
  292. uni.removeStorageSync("phone")
  293. uni.removeStorageSync("openid")
  294. uni.removeStorageSync("userName")
  295. uni.removeStorageSync("relation")
  296. uni.removeStorageSync("relation_id")
  297. uni.removeStorageSync("isInvitation")
  298. uni.removeStorageSync("zhiFuBao")
  299. uni.removeStorageSync("zhiFuBaoName")
  300. uni.showToast({
  301. title: '请先登录,以便更好使用!',
  302. icon: 'none'
  303. })
  304. } else if (result.data.code != 0 && result.data.msg != '未进入公司') {
  305. uni.showToast({
  306. title: result.data.msg || '查询失败',
  307. icon: 'none'
  308. })
  309. }
  310. succ.call(self, result.data)
  311. },
  312. fail: function(e) {
  313. error.call(self, e)
  314. }
  315. })
  316. })
  317. } else {
  318. return new Promise((succ, error) => {
  319. uni.request({
  320. url: fullUrl,
  321. data: data,
  322. method: "GET",
  323. header: {
  324. "content-type": header
  325. },
  326. success: function(result) {
  327. if (result.data.code != 0 && result.data.msg != '未进入公司') {
  328. uni.showToast({
  329. title: result.data.msg || '查询失败',
  330. icon: 'none'
  331. })
  332. }
  333. succ.call(self, result.data)
  334. },
  335. fail: function(e) {
  336. error.call(self, e)
  337. }
  338. })
  339. })
  340. }
  341. });
  342. },
  343. get: function(url, data, header) {
  344. header = header || "application/x-www-form-urlencoded";
  345. return ensureDomainReady().then(() => {
  346. const host = this.config('APIHOST')
  347. const fullUrl = host + url
  348. let token = uni.getStorageSync("token");
  349. return new Promise((succ, error) => {
  350. uni.request({
  351. url: fullUrl,
  352. data: data,
  353. method: "GET",
  354. header: {
  355. "content-type": header,
  356. "token": token
  357. },
  358. success: function(result) {
  359. if (result.data.code == 401) {
  360. uni.removeStorageSync("token")
  361. uni.removeStorageSync("userId")
  362. uni.removeStorageSync("phone")
  363. uni.removeStorageSync("openid")
  364. uni.removeStorageSync("userName")
  365. uni.removeStorageSync("relation")
  366. uni.removeStorageSync("relation_id")
  367. uni.removeStorageSync("isInvitation")
  368. uni.removeStorageSync("zhiFuBao")
  369. uni.removeStorageSync("zhiFuBaoName")
  370. uni.showToast({
  371. title: '请先登录,以便更好使用!',
  372. icon: 'none'
  373. })
  374. } else if (result.data.code != 0 && result.data.msg != '未进入公司') {
  375. uni.showToast({
  376. title: result.data.msg || '查询失败',
  377. icon: 'none'
  378. })
  379. }
  380. succ.call(self, result.data)
  381. },
  382. fail: function(e) {
  383. error.call(self, e)
  384. }
  385. })
  386. })
  387. });
  388. },
  389. getMsg: function(url, data, header) {
  390. header = header || "application/x-www-form-urlencoded";
  391. return ensureDomainReady().then(() => {
  392. const host = this.config('APIHOST2')
  393. const fullUrl = host + url
  394. let token = uni.getStorageSync("token");
  395. return new Promise((succ, error) => {
  396. uni.request({
  397. url: fullUrl,
  398. data: data,
  399. method: "GET",
  400. header: {
  401. "content-type": header,
  402. "token": token
  403. },
  404. success: function(result) {
  405. if (result.data.code == 401) {
  406. uni.removeStorageSync("token")
  407. uni.removeStorageSync("userId")
  408. uni.removeStorageSync("phone")
  409. uni.removeStorageSync("openid")
  410. uni.removeStorageSync("userName")
  411. uni.removeStorageSync("relation")
  412. uni.removeStorageSync("relation_id")
  413. uni.removeStorageSync("isInvitation")
  414. uni.removeStorageSync("zhiFuBao")
  415. uni.removeStorageSync("zhiFuBaoName")
  416. uni.showToast({
  417. title: '请先登录,以便更好使用!',
  418. icon: 'none'
  419. })
  420. }
  421. succ.call(self, result.data)
  422. },
  423. fail: function(e) {
  424. error.call(self, e)
  425. }
  426. })
  427. })
  428. });
  429. }
  430. }