控制台应用,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.

Models.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. // | 模型模型
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\model\cms;
  15. use app\common\model\Modelbase;
  16. use think\Db;
  17. use think\Exception;
  18. use think\facade\Cache;
  19. use think\facade\Config;
  20. use think\Model;
  21. /**
  22. * 模型
  23. */
  24. class Models extends Modelbase
  25. {
  26. protected $name = 'model';
  27. protected $autoWriteTimestamp = true;
  28. protected static $ext_table = '_data';
  29. protected static function init()
  30. {
  31. //添加
  32. self::beforeInsert(function ($row) {
  33. $setting['category_template'] = $row->category_template;
  34. $setting['list_template'] = $row->list_template;
  35. $setting['show_template'] = $row->show_template;
  36. $row['setting'] = serialize($setting);
  37. $row['module'] = 'cms';
  38. $info = null;
  39. try {
  40. $info = Db::name($row['tablename'])->getPk();
  41. } catch (\Exception $e) {
  42. }
  43. if ($info) {
  44. throw new Exception("数据表已经存在");
  45. }
  46. });
  47. self::afterInsert(function ($row) {
  48. cache::set("Model", null);
  49. cache::set('ModelField', null);
  50. //创建模型表和模型字段
  51. if (self::createTable($row)) {
  52. self::addFieldRecord($row['id'], $row['type']);
  53. }
  54. });
  55. //编辑
  56. self::beforeUpdate(function ($row) {
  57. $changedData = $row->getChangedData();
  58. $setting = [];
  59. if (isset($changedData['category_template'])) {
  60. $setting['category_template'] = $row->category_template;
  61. }
  62. if (isset($changedData['list_template'])) {
  63. $setting['list_template'] = $row->list_template;
  64. }
  65. if (isset($changedData['show_template'])) {
  66. $setting['show_template'] = $row->show_template;
  67. }
  68. if ($setting) {
  69. $row['setting'] = serialize($setting);
  70. }
  71. if (isset($changedData['tablename'])) {
  72. $info = null;
  73. try {
  74. $info = Db::name($row['tablename'])->getPk();
  75. } catch (\Exception $e) {
  76. }
  77. if ($info) {
  78. throw new Exception("数据表已经存在");
  79. }
  80. try {
  81. $info = Db::name($row['tablename'] . self::$ext_table)->getPk();
  82. } catch (\Exception $e) {
  83. }
  84. if ($info) {
  85. throw new Exception("数据表已经存在");
  86. }
  87. }
  88. });
  89. self::afterUpdate(function ($row) {
  90. //更新缓存
  91. cache::set("Model", null);
  92. cache::set('ModelField', null);
  93. Cache::set('getModel_' . $row['id'], '');
  94. $changedData = $row->getChangedData();
  95. if (isset($changedData['tablename'])) {
  96. //表前缀
  97. $dbPrefix = Config::get("database.prefix");
  98. //表名更改
  99. Db::execute("RENAME TABLE `{$dbPrefix}{$row->getOrigin('tablename')}` TO `{$dbPrefix}{$changedData['tablename']}` ;");
  100. //修改副表
  101. if ($row['type'] == 2) {
  102. Db::execute("RENAME TABLE `{$dbPrefix}{$row->getOrigin('tablename')}_data` TO `{$dbPrefix}{$changedData['tablename']}_data` ;");
  103. }
  104. }
  105. });
  106. //删除
  107. self::beforeDelete(function ($row) {
  108. $exist = Category::where('modelid', $row['id'])->find();
  109. if ($exist) {
  110. throw new Exception("该模型使用中,删除栏目后再删除!");
  111. }
  112. });
  113. self::afterDelete(function ($row) {
  114. cache::set("Model", null);
  115. cache::set('ModelField', null);
  116. //删除所有和这个模型相关的字段
  117. Db::name("ModelField")->where("modelid", $row['id'])->delete();
  118. //删除主表
  119. $table_name = Config::get("database.prefix") . $row['tablename'];
  120. Db::execute("DROP TABLE IF EXISTS `{$table_name}`");
  121. if ((int) $row['type'] == 2) {
  122. //删除副表
  123. $table_name .= self::$ext_table;
  124. Db::execute("DROP TABLE IF EXISTS `{$table_name}`");
  125. }
  126. });
  127. }
  128. public function getSettingAttr($value, $data)
  129. {
  130. return unserialize($value);
  131. }
  132. /**
  133. * 创建内容模型
  134. */
  135. public static function createTable($data)
  136. {
  137. $data['tablename'] = strtolower($data['tablename']);
  138. $table = Config::get("database.prefix") . $data['tablename'];
  139. $sql = <<<EOF
  140. CREATE TABLE `{$table}` (
  141. `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '文档ID',
  142. `catid` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '栏目ID',
  143. `title` varchar(255) DEFAULT '' COMMENT '标题',
  144. `thumb` varchar(255) NOT NULL DEFAULT '' COMMENT '缩略图',
  145. `flag` varchar(32) DEFAULT '' COMMENT '属性',
  146. `keywords` varchar(255) DEFAULT '' COMMENT 'SEO关键词',
  147. `description` varchar(255) DEFAULT '' COMMENT 'SEO描述',
  148. `tags` varchar(255) DEFAULT '' COMMENT 'Tags标签',
  149. `listorder` smallint(5) unsigned NOT NULL DEFAULT '100' COMMENT '排序',
  150. `uid` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '用户id',
  151. `username` varchar(20) DEFAULT '' COMMENT '用户名',
  152. `sysadd` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否后台添加',
  153. `hits` mediumint(8) UNSIGNED DEFAULT 0 COMMENT '点击量' ,
  154. `create_time` int(10) unsigned DEFAULT NULL COMMENT '创建时间',
  155. `update_time` int(10) unsigned DEFAULT NULL COMMENT '更新时间',
  156. `url` varchar(100) NOT NULL DEFAULT '' COMMENT '链接地址',
  157. `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态',
  158. PRIMARY KEY (`id`),
  159. KEY `status` (`catid`,`status`)
  160. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='{$data['name']}模型表';
  161. EOF;
  162. $res = Db::execute($sql);
  163. if ($data['type'] == 2) {
  164. $table = $table . self::$ext_table;
  165. // 新建附属表
  166. $sql = <<<EOF
  167. CREATE TABLE `{$table}` (
  168. `did` mediumint(8) unsigned NOT NULL DEFAULT '0',
  169. `content` mediumtext COMMENT '内容',
  170. PRIMARY KEY (`did`)
  171. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='{$data['name']}模型表';
  172. EOF;
  173. $res = Db::execute($sql);
  174. }
  175. return true;
  176. }
  177. /**
  178. * 添加默认字段
  179. */
  180. public static function addFieldRecord($modelid, $type)
  181. {
  182. $fieldsArr = [];
  183. $default = [
  184. 'modelid' => $modelid,
  185. 'pattern' => '',
  186. 'errortips' => '',
  187. 'create_time' => time(),
  188. 'update_time' => time(),
  189. 'ifsystem' => 1,
  190. 'status' => 1,
  191. 'listorder' => 100,
  192. 'ifsearch' => 0,
  193. 'iffixed' => 1,
  194. 'remark' => '',
  195. 'isadd' => 0,
  196. 'iscore' => 0,
  197. 'ifrequire' => 0,
  198. 'setting' => null,
  199. ];
  200. $data = [
  201. [
  202. 'name' => 'id',
  203. 'title' => '文档id',
  204. 'type' => 'hidden',
  205. 'isadd' => 1,
  206. ],
  207. [
  208. 'name' => 'catid',
  209. 'title' => '栏目id',
  210. 'type' => 'hidden',
  211. 'isadd' => 1,
  212. ],
  213. [
  214. 'name' => 'uid',
  215. 'title' => '用户id',
  216. 'type' => 'number',
  217. 'iscore' => 1,
  218. ],
  219. [
  220. 'name' => 'username',
  221. 'title' => '用户名',
  222. 'type' => 'text',
  223. 'iscore' => 1,
  224. ],
  225. [
  226. 'name' => 'sysadd',
  227. 'title' => '是否后台添加',
  228. 'type' => 'number',
  229. 'iscore' => 1,
  230. ],
  231. [
  232. 'name' => 'title',
  233. 'title' => '标题',
  234. 'type' => 'text',
  235. 'ifsearch' => 1,
  236. 'ifrequire' => 1,
  237. 'setting' => "a:3:{s:6:\"define\";s:32:\"varchar(255) NOT NULL DEFAULT ''\";s:7:\"options\";s:0:\"\";s:5:\"value\";s:0:\"\";}",
  238. 'isadd' => 1,
  239. 'listorder' => 300,
  240. ],
  241. [
  242. 'name' => 'flag',
  243. 'title' => '属性',
  244. 'type' => 'checkbox',
  245. 'setting' => "a:3:{s:6:\"define\";s:31:\"varchar(32) NOT NULL DEFAULT ''\";s:7:\"options\";s:76:\"1:置顶[1]\r\n2:头条[2]\r\n3:特荐[3]\r\n4:推荐[4]\r\n5:热点[5]\r\n6:幻灯[6]\";s:5:\"value\";s:0:\"\";}",
  246. 'listorder' => 295,
  247. ],
  248. [
  249. 'name' => 'keywords',
  250. 'title' => 'SEO关键词',
  251. 'type' => 'tags',
  252. 'iffixed' => 0,
  253. 'remark' => '关键词用回车确认',
  254. 'setting' => "a:3:{s:6:\"define\";s:32:\"varchar(255) NOT NULL DEFAULT ''\";s:7:\"options\";s:0:\"\";s:5:\"value\";s:0:\"\";}",
  255. 'isadd' => 1,
  256. 'listorder' => 290,
  257. ],
  258. [
  259. 'name' => 'description',
  260. 'title' => 'SEO摘要',
  261. 'type' => 'textarea',
  262. 'iffixed' => 0,
  263. 'remark' => '如不填写,则自动截取附表中编辑器的200字符',
  264. 'setting' => "a:3:{s:6:\"define\";s:32:\"varchar(255) NOT NULL DEFAULT ''\";s:7:\"options\";s:0:\"\";s:5:\"value\";s:0:\"\";}",
  265. 'isadd' => 1,
  266. 'listorder' => 285,
  267. ],
  268. [
  269. 'name' => 'tags',
  270. 'title' => 'Tags标签',
  271. 'type' => 'tags',
  272. 'iffixed' => 0,
  273. 'remark' => '关键词用回车确认',
  274. 'setting' => "a:3:{s:6:\"define\";s:32:\"varchar(255) NOT NULL DEFAULT ''\";s:7:\"options\";s:0:\"\";s:5:\"value\";s:0:\"\";}",
  275. 'listorder' => 280,
  276. ],
  277. [
  278. 'name' => 'thumb',
  279. 'title' => '缩略图',
  280. 'type' => 'image',
  281. 'ifrequire' => 0,
  282. 'iffixed' => 0,
  283. 'setting' => "a:3:{s:6:\"define\";s:13:\"text NOT NULL\";s:7:\"options\";s:0:\"\";s:5:\"value\";s:0:\"\";}",
  284. 'isadd' => 1,
  285. 'listorder' => 275,
  286. ],
  287. [
  288. 'name' => 'hits',
  289. 'title' => '点击量',
  290. 'type' => 'number',
  291. 'listorder' => 265,
  292. 'setting' => "a:3:{s:6:\"define\";s:42:\"mediumint(8) UNSIGNED NOT NULL DEFAULT '0'\";s:7:\"options\";s:0:\"\";s:5:\"value\";s:1:\"0\";}",
  293. ],
  294. [
  295. 'name' => 'listorder',
  296. 'title' => '排序',
  297. 'type' => 'number',
  298. 'setting' => "a:3:{s:6:\"define\";s:40:\"tinyint(3) UNSIGNED NOT NULL DEFAULT '0'\";s:7:\"options\";s:0:\"\";s:5:\"value\";s:3:\"100\";}",
  299. 'listorder' => 260,
  300. ],
  301. [
  302. 'name' => 'url',
  303. 'title' => '链接地址',
  304. 'type' => 'text',
  305. 'listorder' => 255,
  306. 'setting' => "a:3:{s:6:\"define\";s:32:\"varchar(100) NOT NULL DEFAULT ''\";s:7:\"options\";s:0:\"\";s:5:\"value\";s:0:\"\";}",
  307. 'remark' => '有值时生效,内部链接格式:模块/控制器/操作?参数=参数值&...,外部链接则必须http://开头',
  308. ],
  309. [
  310. 'name' => 'create_time',
  311. 'title' => '创建时间',
  312. 'type' => 'datetime',
  313. 'listorder' => 250,
  314. 'setting' => "a:3:{s:6:\"define\";s:29:\"int(10) UNSIGNED DEFAULT NULL\";s:7:\"options\";s:0:\"\";s:5:\"value\";s:0:\"\";}",
  315. ],
  316. [
  317. 'name' => 'update_time',
  318. 'title' => '更新时间',
  319. 'type' => 'datetime',
  320. 'listorder' => 245,
  321. 'setting' => "a:3:{s:6:\"define\";s:29:\"int(10) UNSIGNED DEFAULT NULL\";s:7:\"options\";s:0:\"\";s:5:\"value\";s:0:\"\";}",
  322. 'iscore' => 1,
  323. ],
  324. [
  325. 'name' => 'status',
  326. 'title' => '状态',
  327. 'type' => 'radio',
  328. 'setting' => "a:3:{s:6:\"define\";s:40:\"tinyint(1) UNSIGNED NOT NULL DEFAULT '0'\";s:7:\"options\";s:18:\"0:禁用\r\n1:启用\";s:5:\"value\";s:1:\"1\";}",
  329. 'listorder' => 240,
  330. ],
  331. ];
  332. if ($type == 2) {
  333. array_push($data, [
  334. 'name' => 'did',
  335. 'title' => '附表文档id',
  336. 'type' => 'hidden',
  337. 'iscore' => 1,
  338. 'ifsystem' => 0,
  339. ],
  340. [
  341. 'name' => 'content',
  342. 'title' => '内容',
  343. 'type' => 'Ueditor',
  344. 'ifsystem' => 0,
  345. 'iffixed' => 0,
  346. 'setting' => "a:3:{s:6:\"define\";s:13:\"text NOT NULL\";s:7:\"options\";s:0:\"\";s:5:\"value\";s:0:\"\";}",
  347. 'isadd' => 1,
  348. 'listorder' => 270,
  349. ]);
  350. }
  351. foreach ($data as $item) {
  352. $fieldsArr[] = array_merge($default, $item);
  353. //Db::name('model_field')->insert($item);
  354. }
  355. Db::name('model_field')->insertAll($fieldsArr);
  356. return true;
  357. }
  358. }