深圳全飞鸿

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 103|回复: 5
打印 上一主题 下一主题

fastadmin生产代码的分析01

[复制链接]

228

主题

466

帖子

2184

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2184
跳转到指定楼层
楼主
发表于 2025-1-17 22:20:12 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
php think crud -t test


一、在项目的根目录里执行(不是在Public目录)


二、执行成功后,比对代码变化
  1. Execute start!
  2. [COPY  ]$\application\admin\controller\Test.php to #\application\admin\controller\Test.php
  3. [COPY  ]$\application\admin\lang\zh-cn\test.php to #\application\admin\lang\zh-cn\test.php
  4. [COPY  ]$\application\admin\model\Test.php to #\application\admin\model\Test.php
  5. [COPY  ]$\application\admin\validate\Test.php to #\application\admin\validate\Test.php
  6. [MKDIR]$\application\admin\view\test\
  7. [COPY  ]$\application\admin\view\test\add.html to #\application\admin\view\test\add.html
  8. [COPY  ]$\application\admin\view\test\edit.html to #\application\admin\view\test\edit.html
  9. [COPY  ]$\application\admin\view\test\index.html to #\application\admin\view\test\index.html
  10. [COPY  ]$\application\admin\view\test\recyclebin.html to #\application\admin\view\test\recyclebin.html
  11. [COPY  ]$\public\assets\js\backend\test.js to #\public\assets\js\backend\test.js
  12. Execute finish!
复制代码


回复

使用道具 举报

228

主题

466

帖子

2184

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2184
沙发
 楼主| 发表于 2025-1-17 22:21:39 | 只看该作者
C:\Apache24\htdocs\fastadmin\application\admin\validate\Test.php
  1. namespace app\admin\validate;

  2. use think\Validate;

  3. class Test extends Validate
  4. {
  5.     /**
  6.      * 验证规则
  7.      */
  8.     protected $rule = [
  9.     ];
  10.     /**
  11.      * 提示消息
  12.      */
  13.     protected $message = [
  14.     ];
  15.     /**
  16.      * 验证场景
  17.      */
  18.     protected $scene = [
  19.         'add'  => [],
  20.         'edit' => [],
  21.     ];
  22.    
  23. }
复制代码
回复 支持 反对

使用道具 举报

228

主题

466

帖子

2184

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2184
板凳
 楼主| 发表于 2025-1-17 22:25:08 | 只看该作者
C:\Apache24\htdocs\fastadmin\application\admin\controller\Test.php
  1. namespace app\admin\controller;

  2. use app\common\controller\Backend;

  3. /**
  4. * 测试管理
  5. *
  6. * @icon fa fa-circle-o
  7. */
  8. class Test extends Backend
  9. {

  10.     /**
  11.      * Test模型对象
  12.      * @var \app\admin\model\Test
  13.      */
  14.     protected $model = null;

  15.     public function _initialize()
  16.     {
  17.         parent::_initialize();
  18.         $this->model = new \app\admin\model\Test;
  19.         $this->view->assign("weekList", $this->model->getWeekList());
  20.         $this->view->assign("flagList", $this->model->getFlagList());
  21.         $this->view->assign("genderdataList", $this->model->getGenderdataList());
  22.         $this->view->assign("hobbydataList", $this->model->getHobbydataList());
  23.         $this->view->assign("statusList", $this->model->getStatusList());
  24.         $this->view->assign("stateList", $this->model->getStateList());
  25.     }



  26.     /**
  27.      * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28.      * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29.      * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30.      */


  31. }
复制代码
回复 支持 反对

使用道具 举报

228

主题

466

帖子

2184

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2184
地板
 楼主| 发表于 2025-1-17 22:27:24 | 只看该作者
