With cloud publishing and development to achieve real show Shu applet circle of friends

Knowledge skill points

  • 1, a small cloud development program
  • 2, the applet cloud storage
  • 3, the applet cloud database
  • 4, preview the picture to enlarge
  • 5, select and delete pictures

Draw a flow chart released give


Here is what we really keep the data in the database.

Then we just need to request data in the database in the circle of friends page, and then show it to the page as shown below

so we next come to realize the function publish and display

Published circle of friends

First, you must first create a small program project

Here is not to say, note: you must use their own appid, so you need to register a small program (individual line)

Second, create a publication page

We publish page layout is relatively simple, a text input box, a picture display area, a release button.

First release page layout wxml posted

<textarea class="desc" placeholder="请输入内容" bindinput="getInput" />
<view class="iamgeRootAll">
  <view class="imgRoot" wx:for="{{imgList}}" wx:key="{{index}}" bindtap="ViewImage" data-url="{{imgList[index]}}">
    <view wx:if="{{imgList.length==(index+1)&& imgList.length<8}}" class="imgItem" bindtap="ChooseImage">
      <image class="photo" src="../../images/photo.png"></image>
    </view>
    <view wx:else class="imgItem" data-index="{{index}}">
      <image class="img" src='{{item}}' mode='aspectFill'></image>
      <image class="closeImg" bindtap="DeleteImg" src="../../images/close.png" data-index="{{index}}"></image>
    </view>
  </view>
  <!-- 一开始用来占位 -->
  <view wx:if="{{imgList.length==0}}" class="imgItem" bindtap="ChooseImage">
    <image class="photo" src="../../images/photo.png"></image>
  </view>
</view>

<button type="primary" bindtap="publish">发布朋友圈</button>

The only difficulty here is that the distribution of the images below, the number of pictures because we choose each time the user is not fixed, which considered the situation minute to go.
wx: if = "{{imgList.length == (index + 1) && imgList.length <8}}" This code is used to control the number ➕ we release the hidden display.

This ➕ numbers have the following three conditions need to be considered

  • 1, when not added any images to show only ➕ No.
  • 2, there are pictures, but when less than 8, we need to show pictures plus sign.
  • 3, there are eight pictures, the plus sign will hide.
    Carefully look above wxml the code, the code is reflected.

Third, to achieve the selection and display pictures

