httpRequest.js 13 KB

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