C:\Apache24\htdocs\fastadmin\application\admin\model\Test.php
  1. <?php

  2. namespace app\admin\model;

  3. use think\Model;
  4. use traits\model\SoftDelete;

  5. class Test extends Model
  6. {

  7.     use SoftDelete;

  8.    

  9.     // 表名
  10.     protected $name = 'test';
  11.    
  12.     // 自动写入时间戳字段
  13.     protected $autoWriteTimestamp = 'integer';

  14.     // 定义时间戳字段名
  15.     protected $createTime = 'createtime';
  16.     protected $updateTime = 'updatetime';
  17.     protected $deleteTime = 'deletetime';

  18.     // 追加属性
  19.     protected $append = [
  20.         'week_text',
  21.         'flag_text',
  22.         'genderdata_text',
  23.         'hobbydata_text',
  24.         'refreshtime_text',
  25.         'status_text',
  26.         'state_text'
  27.     ];
  28.    

  29.     protected static function init()
  30.     {
  31.         self::afterInsert(function ($row) {
  32.             if (!$row['weigh']) {
  33.                 $pk = $row->getPk();
  34.                 $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  35.             }
  36.         });
  37.     }

  38.    
  39.     public function getWeekList()
  40.     {
  41.         return ['monday' => __('Week monday'), 'tuesday' => __('Week tuesday'), 'wednesday' => __('Week wednesday')];
  42.     }

  43.     public function getFlagList()
  44.     {
  45.         return ['hot' => __('Flag hot'), 'index' => __('Flag index'), 'recommend' => __('Flag recommend')];
  46.     }

  47.     public function getGenderdataList()
  48.     {
  49.         return ['male' => __('Genderdata male'), 'female' => __('Genderdata female')];
  50.     }

  51.     public function getHobbydataList()
  52.     {
  53.         return ['music' => __('Hobbydata music'), 'reading' => __('Hobbydata reading'), 'swimming' => __('Hobbydata swimming')];
  54.     }

  55.     public function getStatusList()
  56.     {
  57.         return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  58.     }

  59.     public function getStateList()
  60.     {
  61.         return ['0' => __('State 0'), '1' => __('State 1'), '2' => __('State 2')];
  62.     }


  63.     public function getWeekTextAttr($value, $data)
  64.     {
  65.         $value = $value ? $value : (isset($data['week']) ? $data['week'] : '');
  66.         $list = $this->getWeekList();
  67.         return isset($list[$value]) ? $list[$value] : '';
  68.     }


  69.     public function getFlagTextAttr($value, $data)
  70.     {
  71.         $value = $value ?: ($data['flag'] ?? '');
  72.         $valueArr = explode(',', $value);
  73.         $list = $this->getFlagList();
  74.         return implode(',', array_intersect_key($list, array_flip($valueArr)));
  75.     }


  76.     public function getGenderdataTextAttr($value, $data)
  77.     {
  78.         $value = $value ? $value : (isset($data['genderdata']) ? $data['genderdata'] : '');
  79.         $list = $this->getGenderdataList();
  80.         return isset($list[$value]) ? $list[$value] : '';
  81.     }


  82.     public function getHobbydataTextAttr($value, $data)
  83.     {
  84.         $value = $value ? $value : (isset($data['hobbydata']) ? $data['hobbydata'] : '');
  85.         $valueArr = explode(',', $value);
  86.         $list = $this->getHobbydataList();
  87.         return implode(',', array_intersect_key($list, array_flip($valueArr)));
  88.     }


  89.     public function getRefreshtimeTextAttr($value, $data)
  90.     {
  91.         $value = $value ? $value : (isset($data['refreshtime']) ? $data['refreshtime'] : '');
  92.         return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  93.     }


  94.     public function getStatusTextAttr($value, $data)
  95.     {
  96.         $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  97.         $list = $this->getStatusList();
  98.         return isset($list[$value]) ? $list[$value] : '';
  99.     }


  100.     public function getStateTextAttr($value, $data)
  101.     {
  102.         $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
  103.         $list = $this->getStateList();
  104.         return isset($list[$value]) ? $list[$value] : '';
  105.     }

  106.     protected function setFlagAttr($value)
  107.     {
  108.         return is_array($value) ? implode(',', $value) : $value;
  109.     }

  110.     protected function setHobbydataAttr($value)
  111.     {
  112.         return is_array($value) ? implode(',', $value) : $value;
  113.     }

  114.     protected function setRefreshtimeAttr($value)
  115.     {
  116.         return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  117.     }


  118. }
