Browse Source

Update utils.js

chinadelu 5 years ago
parent
commit
9d1a104847
3 changed files with 55 additions and 43 deletions
  1. 11 12
      pages/index/bangdan/bangdan.js
  2. 21 22
      pages/index/index.js
  3. 23 9
      utils/utils.js

+ 11 - 12
pages/index/bangdan/bangdan.js

@@ -1,4 +1,5 @@
 const app = getApp();
 const app = getApp();
+var utils = require('../../../utils/utils.js')
 // 定义一个全局变量保存从接口获取到的数据,以免重复请求接口
 // 定义一个全局变量保存从接口获取到的数据,以免重复请求接口
 var resut; 
 var resut; 
 Page({
 Page({
@@ -26,23 +27,21 @@ Page({
    */
    */
   onLoad: function (options) {
   onLoad: function (options) {
     var that = this;
     var that = this;
-    wx.request({
-      url: 'http://mobile.ximalaya.com/mobile/discovery/v3/recommend/hotAndGuess?code=43_310000_3100&device=android&version=5.4.45',
-      
-      header: {'content-type':'application/json'},
-      method: 'GET',
-      dataType: 'json',
-      responseType: 'text',
-      success: (res)=>{
-        resut = res;
+    var url = 'http://mobile.ximalaya.com/mobile/discovery/v3/recommend/hotAndGuess?code=43_310000_3100&device=android&version=5.4.45'
+    utils.myRequest({
+      url:url,
+      methods:'GET',
+      success:function(res){
         console.log(res);
         console.log(res);
+        resut = res;
         that.setData({
         that.setData({
           list: res.data.hotRecommends.list[0].list
           list: res.data.hotRecommends.list[0].list
         })
         })
       },
       },
-      fail: ()=>{},
-      complete: ()=>{}
-    });
+      fail:function(){
+
+      }
+    })
   },
   },
   handleClick(e) {
   handleClick(e) {
     let currentTab = e.currentTarget.dataset.index;
     let currentTab = e.currentTarget.dataset.index;

+ 21 - 22
pages/index/index.js

@@ -1,6 +1,7 @@
 //index.js
 //index.js
 //获取应用实例
 //获取应用实例
 const app = getApp()
 const app = getApp()
+var utils = require('../../utils/utils.js');
 
 
 Page({
 Page({
 
 
@@ -38,29 +39,27 @@ Page({
    */
    */
   onLoad: function (options) {
   onLoad: function (options) {
     var that = this;
     var that = this;
-    wx.request({
-      url: 'http://mobile.ximalaya.com/mobile/discovery/v3/recommend/hotAndGuess?code=43_310000_3100&device=android&version=5.4.45',
-      data: {},
-      header: {'content-type':'application/json'},
-      method: 'GET',
-      dataType: 'json',
-      responseType: 'text',
-      success: (result)=>{
-        console.log(result);
-        if(result.statusCode == 200){
-          that.setData({
-            showitem:true,
-            guess:result.data.paidArea.list,
-            xiaoshuocontent:result.data.hotRecommends.list[0].list,
-            xiangshengcontent:result.data.hotRecommends.list[2].list,
-            tuokocontent:result.data.hotRecommends.list[4].list
-          })
-        }else{
-          that.setData({
-            showitem:false
-          })
-        }
+    var url = 'http://mobile.ximalaya.com/mobile/discovery/v3/recommend/hotAndGuess?code=43_310000_3100&device=android&version=5.4.45';
+
+    // 调用的是自己封装的工具函数,在utils中
+    utils.myRequest({
+      url:url,
+      methods:'GET',
+      success:function(result){
+        console.log("我被执行了")
+        that.setData({
+          showitem:true,
+          guess:result.data.paidArea.list,
+          xiaoshuocontent:result.data.hotRecommends.list[0].list,
+          xiangshengcontent:result.data.hotRecommends.list[2].list,
+          tuokocontent:result.data.hotRecommends.list[4].list
+        })
       },
       },
+      fail:function(){
+        that.setData({
+          showitem:false
+        })
+      }
     });
     });
   },
   },
   goToBangDan:function(){
   goToBangDan:function(){

+ 23 - 9
utils/utils.js

@@ -1,21 +1,35 @@
 /**
 /**
  * 本文件主要是工具类函数
  * 本文件主要是工具类函数
  */
  */
-let myRequest = function(url,methods,data = {}){
+
+
+ /*
+  自己基于wx.request封装的一个请求函数(粗陋封装各位不要笑话)
+  因为在小程序开发中request是最常用的api所以会造成很多的代码重复
+  因此将其在封装之后可以大大的减少代码的复用
+*/
+let myRequest = function(args = {url:'',methods:'GET', data:{}, success:function(){},fail:function(){}}){
     wx.request({
     wx.request({
-        url: url,
-        data: data,
+        url: args.url,
+        data: args.data,
         header: {'content-type':'application/json'},
         header: {'content-type':'application/json'},
-        method: methods,
+        method: args.methods,
         dataType: 'json',
         dataType: 'json',
         responseType: 'text',
         responseType: 'text',
-        success: (result)=>{
-          console.log(result);
-          if(result.statusCode == 200){
-              // TODO
+        success: (res)=>{
+          console.log(res);
+          if(res.statusCode == 200){
+            // 请求成功执行回调函数
+            args.success(res)
           }else{
           }else{
-            // TODO
+            // 请求失败执行回调函数
+            args.fail()
           }
           }
         },
         },
     })
     })
+}
+
+// 向外暴露接口
+module.exports = {
+  myRequest : myRequest
 }
 }