手把手教你开发easyswoole 接口网站

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 手把手教你开发easyswoole 接口网站

基础开发示例

demo地址

基础开发示例已经开源,地址:https://github.com/easy-swoole/demo/tree/3.x

安装

框架安装

  • 我们先安装好swooole拓展,执行 php --ri swoole 确保可以看到swoole拓展最版本为4.4.3
  • 建立一个目录,名为Test,执行composer require easyswoole/easyswoole=3.x 引入easyswoole
  • 执行php vendor/bin/easyswoole install 进行安装

命名空间注册

编辑Test根目录下的 composer.json 文件,加入"App\\": "App/" ,大体结构如下:

{
    "autoload": {
        "psr-4": {
            "App\\\": "App/"
        }
    },
    "require": {
        "easyswoole/easyswoole": "3.x"
    }}

安装后目录结构

Test                   项目部署目录
├─App                     应用目录
│  ├─HttpController       控制器目录(需要自己创建)
├─Log                     日志文件目录(启动后创建)
├─Temp                    临时文件目录(启动后创建)
├─vendor                  第三方类库目录
├─composer.json           Composer架构
├─composer.lock           Composer锁定
├─EasySwooleEvent.php     框架全局事件
├─easyswoole              框架管理脚本
├─easyswoole.install      框架安装锁定文件
├─dev.php                 开发配置文件
├─produce.php             生产配置文件

执行以下命令进行名称空间的更新:

composer dumpautoload

连接池实现

配置项

我们在dev.php 配置文件中,加入以下配置信息,注意:请跟进自己的mysql服务器信息填写账户密码

'MYSQL'  => \[
        'host'          => '',
        'port'          => 3300,
        'user'          => '',
        'password'      => '',
        'database'      => '',
        'timeout'       => 5,
        'charset'       => 'utf8mb4',
 \]

引入Mysqli库

执行以下命令用于实现Mysqli库的引入

composer require easyswoole/mysqli

再引入mysqli-pool库

composer require easyswoole/mysqli-pool

事件注册

我们编辑根目录下的EasySwooleEvent.php文件,在mainServerCreate事件中进行连接池的注册,大体结构如下:

<?php/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/5/28
 * Time: 下午6:33
 */namespace EasySwoole\\EasySwoole;use EasySwoole\\Component\\Timer;use EasySwoole\\EasySwoole\\Swoole\\EventRegister;use EasySwoole\\EasySwoole\\AbstractInterface\\Event;use EasySwoole\\Http\\Request;use EasySwoole\\Http\\Response;class EasySwooleEvent implements Event{
    public static function initialize()
    {
        // TODO: Implement initialize() method.
        date\_default\_timezone_set('Asia/Shanghai');
        $configData = Config::getInstance()->getConf('MYSQL');
        $config = new \\EasySwoole\\Mysqli\\Config($configData);
        $poolConf = \\EasySwoole\\MysqliPool\\Mysql::getInstance()->register('mysql', $config);
        $poolConf->setMaxObjectNum(20);
    }
    public static function mainServerCreate(EventRegister $register)
    {
    }
    public static function onRequest(Request $request, Response $response): bool    {
        // TODO: Implement onRequest() method.
        return true;
    }
    public static function afterRequest(Request $request, Response $response): void    {
        // TODO: Implement afterAction() method.
    }}

模型定义

基础模型定义

新建App/Model/BaseModel.php文件:

<?phpnamespace App\\Model;use EasySwoole\\Mysqli\\Mysqli;class BaseModel{
    protected $db;
    protected $table;
    function __construct(Mysqli $connection)
    {
        $this->db = $connection;
    }
    function getDbConnection():Mysqli    {
        return $this->db;
    }
    /**
     * @return mixed
     */
    public function getTable()
    {
        return $this->table;
    }}

管理员模型

新增管理员用户表:

