麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

小程序云開發(fā)如何實(shí)現(xiàn)圖片上傳及發(fā)表文字

2019-11-19 11:32:18
字體:
供稿:網(wǎng)友

微信給了我們存儲空間以及圖片上傳的功能,我們怎么可以輕易放過呢?

先看看界面:

二話不說, 先實(shí)現(xiàn)界面:

<!--pages/pulish/pulish.wxml--><view class='flexDownC'> <view class='flexDownC w100'>  <textarea class='input' bindinput='textInput' placeholder-style='margin-left:20rpx;' maxlength='500' placeholder='和大家一起分享你遇到的趣事,糗事吧' value='{{text}}' auto-focus auto-height></textarea> </view> <view class='btm flexSpaceBet w100 publishItem'> <image src='../../images/pic.png' class='pic' bindtap='doUpload'></image> <view class='pulishBtn' bindtap='pulish'> 發(fā)布</view>  </view> <!-- wx:for='{{imgUrl}}' --> <view class='flexCenter w100' wx:if='{{imgUrl}}'> <!-- <view wx:for='{{imgUrl}}' class='{{imgUrl.length == 1? "imgUrlClass1": imgUrl.length == 2? "imgUrlClass2": imgUrl.length == 3? "imgUrlClass3": "imgUrlClass4"}}' > -->  <image src='{{imgUrl}}' class='w100' mode="aspectFit" ></image> <!-- </view> -->  </view> <!-- <image class='w100' src='cloud://qiupihu-d1e452.7169-qiupihu-d1e452/1451.gif'></image> --></view>

wxss:

