控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Cms.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | CMS模型
  13. // +----------------------------------------------------------------------
  14. namespace app\cms\model;
  15. use addons\cms\library\FulltextSearch;
  16. use addons\cms\library\Service;
  17. use app\common\model\Modelbase;
  18. use think\Db;
  19. use think\Model;
  20. /**
  21. * 模型
  22. */
  23. class Cms extends Modelbase
  24. {
  25. protected $autoWriteTimestamp = true;
  26. protected $insert = ['status' => 1];
  27. protected $ext_table = '_data';
  28. protected $name = 'ModelField';
  29. protected static $config = [];
  30. protected static function init()
  31. {
  32. self::$config = get_addon_config('cms');
  33. }
  34. /**
  35. * 根据模型ID,返回表名
  36. * @param type $modelid
  37. * @param type $modelid
  38. * @return string
  39. */
  40. protected function getModelTableName($modelid, $ifsystem = 1)
  41. {
  42. $model_cache = cache("Model");
  43. //表名获取
  44. $model_table = isset($model_cache[$modelid]['tablename']) ? ucwords($model_cache[$modelid]['tablename']) : '';
  45. //完整表名获取 判断主表 还是副表
  46. $tablename = $ifsystem ? $model_table : $model_table . $this->ext_table;
  47. return $tablename;
  48. }
  49. //添加模型内容
  50. public function addModelData($data, $dataExt = [])
  51. {
  52. $catid = (int) $data['catid'];
  53. if (isset($data['modelid'])) {
  54. $modelid = $data['modelid'];
  55. unset($data['modelid']);
  56. } else {
  57. $modelid = getCategory($catid, 'modelid');
  58. }
  59. //完整表名获取
  60. $tablename = $this->getModelTableName($modelid);
  61. if (!$this->table_exists($tablename)) {
  62. throw new \Exception('数据表不存在!');
  63. }
  64. Service::getAfterText($data, $dataExt);
  65. $data['uid'] = \app\member\service\User::instance()->id;
  66. $data['username'] = \app\member\service\User::instance()->username;
  67. $data['sysadd'] = 0;
  68. //处理数据
  69. $dataAll = Service::dealModelPostData($modelid, $data, $dataExt);
  70. list($data, $dataExt) = $dataAll;
  71. if (!isset($data['create_time'])) {
  72. $data['create_time'] = request()->time();
  73. }
  74. if (!isset($data['update_time'])) {
  75. $data['update_time'] = request()->time();
  76. }
  77. try {
  78. //主表
  79. $id = Db::name($tablename)->insertGetId($data);
  80. //TAG标签处理
  81. if (!empty($data['tags'])) {
  82. $this->tagDispose($data['tags'], $id, $catid, $modelid);
  83. }
  84. //附表
  85. if (!empty($dataExt)) {
  86. $dataExt['did'] = $id;
  87. Db::name($tablename . $this->ext_table)->insert($dataExt);
  88. }
  89. } catch (\Exception $e) {
  90. throw new \Exception($e->getMessage());
  91. }
  92. //更新栏目统计数据
  93. $this->updateCategoryItems($catid, 'add', 1);
  94. //推送到站的聚合插件
  95. if (self::$config['web_site_weburlpush']) {
  96. hook("weburlpush", buildContentUrl($catid, $id, $data['url'], true, true));
  97. }
  98. //新增讯搜索引
  99. if (self::$config['web_site_searchtype'] === 'xunsearch') {
  100. FulltextSearch::update($modelid, $catid, $id, $data, $dataExt);
  101. }
  102. return $id;
  103. }
  104. //编辑模型内容
  105. public function editModelData($data, $dataExt = [])
  106. {
  107. $catid = (int) $data['catid'];
  108. $id = (int) $data['id'];
  109. $modelid = getCategory($catid, 'modelid');
  110. //完整表名获取
  111. $tablename = $this->getModelTableName($modelid);
  112. if (!$this->table_exists($tablename)) {
  113. throw new \Exception('数据表不存在!');
  114. }
  115. Service::getAfterText($data, $dataExt);
  116. //TAG标签处理
  117. if (!empty($data['tags'])) {
  118. $this->tagDispose($data['tags'], $id, $catid, $modelid);
  119. } else {
  120. $this->tagDispose([], $id, $catid, $modelid);
  121. }
  122. $dataAll = Service::dealModelPostData($modelid, $data, $dataExt);
  123. list($data, $dataExt) = $dataAll;
  124. if (!isset($data['update_time'])) {
  125. $data['update_time'] = request()->time();
  126. }
  127. //主表
  128. Db::name($tablename)->where('id', $id)->update($data);
  129. //附表
  130. if (!empty($dataExt)) {
  131. //查询是否存在ID 不存在则新增
  132. if (Db::name($tablename . $this->ext_table)->where('did', $id)->find()) {
  133. Db::name($tablename . $this->ext_table)->where('did', $id)->update($dataExt);
  134. } else {
  135. $dataExt['did'] = $id;
  136. Db::name($tablename . $this->ext_table)->insert($dataExt);
  137. };
  138. }
  139. //标签
  140. hook('content_edit_end', $data);
  141. //更新讯搜索引
  142. if (self::$config['web_site_searchtype'] === 'xunsearch') {
  143. FulltextSearch::update($modelid, $catid, $id, $data, $dataExt);
  144. }
  145. }
  146. //删除模型内容
  147. public function deleteModelData($modeId, $id, $no_delete = false)
  148. {
  149. $modelInfo = cache('Model');
  150. $modelInfo = $modelInfo[$modeId];
  151. $data = Db::name($modelInfo['tablename'])->where('id', $id)->find();
  152. if (empty($data)) {
  153. throw new \Exception("该信息不存在!");
  154. }
  155. //处理tags
  156. if (!empty($data['tags'])) {
  157. $this->tagDispose([], $data['id'], $data['catid'], $modeId);
  158. }
  159. if ($no_delete) {
  160. Db::name($modelInfo['tablename'])->where('id', $id)->setField('status', -1);
  161. } else {
  162. Db::name($modelInfo['tablename'])->where('id', $id)->delete();
  163. if (2 == $modelInfo['type']) {
  164. Db::name($modelInfo['tablename'] . $this->ext_table)->where('did', $id)->delete();
  165. }
  166. //更新栏目统计
  167. $this->updateCategoryItems($data['catid'], 'delete');
  168. }
  169. //标签
  170. hook('content_delete_end', $data);
  171. //更新讯搜索引
  172. if (self::$config['web_site_searchtype'] === 'xunsearch') {
  173. FulltextSearch::del($data['catid'], $id);
  174. }
  175. }
  176. /**
  177. * 列表页
  178. * @param $modeId [模型ID]
  179. * @param $where [查询条件]
  180. * @param $moreifo [是否含附表]
  181. * @param $field []
  182. * @param $order []
  183. * @param $limit [条数]
  184. * @param $page [是否有分页]
  185. * @param int|bool $simple 是否简洁模式或者总记录数
  186. * @param array $config 配置参数
  187. */
  188. public function getList($modeId, $where, $moreifo, $field = '*', $order = '', $limit = 10, $page = null, $simple = false, $config = [])
  189. {
  190. $url_mode = self::$config['site_url_mode'];
  191. $config['query'] = isset($config['query']) ? array_merge($config['query'], request()->get()) : request()->get();
  192. $tableName = $this->getModelTableName($modeId);
  193. $result = [];
  194. if (isset($tableName) && !empty($tableName)) {
  195. if (2 == getModel($modeId, 'type') && $moreifo) {
  196. $extTable = $tableName . $this->ext_table;
  197. $cmsModel = Db::view($tableName, $field)->where($where)->view($extTable, '*', $tableName . '.id=' . $extTable . '.did', 'LEFT')->order($order);
  198. if ($page) {
  199. $result = $cmsModel->paginate($limit, $simple, $config);
  200. } else {
  201. $result = $cmsModel->limit($limit)->select();
  202. }
  203. } else {
  204. $cmsModel = Db::name($tableName)->where($where)->order($order)->field($field);
  205. if ($page) {
  206. $result = $cmsModel->paginate($limit, $simple, $config);
  207. } else {
  208. $result = $cmsModel->limit($limit)->select();
  209. }
  210. }
  211. }
  212. //数据格式化处理
  213. if (!empty($result)) {
  214. $ModelField = cache('ModelField');
  215. $Category = cache('Category');
  216. foreach ($result as $key => $vo) {
  217. $vo = Service::dealModelShowData($ModelField[$modeId], $vo);
  218. $cat = $url_mode == 1 ? $vo['catid'] : (isset($Category[$vo['catid']]) ? $Category[$vo['catid']]['catdir'] : getCategory($vo['catid'], 'catdir'));
  219. $vo['url'] = buildContentUrl($cat, $vo['id'], $vo['url']);
  220. $result[$key] = $vo;
  221. }
  222. }
  223. return $result;
  224. }
  225. /**
  226. * 详情页
  227. * @param [type] $modeId [模型ID]
  228. * @param [type] $where [查询条件]
  229. * @param boolean $moreifo [是否含附表]
  230. * @param string $field []
  231. * @param string $order []
  232. */
  233. public function getContent($modeId, $where, $moreifo = false, $field = '*', $order = '', $cache = false)
  234. {
  235. $url_mode = self::$config['site_url_mode'];
  236. $tableName = $this->getModelTableName($modeId);
  237. if (2 == getModel($modeId, 'type') && $moreifo) {
  238. $extTable = $tableName . $this->ext_table;
  239. $dataInfo = Db::view($tableName, '*')->where($where)->cache($cache)->view($extTable, '*', $tableName . '.id=' . $extTable . '.did', 'LEFT')->find();
  240. } else {
  241. $dataInfo = Db::name($tableName)->field($field)->cache($cache)->where($where)->order($order)->find();
  242. }
  243. if (!empty($dataInfo)) {
  244. $ModelField = cache('ModelField');
  245. $Category = cache('Category');
  246. $dataInfo = Service::dealModelShowData($ModelField[$modeId], $dataInfo);
  247. $cat = $url_mode == 1 ? $dataInfo['catid'] : (isset($Category[$dataInfo['catid']]) ? $Category[$dataInfo['catid']]['catdir'] : getCategory($dataInfo['catid'], 'catdir'));
  248. $dataInfo['url'] = buildContentUrl($cat, $dataInfo['id'], $dataInfo['url']);
  249. }
  250. return $dataInfo;
  251. }
  252. /**
  253. * TAG标签处理
  254. */
  255. private function tagDispose($tags, $id, $catid, $modelid)
  256. {
  257. $tags_mode = model('cms/Tags');
  258. if (!empty($tags)) {
  259. if (strpos($tags, ',') === false) {
  260. $keyword = explode(' ', $tags);
  261. } else {
  262. $keyword = explode(',', $tags);
  263. }
  264. $keyword = array_unique($keyword);
  265. if ('add' == request()->action()) {
  266. $tags_mode->addTag($keyword, $id, $catid, $modelid);
  267. } else {
  268. $tags_mode->updata($keyword, $id, $catid, $modelid);
  269. }
  270. } else {
  271. //直接清除已有的tags
  272. $tags_mode->deleteAll($id, $catid, $modelid);
  273. }
  274. }
  275. private function updateCategoryItems($catid, $action = 'add', $cache = 0)
  276. {
  277. if ($action == 'add') {
  278. Db::name('Category')->where('id', $catid)->setInc('items');
  279. } else {
  280. Db::name('Category')->where('id', $catid)->where('items', '>', 0)->setDec('items');
  281. }
  282. }
  283. }