Api.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace addons\cms\controller;
  3. use addons\cms\library\Service;
  4. use addons\cms\model\Archives;
  5. use addons\cms\model\Channel;
  6. use addons\cms\model\Diydata;
  7. use addons\cms\model\Fields;
  8. use addons\cms\model\Modelx;
  9. use think\Config;
  10. use think\Db;
  11. use think\Exception;
  12. use think\exception\PDOException;
  13. /**
  14. * Api接口控制器
  15. *
  16. * 仅限用于数据迁移或内部接口,不建议用于对接外部API接口
  17. * Class Api
  18. * @package addons\cms\controller
  19. */
  20. class Api extends Base
  21. {
  22. public function _initialize()
  23. {
  24. Config::set('default_return_type', 'json');
  25. $apikey = $this->request->post('apikey');
  26. $config = get_addon_config('cms');
  27. if (!$config['apikey']) {
  28. $this->error('请先在后台配置API密钥');
  29. }
  30. if ($config['apikey'] != $apikey) {
  31. $this->error('密钥不正确');
  32. }
  33. return parent::_initialize();
  34. }
  35. /**
  36. * 文档数据写入接口
  37. */
  38. public function index()
  39. {
  40. $data = $this->request->post();
  41. if (isset($data['user']) && $data['user']) {
  42. $user = \app\common\model\User::where('nickname', $data['user'])->find();
  43. if ($user) {
  44. $data['user_id'] = $user->id;
  45. }
  46. }
  47. //如果有传栏目名称
  48. if (isset($data['channel']) && $data['channel']) {
  49. $channel = Channel::where('name', $data['channel'])->find();
  50. if (!$channel || $channel['status'] != 'normal') {
  51. $this->error('栏目未找到');
  52. }
  53. $data['channel_id'] = $channel->id;
  54. unset($data['channel']);
  55. } else {
  56. $channel_id = $this->request->request('channel_id');
  57. $channel = Channel::get($channel_id);
  58. if (!$channel || $channel['status'] != 'normal') {
  59. $this->error('栏目未找到');
  60. }
  61. }
  62. $model = Modelx::get($channel['model_id']);
  63. if (!$model) {
  64. $this->error('模型未找到');
  65. }
  66. $data['model_id'] = $model['id'];
  67. $data['content'] = $this->request->post("content", "", "trim");
  68. $data['publishtime'] = time();
  69. $data['weigh'] = 0;
  70. Db::startTrans();
  71. try {
  72. //副表数据插入会在模型事件中完成
  73. $archives = new \app\admin\model\cms\Archives;
  74. $archives->allowField(true)->save($data);
  75. Db::commit();
  76. $data = [
  77. 'id' => $archives->id,
  78. 'url' => $archives->fullurl
  79. ];
  80. } catch (PDOException $e) {
  81. Db::rollback();
  82. $this->error($e->getMessage());
  83. } catch (Exception $e) {
  84. Db::rollback();
  85. $this->error($e->getMessage());
  86. }
  87. $this->success('新增成功', '', $data);
  88. return;
  89. }
  90. /**
  91. * 读取文章数据
  92. */
  93. public function archives()
  94. {
  95. $id = $this->request->request("id/d");
  96. $archives = Archives::get($id, ['channel']);
  97. if (!$archives || $archives['status'] != 'normal' || $archives['deletetime']) {
  98. $this->error("文章未找到");
  99. }
  100. $channel = Channel::get($archives['channel_id']);
  101. if (!$channel) {
  102. $this->error("栏目未找到");
  103. }
  104. $model = Modelx::get($channel['model_id'], [], true);
  105. if (!$model) {
  106. $this->error("文章模型未找到");
  107. }
  108. $addon = db($model['table'])->where('id', $archives['id'])->find();
  109. if ($addon) {
  110. if ($model->fields) {
  111. Service::appendTextAndList('model', $model->id, $addon);
  112. }
  113. $archives->setData($addon);
  114. } else {
  115. $this->error('文章副表数据未找到');
  116. }
  117. $content = $archives->content;
  118. //移除分页数据
  119. $content = str_replace("##pagebreak##", "<br>", $content);
  120. $archives->content = $content;
  121. $this->success(__('读取成功'), '', $archives->toArray());
  122. }
  123. /**
  124. * 读取文章列表
  125. */
  126. public function arclist()
  127. {
  128. $params = [];
  129. $model = (int)$this->request->request('model');
  130. $channel = (int)$this->request->request('channel');
  131. $page = (int)$this->request->request('page');
  132. $pagesize = (int)$this->request->request('pagesize');
  133. $pagesize = $pagesize ? $pagesize : 10;
  134. if ($model) {
  135. $params['model'] = $model;
  136. }
  137. if ($channel) {
  138. $channelInfo = Channel::get($channel);
  139. if (!$channelInfo || $channelInfo['status'] != 'normal') {
  140. $channel = -1;
  141. }
  142. $params['channel'] = $channel;
  143. }
  144. $page = max(1, $page);
  145. $params['limit'] = ($page - 1) * $pagesize . ',' . $pagesize;
  146. $list = Archives::getArchivesList($params);
  147. $list = collection($list)->toArray();
  148. foreach ($list as $index => &$item) {
  149. $item['url'] = $item['fullurl'];
  150. unset($item['imglink'], $item['textlink'], $item['channellink'], $item['taglist'], $item['weigh'], $item['status'], $item['deletetime'], $item['memo'], $item['img']);
  151. }
  152. $this->success('读取成功', '', $list);
  153. }
  154. /**
  155. * 获取栏目列表
  156. */
  157. public function channel()
  158. {
  159. $channelList = Channel::where('status', 'normal')
  160. ->where('type', 'list')
  161. ->order('weigh DESC,id DESC')
  162. ->column('id,name');
  163. $this->success(__('读取成功'), '', $channelList);
  164. }
  165. /**
  166. * 评论数据写入接口
  167. */
  168. public function comment()
  169. {
  170. try {
  171. $params = $this->request->post();
  172. \addons\cms\model\Comment::postComment($params);
  173. } catch (Exception $e) {
  174. $this->error($e->getMessage());
  175. }
  176. $this->success(__('评论成功'), '');
  177. }
  178. /**
  179. * 自定义表单数据写入接口
  180. */
  181. public function diyform()
  182. {
  183. $id = $this->request->request("diyform_id/d");
  184. $diyform = \addons\cms\model\Diyform::get($id);
  185. if (!$diyform || $diyform['status'] != 'normal') {
  186. $this->error("自定义表单未找到");
  187. }
  188. //是否需要登录判断
  189. if ($diyform['needlogin'] && !$this->auth->isLogin()) {
  190. $this->error("请登录后再操作", "index/user/login");
  191. }
  192. $diydata = new Diydata([], $diyform);
  193. if (!$diydata) {
  194. $this->error("自定义表未找到");
  195. }
  196. $data = $this->request->request();
  197. try {
  198. $diydata->allowField(true)->save($data);
  199. } catch (Exception $e) {
  200. $this->error("数据提交失败");
  201. }
  202. $this->success("数据提交成功", $diyform['redirecturl'] ? $diyform['redirecturl'] : addon_url('cms/index/index'));
  203. }
  204. }