request.js 9.2 KB

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