Article.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace app\admin\controller\article;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\SystemAttachment;
  5. use crmeb\services\{
  6. UtilService as Util, JsonService as Json
  7. };
  8. use app\admin\model\article\{
  9. ArticleCategory as ArticleCategoryModel, Article as ArticleModel
  10. };
  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. $this->assign('where', $where);
  32. $where['merchant'] = 0;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  33. $tree = sort_list_tier(ArticleCategoryModel::getArticleCategoryList());
  34. $this->assign(compact('tree'));
  35. $this->assign(ArticleModel::getAll($where));
  36. return $this->fetch();
  37. }
  38. /**
  39. * TODO 文件添加和修改
  40. * @return mixed
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @throws \think\exception\DbException
  44. */
  45. public function create()
  46. {
  47. $id = $this->request->param('id');
  48. $cid = $this->request->param('cid');
  49. $news = [];
  50. $all = [];
  51. $news['id'] = '';
  52. $news['image_input'] = '';
  53. $news['title'] = '';
  54. $news['author'] = '';
  55. $news['is_banner'] = '';
  56. $news['is_hot'] = '';
  57. $news['content'] = '';
  58. $news['synopsis'] = '';
  59. $news['url'] = '';
  60. $news['cid'] = [];
  61. $select = 0;
  62. if ($id) {
  63. $news = ArticleModel::where('n.id', $id)->alias('n')->field('n.*,c.content')->join('ArticleContent c', 'c.nid=n.id', 'left')->find();
  64. if (!$news) return $this->failed('数据不存在!');
  65. $news['cid'] = explode(',', $news['cid']);
  66. $news['content'] = htmlspecialchars_decode($news['content']);
  67. }
  68. if ($cid && in_array($cid, ArticleCategoryModel::getArticleCategoryInfo(0, 'id'))) {
  69. $all = ArticleCategoryModel::getArticleCategoryInfo($cid);
  70. $select = 1;
  71. }
  72. if (!$select) {
  73. $list = ArticleCategoryModel::getTierList();
  74. foreach ($list as $menu) {
  75. $all[$menu['id']] = $menu['html'] . $menu['title'];
  76. }
  77. }
  78. $this->assign('all', $all);
  79. $this->assign('news', $news);
  80. $this->assign('cid', $cid);
  81. $this->assign('select', $select);
  82. return $this->fetch();
  83. }
  84. /**
  85. * 上传图文图片
  86. * @return \think\response\Json
  87. */
  88. public function upload_image()
  89. {
  90. $res = Upload::instance()->setUploadPath('wechat/image/' . date('Ymd'))->image($_POST['file']);
  91. if (!is_array($res)) return Json::fail($res);
  92. SystemAttachment::attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 5, $res['image_type'], $res['time']);
  93. return Json::successful('上传成功!', ['url' => $res['dir']]);
  94. }
  95. /**
  96. * 添加和修改图文
  97. */
  98. public function add_new()
  99. {
  100. $data = Util::postMore([
  101. ['id', 0],
  102. ['cid', []],
  103. 'title',
  104. 'author',
  105. 'image_input',
  106. 'content',
  107. 'synopsis',
  108. 'share_title',
  109. 'share_synopsis',
  110. ['visit', 0],
  111. ['sort', 0],
  112. 'url',
  113. ['is_banner', 0],
  114. ['is_hot', 0],
  115. ['status', 1],]);
  116. $data['cid'] = implode(',', $data['cid']);
  117. $content = $data['content'];
  118. unset($data['content']);
  119. if ($data['id']) {
  120. $id = $data['id'];
  121. unset($data['id']);
  122. $res = false;
  123. ArticleModel::beginTrans();
  124. $res1 = ArticleModel::edit($data, $id, 'id');
  125. $res2 = ArticleModel::setContent($id, $content);
  126. if ($res1 && $res2) {
  127. $res = true;
  128. }
  129. ArticleModel::checkTrans($res);
  130. if ($res)
  131. return Json::successful('修改图文成功!', $id);
  132. else
  133. return Json::fail('修改图文失败,您并没有修改什么!', $id);
  134. } else {
  135. $data['add_time'] = time();
  136. $data['admin_id'] = $this->adminId;
  137. $res = false;
  138. ArticleModel::beginTrans();
  139. $res1 = ArticleModel::create($data);
  140. $res2 = false;
  141. if ($res1)
  142. $res2 = ArticleModel::setContent($res1->id, $content);
  143. if ($res1 && $res2) {
  144. $res = true;
  145. }
  146. ArticleModel::checkTrans($res);
  147. if ($res)
  148. return Json::successful('添加图文成功!', $res1->id);
  149. else
  150. return Json::successful('添加图文失败!', $res1->id);
  151. }
  152. }
  153. /**
  154. * 删除图文
  155. * @param $id
  156. * @return \think\response\Json
  157. */
  158. public function delete($id)
  159. {
  160. $res = ArticleModel::del($id);
  161. if (!$res)
  162. return Json::fail('删除失败,请稍候再试!');
  163. else
  164. return Json::successful('删除成功!');
  165. }
  166. public function merchantIndex()
  167. {
  168. $where = Util::getMore([
  169. ['title', '']
  170. ], $this->request);
  171. $this->assign('where', $where);
  172. $where['cid'] = input('cid');
  173. $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  174. $this->assign(ArticleModel::getAll($where));
  175. return $this->fetch();
  176. }
  177. /**
  178. * 关联文章 id
  179. * @param int $id
  180. */
  181. public function relation($id = 0)
  182. {
  183. $this->assign('id', $id);
  184. return $this->fetch();
  185. }
  186. /**
  187. * 保存选择的产品
  188. * @param int $id
  189. */
  190. public function edit_article($id = 0)
  191. {
  192. if (!$id) return Json::fail('缺少参数');
  193. list($product_id) = Util::postMore([
  194. ['product_id', 0]
  195. ], $this->request, true);
  196. if (ArticleModel::edit(['product_id' => $product_id], ['id' => $id]))
  197. return Json::successful('保存成功');
  198. else
  199. return Json::fail('保存失败');
  200. }
  201. /**
  202. * 取消绑定的产品id
  203. * @param int $id
  204. */
  205. public function unrelation($id = 0)
  206. {
  207. if (!$id) return Json::fail('缺少参数');
  208. if (ArticleModel::edit(['product_id' => 0], $id))
  209. return Json::successful('取消关联成功!');
  210. else
  211. return Json::fail('取消失败');
  212. }
  213. }