Api.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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) {
  51. $data['channel_id'] = $channel->id;
  52. } else {
  53. $this->error('栏目未找到');
  54. }
  55. unset($data['channel']);
  56. } else {
  57. $channel_id = $this->request->request('channel_id');
  58. $channel = Channel::get($channel_id);
  59. if (!$channel) {
  60. $this->error('栏目未找到');
  61. }
  62. }
  63. $model = Modelx::get($channel['model_id']);
  64. if (!$model) {
  65. $this->error('模型未找到');
  66. }
  67. $data['model_id'] = $model['id'];
  68. $data['content'] = $this->request->post("content", "", "trim");
  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. $fieldsContentList = Fields::getFieldsContentList('model', $model->id);
  112. Service::appendTextAttr($fieldsContentList, $addon);
  113. }
  114. $archives->setData($addon);
  115. } else {
  116. $this->error('文章副表数据未找到');
  117. }
  118. $content = $archives->content;
  119. //移除分页数据
  120. $content = str_replace("##pagebreak##", "<br>", $content);
  121. $archives->content = $content;
  122. $this->success(__('读取成功'), '', $archives->toArray());
  123. }
  124. /**
  125. * 读取文章列表
  126. */
  127. public function arclist()
  128. {
  129. $params = [];
  130. $model = (int)$this->request->request('model');
  131. $channel = (int)$this->request->request('channel');
  132. $page = (int)$this->request->request('page');
  133. $pagesize = (int)$this->request->request('pagesize');
  134. $pagesize = $pagesize ? $pagesize : 10;
  135. if ($model) {
  136. $params['model'] = $model;
  137. }
  138. if ($channel) {
  139. $params['channel'] = $channel;
  140. }
  141. $page = max(1, $page);
  142. $params['limit'] = ($page - 1) * $pagesize . ',' . $pagesize;
  143. $list = Archives::getArchivesList($params);
  144. $list = collection($list)->toArray();
  145. foreach ($list as $index => &$item) {
  146. $item['url'] = $item['fullurl'];
  147. unset($item['imglink'], $item['textlink'], $item['channellink'], $item['taglist'], $item['weigh'], $item['status'], $item['deletetime'], $item['memo'], $item['img']);
  148. }
  149. $this->success('读取成功', '', $list);
  150. }
  151. /**
  152. * 获取栏目列表
  153. */
  154. public function channel()
  155. {
  156. $channelList = Channel::where('status', '<>', 'hidden')
  157. ->where('type', 'list')
  158. ->order('weigh DESC,id DESC')
  159. ->column('id,name');
  160. $this->success(__('读取成功'), '', $channelList);
  161. }
  162. /**
  163. * 评论数据写入接口
  164. */
  165. public function comment()
  166. {
  167. try {
  168. $params = $this->request->post();
  169. \addons\cms\model\Comment::postComment($params);
  170. } catch (Exception $e) {
  171. $this->error($e->getMessage());
  172. }
  173. $this->success(__('评论成功'), '');
  174. }
  175. /**
  176. * 自定义表单数据写入接口
  177. */
  178. public function diyform()
  179. {
  180. $id = $this->request->request("diyform_id/d");
  181. $diyform = \addons\cms\model\Diyform::get($id);
  182. if (!$diyform || $diyform['status'] != 'normal') {
  183. $this->error("自定义表单未找到");
  184. }
  185. //是否需要登录判断
  186. if ($diyform['needlogin'] && !$this->auth->isLogin()) {
  187. $this->error("请登录后再操作");
  188. }
  189. $diydata = new Diydata([], $diyform);
  190. if (!$diydata) {
  191. $this->error("自定义表未找到");
  192. }
  193. $data = $this->request->request();
  194. try {
  195. $diydata->allowField(true)->save($data);
  196. } catch (Exception $e) {
  197. $this->error("数据提交失败");
  198. }
  199. $this->success("数据提交成功", $diyform['redirecturl'] ? $diyform['redirecturl'] : addon_url('cms/index/index'));
  200. }
  201. }