// pages/article/publish.js import request from '../../utils/request' Page({ /** * 页面的初始数据 */ data: { formats: {}, readOnly: false, placeholder: '开始输入...', editorHeight: 300, keyboardHeight: 0, isIOS: false, categoryList:[], currentTab: 0, layerHide:false, typeName:'', countPic:9,//上传图片最大数量 showImgUrl: "https://api.fireflysg.com", //路径拼接,一般上传返回的都是文件名, uploadImgUrl:'https://api.fireflysg.com/firefly/media-room/photo/upload',//图片的上传的路径 switch: false, categoryId:1,//文章分类 Id, imgList:[], title:'', content:'', achvTitleId:'' }, onEditorReady() { const that = this wx.createSelectorQuery().select('#editor').context(function (res) { that.editorCtx = res.context }).exec() }, readOnlyChange() { this.setData({ readOnly: !this.data.readOnly }) }, insertImage() { const that = this wx.chooseImage({ count: 1, success: function (res) { that.editorCtx.insertImage({ src: res.tempFilePaths[0], data: { id: 'abcd', role: 'god' }, width: '80%', success: function () { console.log('insert image success') } }) } }) }, submit(){ const that = this that.editorCtx.getContents({ success: function (res) { that.getInfo(res.html) } }) }, getInfo(){ let datas={ title:this.data.title, categoryId:this.data.categoryId, achvTitleId:this.data.achvTitleId, content: this.data.content, contentStyle: 1, display:this.data.switch?1:2, mediaList:this.data.imgList } request._post('/firefly/article/add/new',datas, res=>{ if(res.retCode == 0){ wx.hideLoading(); setTimeout(() => { wx.showToast({ title: '发布成功,获得'+res.body.points+'积分!', icon: 'none', duration:2000 }) }, 100); wx.switchTab({ url: '/pages/home/home', }) } }) }, getInputTitle(e){ this.setData({ title: e.detail.value }) }, getInputContent(e){ this.setData({ content: e.detail.value }) }, //监听组件事件,返回的结果 myEventListener:function(e){ console.log("上传的图片结果集合") console.log(e.detail.picsList) this.setData({ imgList:e.detail.picsList }) }, currentTab: function (e) { var categoryItem = e.currentTarget.dataset.item if (this.data.categoryId == categoryItem.id){ return; } this.setData({ categoryId: categoryItem.id, typeName: categoryItem.name, layerHide: false }) }, typeEvent(){ if(!this.data.achvTitleId){ this.setData({layerHide:true}) } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { // achvTitleId=3&categoryId=1 this.getcategory(options.categoryId) this.setData({ categoryId:options.categoryId||'', achvTitleId:options.achvTitleId||'' }) }, getcategory(id){ request._post('/firefly/category/list',{type:1}, res=>{ if(res.retCode == 0){ if(id){ var defaultinfo = res.body.filter(item=>item.id==id) } console.log(defaultinfo) this.setData({ categoryList:res.body, typeName: id?defaultinfo[0].name:'' }) } }) const platform = wx.getSystemInfoSync().platform const isIOS = platform === 'ios' this.setData({ isIOS}) const that = this this.updatePosition(0) let keyboardHeight = 0 wx.onKeyboardHeightChange(res => { if (res.height === keyboardHeight) return const duration = res.height > 0 ? res.duration * 1000 : 0 keyboardHeight = res.height setTimeout(() => { wx.pageScrollTo({ scrollTop: 0, success() { that.updatePosition(keyboardHeight) that.editorCtx.scrollIntoView() } }) }, duration) }) }, updatePosition(keyboardHeight) { const toolbarHeight = 50 const { windowHeight, platform } = wx.getSystemInfoSync() let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight this.setData({ editorHeight, keyboardHeight }) }, calNavigationBarAndStatusBar() { const systemInfo = wx.getSystemInfoSync() const { statusBarHeight, platform } = systemInfo const isIOS = platform === 'ios' const navigationBarHeight = isIOS ? 44 : 48 return statusBarHeight + navigationBarHeight }, // onEditorReady() { // const that = this // wx.createSelectorQuery().select('#editor').context(function (res) { // that.editorCtx = res.context // }).exec() // }, blur() { this.editorCtx.blur() }, format(e) { let { name, value } = e.target.dataset if (!name) return console.log('format', name, value) this.editorCtx.format(name, value) }, onStatusChange(e) { const formats = e.detail console.log(formats) this.setData({ formats }) }, insertDivider() { this.editorCtx.insertDivider({ success: function () { console.log('insert divider success') } }) }, clear() { this.editorCtx.clear({ success: function (res) { console.log("clear success") } }) }, removeFormat() { this.editorCtx.removeFormat() }, insertDate() { const date = new Date() const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}` this.editorCtx.insertText({ text: formatDate }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })