TS+Promise 包裹// export const useFile = async (originalUrl: string): Promise<PreviewState> => {// const state: PreviewState = reactive({// fileUrl: '',// contentType: '',// originalUrl: originalUrl,// loading: false,// });// if (originalUrl) {// state.loading = true;// const response = await previewFile(originalUrl);// const contentType = response.headers['content-type'];// const blob = new Blob([response.data], { type: contentType });// const url = window.URL.createObjectURL(blob);// state.contentType = contentType;// state.fileUrl = url;// state.loading = false;// }// return Promise.resolve(state);// };
1 小程序中页面跳转
封装
http请求var basesUrl ='http://47.108.197.28:4000';function http(url){ return new Promise((resolve,reject)=>{ wx.request({ url:basesUrl+url, method:"GET", dataType:"json", responseType:"text", success:(res)=>{ resolve(res) }, error:(err)=>{ reject(err) } }) })} 处理评分function handleStar(num){ var arr =[]; for(var i =0;i<5;i++){ if(num>=2){ arr.push(2) }else if(num>0){ arr.push(Number(num.toFixed(1))) }else{ arr.push(0) } num -= 2; } return arr}处理字符长度function handleStr(str){ if(str.length>6){ str = str.slice(0,6)+"..."}return str;}封装接口function getReadHttp(){ return http('/api/movie/top250')}function getReadDetail(id){ return http(`/api/movieDetail?id=${id}`)}module.exports = { http, handleStar, handleStr, getReadHttp, getReadDetail}
跳转页
#wxss<view> <image mode="widthFix" src="/images/music/b02.jpg"></image></view><view class="container"> <view class="item" bindtap="onClick" data-aid="{{item._id}}" wx:for="{{read}}" wx:key="id" > <image src="{{item.pic}}"></image> <view>{{item.title}}</view> <view class="star"> <block wx:for="{{item.star}}" wx:key="id"> <image wx:if="{{item>=1.5}}" src="/images/star.png"></image> <image wx:elif="{{item>=0.5}}" src="/images/ban-star.png"></image> <image wx:else src="/images/none-star.png"></image> </block> {{item.rating}} </view> </view></view>
#jsconst {handleStr,handleStar,getReadHttp} = require("../../utils/index")Page({ data:{ read:[] }, onLoad(){ getReadHttp().then(res=>{ this.handleReadData(res) }) }, handleReadData(res){ var read= [] res.data.res.forEach(item=>{ var {_id,pic,title,rating} =item; read.push({ _id, pic, title:handleStr(title), rating, star:handleStar(rating) }) }) this.setData({ read, }) }, onClick(event){ var {aid} = event.currentTarget.dataset; wx.navigateTo({ url: `/pages/detail/detail?id=${aid}`, }) }})
详情页
#wxss<view><image src="{{pic}}"></image> </view><view>{{title}}</view><view>{{slogo}}</view><view>{{msg}}</view>
#jsconst {getReadDetail} = require("../../utils/index")Page({ data: { pic:"", title:"", slogo:"", msg:"" }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var {id} = options; console.log(id); // var url = `http://47.108.197.28:4000/api/movieDetail?id=${id}` getReadDetail(id).then(res=>{ console.log(res); this.handleDetailData(res) }) }, handleDetailData(res){ var{pic,title,slogo} = res.data.res[0] console.log(pic); console.log(res.data.msg); this.setData({ pic, title, slogo, msg:res.data.msg }) }})