request.js 9.1 KB

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