Article.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. namespace app\admin\controller\article;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\SystemAttachment;
  5. use app\admin\model\enterprise\EnterPriseUser;
  6. use app\models\user\InterestUser;
  7. use crmeb\services\{JsonService, UtilService as Util, JsonService as Json, UtilService};
  8. use app\admin\model\article\{ArticleCategory as ArticleCategoryModel, Article as ArticleModel, ArticleReply};
  9. use crmeb\services\FormBuilder as Form;
  10. use think\facade\Route as Url;
  11. /**
  12. * 图文管理
  13. * Class WechatNews
  14. * @package app\admin\controller\wechat
  15. */
  16. class Article extends AuthController
  17. {
  18. /**
  19. * TODO 显示后台管理员添加的图文
  20. * @return mixed
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. */
  25. public function index()
  26. {
  27. $where = Util::getMore([
  28. ['title', ''],
  29. ['cid', $this->request->param('pid', '')],
  30. ], $this->request);
  31. $where['merchant'] = $this->adminInfo['mer_id'];//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  32. $tree = sort_list_tier(ArticleCategoryModel::getArticleCategoryList());
  33. $this->assign(compact('tree'));
  34. $this->assign('mer_id', $this->adminInfo['mer_id']);
  35. $this->assign('where', $where);
  36. $this->assign(ArticleModel::getAll($where));
  37. return $this->fetch();
  38. }
  39. /**
  40. * TODO 文件添加和修改
  41. * @return mixed
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. * @throws \think\exception\DbException
  45. */
  46. public function create()
  47. {
  48. $id = $this->request->param('id');
  49. $cid = $this->request->param('cid');
  50. $news = [];
  51. $all = [];
  52. $news['id'] = '';
  53. $news['image_input'] = '';
  54. $news['title'] = '';
  55. $news['author'] = '';
  56. $news['is_banner'] = '';
  57. $news['is_hot'] = '';
  58. $news['content'] = '';
  59. $news['synopsis'] = '';
  60. $news['url'] = '';
  61. $news['cid'] = [];
  62. $news['mer_id'] = [];
  63. $news['mer'] = [];
  64. $news['add_time']='';
  65. $mer_id = $this->adminInfo['mer_id'];
  66. $select = 0;
  67. if ($id) {
  68. $news = ArticleModel::where('n.id', $id)->alias('n')->field('n.*,c.content')->join('ArticleContent c', 'c.nid=n.id', 'left')->find();
  69. if (!$news) return $this->failed('数据不存在!');
  70. $news['image_input'] = explode(',', $news['image_input']);
  71. $news['cid'] = explode(',', $news['cid']);
  72. // 通过文章ID获取文章企业名
  73. $news['add_time']=date("Y-m-d H:i:s",$news['add_time']);
  74. $news['rename'] =EnterPriseUser::where('id',$news['mer_id'])->value('name');
  75. $news['mer_id'] = explode(',', $news['mer_id']);
  76. $news['content'] = htmlspecialchars_decode($news['content']);
  77. }
  78. if ($cid && in_array($cid, ArticleCategoryModel::getArticleCategoryInfo(0, 'id'))) {
  79. $all = ArticleCategoryModel::getArticleCategoryInfo($cid);
  80. $select = 1;
  81. }
  82. if (!$select) {
  83. $list = ArticleCategoryModel::getTierList();
  84. foreach ($list as $menu) {
  85. $all[$menu['id']] = $menu['html'] . $menu['title'];
  86. }
  87. }
  88. $this->assign('all', $all);
  89. $this->assign('news', $news);
  90. $this->assign('mer_id', $mer_id);
  91. $this->assign('cid', $cid);
  92. $this->assign('select', $select);
  93. return $this->fetch();
  94. }
  95. public function preview()
  96. {
  97. return $this->create();
  98. }
  99. /**
  100. * 上传图文图片
  101. * @return \think\response\Json
  102. */
  103. public function upload_image()
  104. {
  105. $res = Upload::instance()->setUploadPath('wechat/image/' . date('Ymd'))->image($_POST['file']);
  106. if (!is_array($res)) return Json::fail($res);
  107. SystemAttachment::attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 5, $res['image_type'], $res['time'], 1, $this->adminInfo['mer_id']);
  108. return Json::successful('上传成功!', ['url' => $res['dir']]);
  109. }
  110. /**
  111. * 添加和修改图文
  112. */
  113. public function add_new()
  114. {
  115. $data = Util::postMore([
  116. ['id', 0],
  117. ['cid', []],
  118. ['mer',0],
  119. 'title',
  120. 'author',
  121. 'content',
  122. 'synopsis',
  123. 'share_title',
  124. 'share_synopsis',
  125. ['visit', 0],
  126. ['sort', 0],
  127. 'url',
  128. ['is_banner', 0],
  129. 'add_time',
  130. ['is_hot', 0],
  131. ['status', 1],]);
  132. $data['image_input'] = implode(',', $this->request->post('image_input'));
  133. $data['cid'] = implode(',', $data['cid']);
  134. $content = $data['content'];
  135. $data['add_time'] =strtotime($data['add_time']);
  136. //判断是新增还是修改
  137. if($this->adminInfo['mer_id']==0){
  138. $data['is_check'] =1;
  139. }else{
  140. $data['is_check'] =0;
  141. }
  142. unset($data['content']);
  143. if ($data['id']) {
  144. $id = $data['id'];
  145. unset($data['id']);
  146. $res = false;
  147. ArticleModel::beginTrans();
  148. $res1 = ArticleModel::edit($data, $id, 'id');
  149. $res2 = ArticleModel::setContent($id, $content);
  150. if ($res1 && $res2) {
  151. $res = true;
  152. }
  153. ArticleModel::checkTrans($res);
  154. if ($res)
  155. return Json::successful('修改图文成功!', $id);
  156. else
  157. return Json::fail('修改图文失败,您并没有修改什么!', $id);
  158. } else {
  159. $data['mer_id'] = $this->adminInfo['mer_id'];
  160. $data['admin_id'] = $this->adminId;
  161. //判断是否是总后台添加的文章
  162. if($data['mer_id']==0){
  163. $data['is_check'] =1;
  164. }else{
  165. $data['is_check'] =0;
  166. }
  167. $res = false;
  168. ArticleModel::beginTrans();
  169. $res1 = ArticleModel::create($data);
  170. $res2 = false;
  171. if ($res1)
  172. $res2 = ArticleModel::setContent($res1->id, $content);
  173. if ($res1 && $res2) {
  174. $res = true;
  175. }
  176. ArticleModel::checkTrans($res);
  177. if ($res)
  178. return Json::successful('添加图文成功!', $res1->id);
  179. else
  180. return Json::successful('添加图文失败!', $res1->id);
  181. }
  182. }
  183. /**
  184. * 删除图文
  185. * @param $id
  186. * @return \think\response\Json
  187. */
  188. public function delete($id)
  189. {
  190. $res = ArticleModel::del($id);
  191. if (!$res)
  192. return Json::fail('删除失败,请稍候再试!');
  193. else
  194. return Json::successful('删除成功!');
  195. }
  196. public function merchantIndex()
  197. {
  198. $where = Util::getMore([
  199. ['title', '']
  200. ], $this->request);
  201. $this->assign('where', $where);
  202. $where['cid'] = input('cid');
  203. $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  204. $this->assign(ArticleModel::getAll($where));
  205. return $this->fetch();
  206. }
  207. /**
  208. * 关联文章 id
  209. * @param int $id
  210. */
  211. public function relation($id = 0)
  212. {
  213. $this->assign('id', $id);
  214. return $this->fetch();
  215. }
  216. /**
  217. * 取消绑定的产品id
  218. * @param int $id
  219. */
  220. public function unrelation($id = 0)
  221. {
  222. if (!$id) return Json::fail('缺少参数');
  223. if (ArticleModel::edit(['product_id' => 0], $id))
  224. return Json::successful('取消关联成功!');
  225. else
  226. return Json::fail('取消失败');
  227. }
  228. /**
  229. * 文章评论
  230. * @param $id
  231. * @return void
  232. */
  233. public function reply_list($id)
  234. {
  235. $where = Util::getMore([
  236. ['title', ''],
  237. ], $this->request);
  238. $this->assign('where', $where);
  239. $this->assign(ArticleReply::list($where, $id));
  240. $this->assign('id', $id);
  241. return $this->fetch();
  242. }
  243. public function delete_reply($id)
  244. {
  245. $res = ArticleReply::del($id);
  246. if (!$res)
  247. return Json::fail('删除失败,请稍候再试!');
  248. else
  249. return Json::successful('删除成功!');
  250. }
  251. public function reply($id)
  252. {
  253. $this->assign('id', $id);
  254. return $this->fetch();
  255. }
  256. /*
  257. * 创建form表单
  258. * */
  259. public function upcreate($id =0)
  260. {
  261. $vipinfo = ArticleModel::where('id',$id)->find();
  262. $field[] = Form::input('off', '拒绝理由', isset($vipinfo) ? $vipinfo->off: '')->col(Form::col(24));
  263. $form = Form::make_post_form('添加拒绝理由', $field, Url::buildUrl('save', ['id' => $id]), 2);
  264. $this->assign(compact('form'));
  265. return $this->fetch('public/form-builder');
  266. }
  267. /*
  268. * 审核/拒绝
  269. * @param $id 文章id
  270. * @return json
  271. * */
  272. public function save($id = 0)
  273. {
  274. $data = UtilService::postMore([
  275. ['off', ''],
  276. ]);
  277. if (!$data['off']) return JsonService::fail('请输入拒绝理由');
  278. if($data['off']){
  279. $res=ArticleModel::where('id',$id)->update(['reason' =>$data['off'],'is_check' =>2]);
  280. if($res>0){
  281. return Json::successful('审核成功');
  282. }else{
  283. return Json::fail('审核失败');
  284. }
  285. }
  286. }
  287. public function add_reply()
  288. {
  289. $data = Util::postMore([
  290. 'content',
  291. 'id'
  292. ]);
  293. $reply = ArticleReply::where('id', $data['id'])->find();
  294. if (empty($reply)) return Json::fail('数据不存在');
  295. $admin = $this->adminInfo;
  296. $res = ArticleReply::create([
  297. 'aid' => $reply['aid'],
  298. 'content' => $data['content'],
  299. 'uid' => $admin['mer_id'],
  300. 'to_id' => $data['id'],
  301. 'add_time' => time()
  302. ]);
  303. if (!$res)
  304. return Json::fail('回复失败!');
  305. else
  306. return Json::successful('回复成功!');
  307. }
  308. public function fans()
  309. {
  310. return $this->fetch();
  311. }
  312. public function fansList()
  313. {
  314. $where = UtilService::getMore([
  315. ['page', 1],
  316. ['limit', 20],
  317. ]);
  318. $where['mer_id'] = $this->adminInfo['mer_id'];
  319. JsonService::successlayui(InterestUser::getList($where));
  320. }
  321. public function delete_fan($id)
  322. {
  323. $info = InterestUser::where('id', $id)->find();
  324. if (!$info) {
  325. JsonService::fail('移除失败,找不到记录');
  326. }
  327. if ($this->adminInfo['mer_id'] > 0 && $this->adminInfo['mer_id'] != $info['mer_id']) {
  328. JsonService::fail('移除失败,不可移除非自己媒体的粉丝');
  329. }
  330. $res = InterestUser::remove($id);
  331. if ($res) {
  332. JsonService::success('移除成功');
  333. } else {
  334. JsonService::fail('移除失败');
  335. }
  336. }
  337. public function set_top($id)
  338. {
  339. if ($this->adminInfo['mer_id']) {
  340. JsonService::fail('权限不足');
  341. }
  342. $info = \app\models\article\Article::where('id', $id)->find();
  343. if (!$info) {
  344. JsonService::fail('置顶失败,找不到记录');
  345. }
  346. $res1 = \app\models\article\Article::where('is_top',1)->find();
  347. if($res1){
  348. $res = \app\models\article\Article::where(1)->update(['is_top' => 0]) && \app\models\article\Article::where('id', $id)->update(['is_top' => 1]);
  349. }else{
  350. $res=\app\models\article\Article::where('id', $id)->update(['is_top' => 1]);
  351. }
  352. if ($res) {
  353. JsonService::success('置顶成功');
  354. } else {
  355. JsonService::fail('置顶失败');
  356. }
  357. }
  358. public function set_check($id)
  359. {
  360. if ($this->adminInfo['mer_id']) {
  361. JsonService::fail('权限不足');
  362. }
  363. $info = \app\models\article\Article::where('id', $id)->find();
  364. if (!$info) {
  365. JsonService::fail('审核失败,找不到记录');
  366. }
  367. $res=\app\models\article\Article::where('id', $id)->update(['is_check' => 1]);
  368. if ($res) {
  369. JsonService::success('审核成功');
  370. } else {
  371. JsonService::fail('审核失败');
  372. }
  373. }
  374. public function set_hot($id)
  375. {
  376. if ($this->adminInfo['mer_id']) {
  377. JsonService::fail('权限不足');
  378. }
  379. $info = \app\models\article\Article::where('id', $id)->find();
  380. if (!$info) {
  381. JsonService::fail('推荐失败,找不到记录');
  382. }
  383. if($info['is_hot']==0){
  384. $type=1;
  385. }else{
  386. $type=0;
  387. }
  388. $res=\app\models\article\Article::where('id', $id)->update(['is_hot' =>$type]);
  389. if ($res) {
  390. JsonService::success('成功');
  391. } else {
  392. JsonService::fail('失败');
  393. }
  394. }
  395. }