|
| 1 | +'use strict'; |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
| 4 | +import Base from './base.js'; |
| 5 | + |
| 6 | +export default class extends Base { |
| 7 | + /** |
| 8 | + * index action |
| 9 | + * @return {Promise} [] |
| 10 | + */ |
| 11 | + async indexAction(){ |
| 12 | + this.assign("title","新建主题"); |
| 13 | + let uinfo=await this.session('uInfo'); |
| 14 | + if(!think.isEmpty(uinfo)){ |
| 15 | + let upic=await this.model("topic").getUserPic({name:uinfo.name}); |
| 16 | + let itemList=await this.model("topic").findAll("topic_item"); |
| 17 | + this.assign('itemList',itemList); |
| 18 | + this.assign('uinfo',uinfo); |
| 19 | + this.assign('upic',upic.pic); |
| 20 | + return this.displayView("create_index"); |
| 21 | + }else { |
| 22 | + return this.redirect('/login.html'); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + async doaddAction(){ //编辑或者新增主题 |
| 27 | + let mycreatetime=think.datetime(this.post('createtime')); |
| 28 | + let updatetime=think.datetime(this.post('updatetime')); |
| 29 | + let data=await this.post(); |
| 30 | + data.createtime=mycreatetime; |
| 31 | + data.updatetime=updatetime; |
| 32 | + if(!think.isEmpty(this.post("id"))){ |
| 33 | + let rs=await this.model("topic").updateRecord("topic",{id:data.id},data); |
| 34 | + if(rs) return this.success(); |
| 35 | + }else{ |
| 36 | + let rs=await this.model("topic").addRecord("topic",data); |
| 37 | + let points=await this.model("topic").increpoint({name:data.author},this.config('point.addtopic')); |
| 38 | + if(rs) return this.success(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + async editAction(){ |
| 43 | + this.assign("title","编辑主题"); |
| 44 | + let myid=this.get("tid"); |
| 45 | + let uinfo=await this.session('uInfo'); |
| 46 | + if(!think.isEmpty(uinfo)){ |
| 47 | + let topicInfo=await this.model("topic").findOne("topic",{id:myid,author:uinfo.name}); |
| 48 | + if(!think.isEmpty(topicInfo)){ |
| 49 | + let upic=await this.model("topic").getUserPic({name:uinfo.name}); |
| 50 | + this.assign('upic',upic.pic); |
| 51 | + let itemList=await this.model("topic").findAll("topic_item"); |
| 52 | + this.assign('uinfo',uinfo); |
| 53 | + this.assign("topicInfo",topicInfo) |
| 54 | + this.assign("itemList",itemList) |
| 55 | + return this.displayView("create_edit"); |
| 56 | + }else { |
| 57 | + return this.displayView("../common/error_404"); |
| 58 | + } |
| 59 | + }else { |
| 60 | + return this.redirect('/login.html'); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + async savereplyAction(){ |
| 65 | + //编辑或者新增回复 |
| 66 | + let marked = require('marked'); |
| 67 | + marked.setOptions({ |
| 68 | + renderer: new marked.Renderer(), |
| 69 | + gfm: true, |
| 70 | + tables: true, |
| 71 | + breaks: true, |
| 72 | + pedantic: false, |
| 73 | + sanitize: true, |
| 74 | + smartLists: true, |
| 75 | + smartypants: false |
| 76 | + }); |
| 77 | + let mycreatetime=think.datetime(this.post('createtime')); |
| 78 | + let data=await this.post(); |
| 79 | + data.createtime=mycreatetime; |
| 80 | + if(data.text===''){ |
| 81 | + return this.json({status:0,errno:1,errmsg:'回复不能为空!'}); |
| 82 | + }else{ |
| 83 | + // 解析markdown |
| 84 | + let html=marked(data.comment); |
| 85 | + // data.comment=html; |
| 86 | + if(!think.isEmpty(data.id)){ |
| 87 | + //编辑 |
| 88 | + let isexist=await this.model("topic").findOne("topic_comment",{id:data.id}); |
| 89 | + let tid=await this.model("topic").findOne("topic",{id:data.tid}); |
| 90 | + if(!think.isEmpty(isexist)&&!think.isEmpty(tid)){ |
| 91 | + //更新最后回复数据 |
| 92 | + let updata={ |
| 93 | + updatetime:mycreatetime, |
| 94 | + updateauthor:data.author, |
| 95 | + updatepic:data.pic |
| 96 | + } |
| 97 | + let updatetime=await this.model("topic").updateRecord("topic",{id:data.tid},updata); |
| 98 | + //更新最后回复数据 |
| 99 | + // 更新回复 |
| 100 | + let rs=await this.model("topic").updateRecord("topic_comment",{id:data.id},data); |
| 101 | + if(rs) return this.success(); |
| 102 | + }else{ |
| 103 | + return this.fail("该主题或回复不存在或已删除!"); |
| 104 | + } |
| 105 | + }else{ |
| 106 | + //更新最后回复数据 |
| 107 | + let updata={ |
| 108 | + updatetime:mycreatetime, |
| 109 | + updateauthor:data.author, |
| 110 | + updatepic:data.pic |
| 111 | + } |
| 112 | + let updatetime=await this.model("topic").updateRecord("topic",{id:data.tid},updata); |
| 113 | + //更新最后回复数据 |
| 114 | + //增加 |
| 115 | + let rs=await this.model("topic_comment").add(data); |
| 116 | + // 增加积分 |
| 117 | + let points=await this.model("topic").increpoint({name:data.author},this.config('point.addcomment')); |
| 118 | + // 增加回复数 |
| 119 | + let replycount=await this.model("topic").where({id:data.tid}).increment('replycount',1); |
| 120 | + if(rs) return this.success(); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + async postlikeAction(){ |
| 126 | + let data=await this.post(); |
| 127 | + let liker=data.likers; |
| 128 | + let myid=data.id; |
| 129 | + if(!think.isEmpty(myid)){ |
| 130 | + let item=await this.model("topic").findOne("topic_comment",{id:myid}); |
| 131 | + let arr=(!item.likers)?[]:(item.likers).split(","); |
| 132 | + let likers=arr||[]; |
| 133 | + let n=likers.indexOf(liker); |
| 134 | + if(n<0){ |
| 135 | + likers.push(liker); |
| 136 | + let newlikers=likers.join(","); |
| 137 | + let m=likers.length; |
| 138 | + let rs=await this.model("topic_comment").where({id:myid}).update({like:m,likers:newlikers}); |
| 139 | + if(rs) return this.success({likeCount:m}); |
| 140 | + }else{ |
| 141 | + likers.splice(n,1); |
| 142 | + let m=likers.length; |
| 143 | + let newlikers=likers.join(","); |
| 144 | + let rs=await this.model("topic_comment").where({id:myid}).update({like:m,likers:newlikers}); |
| 145 | + if(rs) return this.success({likeCount:m}); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + async removereplyAction(){ |
| 151 | + let data=await this.post(); |
| 152 | + let myid=data.id; |
| 153 | + if(!think.isEmpty(myid)){ |
| 154 | + let rs=await this.model("topic_comment").where({id:myid}).delete(); |
| 155 | + // 减少回复数 |
| 156 | + let replycount=await this.model("topic").where({id:data.tid}).decrement('replycount',1); |
| 157 | + if(rs) return this.success(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + async removeitemAction(){ |
| 162 | + let data=await this.post(); |
| 163 | + let myid=data.id; |
| 164 | + if(!think.isEmpty(myid)){ |
| 165 | + let rs=await this.model("topic").where({id:myid}).delete(); |
| 166 | + if(rs) return this.success(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + //上传图片接口 |
| 171 | + async uploadeditorAction() |
| 172 | + { |
| 173 | + let IS_USE_OSS=think.config('OSS.on'); |
| 174 | + if(IS_USE_OSS){ |
| 175 | + //上传OSS图片接口 |
| 176 | + let ALIOSS = think.service("alioss"); |
| 177 | + let alioss = new ALIOSS(); |
| 178 | + let file = think.extend({}, this.file('img')); |
| 179 | + let rs=await alioss.upload(file); |
| 180 | + if(rs){ |
| 181 | + return this.json(think.config('OSS.domain')+"/"+rs.name); |
| 182 | + }else{ |
| 183 | + return this.json("上传失败!"); |
| 184 | + } |
| 185 | + }else{ |
| 186 | + //上传应用服务器图片接口 |
| 187 | + let file = think.extend({}, this.file('img')); |
| 188 | + let filepath = file.path; |
| 189 | + let newpath = liFormatDate(new Date().toLocaleDateString()); |
| 190 | + let uploadPath = think.UPLOAD_PATH + '/pics/' + newpath; |
| 191 | + think.mkdir(uploadPath); |
| 192 | + let basename = path.basename(filepath); |
| 193 | + fs.renameSync(filepath, uploadPath + basename); |
| 194 | + this.json("/static/upload/pics/" + newpath + basename); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments