Przeglądaj źródła

小程序:网络请求

大侠 2 lat temu
rodzic
commit
087e960e66

+ 20 - 32
16_miniapp/day-4/code/pages/camera/camera.js

@@ -1,11 +1,12 @@
 // pages/camera/camera.js
+const request = require('../../utils/http')
 Page({
 
   /**
    * 页面的初始数据
    */
   data: {
-
+    result: {}
   },
 
   /**
@@ -26,41 +27,28 @@ Page({
    * 生命周期函数--监听页面显示
    */
   onShow() {
-
-  },
-
-  /**
-   * 生命周期函数--监听页面隐藏
-   */
-  onHide() {
-
+    this.getTags()
   },
+  //  获取 电影标签数据
+  async getTags() {
+    wx.showLoading({
+      title: '加载中...',
+    })
 
-  /**
-   * 生命周期函数--监听页面卸载
-   */
-  onUnload() {
+    let res = await request({
+      url: 'http://v.juhe.cn/toutiao/index?type=top&key=acf8f2ff52da5cb4aa65eb8b7c51583d',
+    })
 
-  },
+    console.log(res)
+   
+    setTimeout(() => {
+      wx.hideLoading()
+    }, 300)
 
-  /**
-   * 页面相关事件处理函数--监听用户下拉动作
-   */
-  onPullDownRefresh() {
-
-  },
-
-  /**
-   * 页面上拉触底事件的处理函数
-   */
-  onReachBottom() {
-
-  },
-
-  /**
-   * 用户点击右上角分享
-   */
-  onShareAppMessage() {
+    if(res.error_code === 0){
+      this.setData({result: res.result})
+    }
 
   }
+
 })

+ 12 - 0
16_miniapp/day-4/code/pages/camera/camera.wxml

@@ -2,3 +2,15 @@
 <text>pages/camera/camera.wxml</text>
 <navigator url="../index/index" open-type="navigateTo">回到主页-错误</navigator>
 <navigator url="../index/index" open-type="switchTab">回到主页-正确的</navigator>
+
+<view wx:for="{{result.data}}" wx:key="uniquekey" style="border-bottom: 1px solid #ccc; padding-bottom: 5px; margin-top: 5px;">
+  <view>
+    <image src="{{item.thumbnail_pic_s}}" alt=""/>
+  </view>
+  <view>
+    {{item.title}}
+  </view>
+  <view>
+    <text>作者:{{item.author_name}}</text>
+  </view>
+</view>

+ 5 - 0
16_miniapp/day-4/code/pages/index/index.js

@@ -39,5 +39,10 @@ Page({
     this.setData({
       todolist: this.data.todolist.filter(todo => todo.id !== delId)
     })
+
+    wx.showToast({
+      title: '删除成功',
+      icon: 'success',
+    })
   }
 });

+ 2 - 1
16_miniapp/day-4/code/project.private.config.json

@@ -2,7 +2,8 @@
   "description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
   "projectname": "miniapp",
   "setting": {
-    "compileHotReLoad": true
+    "compileHotReLoad": true,
+    "urlCheck": false
   },
   "libVersion": "2.30.2"
 }

+ 15 - 0
16_miniapp/day-4/code/utils/http.js

@@ -0,0 +1,15 @@
+function request(opts){
+  return new Promise((resolve, reject) => {
+    wx.request({
+      ...opts,
+      success(result){
+        resolve(result.data)
+      },
+      fail(err){
+        reject(err)
+      }
+    })
+  })
+}
+
+module.exports = request