270 lines
8.1 KiB
PHP
270 lines
8.1 KiB
PHP
|
<?php
|
|||
|
|
|||
|
namespace app\controller\manager;
|
|||
|
|
|||
|
use app\model\Business as BusinessModel;
|
|||
|
use app\model\Log;
|
|||
|
use app\model\Member;
|
|||
|
use app\model\Member as MemberModel;
|
|||
|
use app\repository\BusinessRepository;
|
|||
|
use Exception;
|
|||
|
use tauthz\facade\Enforcer;
|
|||
|
use think\db\exception\DataNotFoundException;
|
|||
|
use think\db\exception\DbException;
|
|||
|
use think\db\exception\ModelNotFoundException;
|
|||
|
use think\exception\ValidateException;
|
|||
|
use think\facade\Db;
|
|||
|
use think\response\Json;
|
|||
|
use think\response\Redirect;
|
|||
|
use think\response\View;
|
|||
|
|
|||
|
//工作人员
|
|||
|
class Agency extends Base
|
|||
|
{
|
|||
|
|
|||
|
/**
|
|||
|
* 添加
|
|||
|
*
|
|||
|
* @return Json|View
|
|||
|
* @throws Exception
|
|||
|
*/
|
|||
|
public function add()
|
|||
|
{
|
|||
|
if ($this->request->isPost()) {
|
|||
|
$item = input('post.');
|
|||
|
|
|||
|
$validate = $this->validateByApi($item, [
|
|||
|
'username|用户名' => 'require|alphaDash|min:4|max:16|unique:member',
|
|||
|
'mobile|手机号' => 'require|unique:member',
|
|||
|
'nickname|昵称' => 'require|chsAlphaNum|min:2|max:10',
|
|||
|
'password|密码' => 'require|min:4|max:16',
|
|||
|
'remark|备注信息' => 'max:255',
|
|||
|
]);
|
|||
|
|
|||
|
if ($validate !== true) {
|
|||
|
return $validate;
|
|||
|
}
|
|||
|
|
|||
|
if (!checkMobile($item['mobile'])) {
|
|||
|
return $this->json(4002, '请输入正确的手机号码');
|
|||
|
}
|
|||
|
|
|||
|
$item['roles'] = [Member::ANENT_ROLE_ID];
|
|||
|
$item['status'] = Member::COMMON_ON;
|
|||
|
|
|||
|
$roles = [];
|
|||
|
if ($item['roles']) {
|
|||
|
$roles = $item['roles'];
|
|||
|
$item['roles'] = implode(',', $item['roles']);
|
|||
|
}
|
|||
|
|
|||
|
Db::startTrans();
|
|||
|
try {
|
|||
|
|
|||
|
//如果关联商家
|
|||
|
if (!empty($item['business_code'])) {
|
|||
|
$Business = BusinessRepository::getInstance()->findOneByWhere(["code" => $item['business_code']]);
|
|||
|
if (empty($Business)) {
|
|||
|
Db::rollback();
|
|||
|
return $this->json(4001, "指定商家不存在");
|
|||
|
}
|
|||
|
$Business->save(["is_agency" => BusinessModel::COMMON_ON]);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
$item['password'] = md5($item['password'] . $item['username']);
|
|||
|
$member = MemberModel::create($item);
|
|||
|
foreach ($roles as $role) {
|
|||
|
Enforcer::addRoleForUser($member['id'], $role);
|
|||
|
}
|
|||
|
Db::commit();
|
|||
|
return $this->json();
|
|||
|
} catch (ValidateException $e) {
|
|||
|
Db::rollback();
|
|||
|
return $this->json(4001, $e->getError());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
$this->data['roleJson'] = $this->roleJson();
|
|||
|
$this->data['business'] = BusinessRepository::getInstance()->getBusinessAll();
|
|||
|
|
|||
|
return $this->view();
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 编辑
|
|||
|
*
|
|||
|
* @return Json|View
|
|||
|
* @throws DataNotFoundException
|
|||
|
* @throws DbException
|
|||
|
* @throws ModelNotFoundException
|
|||
|
* @throws Exception
|
|||
|
*/
|
|||
|
public function edit()
|
|||
|
{
|
|||
|
$id = input('id/d', 0);
|
|||
|
|
|||
|
if (!$info = MemberModel::findById($id)) {
|
|||
|
return $this->json(4001, '记录不存在');
|
|||
|
}
|
|||
|
|
|||
|
if ($this->request->isPost()) {
|
|||
|
$item = input('post.');
|
|||
|
|
|||
|
$validate = $this->validateByApi($item, [
|
|||
|
'mobile|手机号' => 'require|unique:member,mobile,' . $id,
|
|||
|
'nickname|昵称' => 'require|chsAlphaNum|min:2|max:10',
|
|||
|
'remark|备注信息' => 'max:255',
|
|||
|
]);
|
|||
|
|
|||
|
if ($validate !== true) {
|
|||
|
return $validate;
|
|||
|
}
|
|||
|
|
|||
|
if (!checkMobile($item['mobile'])) {
|
|||
|
return $this->json(4002, '请输入正确的手机号码');
|
|||
|
}
|
|||
|
$item['roles'] = [Member::ANENT_ROLE_ID];
|
|||
|
$roles = [];
|
|||
|
if ($item['roles']) {
|
|||
|
$roles = $item['roles'];
|
|||
|
$item['roles'] = implode(',', $item['roles']);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
Db::startTrans();
|
|||
|
try {
|
|||
|
//之前关联的商家不为空 并且已经改变 吧之前的商家 从代理商变成普通商家
|
|||
|
if ($info['business_code'] != $item['business_code']) {
|
|||
|
if (!empty($info['business_code'])) {
|
|||
|
$oldBusiness = BusinessRepository::getInstance()->findOneByWhere(["code" => $info['business_code']]);
|
|||
|
if (!empty($oldBusiness)) {
|
|||
|
$oldBusiness->save(["is_agency" => BusinessModel::COMMON_OFF]);
|
|||
|
}
|
|||
|
}
|
|||
|
//如果改变了关联商家
|
|||
|
if (!empty($item['business_code'])) {
|
|||
|
$Business = BusinessRepository::getInstance()->findOneByWhere(["code" => $item['business_code']]);
|
|||
|
if (empty($Business)) {
|
|||
|
Db::rollback();
|
|||
|
return $this->json(4001, "指定商家不存在");
|
|||
|
}
|
|||
|
$Business->save(["is_agency" => BusinessModel::COMMON_ON]);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
$info->save($item);
|
|||
|
//删除所有角色
|
|||
|
Enforcer::deleteRolesForUser($id);
|
|||
|
//新增角色
|
|||
|
foreach ($roles as $role) {
|
|||
|
Enforcer::addRoleForUser($id, $role);
|
|||
|
}
|
|||
|
Db::commit();
|
|||
|
return $this->json();
|
|||
|
} catch (ValidateException $e) {
|
|||
|
Db::rollback();
|
|||
|
return $this->json(4001, $e->getError());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
$this->data['item'] = $info;
|
|||
|
$this->data['roleJson'] = $this->roleJson(explode(',', $info['roles']));
|
|||
|
|
|||
|
$this->data['business'] = BusinessRepository::getInstance()->getBusinessAll();
|
|||
|
|
|||
|
return $this->view();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* 轮播图列表
|
|||
|
*
|
|||
|
* @return Json|View
|
|||
|
* @throws Exception
|
|||
|
*/
|
|||
|
public function index()
|
|||
|
{
|
|||
|
|
|||
|
if ($this->request->isPost()) {
|
|||
|
$page = $this->request->param('page/d', 1);
|
|||
|
$size = $this->request->param('size/d', 30);
|
|||
|
|
|||
|
//只查询拥有渠道商的账号
|
|||
|
$whereMap = [['roles', "=", Member::ANENT_ROLE_ID], ['id', "<>", 1]];
|
|||
|
$orders = ['id' => 'asc'];
|
|||
|
|
|||
|
$list = Member::findList($whereMap, [], $page, $size, null, $orders);
|
|||
|
|
|||
|
return $this->json(0, 'success', $list);
|
|||
|
}
|
|||
|
return $this->view();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* 删除
|
|||
|
*
|
|||
|
* @return Json
|
|||
|
*/
|
|||
|
public function del(): Json
|
|||
|
{
|
|||
|
if ($this->request->isPost()) {
|
|||
|
$ids = input('post.ids/a', []);
|
|||
|
if (empty($ids)) {
|
|||
|
$ids[] = input('post.id/d');
|
|||
|
}
|
|||
|
MemberModel::deleteByIds($ids);
|
|||
|
foreach ($ids as $id) {
|
|||
|
Enforcer::deleteRolesForUser($id);
|
|||
|
}
|
|||
|
Log::write(get_class() . 'Del', 'del', '涉及到的ID为:' . implode(',', $ids));
|
|||
|
return $this->json();
|
|||
|
}
|
|||
|
return $this->json(4001, '非法请求!');
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* 修改密码
|
|||
|
*
|
|||
|
* @return Json|View|Redirect
|
|||
|
* @throws Exception
|
|||
|
*/
|
|||
|
public function password()
|
|||
|
{
|
|||
|
$id = input('id/d', 0);
|
|||
|
|
|||
|
if (!$item = MemberModel::findById($id)) {
|
|||
|
if ($this->request->isAjax()) {
|
|||
|
return $this->json(4001, '记录不存在');
|
|||
|
}
|
|||
|
return $this->error('记录不存在');
|
|||
|
}
|
|||
|
|
|||
|
if ($this->request->isPost()) {
|
|||
|
$post = input('post.');
|
|||
|
$validate = $this->validateByApi($post, [
|
|||
|
'password|密码' => 'require|confirm',
|
|||
|
]);
|
|||
|
|
|||
|
if ($validate !== true) {
|
|||
|
return $validate;
|
|||
|
}
|
|||
|
|
|||
|
$password = md5($post['password'] . $item['username']);
|
|||
|
|
|||
|
try {
|
|||
|
$item->save(['password' => $password]);
|
|||
|
return $this->json();
|
|||
|
} catch (ValidateException $e) {
|
|||
|
return $this->json(4001, $e->getError());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
$this->data['item'] = $item;
|
|||
|
|
|||
|
return $this->view();
|
|||
|
}
|
|||
|
|
|||
|
}
|