最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年05月17日 11:47:04   作者:JohnnyLiao_WJ  
這篇文章主要為大家詳細(xì)介紹了小程序云開發(fā)教程,如何實(shí)現(xiàn)圖片上傳及發(fā)表文字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

微信給了我們存儲(chǔ)空間以及圖片上傳的功能,我們?cè)趺纯梢暂p易放過呢?

先看看界面:

二話不說, 先實(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.js
var 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: '您還未登錄,請(qǐng)先登錄~',
  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), //將圖片儲(chǔ)存為數(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, 反對(duì)時(shí)候-1
 //validTime: 審核次數(shù), 最多5次,如果反對(duì)的人大于等于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: '請(qǐng)?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)贊功能就基本完成了, 請(qǐng)看詳細(xì)代碼

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JS面向?qū)ο笾噙x框?qū)崿F(xiàn)

    JS面向?qū)ο笾噙x框?qū)崿F(xiàn)

    這篇文章主要為大家詳細(xì)介紹了JS面向?qū)ο笾噙x框?qū)崿F(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • js算法中的排序、數(shù)組去重詳細(xì)概述

    js算法中的排序、數(shù)組去重詳細(xì)概述

    在js中實(shí)現(xiàn)數(shù)組排序,采用數(shù)組中sort方法實(shí)現(xiàn)還是比較簡(jiǎn)單的,下面有個(gè)不錯(cuò)的示例大家可以參考下
    2013-10-10
  • ymyang 繪圖 實(shí)例代碼

    ymyang 繪圖 實(shí)例代碼

    非常不錯(cuò)的ymyang 繪圖效果代碼。
    2009-04-04
  • 關(guān)于js datetime的那點(diǎn)事

    關(guān)于js datetime的那點(diǎn)事

    關(guān)于js datetime的一些使用經(jīng)驗(yàn)分享,想要了解datetime日期操作的朋友可以參考下。
    2011-11-11
  • 圖片懶加載插件實(shí)例分享(含解析)

    圖片懶加載插件實(shí)例分享(含解析)

    本文主要介紹了圖片懶加載插件,函數(shù)節(jié)流的應(yīng)用以及函數(shù)節(jié)流具體的好處、常用的場(chǎng)景。具有一定的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • 詳解小程序之簡(jiǎn)單登錄注冊(cè)表單驗(yàn)證

    詳解小程序之簡(jiǎn)單登錄注冊(cè)表單驗(yàn)證

    這篇文章主要介紹了小程序之簡(jiǎn)單登錄注冊(cè)表單驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 在JSP中如何實(shí)現(xiàn)MD5加密的方法

    在JSP中如何實(shí)現(xiàn)MD5加密的方法

    這篇文章主要介紹了在JSP中如何實(shí)現(xiàn)MD5加密的方法,較為詳細(xì)的分析了JSP采用MD5加密的功能、特點(diǎn)及實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下.
    2016-11-11
  • js設(shè)置和獲取自定義屬性的方法

    js設(shè)置和獲取自定義屬性的方法

    下面小編就為大家?guī)硪黄猨s設(shè)置和獲取自定義屬性的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-10-10
  • 借助云開發(fā)實(shí)現(xiàn)小程序短信驗(yàn)證碼的發(fā)送

    借助云開發(fā)實(shí)現(xiàn)小程序短信驗(yàn)證碼的發(fā)送

    這篇文章主要介紹了借助云開發(fā)實(shí)現(xiàn)小程序短信驗(yàn)證碼的發(fā)送,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • JavaScript 對(duì)Cookie 操作的封裝小結(jié)

    JavaScript 對(duì)Cookie 操作的封裝小結(jié)

    通過本篇,您能了解到: 匿名函數(shù) 閉包的產(chǎn)生 JavaScript實(shí)現(xiàn)private 以及 public 訪問權(quán)限 document.cookie 的操作
    2009-12-12

最新評(píng)論

巴林左旗| 荔浦县| 团风县| 股票| 颍上县| 衡阳县| 朝阳市| 乡宁县| 石景山区| 栾城县| 青铜峡市| 玛纳斯县| 佛学| 黔西县| 南江县| 潍坊市| 抚远县| 隆安县| 青河县| 威远县| 大名县| 永康市| 彭州市| 龙井市| 府谷县| 铜山县| 周口市| 宾阳县| 乌兰县| 静安区| 德州市| 大渡口区| 绍兴县| 清流县| 施甸县| 白水县| 吉安县| 石柱| 南汇区| 石楼县| 天气|