CREATE TABLE \`admin_list\` (
  \`adminId\` int(11) NOT NULL AUTO_INCREMENT,
  \`adminName\` varchar(15) DEFAULT NULL,
  \`adminAccount\` varchar(18) DEFAULT NULL,
  \`adminPassword\` varchar(32) DEFAULT NULL,
  \`adminSession\` varchar(32) DEFAULT NULL,
  \`adminLastLoginTime\` int(11) DEFAULT NULL,
  \`adminLastLoginIp\` varchar(20) DEFAULT NULL,
  PRIMARY KEY (\`adminId\`),
  UNIQUE KEY \`adminAccount\` (\`adminAccount\`),
  KEY \`adminSession\` (\`adminSession\`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
INSERT INTO \`admin_list\` VALUES ('1', '仙士可', 'xsk', 'e10adc3949ba59abbe56e057f20f883e', '', '1566279458', '192.168.159.1');

新增bean文件

新增 App/Model/Admin/AdminBean.php 文件:

<?phpnamespace App\\Model\\Admin;/**
 * Class AdminBean
 * Create With Automatic Generator
 * @property int adminId |
 * @property string adminName |
 * @property string adminAccount |
 * @property string adminPassword |
 * @property string adminSession |
 * @property int adminLastLoginTime |
 * @property string adminLastLoginIp |
 */class AdminBean extends \\EasySwoole\\Spl\\SplBean{
    protected $adminId;
    protected $adminName;
    protected $adminAccount;
    protected $adminPassword;
    protected $adminSession;
    protected $adminLastLoginTime;
    protected $adminLastLoginIp;
    public function setAdminId($adminId)
    {
        $this->adminId = $adminId;
    }
    public function getAdminId()
    {
        return $this->adminId;
    }
    public function setAdminName($adminName)
    {
        $this->adminName = $adminName;
    }
    public function getAdminName()
    {
        return $this->adminName;
    }
    public function setAdminAccount($adminAccount)
    {
        $this->adminAccount = $adminAccount;
    }
    public function getAdminAccount()
    {
        return $this->adminAccount;
    }
    public function setAdminPassword($adminPassword)
    {
        $this->adminPassword = $adminPassword;
    }
    public function getAdminPassword()
    {
        return $this->adminPassword;
    }
    public function setAdminSession($adminSession)
    {
        $this->adminSession = $adminSession;
    }
    public function getAdminSession()
    {
        return $this->adminSession;
    }
    public function setAdminLastLoginTime($adminLastLoginTime)
    {
        $this->adminLastLoginTime = $adminLastLoginTime;
    }
    public function getAdminLastLoginTime()
    {
        return $this->adminLastLoginTime;
    }
    public function setAdminLastLoginIp($adminLastLoginIp)
    {
        $this->adminLastLoginIp = $adminLastLoginIp;
    }
    public function getAdminLastLoginIp()
    {
        return $this->adminLastLoginIp;
    }}

新增model文件

新增 App/Model/Admin/AdminModel.php文件:

<?phpnamespace App\\Model\\Admin;/**
 * Class AdminModel
 * Create With Automatic Generator
 */class AdminModel extends \\App\\Model\\BaseModel{
    protected $table = 'admin_list';
    protected $primaryKey = 'adminId';
    /**
     * @getAll
     * @keyword adminName
     * @param  int  page  1
     * @param  string  keyword
     * @param  int  pageSize  10
     * @return array\[total,list\]
     */
    public function getAll(int $page = 1, string $keyword = null, int $pageSize = 10): array
    {
        if (!empty($keyword)) {
            $this->getDbConnection()->where('adminAccount', '%' . $keyword . '%', 'like');
        }
        $list = $this->getDbConnection()
            ->withTotalCount()
            ->orderBy($this->primaryKey, 'DESC')
            ->get($this->table, \[$pageSize * ($page - 1), $pageSize\]);
        $total = $this->getDbConnection()->getTotalCount();
        return \['total' => $total, 'list' => $list\];
    }
    /**
     * 默认根据主键(adminId)进行搜索
     * @getOne
     * @param  AdminBean $bean
     * @return AdminBean
     */
    public function getOne(AdminBean $bean): ?AdminBean    {
        $info = $this->getDbConnection()->where($this->primaryKey, $bean->getAdminId())->getOne($this->table);
        if (empty($info)) {
            return null;
        }
        return new AdminBean($info);
    }
    /**
     * 默认根据bean数据进行插入数据
     * @add
     * @param  AdminBean $bean
     * @return bool
     */
    public function add(AdminBean $bean): bool    {
        return $this->getDbConnection()->insert($this->table, $bean->toArray(null, $bean::FILTER\_NOT\_NULL));
    }
    /**
     * 默认根据主键(adminId)进行删除
     * @delete
     * @param  AdminBean $bean
     * @return bool
     */
    public function delete(AdminBean $bean): bool    {
        return $this->getDbConnection()->where($this->primaryKey, $bean->getAdminId())->delete($this->table);
    }
    /**
     * 默认根据主键(adminId)进行更新
     * @delete
     * @param  AdminBean $bean
     * @param  array     $data
     * @return bool
     */
    public function update(AdminBean $bean, array $data): bool    {
        if (empty($data)) {
            return false;
        }
        return $this->getDbConnection()->where($this->primaryKey, $bean->getAdminId())->update($this->table, $data);
    }
    /*
     * 登录成功后请返回更新后的bean
     */
    function login(AdminBean $userBean): ?AdminBean    {
        $user = $this->getDbConnection()
            ->where('adminAccount', $userBean->getAdminAccount())
            ->where('adminPassword', $userBean->getAdminPassword())
            ->getOne($this->table);
        if (empty($user)) {
            return null;
        }
        return new AdminBean($user);
    }
    /*
     * 以account进行查询
     */
    function accountExist(AdminBean $userBean): ?AdminBean    {
        $user = $this->getDbConnection()
            ->where('adminAccount', $userBean->getAdminAccount())
            ->getOne($this->table);
        if (empty($user)) {
            return null;
        }
        return new AdminBean($user);
    }
    function getOneBySession($session)
    {
        $user = $this->getDbConnection()
            ->where('adminSession', $session)
            ->getOne($this->table);
        if (empty($user)) {
            return null;
        }
        return new AdminBean($user);
    }
    function logout(AdminBean $bean){
        $update = \[
            'adminSession'=>'',
        \];
        return $this->getDbConnection()->where($this->primaryKey, $bean->getAdminId())->update($this->table, $update);
    }}

普通用户模型

普通用户模型和管理员模型同理

建表

CREATE TABLE \`user_list\` (
  \`userId\` int(11) NOT NULL AUTO_INCREMENT,
  \`userName\` varchar(32) NOT NULL,
  \`userAccount\` varchar(18) NOT NULL,
  \`userPassword\` varchar(32) NOT NULL,
  \`phone\` varchar(18) NOT NULL,
  \`addTime\` int(11) DEFAULT NULL,
  \`lastLoginIp\` varchar(20) DEFAULT NULL,
  \`lastLoginTime\` int(10) DEFAULT NULL,
  \`userSession\` varchar(32) DEFAULT NULL,
  \`state\` tinyint(2) DEFAULT NULL,
  \`money\` int(10) NOT NULL DEFAULT '0' COMMENT '用户余额',
  \`frozenMoney\` int(10) NOT NULL DEFAULT '0' COMMENT '冻结余额',
  PRIMARY KEY (\`userId\`),
  UNIQUE KEY \`pk_userAccount\` (\`userAccount\`),
  KEY \`userSession\` (\`userSession\`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

新增bean文件

App/Model/User/UserBean.php 文件:

<?phpnamespace App\\Model\\User;/**
 * Class UserBean
 * Create With Automatic Generator
 * @property int userId |
 * @property string userName |
 * @property string userAccount |
 * @property string userPassword |
 * @property string phone |
 * @property string money |
 * @property int addTime |
 * @property string lastLoginIp |
 * @property int lastLoginTime |
 * @property string userSession |
 * @property int state |
 */class UserBean extends \\EasySwoole\\Spl\\SplBean{
    protected $userId;
    protected $userName;
    protected $userAccount;
    protected $userPassword;
    protected $userAvatar;
    protected $phone;
    protected $money;
    protected $frozenMoney;
    protected $addTime;
    protected $lastLoginIp;
    protected $lastLoginTime;
    protected $userSession;
    protected $state;
    const STATE_PROHIBIT = 0;//禁用状态
    const STATE_NORMAL = 1;//正常状态
    public function setUserId($userId)
    {
        $this->userId = $userId;
    }
    public function getUserId()
    {
        return $this->userId;
    }
    public function setUserName($userName)
    {
        $this->userName = $userName;
    }
    public function getUserName()
    {
        return $this->userName;
    }
    public function setUserAccount($userAccount)
    {
        $this->userAccount = $userAccount;
    }
    public function getUserAccount()
    {
        return $this->userAccount;
    }
    /**
     * @return mixed
     */
    public function getUserAvatar()
    {
        return $this->userAvatar;
    }
    /**
     * @param mixed $userAvatar
     */
    public function setUserAvatar($userAvatar): void    {
        $this->userAvatar = $userAvatar;
    }
    public function setUserPassword($userPassword)
    {
        $this->userPassword = $userPassword;
    }
    public function getUserPassword()
    {
        return $this->userPassword;
    }
    public function setPhone($phone)
    {
        $this->phone = $phone;
    }
    public function getPhone()
    {
        return $this->phone;
    }
    public function setMoney($money)
    {
        $this->money = $money;
    }
    public function getMoney()
    {
        return $this->money;
    }
    public function setAddTime($addTime)
    {
        $this->addTime = $addTime;
    }
    public function getAddTime()
    {
        return $this->addTime;
    }
    public function setLastLoginIp($lastLoginIp)
    {
        $this->lastLoginIp = $lastLoginIp;
    }
    public function getLastLoginIp()
    {
        return $this->lastLoginIp;
    }
    public function setLastLoginTime($lastLoginTime)
    {
        $this->lastLoginTime = $lastLoginTime;
    }
    public function getLastLoginTime()
    {
        return $this->lastLoginTime;
    }
    public function setUserSession($userSession)
    {
        $this->userSession = $userSession;
    }
    public function getUserSession()
    {
        return $this->userSession;
    }
    public function setState($state)
    {
        $this->state = $state;
    }
    public function getState()
    {
        return $this->state;
    }
    /**
     * @return mixed
     */
    public function getFrozenMoney()
    {
        return $this->frozenMoney;
    }
    /**
     * @param mixed $frozenMoney
     */
    public function setFrozenMoney($frozenMoney)
    {
        $this->frozenMoney = $frozenMoney;
    }}

新增model文件

新增 App/Model/User/UserModel.php 文件:

<?phpnamespace App\\Model\\User;/**
 * Class UserModel
 * Create With Automatic Generator
 */class UserModel extends \\App\\Model\\BaseModel{
    protected $table = 'user_list';
    protected $primaryKey = 'userId';
    /**
     * @getAll
     * @keyword userName
     * @param  int  page  1
     * @param  string  keyword
     * @param  int  pageSize  10
     * @return array\[total,list\]
     */
    public function getAll(int $page = 1, string $keyword = null, int $pageSize = 10): array
    {
        if (!empty($keyword)) {
            $this->getDbConnection()->where('userAccount', '%' . $keyword . '%', 'like');
        }
        $list = $this->getDbConnection()
            ->withTotalCount()
            ->orderBy($this->primaryKey, 'DESC')
            ->get($this->table, \[$pageSize * ($page  - 1), $pageSize\]);
        $total = $this->getDbConnection()->getTotalCount();
        return \['total' => $total, 'list' => $list\];
    }
    /**
     * 默认根据主键(userId)进行搜索
     * @getOne
     * @param  UserBean $bean
     * @return UserBean
     */
    public function getOne(UserBean $bean,$field='*'): ?UserBean    {
        $info = $this->getDbConnection()->where($this->primaryKey, $bean->getUserId())->getOne($this->table,$field);
        if (empty($info)) {
            return null;
        }
        return new UserBean($info);
    }
    public function getOneByPhone($phone,$field='*'): ?UserBean    {
        $info = $this->getDbConnection()->where('phone', $phone)->getOne($this->table,$field);
        if (empty($info)) {
            return null;
        }
        return new UserBean($info);
    }
    /**
     * 默认根据bean数据进行插入数据
     * @add
     * @param  UserBean $bean
     * @return bool
     */
    public function add(UserBean $bean): bool    {
        return $this->getDbConnection()->insert($this->table, $bean->toArray(null, $bean::FILTER\_NOT\_NULL));
    }
    /**
     * 默认根据主键(userId)进行删除
     * @delete
     * @param  UserBean $bean
     * @return bool
     */
    public function delete(UserBean $bean): bool    {
        return  $this->getDbConnection()->where($this->primaryKey, $bean->getUserId())->delete($this->table);
    }
    /**
     * 默认根据主键(userId)进行更新
     * @delete
     * @param  UserBean $bean
     * @param  array    $data
     * @return bool
     */
    public function update(UserBean $bean, array $data): bool    {
        if (empty($data)){
            return false;
        }
        return $this->getDbConnection()->where($this->primaryKey, $bean->getUserId())->update($this->table, $data);
    }
    /*
     * 登录成功后请返回更新后的bean
     */
    function login(UserBean $userBean): ?UserBean    {
        $user = $this->getDbConnection()
            ->where('userAccount', $userBean->getUserAccount())
            ->where('userPassword', $userBean->getUserPassword())
            ->getOne($this->table);
        if (empty($user)) {
            return null;
        }
        return new UserBean($user);
    }
    function getOneBySession($session)
    {
        $user = $this->getDbConnection()
            ->where('userSession', $session)
            ->getOne($this->table);
        if (empty($user)) {
            return null;
        }
        return new UserBean($user);
    }
    function logout(UserBean $bean){
        $update = \[
            'userSession'=>'',
        \];
        return $this->getDbConnection()->where($this->primaryKey, $bean->getUserId())->update($this->table, $update);
    }}

banner模型

建表

CREATE TABLE \`banner_list\` (
  \`bannerId\` int(11) NOT NULL AUTO_INCREMENT,
  \`bannerName\` varchar(32) DEFAULT NULL,
  \`bannerImg\` varchar(255) NOT NULL COMMENT 'banner图片',
  \`bannerDescription\` varchar(255) DEFAULT NULL,
  \`bannerUrl\` varchar(255) DEFAULT NULL COMMENT '跳转地址',
  \`state\` tinyint(3) DEFAULT NULL COMMENT '状态0隐藏 1正常',
  PRIMARY KEY (\`bannerId\`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

新增bean文件

新增 App/Model/Admin/BannerBean.php 文件:

<?phpnamespace App\\Model\\Admin;/**
 * Class BannerBean
 * Create With Automatic Generator
 * @property int bannerId |
 * @property string bannerImg | banner图片
 * @property string bannerUrl | 跳转地址
 * @property int state | 状态0隐藏 1正常
 */class BannerBean extends \\EasySwoole\\Spl\\SplBean{
    protected $bannerId;
    protected $bannerImg;
    protected $bannerUrl;
    protected $bannerName;
    protected $bannerDescription;
    protected $state;
    public function setBannerId($bannerId)
    {
        $this->bannerId = $bannerId;
    }
    public function getBannerId()
    {
        return $this->bannerId;
    }
    public function setBannerImg($bannerImg)
    {
        $this->bannerImg = $bannerImg;
    }
    public function getBannerImg()
    {
        return $this->bannerImg;
    }
    public function setBannerUrl($bannerUrl)
    {
        $this->bannerUrl = $bannerUrl;
    }
    public function getBannerUrl()
    {
        return $this->bannerUrl;
    }
    public function setState($state)
    {
        $this->state = $state;
    }
    public function getState()
    {
        return $this->state;
    }
    /**
     * @return mixed
     */
    public function getBannerName()
    {
        return $this->bannerName;
    }
    /**
     * @param mixed $bannerName
     */
    public function setBannerName($bannerName): void    {
        $this->bannerName = $bannerName;
    }
    /**
     * @return mixed
     */
    public function getBannerDescription()
    {
        return $this->bannerDescription;
    }
    /**
     * @param mixed $bannerDescription
     */
    public function setBannerDescription($bannerDescription): void    {
        $this->bannerDescription = $bannerDescription;
    }}

新增model文件

新增 App/Model/Admin/BannerModel.php 文件:

<?phpnamespace App\\Model\\Admin;/**
 * Class BannerModel
 * Create With Automatic Generator
 */class BannerModel extends \\App\\Model\\BaseModel{
    protected $table = 'banner_list';
    protected $primaryKey = 'bannerId';
    /**
     * @getAll
     * @keyword bannerUrl
     * @param  int  page  1
     * @param  string  keyword
     * @param  int  pageSize  10
     * @return array\[total,list\]
     */
    public function getAll(int $page = 1, string $keyword = null, int $pageSize = 10): array
    {
        if (!empty($keyword)) {
            $this->getDbConnection()->where('bannerUrl', '%' . $keyword . '%', 'like');
        }
        $list = $this->getDbConnection()
            ->withTotalCount()
            ->orderBy($this->primaryKey, 'DESC')
            ->get($this->table, \[$pageSize * ($page  - 1), $pageSize\]);
        $total = $this->getDbConnection()->getTotalCount();
        return \['total' => $total, 'list' => $list\];
    }
    /**
     * getAllByState
     * @param int         $page
     * @param int|null    $state
     * @param string|null $keyword
     * @param int         $pageSize
     * @return array
     * @throws \\EasySwoole\\Mysqli\\Exceptions\\ConnectFail
     * @throws \\EasySwoole\\Mysqli\\Exceptions\\Option
     * @throws \\EasySwoole\\Mysqli\\Exceptions\\OrderByFail
     * @throws \\EasySwoole\\Mysqli\\Exceptions\\PrepareQueryFail
     * @author Tioncico
     * Time: 15:13
     */
    public function getAllByState(int $page = 1, ?int $state = null, string $keyword = null, int $pageSize = 10): array
    {
        if (!empty($keyword)) {
            $this->getDbConnection()->where('bannerUrl', '%' . $keyword . '%', 'like');
        }
        if ($state!==null) {
            $this->getDbConnection()->where('state', $state);
        }
        $list = $this->getDbConnection()
            ->withTotalCount()
            ->orderBy($this->primaryKey, 'DESC')
            ->get($this->table, \[$pageSize * ($page  - 1), $pageSize\]);
        $total = $this->getDbConnection()->getTotalCount();
        return \['total' => $total, 'list' => $list\];
    }
    /**
     * 默认根据主键(bannerId)进行搜索
     * @getOne
     * @param  BannerBean $bean
     * @return BannerBean
     */
    public function getOne(BannerBean $bean): ?BannerBean    {
        $info = $this->getDbConnection()->where($this->primaryKey, $bean->getBannerId())->getOne($this->table);
        if (empty($info)) {
            return null;
        }
        return new BannerBean($info);
    }
    /**
     * 默认根据bean数据进行插入数据
     * @add
     * @param  BannerBean $bean
     * @return bool
     */
    public function add(BannerBean $bean): bool    {
        return $this->getDbConnection()->insert($this->table, $bean->toArray(null, $bean::FILTER\_NOT\_NULL));
    }
    /**
     * 默认根据主键(bannerId)进行删除
     * @delete
     * @param  BannerBean $bean
     * @return bool
     */
    public function delete(BannerBean $bean): bool    {
        return  $this->getDbConnection()->where($this->primaryKey, $bean->getBannerId())->delete($this->table);
    }
    /**
     * 默认根据主键(bannerId)进行更新
     * @delete
     * @param  BannerBean $bean
     * @param  array $data
     * @return bool
     */
    public function update(BannerBean $bean, array $data): bool    {
        if (empty($data)){
            return false;
        }
        return $this->getDbConnection()->where($this->primaryKey, $bean->getBannerId())->update($this->table, $data);
    }}

控制器定义

全局基础控制器定义

新增 App/Httpcontroller/Api/ApiBase.php 文件:

<?phpnamespace App\\HttpController\\Api;use EasySwoole\\EasySwoole\\Core;use EasySwoole\\EasySwoole\\ServerManager;use EasySwoole\\EasySwoole\\Trigger;use EasySwoole\\Http\\AbstractInterface\\Controller;use EasySwoole\\Http\\Message\\Status;use EasySwoole\\Validate\\Validate;abstract class ApiBase extends Controller{
    function index()
    {
        // TODO: Implement index() method.
        $this->actionNotFound('index');
    }
    protected function actionNotFound(?string $action): void    {
        $this->writeJson(Status::CODE\_NOT\_FOUND);
    }
    function onRequest(?string $action): ?bool    {
        if (!parent::onRequest($action)) {
            return false;
        };
        /*
         * 各个action的参数校验
         */
        $v = $this->getValidateRule($action);
        if ($v && !$this->validate($v)) {
            $this->writeJson(Status::CODE\_BAD\_REQUEST, \['errorCode' => 1, 'data' => \[\]\], $v->getError()->__toString());
            return false;
        }
        return true;
    }
    abstract protected function getValidateRule(?string $action): ?Validate;
    protected function onException(\\Throwable $throwable): void    {
        Trigger::getInstance()->throwable($throwable);
        $this->writeJson(Status::CODE\_INTERNAL\_SERVER_ERROR, null, $throwable->getMessage() . " at file {$throwable->getFile()} line {$throwable->getLine()}");
    }
    /**
     * 获取用户的get/post的一个值,可设定默认值
     * input
     * @param      $key
     * @param null $default
     * @return array|mixed|null
     * @author Tioncico
     * Time: 17:27
     */
    protected function input($key, $default = null)
    {
        $value = $this->request()->getRequestParam($key);
        return $value ?? $default;
    }
    /**
     * 获取用户的真实IP
     * @param string $headerName 代理服务器传递的标头名称
     * @return string
     */
    protected function clientRealIP($headerName = 'x-real-ip')
    {
        $server = ServerManager::getInstance()->getSwooleServer();
        $client = $server->getClientInfo($this->request()->getSwooleRequest()->fd);
        $clientAddress = $client\['remote_ip'\];
        $xri = $this->request()->getHeader($headerName);
        $xff = $this->request()->getHeader('x-forwarded-for');
        if ($clientAddress === '127.0.0.1') {
            if (!empty($xri)) {  // 如果有xri 则判定为前端有NGINX等代理
                $clientAddress = $xri\[0\];
            } elseif (!empty($xff)) {  // 如果不存在xri 则继续判断xff
                $list = explode(',', $xff\[0\]);
                if (isset($list\[0\])) $clientAddress = $list\[0\];
            }
        }
        return $clientAddress;
    }}

新增基础控制器,里面的方法用于获取用户ip,以及获取api参数

公共基础控制器定义

新增 App/Httpcontroller/Api/Common/CommonBase.php文件:

<?phpnamespace App\\HttpController\\Api\\Common;use App\\HttpController\\Api\\ApiBase;use EasySwoole\\Validate\\Validate;class CommonBase extends ApiBase{
    function onRequest(?string $action): ?bool    {
        if (parent::onRequest($action)) {
            return true;
        }
        return false;
    }
    protected function getValidateRule(?string $action): ?Validate    {
        return null;
        // TODO: Implement getValidateRule() method.
    }}

公共控制器

公共控制器放不需要登陆即可查看的控制器,例如banner列表查看:

新增 App/HttpController/Api/Common/Banner.php 文件:

<?phpnamespace App\\HttpController\\Api\\Common;use App\\Model\\Admin\\BannerBean;use App\\Model\\Admin\\BannerModel;use EasySwoole\\Http\\Message\\Status;use EasySwoole\\MysqliPool\\Mysql;use EasySwoole\\Validate\\Validate;class Banner extends CommonBase{
    public function getOne()
    {
        $db = Mysql::defer('mysql');
        $param = $this->request()->getRequestParam();
        $model = new BannerModel($db);
        $bean = $model->getOne(new BannerBean(\['bannerId' => $param\['bannerId'\]\]));
        if ($bean) {
            $this->writeJson(Status::CODE_OK, $bean, "success");
        } else {
            $this->writeJson(Status::CODE\_BAD\_REQUEST, \[\], 'fail');
        }
    }
    public function getAll()
    {
        $db = Mysql::defer('mysql');
        $param = $this->request()->getRequestParam();
        $page = $param\['page'\]??1;
        $limit = $param\['limit'\]??20;
        $model = new BannerModel($db);
        $data = $model->getAllByState($page, 1,$param\['keyword'\]??null, $limit);
        $this->writeJson(Status::CODE_OK, $data, 'success');
    }
    function getValidateRule(?string $action): ?Validate    {
        $validate = null;
        switch ($action) {
            case 'getAll':
                $validate = new Validate();
                $validate->addColumn('page', '页数')->optional();
                $validate->addColumn('limit', 'limit')->optional();
                $validate->addColumn('keyword', '关键词')->optional();
                break;
            case 'getOne':
                $validate = new Validate();
                $validate->addColumn('bannerId', '主键id')->required()->lengthMax(11);
                break;
        }
        return $validate;
    }}

测试链接:127.0.0.1:9501/api/common/banner/getAll 需要有数据才能看到具体输出

管理员基础控制器定义

新增 App/HttpController/Api/Admin/AdminBase.php 文件:

<?phpnamespace App\\HttpController\\Api\\Admin;use App\\HttpController\\Api\\ApiBase;use App\\Model\\Admin\\AdminBean;use App\\Model\\Admin\\AdminModel;use EasySwoole\\Http\\Message\\Status;use EasySwoole\\MysqliPool\\Mysql;use EasySwoole\\Validate\\Validate;class AdminBase extends ApiBase{
    protected $who;
    //session的cookie头
    protected $sessionKey = 'adminSession';
    //白名单
    protected $whiteList = \['login'\];
    function onRequest(?string $action): ?bool    {
        if (parent::onRequest($action)) {
            //白名单判断
            if (in_array($action, $this->whiteList)) {
                return true;
            }
            //获取登入信息
            if (!$this->getWho()) {
                $this->writeJson(Status::CODE_UNAUTHORIZED, '', '登入已过期');
                return false;
            }
            return true;
        }
        return false;
    }
    function getWho(): ?AdminBean    {
        if ($this->who instanceof AdminBean) {
            return $this->who;
        }
        $sessionKey = $this->request()->getRequestParam($this->sessionKey);
        if (empty($sessionKey)) {
            $sessionKey = $this->request()->getCookieParams($this->sessionKey);
        }
        if (empty($sessionKey)) {
            return null;
        }
        $db = Mysql::defer('mysql');
        $adminModel = new AdminModel($db);
        $this->who = $adminModel->getOneBySession($sessionKey);
        return $this->who;
    }
    protected function getValidateRule(?string $action): ?Validate    {
        return null;
        // TODO: Implement getValidateRule() method.
    }}

管理员登录控制器

新增 App/HttpController/Api/Admin/Auth.php 文件:

<?phpnamespace App\\HttpController\\Api\\Admin;use App\\Model\\Admin\\AdminBean;use App\\Model\\Admin\\AdminModel;use EasySwoole\\Http\\Message\\Status;use EasySwoole\\MysqliPool\\Mysql;use EasySwoole\\Spl\\SplBean;use EasySwoole\\Validate\\Validate;class Auth extends AdminBase{
    protected $whiteList=\['login'\];
    function login()
    {
        $param = $this->request()->getRequestParam();
        $db = Mysql::defer('mysql');
        $model = new AdminModel($db);
        $bean = new AdminBean();
        $bean->setAdminAccount($param\['account'\]);
        $bean->setAdminPassword(md5($param\['password'\]));
        if ($rs = $model->login($bean)) {
            $bean->restore(\['adminId' => $rs->getAdminId()\]);
            $sessionHash = md5(time() . $rs->getAdminId());
            $model->update($bean, \[
                'adminLastLoginTime' => time(),
                'adminLastLoginIp'   => $this->clientRealIP(),
                'adminSession'       => $sessionHash
            \]);
            $rs = $rs->toArray(null, SplBean::FILTER\_NOT\_NULL);
            unset($rs\['adminPassword'\]);
            $rs\['adminSession'\] = $sessionHash;
            $this->response()->setCookie('adminSession', $sessionHash, time() + 3600, '/');
            $this->writeJson(Status::CODE_OK, $rs);
        } else {
            $this->writeJson(Status::CODE\_BAD\_REQUEST, '', '密码错误');
        }
    }
    function logout()
    {
        $sessionKey = $this->request()->getRequestParam($this->sessionKey);
        if (empty($sessionKey)) {
            $sessionKey = $this->request()->getCookieParams('adminSession');
        }
        if (empty($sessionKey)) {
            $this->writeJson(Status::CODE_UNAUTHORIZED, '', '尚未登入');
            return false;
        }
        $db = Mysql::defer('mysql');
        $adminModel = new AdminModel($db);
        $result = $adminModel->logout($this->getWho());
        if ($result) {
            $this->writeJson(Status::CODE_OK, '', "登出成功");
        } else {
            $this->writeJson(Status::CODE_UNAUTHORIZED, '', 'fail');
        }
    }
    function getInfo()
    {
        $this->writeJson(200, $this->getWho(), 'success');
    }
    protected function getValidateRule(?string $action): ?Validate    {
        $validate = null;
        switch ($action) {
            case 'login':
                $validate = new Validate();
                $validate->addColumn('account')->required()->lengthMax(32);
                $validate->addColumn('password')->required()->lengthMax(32);
                break;
            case 'logout':
                break;
        }
        return $validate;
    }}

请求127.0.0.1:9501/Api/Admin/Auth/login?account=xsk&password=123456 即可返回:

{
"code": 200,
"result": {
"adminId": 1,
"adminName": "仙士可",
"adminAccount": "xsk",
"adminSession": "d45de0cd6dd91122db4bd7e976c7deb8",
"adminLastLoginTime": 1566279458,
"adminLastLoginIp": "192.168.159.1"
},
"msg": null
}

管理员用户管理控制器

新增 App/httpController/Api/Admin/User.php 文件:

<?phpnamespace App\\HttpController\\Api\\Admin;use App\\Model\\User\\UserBean;use App\\Model\\User\\UserModel;use EasySwoole\\Http\\Message\\Status;use EasySwoole\\MysqliPool\\Mysql;use EasySwoole\\Validate\\Validate;class User extends AdminBase{
    function getAll()
    {
        $db = Mysql::defer('mysql');
        $page = (int)$this->input('page', 1);
        $limit = (int)$this->input('limit', 20);
        $model = new UserModel($db);
        $data = $model->getAll($page, $this->input('keyword'), $limit);
        $this->writeJson(Status::CODE_OK, $data, 'success');
    }
    function getOne()
    {
        $db = Mysql::defer('mysql');
        $param = $this->request()->getRequestParam();
        $data\['userId'\] = intval($param\['userId'\]);
        $model = new UserModel($db);
        $rs = $model->getOne(new UserBean($data));
        if ($rs) {
            $this->writeJson(Status::CODE_OK, $rs, "success");
        } else {
            $this->writeJson(Status::CODE\_BAD\_REQUEST, \[\], 'fail');
        }
    }
    function add()
    {
        $db = Mysql::defer('mysql');
        $param = $this->request()->getRequestParam();
        $model = new UserModel($db);
        $bean = new UserBean($param);
        $bean->setUserPassword(md5($param\['userPassword'\]));
        $bean->setState($this->input('state',1));
        $bean->setMoney(0);
        $bean->setAddTime(time());
        $rs = $model->add($bean);
        if ($rs) {
            $this->writeJson(Status::CODE_OK, $rs, "success");
        } else {
            $this->writeJson(Status::CODE\_BAD\_REQUEST, \[\], $db->getLastError());
        }
    }
    function update()
    {
        $db = Mysql::defer('mysql');
        $model = new UserModel($db);
        $userInfo = $model->getOne( new UserBean(\['userId' => $this->input('userId')\]));
        if (!$userInfo){
            $this->writeJson(Status::CODE\_BAD\_REQUEST, \[\], '未找到该会员');
        }
        $password = $this->input('userPassword');
        $updateBean = new UserBean();
        $updateBean->setUserName($this->input('userName',$userInfo->getUserName()));
        $updateBean->setUserPassword($password?md5($password):$userInfo->getUserPassword());
        $updateBean->setState($this->input('state',$userInfo->getState()));
        $updateBean->setPhone($this->input('phone',$userInfo->getPhone()));
        $rs = $model->update($userInfo, $updateBean->toArray(\[\], $updateBean::FILTER\_NOT\_EMPTY));
        if ($rs) {
            $this->writeJson(Status::CODE_OK, $rs, "success");
        } else {
            $this->writeJson(Status::CODE\_BAD\_REQUEST, \[\], $db->getLastError());
        }
    }
    function delete()
    {
        $db = Mysql::defer('mysql');
        $param = $this->request()->getRequestParam();
        $model = new UserModel($db);
        $bean = new UserBean(\['userId' => intval($param\['userId'\])\]);
        $rs = $model->delete($bean);
        if ($rs) {
            $this->writeJson(Status::CODE_OK, $rs, "success");
        } else {
            $this->writeJson(Status::CODE\_BAD\_REQUEST, \[\], '删除失败');
        }
    }
    function getValidateRule(?string $action): ?Validate    {
        $validate = null;
        switch ($action) {
            case 'getAll':
                $validate = new Validate();
                $validate->addColumn('page','页数')->optional();
                $validate->addColumn('limit','limit')->optional();
                $validate->addColumn('keyword','关键词')->optional();
                break;
            case 'getOne':
                $validate = new Validate();
                $validate->addColumn('userId', '会员id')->required()->lengthMax(11);
                break;
            case 'add':
                $validate = new Validate();
                $validate->addColumn('userName', '会员名')->required()->lengthMax(18);
                $validate->addColumn('userAccount', '会员账号')->required()->lengthMax(32);
                $validate->addColumn('userPassword', '会员密码')->required()->lengthMax(18);
                $validate->addColumn('phone', '手机号')->optional()->lengthMax(18);
                $validate->addColumn('state', '状态')->optional()->inArray(\[0,1\]);
                break;
            case 'update':
                $validate = new Validate();
                $validate->addColumn('userId', '会员id')->required()->lengthMax(11);
                $validate->addColumn('userName', '会员名')->optional()->lengthMax(18);
                $validate->addColumn('userPassword', '会员密码')->optional()->lengthMax(18);
                $validate->addColumn('phone', '手机号')->optional()->lengthMax(18);
                $validate->addColumn('state', '状态')->optional()->inArray(\[0,1\]);
                break;
            case 'delete':
                $validate = new Validate();
                $validate->addColumn('userId', '会员id')->required()->lengthMax(11);
                break;
        }
        return $validate;
    }}

后台管理员登陆之后,可通过此文件的接口,去进行curd会员

请求地址为: 127.0.0.1:9501/Api/Admin/User/getAll(等方法)

普通用户基础控制器定义

新增 App/HttpController/Api/User/UserBase.php 文件:

<?phpnamespace App\\HttpController\\Api\\User;use App\\HttpController\\Api\\ApiBase;use App\\Model\\User\\UserBean;use App\\Model\\User\\UserModel;use App\\Utility\\Pool\\MysqlPool;use App\\Utility\\Pool\\RedisPool;use EasySwoole\\Http\\Message\\Status;use EasySwoole\\MysqliPool\\Mysql;use EasySwoole\\Spl\\SplBean;use EasySwoole\\Validate\\Validate;class UserBase extends ApiBase{
    protected $who;
    //session的cookie头
    protected $sessionKey = 'userSession';
    //白名单
    protected $whiteList = \['login', 'register'\];
    function onRequest(?string $action): ?bool    {
        if (parent::onRequest($action)) {
            //白名单判断
            if (in_array($action, $this->whiteList)) {
                return true;
            }
            //获取登入信息
            if (!$data = $this->getWho()) {
                $this->writeJson(Status::CODE_UNAUTHORIZED, '', '登入已过期');
                return false;
            }
            //刷新cookie存活
            $this->response()->setCookie($this->sessionKey, $data->getUserSession(), time() + 3600, '/');
            return true;
        }
        return false;
    }
    function getWho(): ?UserBean    {
        if ($this->who instanceof UserBean) {
            return $this->who;
        }
        $sessionKey = $this->request()->getRequestParam($this->sessionKey);
        if (empty($sessionKey)) {
            $sessionKey = $this->request()->getCookieParams($this->sessionKey);
        }
        if (empty($sessionKey)) {
            return null;
        }
        $db = Mysql::defer('mysql');
        $userModel = new UserModel($db);
        $this->who = $userModel->getOneBySession($sessionKey);
        return $this->who;
    }
    protected function getValidateRule(?string $action): ?Validate    {
        return null;
        // TODO: Implement getValidateRule() method.
    }}

普通用户登录控制器

新增 App/HttpController/Api/User/Auth.php文件:

<?phpnamespace App\\HttpController\\Api\\User;use App\\HttpController\\Api\\ApiBase;use App\\Model\\User\\UserBean;use App\\Model\\User\\UserModel;use App\\Utility\\Pool\\MysqlPool;use App\\Utility\\Pool\\RedisPool;use EasySwoole\\Http\\Message\\Status;use EasySwoole\\MysqliPool\\Mysql;use EasySwoole\\Spl\\SplBean;use EasySwoole\\Validate\\Validate;class UserBase extends ApiBase{
    protected $who;
    //session的cookie头
    protected $sessionKey = 'userSession';
    //白名单
    protected $whiteList = \['login', 'register'\];
    function onRequest(?string $action): ?bool    {
        if (parent::onRequest($action)) {
            //白名单判断
            if (in_array($action, $this->whiteList)) {
                return true;
            }
            //获取登入信息
            if (!$data = $this->getWho()) {
                $this->writeJson(Status::CODE_UNAUTHORIZED, '', '登入已过期');
                return false;
            }
            //刷新cookie存活
            $this->response()->setCookie($this->sessionKey, $data->getUserSession(), time() + 3600, '/');
            return true;
        }
        return false;
    }
    function getWho(): ?UserBean    {
        if ($this->who instanceof UserBean) {
            return $this->who;
        }
        $sessionKey = $this->request()->getRequestParam($this->sessionKey);
        if (empty($sessionKey)) {
            $sessionKey = $this->request()->getCookieParams($this->sessionKey);
        }
        if (empty($sessionKey)) {
            return null;
        }
        $db = Mysql::defer('mysql');
        $userModel = new UserModel($db);
        $this->who = $userModel->getOneBySession($sessionKey);
        return $this->who;
    }
    protected function getValidateRule(?string $action): ?Validate    {
        return null;
        // TODO: Implement getValidateRule() method.
    }}

访问 127.0.0.1:9501/Api/User/Auth/login?userAccount=xsk&userPassword=123456 即可登陆成功

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
4月前
|
前端开发 JavaScript 开发者
Bottle 也太厉害了吧!轻量级 Web 开发的神奇魔法,你还不来试试?
【8月更文挑战第31天】Bottle是一个轻量级且功能全面的Python Web框架,以其高效、简洁的特点受到开发者青睐。通过简单的安装步骤即可快速搭建Web应用。本文档不仅介绍了如何使用Bottle创建基础的路由和响应,还展示了模板渲染、静态文件处理及表单操作等功能,帮助读者快速上手并充分发挥其潜力。
107 0
|
1月前
|
JSON API PHP
如何使用PHP开发API接口?
本文详细介绍了如何使用PHP开发API接口,涵盖从基础概念到实战步骤的全过程。首先解释了API接口的基本原理,包括HTTP协议、REST架构风格、JSON格式和OAuth认证机制。接着介绍了开发环境的设置,包括PHP安装、Web服务器配置、数据库设置等。文章还探讨了API开发的完整流程,从需求确定、框架选择、端点设计到代码编写、测试、安全性考量及性能优化。最后通过一个实战案例演示了如何创建一个简单的API端点,并讨论了部署与监控的方法。
86 0
|
2月前
|
前端开发 JavaScript 小程序
前端uni开发后端用PHP的圈子系统该 如何做源码?
圈子系统系统基于TP6+Uni-app框架开发;客户移动端采用uni-app开发,管理后台TH6开发。系统支持微信公众号端、微信小程序端、H5端、PC端多端账号同步,可快速打包生成APP
|
4月前
|
JSON API 数据安全/隐私保护
哇塞!Django REST framework 太逆天啦!构建 API 服务从未如此轻松,你还不来试试?
【8月更文挑战第31天】Django REST framework(DRF)是基于Django框架的高效Web API开发工具,提供序列化、视图集、路由等功能,简化API构建流程。使用DRF可轻松实现数据的序列化与反序列化,并支持权限管理和认证机制以保障API安全。安装DRF只需通过`pip install djangorestframework`命令。要创建基本项目,先安装Django并创建新应用,定义模型、序列化器及视图集,最后配置路由。测试API时,可通过Postman发送HTTP请求验证功能。无论项目大小,DRF均能提供强大支持。
49 0
|
6月前
|
PHP
PHP简约轻型聊天室留言源码
PHP简约轻型聊天室留言源码
45 1
|
5月前
|
开发框架 前端开发 JavaScript
若依怎样看开发文档,域名搜这个就行ruoyi.vip,建链接点击在线文档,有前端手册和后端手册,若依文档里有项目扩展,项目扩展有大量的开源的软件
若依怎样看开发文档,域名搜这个就行ruoyi.vip,建链接点击在线文档,有前端手册和后端手册,若依文档里有项目扩展,项目扩展有大量的开源的软件
|
7月前
|
前端开发 JavaScript 测试技术
【PHP开发专栏】PHP Web开发基础与流程
【4月更文挑战第29天】本文介绍了PHP Web开发的基础和流程,帮助初学者入门。内容包括Web服务器与PHP解释器的工作原理、HTML/CSS/JavaScript基础知识、PHP语法与数据库操作。开发流程涵盖项目规划、环境搭建、数据库设计、代码编写、测试与调试,以及部署与维护。此外,文中还强调了使用框架、代码组织、安全性及性能优化等进阶知识和最佳实践,旨在培养优秀PHP开发者。
104 0
|
JSON API PHP
漏刻有时API接口实战开发系列(6):PHP开发API接口离不开的curl函数封装
漏刻有时API接口实战开发系列(6):PHP开发API接口离不开的curl函数封装
81 0
|
开发框架 移动开发 网络协议
php进阶编程-easyswoole框架的学习和开发
php进阶编程-easyswoole框架的学习和开发
169 0
php进阶编程-easyswoole框架的学习和开发
|
SQL API 数据库
easyswoole快速实现一个网站的api接口程序
easyswoole快速实现一个网站的api接口程序
134 0
easyswoole快速实现一个网站的api接口程序