Pictures of choice is simple, to use the official api can be. Codes are as follows

  //选择图片
  ChooseImage() {
    wx.chooseImage({
      count: 8 - this.data.imgList.length, //默认9,我们这里最多选择8张
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        console.log("选择图片成功", res)
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },

这里单独说明下 8 - this.data.imgList.length。因为我这里规定最多只能上传8张图片。所以用了count8 ,至于后面为什么要减去this.data.imgList.length。主要是我们用户不一定一次选择8张图片,有可能第一次选择2张,第二次选择2张。。。
所以我们做选择时,每次传入的数量肯定不一样的。而这个imgList.length就是用户已经选择的图片个数。用8减去已选择的个数,就是下次最多能选择的了。

上面代码在选择成功后,会生成一个临时的图片链接。如下图所示,这个链接既可以用来展示我们已经选择的图片,后面的图片上传也要用到。

四,实现图片删除功能

我们每张图片的右上角有个删除按钮,点击删除按钮可以实现图片的删除。

这里比较简单,把代码贴给大家

  //删除图片
  DeleteImg(e) {
    wx.showModal({
      title: '要删除这张照片吗?',
      content: '',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },

五,发布功能

  • 1,发布之前我们需要先校验下内容和图片是否为空
  • 2,由于我们发布的时候要保证所有的图片都要上传成功,所以这里我们这么处理。
  const promiseArr = []
    //只能一张张上传 遍历临时的图片数组
    for (let i = 0; i < this.data.imgList.length; i++) {
      let filePath = this.data.imgList[i]
      let suffix = /\.[^\.]+$/.exec(filePath)[0]; // 正则表达式,获取文件扩展名
      //在每次上传的时候,就往promiseArr里存一个promise,只有当所有的都返回结果时,才可以继续往下执行
      promiseArr.push(new Promise((reslove, reject) => {
        wx.cloud.uploadFile({
          cloudPath: new Date().getTime() + suffix,
          filePath: filePath, // 文件路径
        }).then(res => {
          // get resource ID
          console.log("上传结果", res.fileID)
          this.setData({
            fileIDs: this.data.fileIDs.concat(res.fileID)
          })
          reslove()
        }).catch(error => {
          console.log("上传失败", error)
        })
      }))
    }
    //保证所有图片都上传成功
    Promise.all(promiseArr).then(res => {
    //图片上传成功了,才会执行到这。。。
    })

我们这里用Promise来确保所有的图片都上传成功了,才执行后面的操作。

把完整的发布代码贴给大家吧

/**
 * 编程小石头
 * wehchat:2501902696
 */
let app = getApp();
Page({
  data: {
    imgList: [],
    fileIDs: [],
    desc: ''
  },

  //获取输入内容
  getInput(event) {
    console.log("输入的内容", event.detail.value)
    this.setData({
      desc: event.detail.value
    })
  },


  //选择图片
  ChooseImage() {
    wx.chooseImage({
      count: 8 - this.data.imgList.length, //默认9,我们这里最多选择8张
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        console.log("选择图片成功", res)
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  //删除图片
  DeleteImg(e) {
    wx.showModal({
      title: '要删除这张照片吗?',
      content: '',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },

  //上传数据
  publish() {
    let desc = this.data.desc
    let imgList = this.data.imgList
    if (!desc || desc.length < 6) {
      wx.showToast({
        icon: "none",
        title: '内容大于6个字'
      })
      return
    }
    if (!imgList || imgList.length < 1) {
      wx.showToast({
        icon: "none",
        title: '请选择图片'
      })
      return
    }
    wx.showLoading({
      title: '发布中...',
    })

    const promiseArr = []
    //只能一张张上传 遍历临时的图片数组
    for (let i = 0; i < this.data.imgList.length; i++) {
      let filePath = this.data.imgList[i]
      let suffix = /\.[^\.]+$/.exec(filePath)[0]; // 正则表达式,获取文件扩展名
      //在每次上传的时候,就往promiseArr里存一个promise,只有当所有的都返回结果时,才可以继续往下执行
      promiseArr.push(new Promise((reslove, reject) => {
        wx.cloud.uploadFile({
          cloudPath: new Date().getTime() + suffix,
          filePath: filePath, // 文件路径
        }).then(res => {
          // get resource ID
          console.log("上传结果", res.fileID)
          this.setData({
            fileIDs: this.data.fileIDs.concat(res.fileID)
          })
          reslove()
        }).catch(error => {
          console.log("上传失败", error)
        })
      }))
    }
    //保证所有图片都上传成功
    Promise.all(promiseArr).then(res => {
      wx.cloud.database().collection('timeline').add({
        data: {
          fileIDs: this.data.fileIDs,
          date: app.getNowFormatDate(),
          createTime: db.serverDate(),
          desc: this.data.desc,
          images: this.data.imgList
        },
        success: res => {
          wx.hideLoading()
          wx.showToast({
            title: '发布成功',
          })
          console.log('发布成功', res)
          wx.navigateTo({
            url: '../pengyouquan/pengyouquan',
          })
        },
        fail: err => {
          wx.hideLoading()
          wx.showToast({
            icon: 'none',
            title: '网络不给力....'
          })
          console.error('发布失败', err)
        }
      })
    })
  },
})

到这里我们发布的功能就实现了,发布功能就如下面这个流程图所示。

我们最终的目的是要把文字和图片链接存到云数据库。把图片文件存到云存储。这就是云开发的方便之处,不用我们编写后台代码,就可以轻松实现后台功能。

接下来讲朋友圈展示页。


这个页面主要做的就是

  • 1,从云数据库读取数据
  • 2,展示列表数据

1,读取数据

这里读取数据挺简单,就是从云数据库读数据,这里我们做了一个排序,就是最新发布的数据在最上面。代码如下

wx.cloud.database().collection('timeline')
      .orderBy('createTime', 'desc') //按发布视频排序
      .get({
        success(res) {
          console.log("请求成功", res)
          that.setData({
            dataList: res.data
          })
        },
        fail(res) {
          console.log("请求失败", res)
        }
      })

云数据库的读取也比较简单,有疑问的同学,可参见官方文档。

2,朋友圈列表的展示

这里也比较简单,直接把布局代码贴给大家。dataList就是我们第一步请求到的数据。

<block wx:for="{{dataList}}" wx:key="index">
  <view class="itemRoot">
    <view>
      <text class="desc">{{item.desc}}</text>
    </view>
    <view class="imgRoot">
      <block class="imgList" wx:for="{{item.fileIDs}}" wx:for-item="itemImg" wx:key="index">
        <image class="img" src='{{itemImg}}' mode='aspectFill' data-img='{{[itemImg,item.fileIDs]}}' bindtap="previewImg"></image>
      </block>
    </view>
  </view>
</block>

3,这里还有一个图片预览的功能

功能实现很简单就下面几行代码,但是我们从wxml获取组件上的数据时比较麻烦。

  // 预览图片
  previewImg: function(e) {
    let imgData = e.currentTarget.dataset.img;
    console.log("eeee", imgData[0])
    console.log("图片s", imgData[1])
    wx.previewImage({
      //当前显示图片
      current: imgData[0],
      //所有图片
      urls: imgData[1]
    })
  },

4,点击图片时通过 data- 获取图片列表和当前图片数据

我们点击组件时,可以通过data- 传递数据,但是一个点击如果像传多条数据呢。这时候可以用 data-xxx='{{[xxx,xxx]}}' 来传递数据了。如下代码

<block wx:for="{{item.fileIDs}}" wx:key="item2" wx:for-item="item2">
     <image  src='{{item2}}' data-img='{{[item2,item.fileIDs]}}' mode='aspectFill' bindtap="previewImg"></image>
</block>

//我们再js里可以接收两个数据
previewImg: function(e) {
    let imgData = e.currentTarget.dataset.img;
    console.log("item2", imgData[0])
    console.log("item.fileIDs", imgData[1])
    //大图预览
    wx.previewImage({
      //当前显示图片
      current: imgData[0],
      //所有图片
      urls: imgData[1]
    })
},

上面代码就可以实现,一次点击,通过data- 传递多个数据到js里。

到这里我们就完整的实现了,朋友圈的发布与展示了


朋友圈展示的比较简陋,后期再抽时间做美化吧。

源码地址

https://github.com/TencentCloudBase/Good-practice-tutorial-recommended


如果你想要了解更多关于云开发CloudBase相关的技术故事/技术实战经验,请扫码关注【腾讯云云开发】公众号~

Guess you like

Origin www.cnblogs.com/CloudBase/p/11719075.html