Article.php 7.6 KB

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