WechatNews.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\article;
  12. use app\admin\controller\AuthController;
  13. use service\JsonService as Json;
  14. use service\UploadService as Upload;
  15. use think\Request;
  16. use app\admin\model\article\ArticleCategory as ArticleCategoryModel;
  17. use app\admin\model\article\Article as ArticleModel;
  18. use app\admin\model\system\SystemAttachment;
  19. /**
  20. * 图文管理
  21. * Class WechatNews
  22. * @package app\admin\controller\wechat
  23. */
  24. class WechatNews extends AuthController
  25. {
  26. /**
  27. * 显示后台管理员添加的图文
  28. * @return mixed
  29. */
  30. public function index($cid = 0)
  31. {
  32. $where = parent::getMore([
  33. ['title', '']
  34. ], $this->request);
  35. if ($cid) {
  36. $where['cid'] = $cid;
  37. } else {
  38. $where['cid'] = '';
  39. }
  40. $this->assign('where', $where);
  41. $where['merchant'] = 0;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  42. $this->assign('cid', $cid);
  43. $this->assign(ArticleModel::getAll($where));
  44. return $this->fetch();
  45. }
  46. /**
  47. * 展示页面 添加和删除
  48. * @return mixed
  49. */
  50. public function create()
  51. {
  52. $id = input('id');
  53. $cid = input('cid');
  54. $news = array();
  55. $news['id'] = '';
  56. $news['image_input'] = '';
  57. $news['title'] = '';
  58. $news['author'] = '';
  59. $news['content'] = '';
  60. $news['synopsis'] = '';
  61. $news['url'] = '';
  62. $news['cid'] = array();
  63. if ($id) {
  64. $news = \app\admin\model\wechat\WechatNews::where('n.id', $id)->alias('n')->field('n.*,c.content')->join('__WECHAT_NEWS_CONTENT__ c', 'c.nid=n.id')->find();
  65. if (!$news) return $this->failedNotice('数据不存在!');
  66. $news['cid'] = explode(',', $news['cid']);
  67. }
  68. $all = array();
  69. $select = 0;
  70. if (!$cid) {
  71. $cid = '';
  72. } else {
  73. if ($id) {
  74. $all = ArticleCategoryModel::where('id', $cid)->where('hidden', 'neq', 0)->column('id,title');
  75. $select = 1;
  76. } else {
  77. $all = ArticleCategoryModel::where('id', $cid)->column('id,title');
  78. $select = 1;
  79. }
  80. }
  81. if (empty($all)) {
  82. $all = ArticleCategoryModel::getField('id,title');//新闻分类
  83. $select = 0;
  84. }
  85. $this->assign('all', $all);
  86. $this->assign('news', $news);
  87. $this->assign('cid', $cid);
  88. $this->assign('select', $select);
  89. return $this->fetch();
  90. }
  91. /**
  92. * 上传图文图片
  93. * @return \think\response\Json
  94. */
  95. public function upload_image()
  96. {
  97. $res = Upload::Image($_POST['file'], 'wechat/image/' . date('Ymd'));
  98. //产品图片上传记录
  99. $fileInfo = $res->fileInfo->getinfo();
  100. SystemAttachment::attachmentAdd($res->fileInfo->getSaveName(), $fileInfo['size'], $fileInfo['type'], $res->dir, '', 5);
  101. if (!$res->status) return Json::fail($res->error);
  102. return Json::successful('上传成功!', ['url' => $res->filePath]);
  103. }
  104. /**
  105. * 添加和修改图文
  106. * @param Request $request
  107. * @return \think\response\Json
  108. */
  109. public function add_new(Request $request)
  110. {
  111. $data = parent::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. ['status', 1],], $request);
  125. $data['cid'] = implode(',', $data['cid']);
  126. $content = $data['content'];
  127. unset($data['content']);
  128. if ($data['id']) {
  129. $id = $data['id'];
  130. unset($data['id']);
  131. ArticleModel::beginTrans();
  132. $res1 = ArticleModel::edit($data, $id, 'id');
  133. $res2 = ArticleModel::setContent($id, $content);
  134. if ($res1 && $res2) {
  135. $res = true;
  136. } else {
  137. $res = false;
  138. }
  139. ArticleModel::checkTrans($res);
  140. if ($res) {
  141. return Json::successful('修改图文成功!', $id);
  142. } else {
  143. return Json::fail('修改图文失败!', $id);
  144. }
  145. } else {
  146. $data['add_time'] = time();
  147. $data['admin_id'] = $this->adminId;
  148. ArticleModel::beginTrans();
  149. $res1 = ArticleModel::set($data);
  150. $res2 = false;
  151. if ($res1) {
  152. $res2 = ArticleModel::setContent($res1->id, $content);
  153. }
  154. if ($res1 && $res2) {
  155. $res = true;
  156. } else {
  157. $res = false;
  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. * 删除图文
  169. * @param $id
  170. * @return \think\response\Json
  171. */
  172. public function delete($id)
  173. {
  174. $res = ArticleModel::del($id);
  175. if (!$res) {
  176. return Json::fail('删除失败,请稍候再试!');
  177. } else {
  178. return Json::successful('删除成功!');
  179. }
  180. }
  181. public function merchantIndex()
  182. {
  183. $where = parent::getMore([
  184. ['title', '']
  185. ], $this->request);
  186. $this->assign('where', $where);
  187. $where['cid'] = input('cid');
  188. $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  189. $this->assign(ArticleModel::getAll($where));
  190. return $this->fetch();
  191. }
  192. }