vue 网络请求数据封装

妖狐艹你老母 2021-09-17 22:18 620阅读 0赞
  1. <script type="text/javascript">
  2. // 配置API接口地址
  3. const baseUrl = '';
  4. // 引入 弹窗组件
  5. var modal = weex.requireModule('modal');
  6. // 引入 请求数据组件
  7. var stream = weex.requireModule('stream');
  8. // 自定义判断元素类型JS
  9. function toType (obj) {
  10. return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
  11. }
  12. //==============
  13. // 参数过滤函数
  14. //==============
  15. function filterNull (o) {
  16. for (var key in o) {
  17. if (o[key] === null) {
  18. delete o[key]
  19. }
  20. if (toType(o[key]) === 'string') {
  21. o[key] = o[key].trim()
  22. } else if (toType(o[key]) === 'object') {
  23. o[key] = filterNull(o[key])
  24. } else if (toType(o[key]) === 'array') {
  25. o[key] = filterNull(o[key])
  26. }
  27. }
  28. return o
  29. }
  30. //=========
  31. // 工具方法
  32. //=========
  33. function toParams(obj) {
  34. var param = ""
  35. for(const name in obj) {
  36. if(typeof obj[name] != 'function') {
  37. param += "&" + name + "=" + encodeURI(obj[name])
  38. }
  39. }
  40. return param.substring(1)
  41. };
  42. //============
  43. // 接口处理函数
  44. //============
  45. function apiStream (method, url, params, success, failure) {
  46. // 过滤参数
  47. if (params) {
  48. params = filterNull(params)
  49. }
  50. /*** stream ***/
  51. if(method === 'GET'){
  52. console.log("进入了get方法")
  53. // GET 方法
  54. stream.fetch({
  55. method: 'GET',
  56. type: 'text',
  57. url: baseUrl + url + toParams(params)
  58. }, function(res) {
  59. if (res.ok) {
  60. // 解密
  61. let currentData = jwtdecode.decode(res.data, 'michahzdee2016', 'HS256');
  62. success(currentData);
  63. }else {
  64. modal.toast({
  65. message: '请求失败,请检查网络!',
  66. duration: 2
  67. })
  68. }
  69. })
  70. }else if(method === 'POST'){
  71. // POST 方法
  72. stream.fetch({
  73. method: 'POST',
  74. type: 'text',
  75. url: baseUrl + url,
  76. headers: {'Content-Type':'application/x-www-form-urlencoded'},
  77. body: toParams(params)
  78. }, function(res) {
  79. if (res.ok) {
  80. // 解密
  81. let currentData = jwtdecode.decode(res.data, 'michahzdee2016', 'HS256');
  82. success(currentData);
  83. }else {
  84. modal.toast({
  85. message: '请求失败,请检查网络!',
  86. duration: 2
  87. })
  88. }
  89. },function(progress) {
  90. //
  91. })
  92. }
  93. };
  94. //========================
  95. // 返回在vue模板中的调用接口
  96. //========================
  97. export default {
  98. // get方法
  99. get: function (url, params, success, failure) {
  100. return apiStream('GET', url, params, success, failure)
  101. },
  102. // post方法
  103. post: function (url, params, success, failure) {
  104. return apiStream('POST', url, params, success, failure)
  105. },
  106. // 用于查看域名 || 服务器地址
  107. globalHttpUrl:baseUrl
  108. }
  109. </script>

使用

  1. import global from './common.vue'
  2. global.get('/api/user/channel/downloadInfo/get', params, function(data) {
  3. })
  4. this.COMMON.commonFun()

发表评论

表情:
评论列表 (有 0 条评论,620人围观)

还没有评论,来说两句吧...

相关阅读

    相关 vue-封装axios请求

    最近接手新的vue项目,发现axios竟然没有封装,立马动手封装,这里记录一下完整的封装过程,废话不说,直接上代码 baseConfig.js文件 //存放各个服