Skip to content

命名规范

文件命名规范

  • 后台服务层(admin)功能模块名Service.php,例如 UserService.php

  • 前台服务层(api)功能模块名Service.php,例如 UserService.php

  • 公共核心服务层(core)Core功能模块名Service.php,例如 CoreUserService.php

类命名规范

  • 后台服务层(admin)功能模块名Service,例如 UserService

  • 前台服务层(api)功能模块名Service,例如 UserService

  • 公共核心服务层(core)Core功能模块名Service,例如 CoreUserService

开发编码规范

方法命名基本原则

  • 采用小驼峰命名法

  • 命名应简洁明了,避免过长

  • 名称应准确表达方法的功能意图

文件与业务组织原则

  • 建议一个文件只处理一个模型表的相关业务

  • 对于复杂业务逻辑,可整合到一个文件中,但需遵循以下命名规范:

当多个同类型功能需要写在一个文件内时,可在基础命名前增加业务名称前缀进行区分,例如:

  • getBrandPageList(查询品牌分页列表)

  • getAttrList(查询属性列表)

  • addStat(添加统计)

  • editStat(编辑统计)

  • deleteBrand(删除品牌)

方法命名规范

查询分页:getPage

php
/**
 * 获取分页列表
 * @param array $where
 * @return array
 */
public function getPage(array $where = [])
{
	$field = 'brand_id,brand_namecreate_time';
	$order = 'brand_id desc';

	$search_model = $this->model->where([ [ 'site_id', '=', $this->site_id ] ])->withSearch([ "brand_name" ], $where)->field($field)->order($order);
	$list = $this->pageQuery($search_model);
	return $list;
}

查询列表:getList

php
/**
 * 获取列表
 * @param array $where
 * @param string $field
 * @return array
 */
public function getList(array $where = [], $field = 'brand_id,brand_name,create_time')
{
	$order = 'create_time desc';
	return $this->model->where([ [ 'site_id', '=', $this->site_id ] ])->withSearch([ "brand_name" ], $where)->field($field)->limit(10)->order($order)->select()->toArray();
}

查询信息:getInfo(单表数据)

php
/**
 * 获取信息
 * @param int $id
 * @return array
 */
public function getInfo(int $id)
{
	$field = 'brand_id,brand_name,create_time';
	$info = $this->model->field($field)->where([ [ 'brand_id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->findOrEmpty()->toArray();
	return $info;
}

查询详情:getDetail(关联表数据)

php
/**
 * 获取详情
 * @param int $id
 * @return array
 */
public function getDetail(array $data)
{
  // todo 编写业务代码

添加数据:add

php
/**
 * 添加商品品牌
 * @param array $data
 * @return mixed
 */
public function add(array $data)
{
	$data[ 'create_time' ] = time();
	$data[ 'site_id' ] = $this->site_id;
	$brandInfo = $this->model->where([ [ 'site_id', '=', $this->site_id ], [ 'brand_name', '=', $data[ 'brand_name' ] ] ])->findOrEmpty()->toArray();
	if ($brandInfo) {
		throw new AdminException('品牌已存在,请检查');
	}
	$res = $this->model->create($data);
	return $res->brand_id;

}

编辑数据:edit

php
/**
 * 商品品牌编辑
 * @param int $id
 * @param array $data
 * @return bool
 */
public function edit(int $id, array $data)
{
	$data[ 'update_time' ] = time();
	$brandInfo = $this->model->where([ [ 'site_id', '=', $this->site_id ], [ 'brand_name', '=', $data[ 'brand_name' ] ] ])->findOrEmpty()->toArray();
	if ($brandInfo && $brandInfo[ 'brand_id' ] != $id) {
		throw new AdminException('品牌已存在,请检查');
	}
	$this->model->where([ [ 'brand_id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->update($data);
	return true;
}

删除数据:del

php
/**
 * 删除商品品牌
 * @param int $id
 * @return bool
 */
public function del(int $id)
{
    $model = $this->model->where([ [ 'brand_id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->find();
    $res = $model->delete();
    return $res;
}

修改某个字段:modify+字段名称,例如:modifySort、modifyStatus

php
/**
 * 修改排序
 * @param $data
 * @return Brand
 */
public function modifySort($data)
{
	return $this->model->where([
		[ 'brand_id', '=', $data[ 'brand_id' ] ],
		[ 'site_id', '=', $this->site_id ]
	])->update([ 'sort' => $data[ 'sort' ] ]);
}

基于 MIT 协议发布