requesttoken111.js 2.6 KB

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