new-admin-api/app/controller/merchant/store/service/StoreService.php

273 lines
7.7 KiB
PHP

<?php
namespace app\controller\merchant\store\service;
use app\common\repositories\store\service\StoreServiceLogRepository;
use app\common\repositories\store\service\StoreServiceRepository;
use app\common\repositories\user\UserRepository;
use app\validate\merchant\StoreServiceValidate;
use crmeb\basic\BaseController;
use FormBuilder\Exception\FormBuilderException;
use think\App;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
/**
* Class StoreService
* @package app\controller\merchant\store\service
* @author xaboy
* @day 2020/5/29
*/
class StoreService extends BaseController
{
/**
* @var StoreServiceRepository
*/
protected $repository;
/**
* @var StoreServiceLogRepository
*/
protected $logRepository;
/**
* StoreService constructor.
* @param App $app
* @param StoreServiceRepository $repository
*/
public function __construct(App $app, StoreServiceRepository $repository, StoreServiceLogRepository $logRepository)
{
parent::__construct($app);
$this->repository = $repository;
$this->logRepository = $logRepository;
}
/**
* @return mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author xaboy
* @day 2020/5/29
*/
public function lst()
{
$where = $this->request->params(['keyword', 'status']);
[$page, $limit] = $this->getPage();
$where['mer_id'] = $this->request->merId();
return app('json')->success($this->repository->getList($where, $page, $limit));
}
/**
* @return mixed
* @throws FormBuilderException
* @author xaboy
* @day 2020/5/29
*/
public function createForm()
{
return app('json')->success(formToData($this->repository->form($this->request->merId())));
}
// 添加员工
public function create(StoreServiceValidate $validate){
$data = $this->checkParams($validate);
$data['mer_id'] = $this->request->merId();
$this->repository->createInfo($data);
return app('json')->success('添加成功');
}
/**
* @param StoreServiceValidate $validate
* @return array
* @author xaboy
* @day 2020/5/29
*/
public function checkParams(StoreServiceValidate $validate, $isUpdate = false)
{
$data = $this->request->params([
['uid',[]],
'nickname',
'account',
'pwd',
'confirm_pwd',
'is_open',
['status',0],
'customer',
'is_verify',
'is_goods',
'is_user',
'staff_manage',
'notify',
'avatar',
'phone',
['sort',0]
]);
if ($isUpdate) {
$validate->update();
}
if (!$this->request->merId()) {
$data['is_verify'] = 0;
$data['customer'] = 0;
$data['is_goods'] = 0;
$data['notify'] = 0;
$data['phone'] = '';
}
$validate->check($data);
if (!$data['avatar']) $data['avatar'] = $data['uid']['src'];
if ($data['pwd'] && $data['pwd'] != $data['confirm_pwd']) {
throw new ValidateException('员工密码与确认密码不一致');
}
$data['uid'] = $data['uid']['id'];
unset($data['confirm_pwd']);
return $data;
}
/**
* @param $id
* @return mixed
* @throws FormBuilderException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author xaboy
* @day 2020/5/29
*/
public function updateForm($id)
{
if (!$this->repository->merExists($this->request->merId(), $id))
return app('json')->fail('数据不存在');
return app('json')->success(formToData($this->repository->updateForm($id)));
}
// 修改信息
public function update($id, StoreServiceValidate $validate){
$data = $this->checkParams($validate, true);
$data['mer_id'] = $this->request->merId();
$this->repository->updateInfo($id, $data);
return app('json')->success('修改成功');
}
/**
* @param int $id
* @return mixed
* @throws DbException
* @author xaboy
* @day 2020/5/29
*/
public function changeStatus($id)
{
$status = $this->request->param('status');
if (!$this->repository->merExists($this->request->merId(), $id))
return app('json')->fail('数据不存在');
$this->repository->update($id, ['is_open' => $status == 1 ? 1 : 0]);
return app('json')->success('修改成功');
}
/**
* @param $id
* @return mixed
* @throws DbException
* @author xaboy
* @day 2020/5/29
*/
public function delete($id)
{
if (!$this->repository->merExists($this->request->merId(), $id))
return app('json')->fail('数据不存在');
$this->repository->delete($id);
return app('json')->success('删除成功');
}
/**
* TODO 员工的全部用户
* @param $id
* @return mixed
* @author Qinii
* @day 2020-06-18
*/
public function serviceUserList($id)
{
if (!$this->repository->merExists($this->request->merId(), $id))
return app('json')->fail('数据不存在');
[$page, $limit] = $this->getPage();
return app('json')->success($this->logRepository->getServiceUserList($id, $page, $limit));
}
/**
* TODO 商户的全部用户列表
* @return mixed
* @author Qinii
* @day 2020-06-19
*/
public function merchantUserList()
{
[$page, $limit] = $this->getPage();
return app('json')->success($this->logRepository->getMerchantUserList($this->request->merId(), $page, $limit));
}
/**
* TODO 用户与员工聊天记录
* @param $id
* @param $uid
* @return mixed
* @author Qinii
* @day 2020-06-19
*/
public function getUserMsnByService($id, $uid)
{
[$page, $limit] = $this->getPage();
if (!$this->repository->getWhereCount(['service_id' => $id, 'mer_id' => $this->request->merId()]))
return app('json')->fail('员工不存在');
return app('json')->success($this->logRepository->getUserMsn($uid, $page, $limit, $this->request->merId(), $id));
}
/**
* TODO 用户与商户聊天记录
* @param $id
* @return mixed
* @author Qinii
* @day 2020-06-19
*/
public function getUserMsnByMerchant($id)
{
[$page, $limit] = $this->getPage();
return app('json')->success($this->logRepository->getUserMsn($id, $page, $limit, $this->request->merId()));
}
public function getUserList()
{
[$page, $limit] = $this->getPage();
$where = $this->request->params(['nickname']);
$where['status'] = 1;
$data = app()->make(UserRepository::class)->getPulbicLst($where, $page, $limit);
return app('json')->success($data);
}
public function login($id)
{
if (!$this->repository->merExists($this->request->merId(), $id))
return app('json')->fail('数据不存在');
$adminInfo = $this->repository->get($id);
$tokenInfo = $this->repository->createToken($adminInfo);
$admin = $adminInfo->toArray();
unset($admin['pwd']);
$data = [
'token' => $tokenInfo['token'],
'exp' => $tokenInfo['out'],
'admin' => $admin,
'url' => '/' . config('admin.service_prefix')
];
return app('json')->success($data);
}
}