request.js 8.3 KB

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