requesttoken111.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // 创建request.js文件
  2. //通用uni-app网络请求
  3. export default {
  4. config: {
  5. baseUrl: "http://localhost:8200", //本地开发
  6. // baseUrl: "http://172.27.185.236:8200", //本地开发
  7. // baseUrl: "https://talent.younggee.com:8201", //默认的公共域名
  8. header: {
  9. 'Content-Type': 'application/json;charset=UTF-8', //默认get方式
  10. 'token': uni.getStorageSync('token'),
  11. 'cookie': uni.getStorageSync('cookie'),
  12. },
  13. data: {}, //上行参数
  14. method: "GET", //默认GET方式
  15. dataType: "json",
  16. /* 如设为json,会对返回的数据做一次 JSON.parse */
  17. success() {}, //成功回调
  18. fail() {}, //失败回调
  19. complete() {} //完成回到
  20. },
  21. //封装两种请求方式,delete和put类似
  22. get(url, data, options) {
  23. if (!options) {
  24. options = {}
  25. }
  26. options.url = url
  27. options.data = data
  28. options.method = 'GET'
  29. return this.request(options)
  30. },
  31. post(url, data, options) {
  32. if (!options) {
  33. options = {}
  34. }
  35. options.url = url
  36. options.data = data
  37. options.method = 'POST'
  38. return this.request(options)
  39. },
  40. //综合处理
  41. request(options) {
  42. if (!options) {
  43. options = {}
  44. }
  45. options.baseUrl = options.baseUrl || this.config.baseUrl //设置全局url
  46. options.dataType = options.dataType || this.config.dataType //设置数据类型
  47. options.url = options.baseUrl + options.url //设置当前请求url
  48. options.data = options.data || {} //设置参数
  49. // #ifdef MP-ALIPAY
  50. options.data.platform = 2
  51. // #endif
  52. options.method = options.method || this.config.method //设置请求方式
  53. //header系列修改
  54. if (options.method == "POST") {
  55. this.config.header['Content-Type'] = 'application/x-www-form-urlencoded'
  56. } else {
  57. this.config.header['Content-Type'] = 'application/json;charset=UTF-8'
  58. }
  59. // 数据签名
  60. var token = {
  61. 'token': uni.getStorageSync('token') || ''
  62. }
  63. var cookie = {
  64. 'cookie': uni.getStorageSync('cookie') || ''
  65. }
  66. options.header = Object.assign({}, options.header, this.config.header, token, cookie)
  67. return new Promise((resolve, reject) => {
  68. let _config = null
  69. //定义成功失败的函数
  70. options.success = res => {
  71. // console.log(res)
  72. resolve(res)
  73. // if (res.data.code == 0) {
  74. // console.log(res)
  75. // resolve(res.data)
  76. // } else {
  77. // //获取失败后的逻辑
  78. // console.log('获取失败')
  79. // }
  80. }
  81. options.fail = err => {
  82. // 请求接口错误
  83. reject(res.data)
  84. }
  85. _config = Object.assign({}, this.config, options) //合并请求参数
  86. //执行request请求
  87. uni.request(_config);
  88. });
  89. }
  90. }