coupon-admin/app/controller/manager/Agency.php

468 lines
14 KiB
PHP
Raw Normal View History

2021-11-25 10:11:50 +00:00
<?php
namespace app\controller\manager;
use app\model\Business as BusinessModel;
use app\model\Log;
2022-04-07 06:31:17 +00:00
use app\model\Member;
2021-11-25 10:11:50 +00:00
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()) {
2022-04-07 06:31:17 +00:00
$roles = array_filter( explode(",",$this->auth["roles"]));
//如果是 渠道商或者工作人员
if(!in_array(Member::ANENT_ROLE_ID,$roles)){
return $this->json(4001,"您不是是代理商,不能添加工作人员");
}
2021-11-25 10:11:50 +00:00
$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, '请输入正确的手机号码');
}
2021-11-30 10:31:58 +00:00
$item['roles'] = MemberModel::STAFF_ROLE_ID;
2021-11-30 07:26:38 +00:00
$item['status'] = MemberModel::COMMON_ON;
2021-11-30 10:31:58 +00:00
$item['pid'] = $this->auth["user_id"];
$item['business_code'] = $this->auth["business_code"];
2021-11-25 10:11:50 +00:00
2021-11-30 10:31:58 +00:00
$roles = [MemberModel::STAFF_ROLE_ID];
2021-11-25 10:11:50 +00:00
Db::startTrans();
try {
2021-11-30 10:31:58 +00:00
$item['password'] = md5($item['password'] . $item['username']);
$member = MemberModel::create($item);
foreach ($roles as $role) {
Enforcer::addRoleForUser($member['id'], $role);
2021-11-25 10:11:50 +00:00
}
2021-11-30 10:31:58 +00:00
Db::commit();
return $this->json();
} catch (ValidateException $e) {
Db::rollback();
return $this->json(4001, $e->getError());
}
}
$this->data['roleJson'] = $this->roleJson();
return $this->view();
}
/**
* 添加平台商
*
* @return Json|View
* @throws Exception
*/
public function addChannel()
{
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, '请输入正确的手机号码');
}
2021-11-25 10:11:50 +00:00
2021-11-30 10:31:58 +00:00
$item['roles'] = MemberModel::ANENT_ROLE_ID;
$item['status'] = MemberModel::COMMON_ON;
$item['pid'] = 0;
$roles = [MemberModel::ANENT_ROLE_ID];
2021-11-25 10:11:50 +00:00
2021-11-30 10:31:58 +00:00
Db::startTrans();
try {
2021-11-25 10:11:50 +00:00
$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();
2022-04-07 06:31:17 +00:00
$this->data['business'] = BusinessRepository::getInstance()->getBusinessAll($this->auth["roles"],$this->auth["business_code"]);
2021-11-25 10:11:50 +00:00
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, '请输入正确的手机号码');
}
2021-11-30 10:31:58 +00:00
Db::startTrans();
try {
$info->save($item);
Db::commit();
return $this->json();
} catch (ValidateException $e) {
Db::rollback();
return $this->json(4001, $e->getError());
}
}
$this->data['item'] = $info;
return $this->view();
}
/**
* 编辑
*
* @return Json|View
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws Exception
*/
public function editChannel()
{
$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',
2021-12-22 10:50:19 +00:00
'business_code|商家' => 'length:32',
2021-11-30 10:31:58 +00:00
]);
if ($validate !== true) {
return $validate;
2021-11-25 10:11:50 +00:00
}
2021-11-30 10:31:58 +00:00
if (!checkMobile($item['mobile'])) {
return $this->json(4002, '请输入正确的手机号码');
}
2021-11-25 10:11:50 +00:00
Db::startTrans();
try {
//之前关联的商家不为空 并且已经改变 吧之前的商家 从代理商变成普通商家
if ($info['business_code'] != $item['business_code']) {
2021-12-22 10:50:19 +00:00
if(!empty($info['business_code'])){
2021-11-25 10:11:50 +00:00
$oldBusiness = BusinessRepository::getInstance()->findOneByWhere(["code" => $info['business_code']]);
if (!empty($oldBusiness)) {
$oldBusiness->save(["is_agency" => BusinessModel::COMMON_OFF]);
}
}
//如果改变了关联商家
2021-12-22 10:50:19 +00:00
if(!empty($item['business_code'])){
$business = BusinessRepository::getInstance()->findOneByWhere(["code" => $item['business_code']]);
if (empty($business)) {
2021-11-25 10:11:50 +00:00
Db::rollback();
return $this->json(4001, "指定商家不存在");
}
2021-12-23 10:09:30 +00:00
if($business->is_agency == BusinessModel::COMMON_ON){
Db::rollback();
return $this->json(4001, "该商家已经是代理商,不能重复绑定");
}
2021-12-22 10:50:19 +00:00
$business->save(["is_agency" => BusinessModel::COMMON_ON]);
2021-11-25 10:11:50 +00:00
}
2021-11-30 10:31:58 +00:00
//修改下级工作人员的平台商号
2021-12-22 10:50:19 +00:00
memberModel::where("pid",$info['id'])->update(["business_code"=>$item['business_code']]);
2021-11-25 10:11:50 +00:00
}
$info->save($item);
Db::commit();
return $this->json();
} catch (ValidateException $e) {
Db::rollback();
return $this->json(4001, $e->getError());
}
}
$this->data['item'] = $info;
2022-04-07 06:31:17 +00:00
$this->data['business'] = BusinessRepository::getInstance()->getBusinessAll($this->auth["roles"],$this->auth["business_code"]);
2021-11-25 10:11:50 +00:00
return $this->view();
}
/**
2021-11-30 10:31:58 +00:00
* 工作人员列表
2021-11-25 10:11:50 +00:00
*
* @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);
//只查询拥有渠道商的账号
2021-11-30 07:26:38 +00:00
$whereMap = [['roles', "=", MemberModel::STAFF_ROLE_ID], ['id', "<>", 1]];
2021-11-25 10:11:50 +00:00
$orders = ['id' => 'asc'];
2021-11-30 07:26:38 +00:00
//如果是渠道商或者工作人员 只查看自己的商家
2021-11-30 10:31:58 +00:00
if (MemberModel::is_agency($this->auth['roles'])) {
2021-11-30 07:26:38 +00:00
$whereMap[] = ["pid", "=", $this->auth['user_id']];
}
2021-11-30 10:31:58 +00:00
$list = MemberModel::findList($whereMap, [], $page, $size, null, $orders);
return $this->json(0, 'success', $list);
}
return $this->view();
}
/**
2022-04-07 06:31:17 +00:00
* 代理商列表
2021-11-30 10:31:58 +00:00
*
* @return Json|View
* @throws Exception
*/
public function channelList()
{
if ($this->request->isPost()) {
$page = $this->request->param('page/d', 1);
$size = $this->request->param('size/d', 30);
//只查询拥有渠道商的账号
$whereMap = [['roles', "=", MemberModel::ANENT_ROLE_ID], ['id', "<>", 1]];
$orders = ['id' => 'asc'];
2021-11-30 07:26:38 +00:00
$list = MemberModel::findList($whereMap, [], $page, $size, null, $orders);
2021-11-25 10:11:50 +00:00
2021-11-30 10:31:58 +00:00
$list["list"]->each(function ($item) {
//管理的商家数
$item->business_count = BusinessRepository::getInstance()->agencyHasBusinessCount($item['business_code']);
2022-03-31 06:42:56 +00:00
//管理的商家的签到券数
2021-11-30 10:31:58 +00:00
$item->coupon_count = BusinessRepository::getInstance()->agencyHasCouponCount($item['business_code']);
});
2021-11-25 10:11:50 +00:00
return $this->json(0, 'success', $list);
}
return $this->view();
}
2021-11-30 10:31:58 +00:00
/**
* 单个字段编辑
*
* @return Json
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws Exception
*/
public function modify(): Json
{
if ($this->request->isPost()) {
$item = input('post.');
$validate = $this->validateByApi($item, [
'field' => 'require',
'value' => 'require',
]);
if ($validate !== true) {
return $validate;
}
if (!$info = MemberModel::findById($item['id'])) {
return $this->json(4001, '记录不存在');
}
$update = [$item['field'] => $item['value']];
try {
$info->save($update);
return $this->json();
} catch (ValidateException $e) {
return $this->json(4001, $e->getError());
}
}
return $this->json(4000, '非法请求');
}
2021-11-25 10:11:50 +00:00
/**
* 删除
*
* @return Json
*/
public function del(): Json
{
if ($this->request->isPost()) {
$ids = input('post.ids/a', []);
if (empty($ids)) {
$ids[] = input('post.id/d');
}
2021-11-30 10:31:58 +00:00
foreach ($ids as $id) {
if ($id == 1) {
return $this->json(5000, "错误的删除操作");
}
}
2021-11-25 10:11:50 +00:00
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, '非法请求!');
}
2021-11-30 10:31:58 +00:00
/**
* 删除
*
* @return Json
*/
public function delChannel(): Json
{
if ($this->request->isPost()) {
$ids = input('post.ids/a', []);
if (empty($ids)) {
$ids[] = input('post.id/d');
}
2021-12-22 10:50:19 +00:00
$member = MemberModel::findList([["id","in",$ids]]);
foreach ($member as $item){
if ($item->id == 1) {
2021-11-30 10:31:58 +00:00
return $this->json(5000, "错误的删除操作");
}
2021-12-22 10:50:19 +00:00
if(MemberModel::hasStaff($item->id)){
2021-11-30 10:31:58 +00:00
return $this->json(5000, "还存在员工,不能删除");
}
2021-12-22 10:50:19 +00:00
//修改对应的商家成普通商家
if(!empty($item->business_code)){
BusinessModel::where("code",$item->business_code)->update(["is_agency"=>BusinessModel::COMMON_OFF]);
}
2021-11-30 10:31:58 +00:00
}
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, '非法请求!');
}
2021-11-25 10:11:50 +00:00
/**
* 修改密码
*
* @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();
}
}