requesttoken111.js 2.5 KB

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