复制代码



回复 支持 反对

使用道具 举报

228

主题

466

帖子

2184

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2184
5#
 楼主| 发表于 2025-1-17 22:29:00 | 只看该作者
C:\Apache24\htdocs\fastadmin\public\assets\js\backend\test.js
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

  2.     var Controller = {
  3.         index: function () {
  4.             // 初始化表格参数配置
  5.             Table.api.init({
  6.                 extend: {
  7.                     index_url: 'test/index' + location.search,
  8.                     add_url: 'test/add',
  9.                     edit_url: 'test/edit',
  10.                     del_url: 'test/del',
  11.                     multi_url: 'test/multi',
  12.                     import_url: 'test/import',
  13.                     table: 'test',
  14.                 }
  15.             });

  16.             var table = $("#table");

  17.             // 初始化表格
  18.             table.bootstrapTable({
  19.                 url: $.fn.bootstrapTable.defaults.extend.index_url,
  20.                 pk: 'id',
  21.                 sortName: 'weigh',
  22.                 fixedColumns: true,
  23.                 fixedRightNumber: 1,
  24.                 columns: [
  25.                     [
  26.                         {checkbox: true},
  27.                         {field: 'id', title: __('Id')},
  28.                         {field: 'user_id', title: __('User_id')},
  29.                         {field: 'admin_id', title: __('Admin_id')},
  30.                         {field: 'category_id', title: __('Category_id')},
  31.                         {field: 'category_ids', title: __('Category_ids'), operate: 'LIKE'},
  32.                         {field: 'tags', title: __('Tags'), operate: 'LIKE', formatter: Table.api.formatter.flag},
  33.                         {field: 'week', title: __('Week'), searchList: {"monday":__('Week monday'),"tuesday":__('Week tuesday'),"wednesday":__('Week wednesday')}, formatter: Table.api.formatter.normal},
  34.                         {field: 'flag', title: __('Flag'), searchList: {"hot":__('Flag hot'),"index":__('Flag index'),"recommend":__('Flag recommend')}, operate:'FIND_IN_SET', formatter: Table.api.formatter.label},
  35.                         {field: 'genderdata', title: __('Genderdata'), searchList: {"male":__('Genderdata male'),"female":__('Genderdata female')}, formatter: Table.api.formatter.normal},
  36.                         {field: 'hobbydata', title: __('Hobbydata'), searchList: {"music":__('Hobbydata music'),"reading":__('Hobbydata reading'),"swimming":__('Hobbydata swimming')}, operate:'FIND_IN_SET', formatter: Table.api.formatter.label},
  37.                         {field: 'title', title: __('Title'), operate: 'LIKE'},
  38.                         {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
  39.                         {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
  40.                         {field: 'attachfile', title: __('Attachfile'), operate: false, formatter: Table.api.formatter.file},
  41.                         {field: 'keywords', title: __('Keywords'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
  42.                         {field: 'description', title: __('Description'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
  43.                         {field: 'city', title: __('City'), operate: 'LIKE'},
  44.                         {field: 'price', title: __('Price'), operate:'BETWEEN'},
  45.                         {field: 'views', title: __('Views')},
  46.                         {field: 'workrange', title: __('Workrange'), operate: 'LIKE'},
  47.                         {field: 'startdate', title: __('Startdate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
  48.                         {field: 'activitytime', title: __('Activitytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
  49.                         {field: 'year', title: __('Year')},
  50.                         {field: 'times', title: __('Times')},
  51.                         {field: 'refreshtime', title: __('Refreshtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  52.                         {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  53.                         {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  54.                         {field: 'weigh', title: __('Weigh'), operate: false},
  55.                         {field: 'switch', title: __('Switch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
  56.                         {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
  57.                         {field: 'state', title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1'),"2":__('State 2')}, formatter: Table.api.formatter.normal},
  58.                         {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  59.                     ]
  60.                 ]
  61.             });

  62.             // 为表格绑定事件
  63.             Table.api.bindevent(table);
  64.         },
  65.         recyclebin: function () {
  66.             // 初始化表格参数配置
  67.             Table.api.init({
  68.                 extend: {
  69.                     'dragsort_url': ''
  70.                 }
  71.             });

  72.             var table = $("#table");

  73.             // 初始化表格
  74.             table.bootstrapTable({
  75.                 url: 'test/recyclebin' + location.search,
  76.                 pk: 'id',
  77.                 sortName: 'id',
  78.                 columns: [
  79.                     [
  80.                         {checkbox: true},
  81.                         {field: 'id', title: __('Id')},
  82.                         {field: 'title', title: __('Title'), align: 'left'},
  83.                         {
  84.                             field: 'deletetime',
  85.                             title: __('Deletetime'),
  86.                             operate: 'RANGE',
  87.                             addclass: 'datetimerange',
  88.                             formatter: Table.api.formatter.datetime
  89.                         },
  90.                         {
  91.                             field: 'operate',
  92.                             width: '140px',
  93.                             title: __('Operate'),
  94.                             table: table,
  95.                             events: Table.api.events.operate,
  96.                             buttons: [
  97.                                 {
  98.                                     name: 'Restore',
  99.                                     text: __('Restore'),
  100.                                     classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
  101.                                     icon: 'fa fa-rotate-left',
  102.                                     url: 'test/restore',
  103.                                     refresh: true
  104.                                 },
  105.                                 {
  106.                                     name: 'Destroy',
  107.                                     text: __('Destroy'),
  108.                                     classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
  109.                                     icon: 'fa fa-times',
  110.                                     url: 'test/destroy',
  111.                                     refresh: true
  112.                                 }
  113.                             ],
  114.                             formatter: Table.api.formatter.operate
  115.                         }
  116.                     ]
  117.                 ]
  118.             });

  119.             // 为表格绑定事件
  120.             Table.api.bindevent(table);
  121.         },

  122.         add: function () {
  123.             Controller.api.bindevent();
  124.         },
  125.         edit: function () {
  126.             Controller.api.bindevent();
  127.         },
  128.         api: {
  129.             bindevent: function () {
  130.                 Form.api.bindevent($("form[role=form]"));
  131.             }
  132.         }
  133.     };
  134.     return Controller;
  135. });
复制代码


回复 支持 反对

使用道具 举报

228

主题

466

帖子

2184

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2184
6#
 楼主| 发表于 2025-1-18 00:30:01 | 只看该作者
C:\Apache24\htdocs\fastadmin>php think crud --force=true -t test -u 1
Build Successed


  1. SHOW TABLE STATUS LIKE 'test'
  2. SHOW TABLE STATUS LIKE 'test'
  3. SHOW TABLE STATUS LIKE 'fa_test'
  4. SHOW TABLE STATUS LIKE 'fa_test'
  5. SELECT * FROM `information_schema`.`columns` WHERE TABLE_SCHEMA = 'sfc' AND table_name = 'fa_test' ORDER BY ORDINAL_POSITION
  6. root@192.168.1.253 on sfc using TCP/IP
  7. SHOW COLUMNS FROM `fa_user`
  8. SHOW COLUMNS FROM `fa_user`
  9. SHOW COLUMNS FROM `fa_category`
  10. SHOW COLUMNS FROM `fa_category`
  11. root@192.168.1.253 on sfc using TCP/IP
  12. SHOW COLUMNS FROM `fa_auth_rule`
  13. SHOW COLUMNS FROM `fa_auth_rule`
  14. SELECT * FROM `fa_auth_rule` WHERE `name` = 'test' LIMIT 1
  15. SELECT `id` FROM `fa_auth_rule` WHERE `name` = 'test/index' LIMIT 1
  16. SELECT `id` FROM `fa_auth_rule` WHERE `name` = 'test/recyclebin' LIMIT 1
  17. SELECT `id` FROM `fa_auth_rule` WHERE `name` = 'test/add' LIMIT 1
  18. SELECT `id` FROM `fa_auth_rule` WHERE `name` = 'test/edit' LIMIT 1
  19. SELECT `id` FROM `fa_auth_rule` WHERE `name` = 'test/del' LIMIT 1
  20. SELECT `id` FROM `fa_auth_rule` WHERE `name` = 'test/destroy' LIMIT 1
  21. SELECT `id` FROM `fa_auth_rule` WHERE `name` = 'test/restore' LIMIT 1
  22. SELECT `id` FROM `fa_auth_rule` WHERE `name` = 'test/multi' LIMIT 1

  23. START TRANSACTION
  24. UPDATE `fa_auth_rule` SET `pid`=85,`name`='test/index',`icon`='fa fa-circle-o',`title`='查看',`ismenu`=0,`status`='normal',`py`='zk',`pinyin`='zhakan',`updatetime`=1737130948 WHERE `id` = 86
  25. UPDATE `fa_auth_rule` SET `pid`=85,`name`='test/recyclebin',`icon`='fa fa-circle-o',`title`='回收站',`ismenu`=0,`status`='normal',`py`='hsz',`pinyin`='huishouzhan',`updatetime`=1737130948 WHERE `id` = 87
  26. UPDATE `fa_auth_rule` SET `pid`=85,`name`='test/add',`icon`='fa fa-circle-o',`title`='添加',`ismenu`=0,`status`='normal',`py`='tj',`pinyin`='tianjia',`updatetime`=1737130948 WHERE `id` = 88
  27. UPDATE `fa_auth_rule` SET `pid`=85,`name`='test/edit',`icon`='fa fa-circle-o',`title`='编辑',`ismenu`=0,`status`='normal',`py`='bj',`pinyin`='bianji',`updatetime`=1737130948 WHERE `id` = 89
  28. UPDATE `fa_auth_rule` SET `pid`=85,`name`='test/del',`icon`='fa fa-circle-o',`title`='删除',`ismenu`=0,`status`='normal',`py`='sc',`pinyin`='shanchu',`updatetime`=1737130948 WHERE `id` = 90
  29. UPDATE `fa_auth_rule` SET `pid`=85,`name`='test/destroy',`icon`='fa fa-circle-o',`title`='真实删除',`ismenu`=0,`status`='normal',`py`='zssc',`pinyin`='zhenshishanchu',`updatetime`=1737130948 WHERE `id` = 91
  30. UPDATE `fa_auth_rule` SET `pid`=85,`name`='test/restore',`icon`='fa fa-circle-o',`title`='还原',`ismenu`=0,`status`='normal',`py`='hy',`pinyin`='huanyuan',`updatetime`=1737130948 WHERE `id` = 92
  31. UPDATE `fa_auth_rule` SET `pid`=?,`name`=?,`icon`=?,`title`=?,`ismenu`=?,`status`=?,`py`=?,`pinyin`=?,`updatetime`=? WHERE `id` = ?  
  32. UPDATE `fa_auth_rule` SET `pid`=85,`name`='test/multi',`icon`='fa fa-circle-o',`title`='批量更新',`ismenu`=0,`status`='normal',`py`='plgx',`pinyin`='pilianggengxin',`updatetime`=1737130948 WHERE `id` = 93
  33. COMMIT
  34.   
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|nagomes  

GMT+8, 2025-5-5 03:09 , Processed in 0.033003 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表