requestnew.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import store from '@/store'
  2. import config, {
  3. baseUrl,
  4. foreignUrl
  5. } from '@/config'
  6. import {
  7. getToken,
  8. getServerUrl
  9. } from '@/utils/auth'
  10. import errorCode from '@/utils/errorCode'
  11. import {
  12. toast,
  13. showConfirm,
  14. tansParams
  15. } from '@/utils/common'
  16. let timeout = 200000
  17. console.log(baseUrl)
  18. const request1 = config => {
  19. // 是否需要设置 token
  20. const isToken = (config.headers || {}).isToken === false
  21. config.header = config.header || {}
  22. // if (getToken() && !isToken) {
  23. // // config.header['Authorization'] = 'Bearer ' + getToken()
  24. // config.header['UserKey'] = getToken()
  25. // config.header['ServerUrl'] = getServerUrl()
  26. // } else {
  27. // uni.reLaunch({
  28. // url: '/pages/index/login'
  29. // })
  30. // }
  31. // get请求映射params参数
  32. if (config.params) {
  33. let url = config.url + '?' + tansParams(config.params)
  34. url = url.slice(0, -1)
  35. config.url = url
  36. }
  37. const xml2js = require('xml2js');
  38. return new Promise((resolve, reject) => {
  39. let requestUrl = uni.getStorageSync("Url") + "/k3cloud/services/LUTAIWebService.asmx/"
  40. console.log(requestUrl)
  41. uni.request({
  42. method: config.method || 'get',
  43. timeout: config.timeout || timeout,
  44. url: requestUrl + config.url,
  45. data: config.data,
  46. header: config.header,
  47. dataType: 'json',
  48. success: res => {
  49. if (res.statusCode == 200) {
  50. xml2js.parseString(res.data, (err, result) => {
  51. if (err) {
  52. reject(err);
  53. }
  54. // 解析后的结果保存在result中
  55. const jsonResult = JSON.parse(result.string._);
  56. resolve(jsonResult)
  57. // if(jsonResult.flag){
  58. // resolve(jsonResult.result)
  59. // }else{
  60. // reject(jsonResult.message)
  61. // }
  62. });
  63. } else {
  64. reject("获取数据失败!")
  65. }
  66. },
  67. fail: err => {
  68. reject(err)
  69. },
  70. complete: () => {
  71. //uni.hideLoading()
  72. }
  73. })
  74. })
  75. }
  76. export default request1