zjh 5 anni fa
parent
commit
d2cfa6ae3e
3 ha cambiato i file con 77 aggiunte e 10 eliminazioni
  1. 6 10
      pages/rise/rise.js
  2. 9 0
      utils/api.js
  3. 62 0
      utils/wxRequest.js

+ 6 - 10
pages/rise/rise.js

@@ -1,5 +1,6 @@
 import * as echarts from '../../ec-canvas/echarts';
-
+var Api = require('../../utils/api.js');
+var wxRequest = require('../../utils/wxRequest.js') 
 var app = getApp()
 function initChart(canvas, width, height, dpr, data) {
   const chart = echarts.init(canvas, null, {
@@ -178,6 +179,10 @@ Page({
         });
       }
     });
+    var getinclast = wxRequest.postRequest(Api.getinclast());
+    getinclast.then((res)=>{
+      console.log(res)
+    })
   },
   // 滑动切换tab 
   bindChange: function(e) {
@@ -202,15 +207,6 @@ Page({
     wx.navigateTo({
       url: '../item/item?row='+JSON.stringify(e.currentTarget.dataset.row)
     });
-    wx.request({
-      url: 'http://192.168.1.60:6900/news/mainpage', //仅为示例,并非真实的接口地址
 
-      header: {
-        'content-type': 'application/json' // 默认值
-      },
-      success (res) {
-        console.log(res)
-      }
-    })
   }
 })

+ 9 - 0
utils/api.js

@@ -0,0 +1,9 @@
+
+var HOST_URI = 'http://192.168.1.60:6900';
+module.exports = {  
+  // 获取推荐文章列表数据
+  getinclast: function (obj) {
+    var url =HOST_URI + '/inc/last';
+    return url;
+  },
+}

+ 62 - 0
utils/wxRequest.js

@@ -0,0 +1,62 @@
+
+function wxPromisify(fn) {
+  return function (obj = {}) {
+      return new Promise((resolve, reject) => {
+          obj.success = function (res) {
+              //成功
+              resolve(res)
+          }
+          obj.fail = function (res) {
+              //失败
+              reject(res)
+          }
+          fn(obj)
+      })
+  }
+}
+//无论promise对象最后状态如何都会执行
+Promise.prototype.finally = function (callback) {
+  let P = this.constructor;
+  return this.then(
+      value => P.resolve(callback()).then(() => value),
+      reason => P.resolve(callback()).then(() => { throw reason })
+  );
+};
+/**
+* 微信请求get方法
+* url
+* data 以对象的格式传入
+*/
+function getRequest(url, data) {
+  var getRequest = wxPromisify(wx.request)
+  return getRequest({
+      url: url,
+      method: 'GET',
+      data: data,
+      header: {
+          'Content-Type': 'application/json'
+      }
+  })
+}
+
+/**
+* 微信请求post方法封装
+* url
+* data 以对象的格式传入
+*/
+function postRequest(url, data) {
+  var postRequest = wxPromisify(wx.request)
+  return postRequest({
+      url: url,
+      method: 'POST',
+      data: data,
+      header: {
+          "content-type": "application/json"
+      },
+  })
+}
+
+module.exports = {
+  postRequest: postRequest,
+  getRequest: getRequest
+}