1、发送请求
2、云函数中发送请求,例子request
// 云函数入口文件 const cloud = require(‘wx-server-sdk‘) cloud.init() var rp = require(‘request-promise‘); // 云函数入口函数 exports.main = async(event, context) => { // ES6字符串模板的形式 return rp(`http://api.douban.com/v2/movie/in_theaters?apikey=0df993c66c0c636e29ecbb5344252a4a&start=${event.start}&count=${event.count}`) .then(function(res) { // 显示在云函数的服务端 console.log(res); return res; }) .catch(function(err) { console.error(err); }); }
/** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.getMovieList(); }, /** * 获取电影列表的数据 */ getMovieList() { // 加载框 wx.showLoading({ title: ‘加载中‘, }) wx.cloud.callFunction({ name: ‘movielist‘, data: { start: this.data.movieList.length, count: 10 }, success: res => { this.setData({ // 对返回的字符串进行解析,并且每次返回的数据应该拼接在原有数据的后面 movieList: this.data.movieList.concat(JSON.parse(res.result).subjects) }); // 隐藏加载框 wx: wx.hideLoading() }, fail: error => { wx.showToast({ title: ‘获取电影列表数据失败‘, }) } }) },
/** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { this.getMovieList() }
<button class="movie-comment" catchtap="onCatchSkipToComment" data-movieid="{{item.id}}">评价</button>
/** * 跳转评价的详情页面 */ onCatchSkipToComment(event) { // 跳转新页面,保留上一个页面 wx.navigateTo({ url: `../comment/comment?movieid=${event.target.dataset.movieid}`, }) },
/** * 页面的初始数据 */ data: { movieDetail : {} }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { // 获取上一个页面传过来的参数 this.getMovieDetail(options.movieid) }, /** * 获取电影详情信息 */ getMovieDetail(movieid) { wx.showLoading({ title: ‘加载中‘, }) wx.cloud.callFunction({ name: ‘getDetail‘, data: { movieid: movieid }, success: res => { this.setData({ movieDetail : JSON.parse(res.result) }) console.log(this.data.movieDetail) wx.hideLoading() }, fail: error => { console.log(error) } }) },
// 云函数入口文件 const cloud = require(‘wx-server-sdk‘) cloud.init() var rp = require(‘request-promise‘); // 云函数入口函数 exports.main = async (event, context) => { return rp(`http://api.douban.com/v2/movie/subject/${event.movieid}?apikey=0df993c66c0c636e29ecbb5344252a4a`) .then(function (res) { // 显示在云函数的服务端 console.log(res); return res; }) .catch(function (err) { console.error(err); }); }
3、遇到的问题
<view class=‘detail-container‘ style=‘background: url({{movieDetail.images.large}})‘></view> <view class=‘detail-mask‘></view>
.detail-container { height: 400rpx; filter: blur(40rpx); opacity: 0.4; } .detail-mask { position: absolute; width: 100%; height: 400rpx; background-color: #333; top: 0; left: 0; z-index: -1; }
原文:https://www.cnblogs.com/wxh0929/p/12050298.html