Article.php 7.6 KB

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