/* pages/pulish/pulish.wxss */.input{ font-size: 28rpx; color: #666; width: 100%; min-height: 60rpx; height: auto; border-radius: 10rpx; padding: 20rpx;}.fl{ display: flex; justify-content: flex-start;}.pic{ width: 64rpx; height: 64rpx; margin-left: 20rpx;}.w100{ width: 100%;}.publishItem{ margin-top: 80rpx; height: 80rpx; border-top: 1px solid #ea9518; border-bottom: 1px solid #ea9518;}.pulishBtn{ width: 100rpx; height: 50rpx; color: #fff; font-size: 28rpx; background: #ea9518; border-radius: 0.1; text-align: center; font-weight: bold; margin-right: 20rpx; line-height: 50rpx;}.h100{ height: 100rpx;}.imgUrlClass2{ width: 50%;}.imgUrlClass1{ width: 100%;}.imgUrlClass3{ width: 33%;}.imgUrlClass4{ width: 24%;}

接下來就是js的代碼了:

我們要求發(fā)布的人必須是已登錄狀態(tài),這個(gè)可以查看本地是否有username得知

// pages/pulish/pulish.jsvar app = getApp()const db = wx.cloud.database()const _ = db.command;Page({ /** * 頁面的初始數(shù)據(jù) */ data: { text: '', imgUrl: '', count: 0 }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function (options) { this.getCount()  }, onShow: function(){ let userOpenId = wx.getStorageSync('openId') if (!userOpenId) {  wx.showToast({  title: '您還未登錄,請先登錄~',  icon: 'none'  })  setTimeout(() => {  wx.switchTab({   url: '../me/me',  })  }, 1500) } else {  console.log(userOpenId) } }, getCount: function(){ //已輸入的字?jǐn)?shù) var that = this db.collection('funnys').count({  success: res => {  that.setData({   count: Number(res.total) + 1  })  } }) }, textInput: function(e){ this.setData({  text: e.detail.value }) }, pulish: function(){ var data = {  image: new Array(app.globalData.fileID), //將圖片儲存為數(shù)組類型  content: this.data.text, //用戶輸入的文字  comment: [],  userId: wx.getStorageSync('userId'),  username: wx.getStorageSync('username'), //用戶名  id: Number(this.data.count) +1, //是現(xiàn)在數(shù)據(jù)庫的條數(shù)+1,微信小程序的不知道怎么設(shè)置自增的數(shù)字字段  shareNum: 0,  commentNum: 0,  validStatus: 0,  validTime: 0 } //validStatus: 審核狀態(tài), 通過時(shí)候 +1, 反對時(shí)候-1 //validTime: 審核次數(shù), 最多5次,如果反對的人大于等于3,則不通過 console.log(data) if (data.content){  db.collection('funnys').add({   data: data,   success:res => {   wx.showToast({    title: '發(fā)布成功',   })   setTimeout(()=>{        wx.switchTab({    url: '../index/index',    })   }, 1000)   },   fail: e=>{   wx.showToast({    title: '發(fā)布錯(cuò)誤',   })   console.log(e)   }  }) }else{  wx.showToast({  title: '請?zhí)顚懳淖?,  icon: 'none'  }) } }, // 上傳圖片 //上傳的時(shí)候,我們可以獲得一個(gè)fileId,這個(gè)id我們必須存起來,在別人查看的時(shí)候,image的src使用的就是fileId,然后用戶必 //須得知道上傳的是哪張圖片呀, 所以我們使用的是本地的圖片路徑來展示,即imagePath  doUpload: function () { // 選擇圖片 var that = this; wx.chooseImage({  count: 1,  sizeType: ['compressed'],  sourceType: ['album', 'camera'],  success: function (res) {  wx.showLoading({   title: '上傳中',  })  const filePath = res.tempFilePaths[0]  that.setData({   imgUrl: filePath  })  // 上傳圖片  const cloudPath = that.data.count + filePath.match(//.[^.]+?$/)[0]  //改寫: 數(shù)組 多圖片  // const filePath = res.tempFilePaths, cloudPath = [];  // filePath.forEach((item, i)=>{  // cloudPath.push(that.data.count + '_' + i + filePath[i].match(//.[^.]+?$/)[0])  // })     console.log(cloudPath)  // filePath.forEach((item, i) => {   wx.cloud.uploadFile({   cloudPath,   filePath,   success: res => {    console.log('[上傳文件] 成功:', cloudPath, res)    app.globalData.fileID = res.fileID    app.globalData.cloudPath = cloudPath    app.globalData.imagePath = filePath       },   fail: e => {    console.error('[上傳文件] 失敗:', e)    wx.showToast({    icon: 'none',    title: '上傳失敗',    })   },   complete: () => {    wx.hideLoading()   }   })  // })  },  fail: e => {  console.error(e)  } }) }, /** * 用戶點(diǎn)擊右上角分享 */ onShareAppMessage: function () { }})

到此為止,功能就實(shí)現(xiàn)了。

那么,到此為止,點(diǎn)贊功能就基本完成了, 請看詳細(xì)代碼

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久人人爽人人爽人人片av免费 | 日本成人一区二区 | 一区二区三区在线观看免费 | 国产成人在线观看免费网站 | 久久亚洲国产精品 | 国产成人高清成人av片在线看 | 性欧美极品xxxx欧美一区二区 | 一本一道久久久a久久久精品91 | 天堂在线资源av | 九九热在线视频观看 | 国产精品久久久久久久久久久久午夜 | 精品免费在线视频 | 久久福利在线 | 亚洲人成在线播放网站 | 欧美四级在线观看 | 男女做性免费网站 | 中文字幕在线成人 | 免费在线观看成年人视频 | 黄色网络免费看 | 欧美一级鲁丝片免费看 | 成人免费在线网 | 爱性久久久久久久 | 在线播放av片 | 99视频网址 | 巨根插入| 久久久久免费精品国产小说色大师 | 国产欧美日韩视频在线观看 | 精品国产91久久久久 | 亚洲成人午夜精品 | 中国av免费在线观看 | 成人av一区二区免费播放 | 成人毛片av在线 | 中文字幕爱爱视频 | 免费a级片在线观看 | 国产精品久久久av | 国产免费最爽的乱淫视频a 毛片国产 | 中文字幕一区在线观看视频 | 午夜国产小视频 | 黄色网址在线视频 | 亚洲日本韩国在线观看 | 自拍亚洲伦理 |