前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序富文本编辑器editor初体验-图片上传

微信小程序富文本编辑器editor初体验-图片上传

作者头像
hotqin888
发布2019-06-14 21:01:17
5.6K1
发布2019-06-14 21:01:17
举报
文章被收录于专栏:hotqin888的专栏hotqin888的专栏

https://developers.weixin.qq.com/miniprogram/dev/component/editor.html

以前没有小程序富文本编辑器,只能输入文字,图片上传后,在服务端进行拼接。现在好啦,直接上富文本,向里面传图片,修改图片大小。

主要是富文本里插入图片,选择图片,一个临时地址,预览地址,上传服务器后返回的图片地址。

在微信小程序基础库2.7以前(目前最新为2.7.1)是没有富文本编辑器的,只能通过textarea和图片选择器,将文本和图片在服务端进行组装。目前网络上也几乎没有一个完整的例子出来(因为今年5月份刚推出来)。

有了富文本编辑器,就可以愉快地发布图文了。 瞧它功能还是挺丰富,加粗,斜体,下划线,左中右对齐……背景色,标题,插入当前日期,事项,列表,

当然,  最主要还是图片上传(理论上是不是没有6幅图9幅图的数量限制啦?),点击图片,还可以删除,整个编辑区的删除请使用哪个回收站(垃圾桶)的图标……  当然,它传到服务端的带标签的html,解析还是依靠wxparse 珠三角设代小程序plus版已经整合完毕。

相比之前的文本+图片组装模式,这里可以给每个图片进行说明了。甚至给每个图片下方添加图片名称。当然,手机上操作这些稍嫌麻烦,用语音输入文字,速度就快很多了。

js: 

代码语言:javascript
复制
  insertImage() {
    const that = this
    wx.chooseImage({
      count: 6,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        const images = that.data.images.concat(res.tempFilePaths)
        console.log(res.tempFilePaths)
        that.data.images = images.length <= 6 ? images : images.slice(0, 6)
        $digest(that)
        const arr = []
        // console.log(that.data.images)
        for (let path of that.data.images) {
          arr.push(wxUploadFile({
            // url: config.urls.question + '/image/upload',
            url: config.url + '/wx/uploadwxeditorimg',
            filePath: path,
            name: 'file',
          }))
        }
        // console.log(arr)
        Promise.all(arr).then(res => {
          return res.map(item => JSON.parse(item.data).link)
        }).catch(err => {
          console.log(">>>> upload images error:", err)
        }).then(urls => {
          // console.log(urls)
          for (let i = 0; i < urls.length; ++i) {
            that.editorCtx.insertImage({
              src: config.attachmenturl + urls[i],
              // data: {
              //   id: 'abcd',
              //   role: 'god'
              // },
              success: function () {
                console.log('insert image success')
                that.setData({
                  images: []//这里清0,否则总是将上次的图片带上
                })
                // console.log(that.data.images)
              }
            })
          }
        })
      }
    })
  },
代码语言:javascript
复制
  handleContentInput(e) {
    const value = e.detail.html
    //要将图片的头http://*.*.*.去掉/at/g
    var reg = new RegExp(config.attachmenturl, "g")
    this.data.content = value.replace(reg, '');
  },

  submitForm(e) {
    var that = this
    const title = this.data.title
    const content = this.data.content
    if (title && content) {
      // 登录
      var sessionId = wx.getStorageSync('sessionId')
      //发起网络请求
      wx.request({
        url: config.url + '/wx/addwxeditorarticle',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        data: {
          hotqinsessionid: sessionId,
          title: title,
          content: content
        },
        success: function (res) {
          if (res.data.status == 0) {
            wx.showToast({
              title: res.data.info,
              icon: 'loading',
              duration: 1500
            })
          } else {
            wx.showToast({
              title: res.data.info, //这里打印出成功
              icon: 'success',
              duration: 1000
            })
            //进行清理
            that.editorCtx.clear({
              success: (res) => {
                console.log('succ:' + res)
              },
              fail: (res) => {
                console.log('fail:' + res)
              }
            })
            wx.navigateTo({
              url: `../detail/detail?id=` + res.data.id
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: "标题或正文不能为空!",
        icon: 'loading',
        duration: 1500
      })
    }
  },
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年05月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档