后台角色接口

main
limu 12 months ago
parent 59b4cf5af0
commit 05c2c81eca
  1. 55
      app/common/dao/system/admin/PartnerDao.php
  2. 42
      app/common/model/user/UserPartner.php
  3. 31
      app/common/repositories/system/admin/PartnerRepository.php
  4. 45
      app/controller/admin/system/admin/Partner.php
  5. 9
      route/admin/role.php

@ -17,6 +17,7 @@ namespace app\common\dao\system\admin;
use app\common\dao\BaseDao;
use app\common\model\BaseModel;
use app\common\model\system\admin\Partner;
use app\common\model\user\UserPartner;
use app\common\model\system\admin\Admin;
use think\db\BaseQuery;
use think\db\exception\DataNotFoundException;
@ -55,58 +56,12 @@ class PartnerDao extends BaseDao
return $query;
}
public function exists(int $id)
public function existsUser(int $id)
{
$query = ($this->getModel())::getDB()->where($this->getPk(), $id)->where('is_del', 0);
return $query->count() > 0;
}
/**
* @param int $id
* @return array|Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author xaboy
* @day 2020-04-09
*/
public function get( $id)
{
return Admin::getInstance()->where('is_del', 0)->find($id);
}
/**
* @param $field
* @param $value
* @param int|null $except
* @return bool
* @author xaboy
* @day 2020-03-30
*/
public function fieldExists($field, $value, ?int $except = null): bool
{
$query = ($this->getModel())::getDB()->where($field, $value)->where('is_del', 0);
if (!is_null($except)) $query->where($this->getPk(), '<>', $except);
return $query->count() > 0;
}
/**
* @param string $account
* @return array|Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author xaboy
* @day 2020-04-09
*/
public function accountByAdmin(string $account)
{
return Admin::getInstance()->where('account', $account)
->where('is_del', 0)
->field(['account', 'pwd', 'real_name', 'login_count', 'admin_id', 'status'])
return UserPartner::getInstance()->where('partner_id', $id)
->field(['id'])
->find();
}
}

@ -0,0 +1,42 @@
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\common\model\user;
use app\common\model\BaseModel;
class UserPartner extends BaseModel
{
/**
* @return string
* @author xaboy
* @day 2020-03-30
*/
public static function tablePk(): string
{
return 'id';
}
/**
* @return string
* @author xaboy
* @day 2020-03-30
*/
public static function tableName(): string
{
return 'user_partner';
}
}

@ -49,6 +49,7 @@ class PartnerRepository extends BaseRepository
$this->dao = $dao;
}
/**
* @param array $where
* @param $page
@ -73,6 +74,22 @@ class PartnerRepository extends BaseRepository
}
/**
* @param int $id
* @return Form
* @throws DataNotFoundException
* @throws DbException
* @throws FormBuilderException
* @throws ModelNotFoundException
* @author xaboy
* @day 2020-04-09
*/
public function getDetail(int $id)
{
return $this->dao->get($id)->toArray();
}
/**
*
* 更新
* @param int $id id
* @param array $data 数组
@ -83,14 +100,20 @@ class PartnerRepository extends BaseRepository
*/
public function update(int $id, array $data)
{
if (isset($data['roles']))
$data['roles'] = implode(',', $data['roles']);
return $this->dao->update($id, $data);
}
//删除
public function delete(int $id)
{
return $this->dao->delete($id);
}
//检测角色绑定用户
public function existsUser(int $id)
{
return $this->dao->existsUser($id);
}
}

@ -45,11 +45,56 @@ class Partner extends BaseController
return app('json')->success($this->repository->getList($where, $page, $limit));
}
public function add(PartnerRoleValidate $validate)
{
$data = $this->request->params(['name', 'ratio', 'is_area', 'area_level', 'area_id', ['status', 1]]);
$validate->check($data);
if ($this->repository->fieldExists('area_id', $data['area_id'])) {
return app('json')->fail('该区域已经有了代理角色,请勿重复添加');
}
$this->repository->create($data);
return app('json')->success('添加成功');
}
public function update(PartnerRoleValidate $validate)
{
$data = $this->request->params(['name', 'ratio', 'is_area', 'area_level', 'area_id', 'id', ['status', 1]]);
$validate->check($data);
if ($data['is_area'] == 1) {
if (empty($data['area_id']) && empty($data['area_level'])) {
return app('json')->fail('请选择所属区域');
}
if ($this->repository->fieldExists('area_id', $data['area_id'], $data['id'])) {
return app('json')->fail('该区域已经有了代理角色,请勿重复添加');
}
} else {
$data['area_level'] = 0;
$data['area_id'] = 0;
}
$this->repository->update($data['id'], $data);
return app('json')->success('编辑成功');
}
public function detail($id)
{
if (!$this->repository->exists($id))
return app('json')->fail('数据不存在');
return app('json')->success($this->repository->getDetail($id));
}
public function delete($id)
{
if (!$this->repository->exists($id))
return app('json')->fail('数据不存在');
if ($this->repository->existsUser($id))
return app('json')->fail('该角色下存在绑定用,请先取消绑定');
$this->repository->delete($id);
return app('json')->success('删除成功');
}
}

@ -143,6 +143,15 @@ Route::group(function () {
Route::post('add', '.Partner/add')->name('systemPartnerAdd')->option([
'_alias' => '添加合作人角色',
]);
Route::post('update', '.Partner/update')->name('systemPartnerUpdate')->option([
'_alias' => '编辑合作人角色',
]);
Route::get('detail/:id', '.Partner/detail')->name('systemPartnerDetail')->option([
'_alias' => '读取合作人角色详情',
]);
Route::get('delete/:id', '.Partner/delete')->name('systemPartnerDel')->option([
'_alias' => '删除合作人角色',
]);
})->prefix('admin.system.admin')->option([
'_path' => 'self',
'_auth' => true,

Loading…
Cancel
Save