crmeb多商户二开接口流程文档【1】

流程

主要讲解controller目录,每个controller有自己独立的路由,配置,事件,容器,控制器中可用框架核心及扩展。每个控制器其实就是一个独立的控制类。

请求流程大致分为以下流程,依次从左到右

Middleware

中间件分为前置和后置中间件.

前置中间件在访问控制器之前会被执行调用,通常用来拦截参数,跨域配置,多语言加载,Session初始化,权限验证,登陆验证等处理;

而后置中间件属于返回数据后在执行的中间件,用来做一些返回数据后需要执行的任务等业务逻辑

下面以前置中间件为例:文件存放目录 app/应用名/middleware/AuthMiddleware.php

 
  1. <?php
  2. namespase app\应用名\middleware;
  3. use crmeb\interfaces\MiddlewareInterface;
  4. class AuthMiddleware implements MiddlewareInterface
  5. {
  6. public function handle(Request $request, \Closure $next)
  7. {
  8. //这里可以设置请求header
  9. //可以写权限验证
  10. //验证失败直接可以抛出异常中止请求
  11. if(false)
  12. {
  13. throw new AuthException('无权验证');
  14. }
  15. return $next($request);
  16. }
  17. }

Controller

每个控制器负责相关业务的请求接收,只做最基本的数据接收,并调用相关的sevices业务处理层,返回数据。
例如:

 
  1. <?php
  2. namespace app\controller;
  3. use app\Request;
  4. use app\common\repositories\user\UserRepository;
  5. class User
  6. {
  7. protected $repository;
  8. public function __construct(App $app, repository $repository)
  9. {
  10. parent::__construct($app);
  11. $this->repository = $repository;
  12. }
  13. public function lst($cid)
  14. {
  15. [$page, $limit] = $this->getPage();
  16. $where = $this->request->params(['id']);
  17. $data = $this->repository->search($where, $page, $limit);
  18. return app('json')->success($data);
  19. }
  20. }

Repository

所有的业务都在Repository层中处理,Repository层调用dao层,【注意:每个独立的services层只能调用对应的dao层,不能调用其他模型dao层。
比如:repository/user/UserRepository.php中只能调用dao/user/UserDao.php,无法调用 dao/order/StoreOrderDao.php。要想调用其他模型数据,
只能在UserServices.php中调用services/order/StoreOrderServices.php的方法来实现其他模型数据调用】。
例如:

 
  1. <?php
  2. namespace app\common\repositories\user;
  3. use app\common\dao\user\UserDao;
  4. class UserRepository extends BaseRepository
  5. {
  6. protected $dao;
  7. public function __construct(UserDao $dao)
  8. {
  9. $this->dao = $dao;
  10. }
  11. public function search(array $where, int $page, int $limit)
  12. {
  13. $list = $this->dao->getList($where,'*',$page,$limit);
  14. $count = $this->dao->count($where);
  15. return compact('count','list');
  16. }
  17. }

Dao

dao层中主要用于当前模型基本的数据处理方法。dao层中调用对应的model,同样无法跨层调用。
例如:

 
  1. <?php
  2. namespace app\dao\user;
  3. use app\dao\BaseDao;
  4. use app\model\user\User;
  5. /**
  6. * 用户
  7. * Class UserDao
  8. * @package app\dao\user
  9. */
  10. class UserDao extends BaseDao
  11. {
  12. protected function setModel(): string
  13. {
  14. return User::class;
  15. }
  16. public function getOne($uid)
  17. {
  18. return $this->where(['uid' => $uid])->field('username,phone')->find();
  19. }
  20. }

Model

model主要用于实例化数据表,只做相关数据表的基础设置,搜索器,设置器及表关联等操作。

 
  1. <?php
  2. namespace app\model\user;
  3. use crmeb\basic\BaseModel;
  4. use think\Model;
  5. /**
  6. * Class User
  7. * @package app\model\user
  8. */
  9. class User extends BaseModel
  10. {
  11. /**
  12. * @var string
  13. */
  14. protected $pk = 'uid';
  15. protected $name = 'user';
  16. protected $insert = ['add_time', 'add_ip', 'last_time', 'last_ip'];
  17. protected $hidden = [
  18. 'add_ip', 'account', 'clean_time', 'last_ip', 'pwd'
  19. ];
  20. /**
  21. * 自动转类型
  22. * @var string[]
  23. */
  24. protected $type = [
  25. 'birthday' => 'int'
  26. ];
  27. protected $updateTime = false;
  28. /**
  29. * 修改器
  30. */
  31. protected function setAddTimeAttr($value)
  32. {
  33. return time();
  34. }
  35. /**
  36. * 关联订单
  37. * @return User|\think\model\relation\HasMany
  38. */
  39. public function order()
  40. {
  41. return $this->hasMany(StoreOrder::class, 'uid', 'uid');
  42. }
  43. /**
  44. /**
  45. * 搜索器 用户uid
  46. * @param Model $query
  47. * @param $value
  48. */
  49. public function searchUidAttr($query, $value)
  50. {
  51. if (is_array($value))
  52. $query->whereIn('uid', $value);
  53. else
  54. $query->where('uid', $value);
  55. }
  56. }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

crmeb专业二开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值