小程序之圖片的上傳却盘、刪除和預覽和視頻的上傳和刪除

最近在做一個小程序狰域,帖子中用到了一個關(guān)于文字、圖片和視頻的一些操作黄橘。

最終的樣式

未添加任何圖片和視頻

點擊視頻和圖片的任意一個上傳按鈕就會開始上傳

wx-1.png
添加圖片

這部分可以對圖片進行預覽和刪除

wx-2.png
添加視頻

視頻和刪除和播放

wx-3.png

這個可以實現(xiàn)輸入文字的統(tǒng)計和限制兆览,圖片的上傳、預覽和刪除塞关,視頻的上傳和刪除功能抬探。

如何實現(xiàn)上面的那些樣式呢?

大家可以先閱讀下面的文檔帆赢,會發(fā)現(xiàn)其實很簡單小压。

小程序關(guān)于圖片操作的api文檔:https://developers.weixin.qq.com/miniprogram/dev/api/wx.compressImage.html

小程序關(guān)于視頻操作的api文檔:https://developers.weixin.qq.com/miniprogram/dev/api/wx.saveVideoToPhotosAlbum.html

首先wxml文件

<view class="containor">
  <view class="publish_text_area">
    <!-- 標題 -->
    <view class="text_area_title">
      <input 
        class="title_input" 
        placeholder="請輸入標題" 
        maxlength="25" 
        placeholder-style="color:#b3b3b3;font-size:16px;" 
        bindinput="handleTitleInput"
        value="{{title}}"
      ></input>
      <!-- 標題字數(shù)限制 -->  
      <view 
        class="{{ titleCount < 25 ? 'title_input_counter' : 'title_input_error_counter' }}"
      >{{titleCount}}/25</view>
    </view>
    <!-- 內(nèi)容 -->
    <view class="text_area_content">
      <view class="area_content">
        <view class="area_content_out">
          <textarea 
            class="content-textarea" 
            placeholder="請輸入正文內(nèi)容..." 
            maxlength="255" 
            placeholder-style="color:#b3b3b3;font-size:12px;"
            style="height: 8rem" 
            bindinput="handleContentInput" 
            value="{{content}}"
          />
          <!-- 字數(shù)限制 -->  
          <view class="{{ contentCount < 255 ? 'content_textarea_counter' : 'content_textarea_error_counter'}}">{{contentCount}}/255</view>
        </view>
      </view>
    </view>
  </view>

  <view class="publish_imgs_area">
    <!-- 圖片 -->
    <view class="imgs_area" wx:for="{{images}}" wx:key="*this">
      <view class="iamge_item">
        <image 
          class="iamge_content" 
          src="{{item}}" 
          data-id="{{index}}"
          mode="aspectFill" 
          bindtap="previewIamge"
        />
        <image 
          class="iamge_cancel" 
          src="./images/cancel.png" 
          mode="aspectFill" 
          data-id="{{index}}"
          bindtap="deleteImage"
        />
      </view>
    </view>
    <!-- 視頻 -->
    <view class="video_area" wx:if="{{video != ''}}">
      <video
        class="video_item"
        src="{{video}}"
        controls
      ></video>
      <image 
        class="video_delete" 
        src="./images/video_del.png" 
        mode="aspectFill" 
        bindtap="videoDelete"
      />
    </view>
    <!-- 圖片上傳圖片按鈕 -->
    <view class="imgs_area" bindtap="chooseImage" wx:if="{{images.length < 9 && video == ''}}">
      <view class="iamge_item">
        <image class="iamge_content" src="./images/upload.png" mode="aspectFill" />
      </view>
    </view>
    <!-- 視頻上傳圖片按鈕 -->
    <view class="imgs_area" bindtap="chooseVideo" wx:if="{{video == '' &&  images.length == 0}}">
      <view class="iamge_item">
        <image class="iamge_content" src="./images/video.png" mode="aspectFill" />
      </view>
    </view>
  </view>
  <view class="btn_all_area">
    <button
      class="btn_area"
      type="primary"
      bindtap="submitClick"
    >提交</button>
    <button
      class="btn_area"
      type="warn"
      bindtap="resetClick"
    >重置</button>
  </view>
</view>

接下來wxss文件

page {
    background-color: #f1efef;
}

