Article.php 6.9 KB

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