控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Cms.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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\admin\model\cms;
  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. //添加用户名
  66. $data['uid'] = \app\admin\service\User::instance()->id;
  67. $data['username'] = \app\admin\service\User::instance()->username;
  68. $data['sysadd'] = 1;
  69. //处理数据
  70. $dataAll = Service::dealModelPostData($modelid, $data, $dataExt);
  71. list($data, $dataExt) = $dataAll;
  72. if (!isset($data['create_time'])) {
  73. $data['create_time'] = request()->time();
  74. }
  75. if (!isset($data['update_time'])) {
  76. $data['update_time'] = request()->time();
  77. }
  78. try {
  79. //主表
  80. $id = Db::name($tablename)->insertGetId($data);
  81. //TAG标签处理
  82. if (!empty($data['tags'])) {
  83. $this->tagDispose($data['tags'], $id, $catid, $modelid);
  84. }
  85. //附表
  86. if (!empty($dataExt)) {
  87. $dataExt['did'] = $id;
  88. Db::name($tablename . $this->ext_table)->insert($dataExt);
  89. }
  90. } catch (\Exception $e) {
  91. throw new \Exception($e->getMessage());
  92. }
  93. //更新栏目统计数据
  94. $this->updateCategoryItems($catid, 'add', 1);
  95. //推送到站点聚合插件
  96. if (self::$config['web_site_weburlpush']) {
  97. hook("weburlpush", buildContentUrl($catid, $id, $data['url'], true, true));
  98. }
  99. //新增讯搜索引
  100. if (self::$config['web_site_searchtype'] === 'xunsearch') {
  101. FulltextSearch::update($modelid, $catid, $id, $data, $dataExt);
  102. }
  103. return $id;
  104. }
  105. //编辑模型内容
  106. public function editModelData($data, $dataExt = [])
  107. {
  108. $catid = (int) $data['catid'];
  109. $id = (int) $data['id'];
  110. $modelid = getCategory($catid, 'modelid');
  111. //完整表名获取
  112. $tablename = $this->getModelTableName($modelid);
  113. if (!$this->table_exists($tablename)) {
  114. throw new \Exception('数据表不存在!');
  115. }
  116. Service::getAfterText($data, $dataExt);
  117. //TAG标签处理
  118. if (!empty($data['tags'])) {
  119. $this->tagDispose($data['tags'], $id, $catid, $modelid);
  120. } else {
  121. $this->tagDispose([], $id, $catid, $modelid);
  122. }
  123. $dataAll = Service::dealModelPostData($modelid, $data, $dataExt);
  124. list($data, $dataExt) = $dataAll;
  125. if (!isset($data['update_time'])) {
  126. $data['update_time'] = request()->time();
  127. }
  128. //主表
  129. Db::name($tablename)->where('id', $id)->update($data);
  130. //附表
  131. if (!empty($dataExt)) {
  132. //查询是否存在ID 不存在则新增
  133. if (Db::name($tablename . $this->ext_table)->where('did', $id)->find()) {
  134. Db::name($tablename . $this->ext_table)->where('did', $id)->update($dataExt);
  135. } else {
  136. $dataExt['did'] = $id;
  137. Db::name($tablename . $this->ext_table)->insert($dataExt);
  138. };
  139. }
  140. //标签
  141. hook('content_edit_end', $data);
  142. //更新讯搜索引
  143. if (self::$config['web_site_searchtype'] === 'xunsearch') {
  144. FulltextSearch::update($modelid, $catid, $id, $data, $dataExt);
  145. }
  146. }
  147. //删除模型内容
  148. public function deleteModelData($modeId, $id, $no_delete = false)
  149. {
  150. $modelInfo = cache('Model');
  151. $modelInfo = $modelInfo[$modeId];
  152. $data = Db::name($modelInfo['tablename'])->where('id', $id)->find();
  153. if (empty($data)) {
  154. throw new \Exception("该信息不存在!");
  155. }
  156. //处理tags
  157. if (!empty($data['tags'])) {
  158. $this->tagDispose([], $data['id'], $data['catid'], $modeId);
  159. }
  160. if ($no_delete) {
  161. Db::name($modelInfo['tablename'])->where('id', $id)->setField('status', -1);
  162. } else {
  163. Db::name($modelInfo['tablename'])->where('id', $id)->delete();
  164. if (2 == $modelInfo['type']) {
  165. Db::name($modelInfo['tablename'] . $this->ext_table)->where('did', $id)->delete();
  166. }
  167. //更新栏目统计
  168. $this->updateCategoryItems($data['catid'], 'delete');
  169. }
  170. //标签
  171. hook('content_delete_end', $data);
  172. //更新讯搜索引
  173. if (self::$config['web_site_searchtype'] === 'xunsearch') {
  174. FulltextSearch::del($data['catid'], $id);
  175. }
  176. }
  177. /**
  178. * TAG标签处理
  179. */
  180. private function tagDispose($tags, $id, $catid, $modelid)
  181. {
  182. $tags_mode = model('admin/cms/Tags');
  183. if (!empty($tags)) {
  184. if (strpos($tags, ',') === false) {
  185. $keyword = explode(' ', $tags);
  186. } else {
  187. $keyword = explode(',', $tags);
  188. }
  189. $keyword = array_unique($keyword);
  190. if ('add' == request()->action()) {
  191. $tags_mode->addTag($keyword, $id, $catid, $modelid);
  192. } else {
  193. $tags_mode->updata($keyword, $id, $catid, $modelid);
  194. }
  195. } else {
  196. //直接清除已有的tags
  197. $tags_mode->deleteAll($id, $catid, $modelid);
  198. }
  199. }
  200. private function updateCategoryItems($catid, $action = 'add', $cache = 0)
  201. {
  202. if ($action == 'add') {
  203. Db::name('Category')->where('id', $catid)->setInc('items');
  204. } else {
  205. Db::name('Category')->where('id', $catid)->where('items', '>', 0)->setDec('items');
  206. }
  207. }
  208. }