.containor {
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.publish_text_area {
    background-color: #ffffff;
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin:20rpx 20rpx 6rpx 20rpx;
    border-radius: 15rpx;
}

.text_area_title {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin: 10rpx;
}

.text_area_content {
    
}

.title_input {
    font-size: 30rpx;
    width:590rpx;
}

.title_input_counter {
    font-size:32rpx;
    color:#b3b3b3;
    margin-top:5rpx;
}

.title_input_error_counter {
    font-size:32rpx;
    color:#ce2f2f;
    margin-top:5rpx;
}

.area_content {
    border-top: 1rpx solid #f1efef;
    margin-left: 10rpx;
    margin-right: 10rpx;
}

.area_content_out {
    /* border-top: 1px solid #f1efef; */
    margin-top: 10rpx;
}

.content-textarea {
    width: 690rpx;
    font-size: 24rpx;
}

.content_textarea_counter {
    color:#d4d0d0;
    font-size:30rpx;
    text-align:right;
}

.content_textarea_error_counter {
    color:#ce2f2f;
    font-size:30rpx;
    text-align:right;
}
/* 圖片部分 */
.publish_imgs_area{
    background-color: #ffffff;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    flex-wrap: wrap;
    border-radius: 15rpx;
    margin: 20rpx;
    padding-left: 8rpx;
    padding-top: 8rpx;
    padding-bottom: 8rpx;
    /* height: 450rpx; */
}

.iamge_item {
    width: 225rpx;
    height: 225rpx;
    padding: 4rpx;
}

.iamge_content{
    width: 223rpx;
    height: 223rpx;
    border-radius: 15rpx;
    border: 1px solid #f1efef;
}

.iamge_cancel{
    width:40rpx;
    height:40rpx;
    border-radius:50%;
    position:relative;
    top:-259rpx;
    right:-204rpx;
    z-index:800;
}

/* 視頻部分樣式 */
.video_area {
    width: 700rpx;
    position: relative;
}

.video_item {
    width: 700rpx;
}
.video_delete {
    width: 50rpx;
    height: 50rpx;
    position: absolute;
    top: 10rpx;
    right: 2rpx;    
}

.btn_all_area {
    background-color: #ffffff;
    display: flex;
    flex-direction: row;
    justify-content: center;
    margin:20rpx 20rpx 6rpx 20rpx;
    border-radius: 15rpx;
    /* position: absolute;
    bottom: 0; */
}

.btn_area {
    width: 350rpx;
}

最后是Js文件

對于圖片的上傳,可以存在兩種情況:1椰于、第一次就上傳了九張圖片怠益;2、第一次不夠九張瘾婿,第二次接著上傳蜻牢。所有我們需要對這個條件就行判斷。

Page({
  /**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
    // 標題數(shù)
    titleCount: 0,
    // 詳情數(shù)
    contentCount: 0,
    // 標題內(nèi)容
    title: '',
    // 標題內(nèi)容
    content: '',
    // 圖片列表
    images: [],
    // 視頻
    video : '',
  },

  // 圖片操作的具體函數(shù)
  ImageOperator() {
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res =>  {
        // 上傳的圖片數(shù)據(jù)
        const imgList = res.tempFilePaths;
        // 原始的圖片數(shù)據(jù)
        const imageList = this.data.images;

        // 原來的圖片數(shù)量
        let imageLenght = imageList.length;
        // 當前的圖片數(shù)量
        let nowLenght = imgList.length;
        console.log(imageLenght);

        if ( imageLenght == 9 ) {
          console.log("數(shù)量已經(jīng)有9張憋他,請刪除在添加...");
        }
        if ( imageLenght < 9 ) {
          let images = [];
          // 獲取缺少的圖片張數(shù)
          let residue = 9 - imageLenght;
          // 如果缺少的張數(shù)大于當前的的張數(shù)  
          if ( residue >= nowLenght ) {
            // 直接將兩個數(shù)組合并為一個  
            images = imageList.concat(imgList);
          }else {
            // 否則截取當前的數(shù)組一部分  
            images = imageList.concat(imgList.slice(0, residue));
          }  
          this.setData({
            images
          })
        }
      }
    })
    
  },

  // 標題操作
  handleTitleInput(event) {
    let inputValue = event.detail.value;
    // 確保標題不存在空格  
    if(inputValue.lastIndexOf(" ") != -1){
      inputValue = inputValue.substring(0, inputValue.lastIndexOf(" "));
    }
    let titleCount = inputValue.length;
    if(titleCount <= 25){
      this.setData({
        titleCount: titleCount ,
        title: inputValue
      })
    }
  },
  // 內(nèi)容操作
  handleContentInput(event) {
    let textareaValue = event.detail.value;
    let contentCount = textareaValue.length;
    if(contentCount <= 255){
      this.setData({
        contentCount: contentCount,
        content: textareaValue
      })
    }
  },
  // 圖片獲取
  chooseImage() {
    if (this.data.images.length == 0) {
      wx.showToast({
        title: '視頻和圖片只能選擇上傳一種類型!',
        icon: 'none',
        duration: 2000,
        success: res => {
          this.ImageOperator()
        }
      })
    }else {
      this.ImageOperator()
    }
    
  },
  // 刪除圖片
  deleteImage(event) {
    //獲取數(shù)據(jù)綁定的data-id的數(shù)據(jù)
    const nowIndex = event.currentTarget.dataset.id;
    let images = this.data.images;
    images.splice(nowIndex, 1);
    this.setData({
      images
    })
  },
  // 預覽圖片
  previewIamge(event) {
    const nowIndex = event.currentTarget.dataset.id;
    const images = this.data.images;
    wx.previewImage({
      current: images[nowIndex],  //當前預覽的圖片
      urls: images,  //所有要預覽的圖片
    })
  },
  // 上傳視頻
  chooseVideo() {
    // 彈層  
    wx.showToast({
      title: '視頻和圖片只能選擇上傳一種類型!',
      icon: 'none',
      duration: 2000,
      success: res => {
        wx.chooseVideo({
          sourceType: ['album', 'camera'],
          compressed: true,
          maxDuration: 10,
          camera: 'back',
          success: res => {
            console.log(res);
            const video = res.tempFilePath;
            this.setData({video})
          }
        })
      }
    })
  },
  // 刪除視頻
  videoDelete(){
    wx.showModal({
      title: '警告',
      content: '確定要刪除該視頻嗎',
      success: res => {
        if (res.confirm) {
          this.setData({
            video: ''
          })
        }
      }
    })
  },
  // 表單提交事件
  submitClick() {

  },
  // 重置表單
  resetClick() {
    wx.showModal({
      title: '警告',
      content: '重置表單將需要重新上傳數(shù)據(jù)',
      success: res => {
        if (res.confirm) {
          this.setData({
            titleCount: 0,
            contentCount: 0,
            title: '',
            content: '',
            images: [],
            video : ''
          })
        }
      }
    })
  }
})

這樣就實現(xiàn)了孩饼,代碼還有很多需要優(yōu)化的地方,如果需要請自行斟酌使用竹挡!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末镀娶,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子揪罕,更是在濱河造成了極大的恐慌梯码,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件好啰,死亡現(xiàn)場離奇詭異轩娶,居然都是意外死亡,警方通過查閱死者的電腦和手機框往,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門鳄抒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事许溅∪勘牵” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵贤重,是天一觀的道長茬祷。 經(jīng)常有香客問我,道長并蝗,這世上最難降的妖魔是什么祭犯? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮滚停,結(jié)果婚禮上沃粗,老公的妹妹穿的比我還像新娘。我一直安慰自己铐刘,他們只是感情好陪每,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布影晓。 她就那樣靜靜地躺著镰吵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪挂签。 梳的紋絲不亂的頭發(fā)上疤祭,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天,我揣著相機與錄音饵婆,去河邊找鬼勺馆。 笑死,一個胖子當著我的面吹牛侨核,可吹牛的內(nèi)容都是我干的草穆。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼搓译,長吁一口氣:“原來是場噩夢啊……” “哼悲柱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起些己,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤豌鸡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后段标,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體涯冠,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年逼庞,在試婚紗的時候發(fā)現(xiàn)自己被綠了蛇更。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖派任,靈堂內(nèi)的尸體忽然破棺而出共耍,到底是詐尸還是另有隱情,我是刑警寧澤吨瞎,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布痹兜,位于F島的核電站,受9級特大地震影響颤诀,放射性物質(zhì)發(fā)生泄漏字旭。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一崖叫、第九天 我趴在偏房一處隱蔽的房頂上張望遗淳。 院中可真熱鬧,春花似錦心傀、人聲如沸屈暗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽养叛。三九已至,卻和暖如春宰翅,著一層夾襖步出監(jiān)牢的瞬間弃甥,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工汁讼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留淆攻,地道東北人。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓嘿架,卻偏偏與公主長得像瓶珊,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子耸彪,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 每天的學習記錄伞芹,可能有的地方寫的不對,因為剛學搜囱,以后發(fā)現(xiàn)錯的話會回來改掉整體流程 https://develope...
    有點健忘閱讀 4,672評論 0 7
  • 寫在前面 微信小程序出來也蠻久了丑瞧,經(jīng)過了市場的考驗,已經(jīng)站穩(wěn)腳跟蜀肘,融入到了各行各業(yè)绊汹,市場需求激增打來的是開發(fā)人員的...
    月夢佳期閱讀 1,690評論 1 1
  • 給提問的開發(fā)者的建議:提問之前先查詢 文檔、通過社區(qū)右上角搜索搜索已經(jīng)存在的問題扮宠。 寫一個簡明扼要的標題西乖,并且...
    極樂叔閱讀 13,443評論 0 3
  • 親愛的上帝: 我感覺非常累狐榔。也感覺自己確實像是站在懸崖邊上,昨天晚上的夢魘有增無減获雕,居然開始聽到另外一些精神在唱些...
    輕輕閱讀 162評論 0 1
  • 今天參加完廈門的培訓,搭上動車到了漳州楣颠,那是我本科母校的所在尽纽。和兩個大學同學見面,其中一位還帶了自己的未婚妻過來童漩,...
    大叔開始留胡子了閱讀 263評論 0 0