You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

request.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {
  2. encryption
  3. } from "./encryption"
  4. /**
  5. * 配置信息,针对不同的平台进行配置
  6. */
  7. export const envs = {
  8. //开发环境配置
  9. development: {
  10. // baseUrl: "222.85.144.89:19002",
  11. baseUrl: "192.168.100.63:8087",
  12. },
  13. //生产环境配置
  14. production: {
  15. baseUrl: "192.168.100.63:8087",
  16. }
  17. }
  18. export function request(code, options = {}) {
  19. console.log('接口请求参数:', options)
  20. //公参
  21. const Common = {
  22. }
  23. //Url 地址
  24. options.url = 'http://' + envs['development'].baseUrl + '/ifzt/api/interfaceMidGroundIn'
  25. // options.url = 'https://' + 'skx.mynatapp.cc' + '/ifzt/api/interfaceMidGroundIn'
  26. //判断baseUri是否为空
  27. if (options.baseUrl) {
  28. options.url = options.baseUrl
  29. }
  30. //默认json数据格式提交`
  31. let contentType = 'application/json;charset=UTF-8'
  32. //根据type判断数据传输格式
  33. if (options.type && options.type === 2) {
  34. contentType = 'application/x-www-form-urlencoded '
  35. }
  36. options.header = {
  37. 'content-type': contentType
  38. }
  39. //默认POST提交
  40. options.method = options.method ? options.method : 'POST'
  41. //设置请求超时时间
  42. options.timeout = 60000
  43. //判断code不为空
  44. if (code) {
  45. options.data = encryption(code, Object.assign(Common, {
  46. ...options.data,
  47. 'appId': '52030131'
  48. }), 2);
  49. }
  50. //是否显示加载中
  51. if (!options.showLoading) {
  52. uni.showLoading({
  53. title: options.showTitle ? options.showTitle : '加载中',
  54. mask: true
  55. });
  56. }
  57. //参数返回
  58. return new Promise((resolve, reject) => {
  59. options.success = (res) => {
  60. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  61. uni.hideLoading()
  62. if (res.data.statusCode !== 0) {
  63. uni.showModal({
  64. title: '提示',
  65. content: res.data.errorMsg,
  66. success: function(res) {
  67. if (res.confirm) {
  68. console.log('用户点击确定');
  69. } else if (res.cancel) {
  70. console.log('用户点击取消');
  71. }
  72. }
  73. });
  74. }
  75. //请求成功
  76. resolve(res.data)
  77. }
  78. options.fail = (err) => {
  79. uni.hideLoading()
  80. console.log('请求错误', err)
  81. //处理请求错误
  82. reject(err)
  83. }
  84. uni.request(options)
  85. });
  86. }