requestnew.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. console.log(err)
  52. console.log(result)
  53. if (err) {
  54. reject(err);
  55. }
  56. // 解析后的结果保存在result中
  57. const jsonResult = JSON.parse(result.string._);
  58. resolve(jsonResult)
  59. // if(jsonResult.flag){
  60. // resolve(jsonResult.result)
  61. // }else{
  62. // reject(jsonResult.message)
  63. // }
  64. });
  65. } else {
  66. reject("获取数据失败!")
  67. console.log(res)
  68. }
  69. },
  70. fail: err => {
  71. console.log("获取数据失败")
  72. reject(err)
  73. },
  74. complete: () => {
  75. //uni.hideLoading()
  76. }
  77. })
  78. })
  79. }
  80. export default request1