1. 程式人生 > 其它 >微信小程式實現相簿展示及圖片預覽

微信小程式實現相簿展示及圖片預覽

十年河東,十年河西,莫欺少年窮

學無止境,精益求精

廢話不多說,先上效果圖

 

 

 原始碼如下:

import * as config from "../../../utils/config/config"
const app = getApp()
import * as request from "../../../utils/request/request.js"

Page({

  /**
   * 頁面的初始資料
   */
  data: {
    isHeightMode: true,
    appId: wx.getStorageSync('appId') || app.globalData.appId,
    imgUrl: config.config.swagger.imgUrl,
    produtctdtl: {},
    yynewspic: [],
    mediaList:[],
    licensePicHidden: 
true, cghtPicHidden: true, zfpwPicHidden: true, wthtPicHidden: true, bossmanPicHidden: true, productId: "" }, /** * 生命週期函式--監聽頁面載入 */ onLoad: function (options) { let that = this; let pid = options.uid; // that.GetProductDtl(pid); that.setData({ productId: pid, }) },
async GetProductDtl(uid) { let that = this; let produtctdtl = await request.request({ url: "/api/Product/ProductDetail?uid=" + uid, method: "GET" }); that.setData({ produtctdtl: produtctdtl.data }) if (produtctdtl.isSuccess && produtctdtl.data.uploads) { let yynewspic
= produtctdtl.data.uploads.filter(A => A.fileCate == 'yynewspic'); // let mediaList = []; for (var i = 0; i < yynewspic.length; i++) { let item = { url: yynewspic[i].path }; mediaList.push(item) } that.setData({ mediaList, }) } }, // 預覽圖片 previewImage(e) { let { sources, index } = e.currentTarget.dataset sources.filter(i => { i.type = i.type == 2 ? 'video' : 'image' i.poster = i.imgUrl }) wx.previewMedia({ sources, current: index }) }, // 視訊載入完成,改變視訊的寬高 videometa(e) { var that = this; //獲取系統資訊 wx.getSystemInfo({ success(res) { //視訊的高 var height = e.detail.height; //視訊的寬 var width = e.detail.width; //算出視訊的比例 var proportion = height / width; //res.windowWidth為手機螢幕的寬。 var windowWidth = res.windowWidth; //算出當前寬度下高度的數值 height = proportion * windowWidth; that.setData({ height, width: windowWidth }); } }) }, // 圖片載入後判斷圖片寬高比例 oneImageLoad(e) { const { width, height } = e.detail height >= width && this.setData({ isHeightMode: true }) }, /** * 生命週期函式--監聽頁面初次渲染完成 */ onReady: function () { }, /** * 生命週期函式--監聽頁面顯示 */ onShow: function () { }, /** * 生命週期函式--監聽頁面隱藏 */ onHide: function () { }, /** * 生命週期函式--監聽頁面解除安裝 */ onUnload: function () { }, /** * 頁面相關事件處理函式--監聽使用者下拉動作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函式 */ onReachBottom: function () { }, /** * 使用者點選右上角分享 */ onShareAppMessage: function () { } })

wxml

  <!-- 圖片和視訊 -->
  <navigationBar title="市場運營圖片" navigationtype="navigateTo" navigationurl="/pages/product/productDtl/productDtl?uid={{productId}}" hasback="{{true}}"
  bgc="#fff" color="#000"></navigationBar>
  <view class="img_box">
    <view class="many_img">
      <!-- 遍歷 -->
      <view
        wx:for="{{mediaList}}"
        wx:key="index"
        class="img_item many"
      >
      <image 
            class="img"
            src="{{item.url}}"
            bindtap="previewImage"
            data-sources="{{mediaList}}"
            data-index="{{index}}"
            mode="aspectFill"
          ></image>
     
      </view>
    </view>
  </view>

wxss

.img_box {
  margin-bottom: 12rpx;
  background-color: #fff;
  height: 90vh;
}

.many_img {
  display: flex;
  justify-self: start;
  flex-wrap: wrap;
  background-color: #fff;
}

.many {
  width: 45%;
  margin-left: 3%;
  margin-bottom: 16rpx;
  border-radius: 16rpx;
  overflow: hidden;
}

@天才臥龍的部落格