request.js 9.0 KB

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