1. TS+Promise 包裹
  2. // export const useFile = async (originalUrl: string): Promise<PreviewState> => {
  3. // const state: PreviewState = reactive({
  4. // fileUrl: '',
  5. // contentType: '',
  6. // originalUrl: originalUrl,
  7. // loading: false,
  8. // });
  9. // if (originalUrl) {
  10. // state.loading = true;
  11. // const response = await previewFile(originalUrl);
  12. // const contentType = response.headers['content-type'];
  13. // const blob = new Blob([response.data], { type: contentType });
  14. // const url = window.URL.createObjectURL(blob);
  15. // state.contentType = contentType;
  16. // state.fileUrl = url;
  17. // state.loading = false;
  18. // }
  19. // return Promise.resolve(state);
  20. // };

1 小程序中页面跳转

封装

  1. http请求
  2. var basesUrl ='http://47.108.197.28:4000';
  3. function http(url){
  4. return new Promise((resolve,reject)=>{
  5. wx.request({
  6. url:basesUrl+url,
  7. method:"GET",
  8. dataType:"json",
  9. responseType:"text",
  10. success:(res)=>{
  11. resolve(res)
  12. },
  13. error:(err)=>{
  14. reject(err)
  15. }
  16. })
  17. })
  18. }
  19. 处理评分
  20. function handleStar(num){
  21. var arr =[];
  22. for(var i =0;i<5;i++){
  23. if(num>=2){
  24. arr.push(2)
  25. }else if(num>0){
  26. arr.push(Number(num.toFixed(1)))
  27. }else{
  28. arr.push(0)
  29. }
  30. num -= 2;
  31. }
  32. return arr
  33. }
  34. 处理字符长度
  35. function handleStr(str){
  36. if(str.length>6){
  37. str = str.slice(0,6)+"..."
  38. }
  39. return str;
  40. }
  41. 封装接口
  42. function getReadHttp(){
  43. return http('/api/movie/top250')
  44. }
  45. function getReadDetail(id){
  46. return http(`/api/movieDetail?id=${id}`)
  47. }
  48. module.exports = {
  49. http,
  50. handleStar,
  51. handleStr,
  52. getReadHttp,
  53. getReadDetail
  54. }

跳转页

  1. #wxss
  2. <view>
  3. <image mode="widthFix" src="/images/music/b02.jpg"></image>
  4. </view>
  5. <view class="container">
  6. <view class="item"
  7. bindtap="onClick"
  8. data-aid="{{item._id}}"
  9. wx:for="{{read}}"
  10. wx:key="id"
  11. >
  12. <image src="{{item.pic}}"></image>
  13. <view>{{item.title}}</view>
  14. <view class="star">
  15. <block wx:for="{{item.star}}" wx:key="id">
  16. <image wx:if="{{item>=1.5}}" src="/images/star.png"></image>
  17. <image wx:elif="{{item>=0.5}}" src="/images/ban-star.png"></image>
  18. <image wx:else src="/images/none-star.png"></image>
  19. </block>
  20. {{item.rating}}
  21. </view>
  22. </view>
  23. </view>
  1. #js
  2. const {handleStr,handleStar,getReadHttp} = require("../../utils/index")
  3. Page({
  4. data:{
  5. read:[]
  6. },
  7. onLoad(){
  8. getReadHttp().then(res=>{
  9. this.handleReadData(res)
  10. })
  11. },
  12. handleReadData(res){
  13. var read= []
  14. res.data.res.forEach(item=>{
  15. var {_id,pic,title,rating} =item;
  16. read.push({
  17. _id,
  18. pic,
  19. title:handleStr(title),
  20. rating,
  21. star:handleStar(rating)
  22. })
  23. })
  24. this.setData({
  25. read,
  26. })
  27. },
  28. onClick(event){
  29. var {aid} = event.currentTarget.dataset;
  30. wx.navigateTo({
  31. url: `/pages/detail/detail?id=${aid}`,
  32. })
  33. }
  34. })

详情页

  1. #wxss
  2. <view><image src="{{pic}}"></image> </view>
  3. <view>{{title}}</view>
  4. <view>{{slogo}}</view>
  5. <view>{{msg}}</view>
  1. #js
  2. const {getReadDetail} = require("../../utils/index")
  3. Page({
  4. data: {
  5. pic:"",
  6. title:"",
  7. slogo:"",
  8. msg:""
  9. },
  10. /**
  11. * 生命周期函数--监听页面加载
  12. */
  13. onLoad: function (options) {
  14. var {id} = options;
  15. console.log(id);
  16. // var url = `http://47.108.197.28:4000/api/movieDetail?id=${id}`
  17. getReadDetail(id).then(res=>{
  18. console.log(res);
  19. this.handleDetailData(res)
  20. })
  21. },
  22. handleDetailData(res){
  23. var{pic,title,slogo} = res.data.res[0]
  24. console.log(pic);
  25. console.log(res.data.msg);
  26. this.setData({
  27. pic,
  28. title,
  29. slogo,
  30. msg:res.data.msg
  31. })
  32. }
  33. })