多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 创建数据表 ``` CREATE TABLE `admin_user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(20) NOT NULL COMMENT '用户名', `password` varchar(32) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ``` 在App目录下创建 Model/Admin/UserModel.php ``` <?php namespace App\Model\Admin; use EasySwoole\ORM\AbstractModel; class AdminModel extends AbstractModel { protected $tableName = 'admin_user'; protected $primaryKey = 'id'; public function getAll(int $page = 1, string $keyword = null, int $pageSize = 10): array { $where = []; if (!empty($keyword)) { $where['username'] = ['%' . $keyword . '%', 'like']; } $list = $this->limit($pageSize * ($page - 1), $pageSize)->order($this->primaryKey, 'DESC')->withTotalCount()->all($where); $total = $this->lastQueryResult()->getTotalCount(); return ['total' => $total, 'list' => $list]; } /* * 登录成功后请返回更新后的bean */ public function login():?AdminModel { $info = $this->get(['username' => $this->username, 'password' => $this->password]); return $info; } } ``` 控制器中调用模型 /App/HttpController/Api/User.php ``` <?php namespace App\HttpController\Api; use App\Model\Admin\UserModel; use EasySwoole\Http\AbstractInterface\Controller; class User extends Controller { public function index() { $param = $this->request()->getRequestParam(); $model = new UserModel(); $model->username = $param['username']; $model->password = md5($param['password']); if ($model->login()) { echo '成功'; } else { echo '失败'; } } } ```