Article.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. $data['cid'] = implode(',', $data['cid']);
  128. $content = $data['content'];
  129. unset($data['content']);
  130. $admin_id = $this->request->param('admin_id');
  131. $data['mer_id'] = SystemAdmin::where('id', $admin_id)->value('mer_id');
  132. if ($data['id']) {
  133. $id = $data['id'];
  134. unset($data['id']);
  135. $res = false;
  136. ArticleModel::beginTrans();
  137. $res1 = ArticleModel::edit($data, $id, 'id');
  138. $res2 = ArticleModel::setContent($id, $content);
  139. if ($res1 && $res2) {
  140. $res = true;
  141. }
  142. ArticleModel::checkTrans($res);
  143. if ($res)
  144. return Json::successful('修改图文成功!', $id);
  145. else
  146. return Json::fail('修改图文失败,您并没有修改什么!', $id);
  147. } else {
  148. $data['add_time'] = time();
  149. $data['admin_id'] = $this->adminId;
  150. $res = false;
  151. ArticleModel::beginTrans();
  152. $res1 = ArticleModel::create($data);
  153. $res2 = false;
  154. if ($res1)
  155. $res2 = ArticleModel::setContent($res1->id, $content);
  156. if ($res1 && $res2) {
  157. $res = true;
  158. }
  159. ArticleModel::checkTrans($res);
  160. if ($res)
  161. return Json::successful('添加图文成功!', $res1->id);
  162. else
  163. return Json::successful('添加图文失败!', $res1->id);
  164. }
  165. }
  166. /**
  167. * 删除图文
  168. * @param $id
  169. * @return \think\response\Json
  170. */
  171. public function delete($id)
  172. {
  173. $res = ArticleModel::del($id);
  174. if (!$res)
  175. return Json::fail('删除失败,请稍候再试!');
  176. else
  177. return Json::successful('删除成功!');
  178. }
  179. public function merchantIndex()
  180. {
  181. $where = Util::getMore([
  182. ['title', '']
  183. ], $this->request);
  184. $this->assign('where', $where);
  185. $where['cid'] = input('cid');
  186. $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  187. $this->assign(ArticleModel::getAll($where));
  188. return $this->fetch();
  189. }
  190. /**
  191. * 关联文章 id
  192. * @param int $id
  193. */
  194. public function relation($id = 0)
  195. {
  196. $this->assign('id', $id);
  197. return $this->fetch();
  198. }
  199. /**
  200. * 保存选择的产品
  201. * @param int $id
  202. */
  203. public function edit_article($id = 0)
  204. {
  205. if (!$id) return Json::fail('缺少参数');
  206. list($product_id) = Util::postMore([
  207. ['product_id', 0]
  208. ], $this->request, true);
  209. if (ArticleModel::edit(['product_id' => $product_id], ['id' => $id]))
  210. return Json::successful('保存成功');
  211. else
  212. return Json::fail('保存失败');
  213. }
  214. /**
  215. * 取消绑定的产品id
  216. * @param int $id
  217. */
  218. public function unrelation($id = 0)
  219. {
  220. if (!$id) return Json::fail('缺少参数');
  221. if (ArticleModel::edit(['product_id' => 0], $id))
  222. return Json::successful('取消关联成功!');
  223. else
  224. return Json::fail('取消失败');
  225. }
  226. }