request.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //如果部署在同域名的话可以直接取cookie,后端需 HttpOnly 设置成否
  2. // var cookie_name = 'wolfking.jeesharp.session.id'
  3. // getCookie(cookie_name)
  4. // 是否本地环境
  5. function isLocalEnv() {
  6. let domain = window.location.origin
  7. if (['http://127.0.0.1:5501', 'file://'].includes(domain)) {
  8. return true
  9. } else {
  10. return false
  11. }
  12. }
  13. window.apiType = "sit" // dev 本地测试 , sit 线上测试 uat 客户测试环境 prod 正式环境
  14. if(window.location.search){
  15. apiType = window.location.search.split("=")[1]
  16. }
  17. window.fetchUrl = ""
  18. // window.fetchUrl = "http://172.21.9.236:8081"
  19. if (window.apiType == "dev") {
  20. window.fetchUrl = "http://192.168.1.4:8080"
  21. } else if (window.apiType == "sit") {
  22. window.fetchUrl = "http://172.21.9.236:8081"
  23. } else if (window.apiType == "uat") {
  24. window.fetchUrl = "http://172.21.12.247:8081"
  25. } else if(window.apiType == "prod"){
  26. window.fetchUrl = "http://172.21.12.244:8081"
  27. }
  28. // api 前缀
  29. function getApiUrl() {
  30. // 为线上时
  31. if (!isLocalEnv()) {
  32. return window.location.origin
  33. } else {
  34. // 本地时
  35. // return 'http://sxtc.com:10002' // 内网环境
  36. // return 'http://10.8.8.100:10002' // 内网环境
  37. // return 'http://1.71.170.214:18080' // 线上环境
  38. // return 'http://szxm.sxcig.com:8012' // 线上环境
  39. // return 'http://10.0.0.27:8080' // 超哥环境
  40. // return 'http://10.0.0.22:8080' // 闫敏的环境
  41. // return 'http://10.0.0.8:8080' // 祥慧的环境
  42. // return 'http://10.0.0.115:8080' // 王蒙的环境
  43. return 'http:192.168.1.60:8081' // 焦煤内网
  44. }
  45. }
  46. // websocket 前缀
  47. function getWsUrl() {
  48. let prefix = location.protocol === 'https:' ? 'wss://' : 'ws://'
  49. // 为线上时
  50. if (!isLocalEnv()) {
  51. return prefix + window.location.host
  52. } else {
  53. return prefix + 'sxtc.com:10002'
  54. // return prefix + '10.0.0.115:8080'
  55. }
  56. }
  57. // minio 前缀
  58. function getMinioUrl() {
  59. let isProduction = location.origin.indexOf('1.71.170.214') != -1
  60. if (isProduction) {
  61. return 'http://1.71.170.214:19000/constr-documents/'
  62. } else {
  63. return 'http://10.8.8.191:9000/constr-documents-test/'
  64. }
  65. }
  66. /** axios封装
  67. * 请求拦截、相应拦截、错误统一处理
  68. */
  69. const httpRequest = axios.create({
  70. baseURL: 'http:192.168.1.60:8081', // 测试环境
  71. timeout: 3 * 60 * 1000, // 3分钟等待
  72. withCredentials: true // 携带cookie
  73. })
  74. // 请求拦截器
  75. httpRequest.interceptors.request.use(
  76. function (config) {
  77. if (!isLocalEnv()) {
  78. if (getQueryVariable('token')) {
  79. config.headers['access_token'] = getQueryVariable('token')
  80. }
  81. } else {
  82. config.headers['access_token'] = localStorage.getItem('token')
  83. // config.headers['access_token'] = '4b3ed521-f509-48d9-b907-ab127479df41'
  84. }
  85. // 上传文件
  86. if (config.url == '/apiSys/oss/upload') {
  87. config.headers['Content-Type'] = 'multipart/form-data'
  88. } else {
  89. config.headers['Content-Type'] = 'application/json'
  90. }
  91. return config
  92. },
  93. function (error) {
  94. return Promise.reject(error)
  95. }
  96. )
  97. //响应拦截
  98. httpRequest.interceptors.response.use(
  99. function (response) {
  100. const res = response.data
  101. // if (res.code == 201) {
  102. // this.ELEMENT.Message.error(res.msg)
  103. // if (!isLocalEnv()) {
  104. // // 跳登录页
  105. // window.location = window.location.origin
  106. // } else {
  107. // localEnvLogin()
  108. // }
  109. // }
  110. return response
  111. },
  112. function (error) {
  113. this.ELEMENT.Message.error(error.message)
  114. return Promise.reject(error)
  115. }
  116. )
  117. function get(url, params) {
  118. return new Promise((resolve, reject) => {
  119. httpRequest
  120. .get(url, { params: params })
  121. .then(res => {
  122. resolve(res.data)
  123. })
  124. .catch(err => {
  125. reject(err.data)
  126. })
  127. })
  128. }
  129. // qs.stringify(data)
  130. function post(url, data, config) {
  131. return new Promise((resolve, reject) => {
  132. httpRequest
  133. .post(url, data, config)
  134. .then(res => {
  135. resolve(res.data)
  136. })
  137. .catch(err => {
  138. reject(err)
  139. })
  140. })
  141. }
  142. function postFile(url, blob) {
  143. return new Promise((resolve, reject) => {
  144. let formdata = new FormData()
  145. formdata.append('multipartFile', blob)
  146. httpRequest
  147. .post(url, formdata)
  148. .then(res => {
  149. resolve(res.data)
  150. })
  151. .catch(err => {
  152. reject(err)
  153. })
  154. })
  155. }
  156. function localEnvLogin() {
  157. post(
  158. '/ierp/api/getAppToken.do',
  159. {
  160. username: 'admin',
  161. password: '1q2w3e4r5t'
  162. },
  163. {
  164. transformRequest: [
  165. function (data, headers) {
  166. // 将请求数据转换成功 formdata 接收格式
  167. headers['Content-Type'] = 'application/x-www-form-urlencoded'
  168. return stringify(data)
  169. }
  170. ]
  171. }
  172. ).then(res => {
  173. localStorage.setItem('token', res.data.token || getQueryVariable('token'))
  174. })
  175. }
  176. function localEnvAppToken() {
  177. return new Promise((resole, reject) => {
  178. let data
  179. if (window.apiType == "dev") {
  180. data = {
  181. appId: "jsc",
  182. appSecuret: "G|s26?hAW1TzX5rY",
  183. tenantid: "jmdev",
  184. accountId: "1541173495969351680",
  185. }
  186. } else if (window.apiType == "sit") {
  187. data = {
  188. appId: "jsc",
  189. appSecuret: "OA9#'Jn'4p|f`nBG",
  190. tenantid: "dev",
  191. accountId: "1493393884158362624",
  192. }
  193. } else if (window.apiType == "uat") {
  194. data = {
  195. appId: "jsc",
  196. appSecuret: "Cs?9HeKqacN%gnu3",
  197. tenantid: "jmuatierp",
  198. accountId: "1609428936914108416",
  199. }
  200. } else if(window.apiType == "prod") {
  201. data = {
  202. appId: "jsc",
  203. appSecuret: '{Li$d73F13x"+ASb',
  204. tenantid: "jmierp",
  205. accountId: "1507155316804943872",
  206. }
  207. }else {
  208. console.log("未知类型");
  209. return
  210. }
  211. fetch(fetchUrl + "/ierp/api/getAppToken.do", {
  212. method: "post",
  213. body: JSON.stringify(data),
  214. timeout: 5000,
  215. mode: 'cors'
  216. }).then(res => {
  217. return res.json()
  218. })
  219. .catch(error => {
  220. reject(error)
  221. })
  222. .then(response => {
  223. if (response != undefined) {
  224. resole(response.data)
  225. } else {
  226. reject({ requestType: false })
  227. }
  228. });
  229. })
  230. };
  231. function getLocalEnvAccessToken() {
  232. return new Promise((resole, reject) => {
  233. let data
  234. if (window.apiType == "dev") {
  235. data = {
  236. user: "17649834944",
  237. apptoken: localStorage.getItem("app_token"),
  238. tenantid: "jmdev",
  239. accountId: "1541173495969351680",
  240. usertype: "Mobile",
  241. }
  242. } else if (window.apiType == "sit") {
  243. data = {
  244. user: "17649834944",
  245. apptoken: localStorage.getItem("app_token"),
  246. tenantid: "dev",
  247. accountId: "1493393884158362624",
  248. usertype: "Mobile",
  249. }
  250. } else if (window.apiType == "uat") {
  251. data = {
  252. // appId: "jsc",
  253. apptoken: localStorage.getItem("app_token"),
  254. // appSecuret: "Cs?9HeKqacN%gnu3",
  255. tenantid: "jmuatierp",
  256. user: "17649834944",
  257. usertype: "Mobile",
  258. accountId: "1609428936914108416",
  259. }
  260. } else if(window.apiType == "prod") {
  261. data = {
  262. // appId: "jsc",
  263. user: "18035133443",
  264. usertype: "Mobile",
  265. apptoken: localStorage.getItem("app_token"),
  266. // appSecuret: '{Li$d73F13x"+ASb',
  267. tenantid: "jmierp",
  268. accountId: "1507155316804943872",
  269. }
  270. } else {
  271. console.log("未知类型");
  272. return
  273. }
  274. fetch(fetchUrl + "/ierp/api/login.do", {
  275. method: "post",
  276. body: JSON.stringify(data),
  277. mode: 'cors'
  278. }).then(res => res.json())
  279. .catch(error => {
  280. reject(error)
  281. })
  282. .then(response => {
  283. resole(response.data)
  284. });
  285. })
  286. }
  287. function fetchPostMethods(url, params) {
  288. return new Promise((resole, reject) => {
  289. fetch(fetchUrl + url, {
  290. method: "post",
  291. body: JSON.stringify(params),
  292. headers: new Headers({
  293. "Content-Type": "application/json",
  294. "accesstoken": localStorage.getItem("access_token"),
  295. }),
  296. mode: 'cors'
  297. }).then(res => {
  298. return res.json()
  299. })
  300. .catch(error => {
  301. reject(error)
  302. })
  303. .then(response => {
  304. if (response != undefined) {
  305. resole(response.data)
  306. }
  307. });
  308. })
  309. }
  310. // 将参数转换成功 formdata 接收格式
  311. function stringify(data) {
  312. let ret = ''
  313. for (const it in data) {
  314. ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  315. }
  316. ret = ret.substring(0, ret.lastIndexOf('&'))
  317. return ret
  318. }
  319. // 格式化日期
  320. function formatDate(time) {
  321. let date = time ? new Date(Number(time)) : new Date();
  322. let Y = date.getFullYear() + '-';
  323. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  324. let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
  325. let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
  326. let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
  327. let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  328. return Y + M + D + h + m + s;
  329. }