Archives.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. <?php
  2. namespace addons\cms\controller\api;
  3. use addons\cms\library\Order;
  4. use addons\cms\library\OrderException;
  5. use addons\cms\model\Archives as ArchivesModel;
  6. use addons\cms\model\Channel;
  7. use addons\cms\model\Comment;
  8. use addons\cms\model\Fields;
  9. use addons\cms\model\Modelx;
  10. use addons\cms\library\Service;
  11. use addons\third\model\Third;
  12. use fast\Tree;
  13. use think\Db;
  14. use think\Validate;
  15. use app\common\model\User;
  16. use addons\cms\library\Theme;
  17. /**
  18. * 文档
  19. */
  20. class Archives extends Base
  21. {
  22. protected $noNeedLogin = ['index', 'detail', 'get_channel'];
  23. public function index()
  24. {
  25. $config = get_addon_config('cms');
  26. $diyname = $this->request->param('diyname');
  27. $model_id = (int)$this->request->param('model');
  28. $channel_id = (int)$this->request->param('channel');
  29. $menu_index = (int)$this->request->param('menu_index/d', 0);
  30. //对于首页,应用初始化,无法给传参,默认传-1,取导航的设置参数
  31. if ($model_id == -1) {
  32. $model_id = Theme::getFirstParam('model', $menu_index);
  33. }
  34. if ($channel_id == -1) {
  35. $channel_id = Theme::getFirstParam('channel', $menu_index);
  36. }
  37. $channel = null;
  38. //取栏目
  39. if ($diyname && !is_numeric($diyname)) {
  40. $channel = Channel::getByDiyname($diyname);
  41. } else {
  42. $id = $diyname ? $diyname : ($channel_id ? $channel_id : $this->request->param('id', ''));
  43. $channel = Channel::get($id);
  44. }
  45. if ($model_id && !$channel) {
  46. $channel = Channel::where('type', 'channel')->where('parent_id', 0)->where('model_id', $model_id)->find();
  47. }
  48. if (!$channel) {
  49. $this->getArchivesIndex();
  50. return;
  51. }
  52. $channel['image'] = cdnurl($channel['image'],true);
  53. $filter = $this->request->get('filter/a', []);
  54. $orderby = $this->request->get('orderby', '');
  55. $orderway = $this->request->get('orderway', '', 'strtolower');
  56. $multiple = $this->request->get('multiple/d', 0);
  57. $params = [];
  58. $filter = $this->request->get();
  59. $filter = array_diff_key($filter, array_flip(['orderby', 'orderway', 'page', 'multiple']));
  60. if (isset($filter['filter'])) {
  61. $filter = array_merge($filter, $filter['filter']);
  62. }
  63. if ($filter) {
  64. $filter = array_filter($filter, 'strlen');
  65. $params['filter'] = $filter;
  66. $params = $filter;
  67. }
  68. if ($orderby) {
  69. $params['orderby'] = $orderby;
  70. }
  71. if ($orderway) {
  72. $params['orderway'] = $orderway;
  73. }
  74. if ($multiple) {
  75. $params['multiple'] = $multiple;
  76. }
  77. if ($channel['type'] === 'link') {
  78. // $this->redirect($channel['outlink']);
  79. }
  80. //加载模型数据
  81. $model = Modelx::get($channel['model_id']);
  82. if (!$model) {
  83. $this->error(__('No specified model found'));
  84. }
  85. //默认排序字段
  86. $orders = [
  87. ['name' => 'default', 'field' => 'weigh DESC,id DESC', 'title' => __('Default')],
  88. ];
  89. //合并主表筛选字段
  90. $orders = array_merge($orders, $model->getOrderFields());
  91. //获取过滤列表
  92. list($filterList, $filter, $params, $fields, $multiValueFields, $fieldsList) = Service::getFilterList('model', $model['id'], $filter, $params, $multiple);
  93. //获取排序列表
  94. list($orderList, $orderby, $orderway) = Service::getOrderList($orderby, $orderway, $orders, $params, $fieldsList);
  95. //获取过滤的条件和绑定参数
  96. list($filterWhere, $filterBind) = Service::getFilterWhereBind($filter, $multiValueFields, $multiple);
  97. //加载列表数据
  98. $pageList = ArchivesModel::with(['channel', 'user'])->alias('a')
  99. ->where('a.status', 'normal')
  100. ->whereNull('a.deletetime')
  101. ->where($filterWhere)
  102. ->bind($filterBind)
  103. ->join($model['table'] . ' n', 'a.id=n.id', 'LEFT')
  104. ->field('a.*')
  105. ->field('id,content', true, config('database.prefix') . $model['table'], 'n')
  106. ->where(function ($query) use ($channel) {
  107. $query->where(function ($query) use ($channel) {
  108. if ($channel['listtype'] <= 2) {
  109. $query->whereOr("channel_id", $channel['id']);
  110. }
  111. if ($channel['listtype'] == 1 || $channel['listtype'] == 3) {
  112. $query->whereOr('channel_id', 'in', function ($query) use ($channel) {
  113. $query->name("cms_channel")->where('parent_id', $channel['id'])->field("id");
  114. });
  115. }
  116. if ($channel['listtype'] == 0 || $channel['listtype'] == 4) {
  117. $childrenIds = \addons\cms\model\Channel::getChannelChildrenIds($channel['id'], false);
  118. if ($childrenIds) {
  119. $query->whereOr('channel_id', 'in', $childrenIds);
  120. }
  121. }
  122. })
  123. ->whereOr("(`channel_ids`!='' AND FIND_IN_SET('{$channel['id']}', `channel_ids`))");
  124. })
  125. ->where('model_id', $channel->model_id)
  126. ->order($orderby, $orderway)
  127. ->paginate($channel['pagesize'], $config['pagemode'] == 'simple', ['type' => '\\addons\\cms\\library\\Bootstrap']);
  128. $fieldsContentList = Fields::getFieldsContentList('model', $model->id);
  129. foreach ($pageList as $index => $item) {
  130. Service::appendTextAttr($fieldsContentList, $item);
  131. }
  132. $fieldsContentList = Fields::getFieldsContentList('channel');
  133. Service::appendTextAttr($fieldsContentList, $channel);
  134. $pageList->appends(array_filter($params));
  135. foreach ($pageList as $index => $item) {
  136. if ($item->channel) {
  137. $item->channel->visible(explode(',', 'id,parent_id,name,image,diyname,items'));
  138. }
  139. //小程序只显示9张图
  140. $item->images_list = array_slice(array_filter(explode(',', $item['images'])), 0, 9);
  141. unset($item['weigh'], $item['status'], $item['deletetime'], $item['memo']);
  142. if ($item->user) {
  143. $item->user->visible(explode(',', 'id,nickname,avatar'));
  144. }
  145. }
  146. $this->success('获取成功', [
  147. 'pageList' => $pageList,
  148. 'filterList' => $filterList,
  149. 'orderList' => $orderList,
  150. 'channel' => $channel
  151. ]);
  152. }
  153. /**
  154. * 读取文档列表
  155. */
  156. private function getArchivesIndex()
  157. {
  158. $params = [];
  159. $model = (int)$this->request->param('model');
  160. $channel = (int)$this->request->param('channel');
  161. $page = (int)$this->request->param('page');
  162. $type = $this->request->param('type');
  163. $params['orderby'] = $this->request->param('orderby', 'weigh');
  164. $menu_index = (int)$this->request->param('menu_index/d', 0);
  165. //对于首页,应用初始化,无法给传参,默认传-1,取导航的设置参数
  166. if ($model == -1) {
  167. $model = Theme::getFirstParam('model',$menu_index);
  168. }
  169. if ($channel == -1) {
  170. $channel = Theme::getFirstParam('channel',$menu_index);
  171. }
  172. if ($model) {
  173. $params['model'] = $model;
  174. }
  175. if ($channel) {
  176. $params['channel'] = $channel;
  177. }
  178. if ($type) {
  179. $params['type'] = $type;
  180. }
  181. $page = max(1, $page);
  182. $params['limit'] = ($page - 1) * 10 . ',10';
  183. $params['cache'] = 0;
  184. if (isset($params['channel']) && $params['channel']) {
  185. $channelInfo = Channel::get($params['channel']);
  186. if ($channelInfo) {
  187. $channelIds = Channel::where(function ($query) use ($channelInfo) {
  188. if ($channelInfo['listtype'] <= 2) {
  189. $query->whereOr("id", $channelInfo['id']);
  190. }
  191. if ($channelInfo['listtype'] == 1 || $channelInfo['listtype'] == 3) {
  192. $query->whereOr('id', 'in', function ($query) use ($channelInfo) {
  193. $query->name("cms_channel")->where('parent_id', $channelInfo['id'])->field("id");
  194. });
  195. }
  196. if ($channelInfo['listtype'] == 0 || $channelInfo['listtype'] == 4) {
  197. $childrenIds = \addons\cms\model\Channel::getChannelChildrenIds($channelInfo['id'], false);
  198. if ($childrenIds) {
  199. $query->whereOr('id', 'in', $childrenIds);
  200. }
  201. }
  202. })->column('id');
  203. $params['channel'] = implode(',', $channelIds);
  204. }
  205. }
  206. $params['paginate'] = 10;
  207. $list = ArchivesModel::getArchivesList($params);
  208. foreach ($list as $index => $item) {
  209. if($item->channel){
  210. $item->channel->visible(explode(',', 'id,parent_id,name,image,diyname,items'));
  211. //小程序只显示9张图
  212. $item->images_list = array_slice(array_filter(explode(',', $item['images'])), 0, 9);
  213. unset($item['imglink'], $item['textlink'], $item['channellink'], $item['tagslist'], $item['weigh'], $item['status'], $item['deletetime'], $item['memo'], $item['img']);
  214. }
  215. }
  216. $this->success('', [
  217. 'pageList' => $list,
  218. 'filterList' =>[],
  219. 'orderList' =>[],
  220. 'channel'=>[]
  221. ]);
  222. }
  223. /**
  224. * 文档详情
  225. */
  226. public function detail()
  227. {
  228. $action = $this->request->post("action");
  229. if ($action && $this->request->isPost()) {
  230. return $this->$action();
  231. }
  232. $diyname = $this->request->param('diyname');
  233. if ($diyname && !is_numeric($diyname)) {
  234. $archives = ArchivesModel::with([
  235. 'user' => function ($query) {
  236. $query->field('id,nickname,avatar,bio');
  237. }
  238. ])->getByDiyname($diyname);
  239. } else {
  240. $id = $diyname ? $diyname : $this->request->request('id', '');
  241. $archives = ArchivesModel::get($id, [
  242. 'user' => function ($query) {
  243. $query->field('id,nickname,avatar,bio');
  244. }
  245. ]);
  246. }
  247. if (!$archives || ($archives['user_id'] != $this->auth->id && $archives['status'] != 'normal') || $archives['deletetime']) {
  248. $this->error(__('No specified article found'));
  249. }
  250. if (!$this->auth->id && !$archives['isguest']) {
  251. $this->error(__('Please login first'));
  252. }
  253. $channel = Channel::get($archives['channel_id']);
  254. if (!$channel) {
  255. $this->error(__('No specified channel found'));
  256. }
  257. $model = Modelx::get($channel['model_id']);
  258. if (!$model) {
  259. $this->error(__('No specified model found'));
  260. }
  261. if ($archives['user']) {
  262. $archives['user']['avatar'] = cdnurl($archives['user']['avatar'], true);
  263. }
  264. $archives->setInc("views", 1);
  265. $addon = db($model['table'])->where('id', $archives['id'])->find();
  266. if ($addon) {
  267. if ($model->fields) {
  268. $fieldsContentList = Fields::getFieldsContentList('model', $model->id);
  269. //附加列表字段
  270. array_walk($fieldsContentList, function ($content, $field) use (&$addon) {
  271. $addon[$field . '_text'] = isset($content[$addon[$field]]) ? $content[$addon[$field]] : $addon[$field];
  272. });
  273. }
  274. $archives->setData($addon);
  275. } else {
  276. $this->error(__('No specified article addon found'));
  277. }
  278. //小程序付费阅读将不可见
  279. $content = $archives->content;
  280. if ($archives->is_paid_part_of_content || $archives->ispaid) {
  281. $value = $archives->getData('content');
  282. $pattern = '/<paid>(.*?)<\/paid>/is';
  283. if (preg_match($pattern, $value) && !$archives->ispaid) {
  284. $value = preg_replace($pattern, "<div class='alert alert-warning' style='background:#fcf8e3;border:1px solid #faf3cd;color:#c09853;padding:8px;'>付费内容已经隐藏,请付费后查看</div>", $value);
  285. }
  286. $content = $value;
  287. if (!empty($archives->downloadurl)) {
  288. $archives->downloadurl = json_decode($archives->downloadurl, true);
  289. }
  290. } else {
  291. if (!$archives->ispaid) {
  292. if (isset($channel['vip']) && $channel['vip'] > $this->auth->vip) {
  293. $paytips = "此文章为付费文章,需要VIP {$channel['vip']}" . ($archives->price > 0 ? "或支付¥{$archives->price}元" : "") . "才能查看";
  294. } else {
  295. $paytips = "此文章为付费文章,需要支付¥{$archives->price}元才能查看";
  296. }
  297. $content = "<div class='alert alert-warning alert-paid' style='background:#fcf8e3;border:1px solid #faf3cd;color:#c09853;padding:8px;'>{$paytips}</div>";
  298. $archives->hidden(['downloadurl', 'downloadurl_text']);
  299. }
  300. }
  301. //小程序不支持内容页分页
  302. $content = str_replace("##pagebreak##", "<br>", $content);
  303. $archives->content = $content;
  304. $commentList = Comment::getCommentList(['aid' => $archives['id']]);
  305. $commentList = $commentList->getCollection();
  306. foreach ($commentList as $index => &$item) {
  307. if ($item['user']) {
  308. $item['user']['avatar'] = cdnurl($item['user']['avatar'], true);
  309. }
  310. $item->hidden(['ip', 'useragent', 'deletetime', 'aid', 'subscribe', 'status', 'type', 'updatetime']);
  311. }
  312. $channel = $channel->toArray();
  313. $channel['url'] = $channel['fullurl'];
  314. foreach ($model->fields_list as $diy) {
  315. if ($diy['type'] == 'image' || $diy['type'] == 'file') {
  316. $archives[$diy['name']] = cdnurl($archives[$diy['name']], true);
  317. }
  318. if ($diy['type'] == 'images' || $diy['type'] == 'files') {
  319. if (isset($archives[$diy['name']])) {
  320. $images = explode(',', $archives[$diy['name']]);
  321. foreach ($images as &$img) {
  322. $img = cdnurl($img, true);
  323. }
  324. unset($img);
  325. $archives[$diy['name']] = $images;
  326. }
  327. }
  328. }
  329. unset($channel['channeltpl'], $channel['listtpl'], $channel['showtpl'], $channel['status'], $channel['weigh'], $channel['parent_id']);
  330. if ($archives->channel) {
  331. $archives->channel->visible(explode(',', 'id,parent_id,name,image,diyname,items'));
  332. }
  333. $this->success('', ['archivesInfo' => $archives, 'commentList' => $commentList, '__token__' => $this->request->token()]);
  334. }
  335. /**
  336. * 赞与踩
  337. */
  338. public function vote()
  339. {
  340. $id = (int)$this->request->post("id");
  341. $type = trim($this->request->post("type", ""));
  342. if (!$id || !$type) {
  343. $this->error(__('Operation failed'));
  344. }
  345. $archives = ArchivesModel::get($id);
  346. if (!$archives || ($archives['user_id'] != $this->auth->id && $archives['status'] != 'normal') || $archives['deletetime']) {
  347. $this->error(__('No specified article found'));
  348. }
  349. $archives->where('id', $id)->setInc($type === 'like' ? 'likes' : 'dislikes', 1);
  350. $archives = ArchivesModel::get($id);
  351. $this->success(__('点赞成功!'), ['likes' => $archives->likes, 'dislikes' => $archives->dislikes, 'likeratio' => $archives->likeratio]);
  352. }
  353. /**
  354. * 提交订单
  355. */
  356. public function order()
  357. {
  358. if (!$this->request->isPost()) {
  359. $this->error('请求错误');
  360. }
  361. $id = $this->request->post('id/d');
  362. $paytype = $this->request->param('paytype');
  363. $method = $this->request->param('method');
  364. $appid = $this->request->param('appid'); //为APP的应用id
  365. $returnurl = $this->request->param('returnurl', '', 'trim');
  366. $openid = '';
  367. $archives = \addons\cms\model\Archives::get($id);
  368. if (!$archives) {
  369. $this->error("文档未找到");
  370. }
  371. //非H5 支付,非余额
  372. if (in_array($method, ['miniapp', 'mp']) && $paytype == 'wechat') {
  373. $third = Third::where('platform', 'wechat')
  374. ->where('apptype', $method)
  375. ->where('user_id', $this->auth->id)
  376. ->find();
  377. if (!$third) {
  378. $this->error("未找到登录用户信息", 'bind');
  379. }
  380. $openid = $third['openid'];
  381. }
  382. try {
  383. $response = Order::submit($id, $paytype, $method, $openid, '', $returnurl);
  384. } catch (OrderException $e) {
  385. if ($e->getCode() == 1) {
  386. $this->success($e->getMessage(), null);
  387. } else {
  388. $this->error($e->getMessage());
  389. }
  390. } catch (\Exception $e) {
  391. $this->error($e->getMessage());
  392. }
  393. $this->success("请求成功", $response);
  394. }
  395. /**
  396. * 获取栏目
  397. * @return void
  398. */
  399. public function get_channel()
  400. {
  401. $archives_id = $this->request->param('archives_id');
  402. $archives = $archives_id ? \app\admin\model\cms\Archives::get($archives_id) : null;
  403. $where = [];
  404. $channel_id = 0;
  405. if ($archives) {
  406. $channel = Channel::get($archives['channel_id']);
  407. if (!$channel) {
  408. $this->error(__('未找到指定栏目'));
  409. }
  410. $model = \addons\cms\model\Modelx::get($channel['model_id']);
  411. if (!$model) {
  412. $this->error(__('未找到指定模型'));
  413. }
  414. if ($archives['user_id'] != $this->auth->id) {
  415. $this->error("无法进行越权操作!");
  416. }
  417. $where['model_id'] = $channel['model_id'];
  418. $channel_id = $archives['channel_id'];
  419. }
  420. $channelList = collection(
  421. (new Channel())->field('id,parent_id,name,model_id,diyname,type')
  422. ->where($where)
  423. ->where('type', '<>', 'link')
  424. ->where("((type='list' AND iscontribute='1') OR type='channel')")
  425. ->order("weigh desc,id desc")
  426. ->cache(false)
  427. ->select()
  428. )->toArray();
  429. $tree = Tree::instance()->init($channelList, 'parent_id');
  430. $data = $tree->getTreeList($tree->getTreeArray(0, false));
  431. foreach ($data as &$item) {
  432. $item['disabled'] = $item['type'] != 'list';
  433. }
  434. $this->success('', [
  435. 'channel' => $data,
  436. 'channel_id' => $channel_id
  437. ]);
  438. }
  439. /**
  440. * 获取栏目字段列表
  441. * @internal
  442. */
  443. public function get_channel_fields()
  444. {
  445. $channel_id = $this->request->param('channel_id');
  446. $archives_id = $this->request->param('archives_id');
  447. $channel = Channel::get($channel_id, 'model');
  448. if ($channel && $channel['type'] === 'list') {
  449. $values = [];
  450. //编辑获取数据
  451. $archives = $archives_id ? \app\admin\model\cms\Archives::get($archives_id) : null;
  452. if ($archives) {
  453. $archives->hidden(['admin_id', 'status', 'views']);
  454. $values = db($channel['model']['table'])->where('id', $archives_id)->find(); //编辑附表数据
  455. }
  456. //字段
  457. $fields = Service::getCustomFields('model', $channel['model_id'], $values, ['iscontribute' => 1]);
  458. $setting = $channel->getRelation('model')->setting ?? [];
  459. //副栏目数据
  460. $channelList = collection(
  461. (new Channel())->field('id,parent_id,name,model_id,diyname,type')
  462. ->where('type', '<>', 'link')
  463. ->where("((type='list' AND iscontribute='1') OR type='channel')")
  464. ->order("weigh desc,id desc")
  465. ->cache(false)
  466. ->select()
  467. )->toArray();
  468. $tree = Tree::instance()->init($channelList, 'parent_id');
  469. $data = $tree->getTreeList($tree->getTreeArray(0, false));
  470. $secondList = [];
  471. foreach ($data as &$item) {
  472. $item['disabled'] = $item['type'] != 'list';
  473. if ($item['model_id'] == $channel['model_id']) {
  474. $secondList[] = $item;
  475. }
  476. }
  477. $this->success('', [
  478. 'contributefields' => $setting && isset($setting['contributefields']) ? $setting['contributefields'] : [],
  479. 'fields' => $fields,
  480. 'values' => $values,
  481. 'archives' => $archives,
  482. 'secondList' => $secondList
  483. ]);
  484. } else {
  485. $this->error(__('请选择栏目'));
  486. }
  487. $this->error(__('参数不能为空', 'ids'));
  488. }
  489. /**
  490. * 提交数据
  491. * @return void
  492. */
  493. public function archives_post()
  494. {
  495. // 如果来源于提交
  496. $config = get_addon_config('cms');
  497. if ($this->auth->score < $config['limitscore']['postarchives']) {
  498. $this->error("积分必须大于{$config['limitscore']['postarchives']}才可以发布文章");
  499. }
  500. $row = $this->request->post('', '', 'trim,xss_clean');
  501. $rule = [
  502. 'title|标题' => 'require|length:3,100',
  503. 'channel_id|栏目' => 'require|integer',
  504. ];
  505. $msg = [
  506. 'title.require' => '标题不能为空',
  507. 'title.length' => '标题长度限制在3~100个字符',
  508. 'channel_id' => '栏目不能为空',
  509. 'content.require' => '内容不能为空',
  510. ];
  511. $validate = new Validate($rule, $msg);
  512. $result = $validate->check($row);
  513. if (!$result) {
  514. $this->error($validate->getError());
  515. }
  516. //如果有传栏目名称
  517. if (isset($row['channel']) && $row['channel']) {
  518. $channel = Channel::where('name', $row['channel'])->find();
  519. if ($channel) {
  520. $row['channel_id'] = $channel->id;
  521. } else {
  522. $this->error('栏目未找到');
  523. }
  524. unset($row['channel']);
  525. } else {
  526. $channel_id = (isset($row['channel_id']) && !empty($row['channel_id'])) ? $row['channel_id'] : $this->request->request('channel_id');
  527. $channel = Channel::get($channel_id);
  528. if (!$channel) {
  529. $this->error('栏目未找到');
  530. }
  531. }
  532. $model = Modelx::get($channel['model_id']);
  533. if (!$model) {
  534. $this->error('模型未找到');
  535. }
  536. $channelIds = isset($row['channel_ids']) ? (is_array($row['channel_ids']) ? $row['channel_ids'] : explode(',', $row['channel_ids'])) : [];
  537. $channelIds = array_merge([$channel['id']], $channelIds);
  538. $channelIds = array_filter($channelIds);
  539. $count = Channel::where('id', 'in', $channelIds)->where('iscontribute', 0)->count();
  540. if ($count > 0) {
  541. $this->error("栏目不允许投稿");
  542. }
  543. //审核状态
  544. $status = 'normal';
  545. if ($config['isarchivesaudit'] == 1) {
  546. $status = 'hidden';
  547. } elseif ($config['isarchivesaudit'] == 0) {
  548. $status = 'normal';
  549. } else {
  550. $textArr = array_map(function ($item) {
  551. return is_array($item) ? json_encode($item, JSON_UNESCAPED_UNICODE) : $item;
  552. }, $row);
  553. if (!Service::isContentLegal(implode(' ', $textArr))) {
  554. $status = 'hidden';
  555. }
  556. }
  557. $row['user_id'] = $this->auth->id;
  558. $row['status'] = $status;
  559. $row['publishtime'] = time();
  560. //编辑的时候
  561. $archives = null;
  562. if (isset($row['id']) && !empty((int)$row['id'])) {
  563. $archives = \app\admin\model\cms\Archives::get($row['id']);
  564. if ($archives['user_id'] != $this->auth->id) {
  565. $this->error("无法进行越权操作!");
  566. }
  567. } else {
  568. unset($row['id']);
  569. }
  570. Db::startTrans();
  571. try {
  572. if ($archives) {
  573. $archives->allowField(true)->save($row);
  574. } else {
  575. (new \app\admin\model\cms\Archives)->allowField(true)->save($row);
  576. }
  577. //增加积分
  578. $status == 'normal' && User::score($config['score']['postarchives'], $this->auth->id, '发布文章');
  579. Db::commit();
  580. } catch (\Exception $e) {
  581. Db::rollback();
  582. $this->error("发生错误:" . $e->getMessage());
  583. }
  584. if ($status === 'hidden') {
  585. //发送通知
  586. $status === 'hidden' && Service::notice('CMS收到一篇新的文章审核', $config['auditnotice'], $config['noticetemplateid']);
  587. $this->success("提交成功!请等待审核!");
  588. } else {
  589. $this->success("发布成功!");
  590. }
  591. }
  592. /**
  593. * 我的发布
  594. */
  595. public function my()
  596. {
  597. $archives = new \addons\cms\model\Archives;
  598. $model = null;
  599. $model_id = (int)$this->request->request('model_id');
  600. $channel_id = (int)$this->request->request('channel_id');
  601. // 如果有model_id则调用指定模型
  602. if ($model_id) {
  603. $model = Modelx::get($model_id);
  604. if ($model) {
  605. $archives->where('model_id', $model_id);
  606. }
  607. }
  608. $config = ['query' => []];
  609. if ($model) {
  610. $config['query']['model_id'] = $model_id;
  611. }
  612. if ($channel_id) {
  613. $config['query']['channel_id'] = $channel_id;
  614. }
  615. $user_id = $this->auth->id;
  616. $archivesList = $archives->where('user_id', $user_id)
  617. ->where($config['query'])
  618. ->order('id', 'desc')
  619. ->paginate(10, null, $config);
  620. $channelList = Channel::where('id', 'in', function ($query) use ($user_id) {
  621. $query->name('cms_archives')->where('user_id', $user_id)->field('channel_id');
  622. })->where('status', 'normal')->select();
  623. $this->success('', [
  624. 'archivesList' => $archivesList,
  625. 'channelList' => $channelList
  626. ]);
  627. }
  628. /**
  629. * 删除文档
  630. */
  631. public function delete()
  632. {
  633. if (!$this->request->isPost()) {
  634. $this->error('请求错误');
  635. }
  636. $id = (int)$this->request->post('id/d');
  637. if (!$id) {
  638. $this->error("参数不正确");
  639. }
  640. $archives = \addons\cms\model\Archives::where('id', $id)->where('user_id', $this->auth->id)->find();
  641. if (!$archives) {
  642. $this->error("未找到指定的文档");
  643. }
  644. Db::startTrans();
  645. try {
  646. $archives->delete();
  647. if ($archives->channel->items > 0) {
  648. $archives->channel->setDec("items");
  649. }
  650. Db::commit();
  651. } catch (\Exception $e) {
  652. Db::rollback();
  653. $this->error("删除文档失败");
  654. }
  655. $this->success("删除文档成功");
  656. }
  657. }