Skip to content

Service层

服务层概述

services层属于Model层之下(niucloud-admin框架没有DAO层),是最接近控制器层的。所有的业务逻辑都可以写在services层中。

目录结构

plaintext
niucloud/
├── app/
│   ├── service/
│   │   ├── admin/           # 管理端服务层
│   │   │   ├── member/      # 会员相关服务
│   │   │   ├── sys/         # 系统相关服务
│   │   │   └── ...          # 其他管理端服务模块
│   │   ├── api/             # API服务层(前端接口)
│   │   │   ├── member/      # 会员相关服务
│   │   │   ├── sys/         # 系统相关服务
│   │   │   └── ...          # 其他API服务模块
│   │   └── core/            # 核心服务层(跨站点公共服务)
│   │       ├── member/      # 会员核心服务
│   │       ├── sys/         # 系统核心服务
│   │       └── ...          # 其他核心服务模块

service层包括model实例的变量,request请求两个字段

php
protected $model;  
protected $request;

service内置方法

获取当前分页页码和展示条数,会根据系统配置返回不大于配置的展示条数

php
public function getPageParam()

获取分页列表

php
public function getPageList(Model $model, array $where, string $field = '*', string $order = '', array $append = [], $with = null, $each = null){

分页数据查询,传入model(查询后结果)

php
public function pageQuery($model, $each = null)

分页视图列表查询

php
public function getPageViewList(Model $model, array $where, string $field = '*', string $order = '', array $append = [], $with = null, $each = null)

BaseService类按照端口,有以下子类

BaseCoreService、 BaseAdminService、 BaseApiService

这几个类的文件都定义在 niucloud\core\base 文件夹中。 BaseCoreService是所有内核服务类的基类,对于可能在多个端口都会调用的service类,一般继承自他。

BaseAdminService是Admin管理端的Service的基类,该类实现了站点端(商家端、租户端)的site_id 的变量的赋值和取值,当前登录的用户名和uid字段。

php
class BaseAdminService extends BaseService
{

    protected $site_id;
    protected $username;
    protected $uid;

    protected $app_type;
    public function __construct()
    {
        parent::__construct();
        $this->app_type = $this->request->appType();
        $this->site_id = $this->request->siteId();
        $this->username = $this->request->username();
        $this->uid = $this->request->uid();
    }
}

BaseApiService是前端API(web、uni-app)的Service的基类,该类实现了site_id 的变量的取值,当前登录的会员ID(member_id)和登录方式的字段取值。

php
class BaseApiService extends BaseService
{

    protected $site_id;
    protected $member_id;
    protected $channel;

    public function __construct()
    {
        parent::__construct();
        $this->site_id = $this->request->siteId();
        $this->member_id = $this->request->memberId();
        $this->channel = $this->request->getChannel();
    }
}

基于 MIT 协议发布