123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- declare (strict_types=1);
- namespace app\api\controller;
- use app\api\BaseController;
- use app\api\exception\ApiException;
- use app\api\model\AdminModel;
- use app\api\model\GroupModel;
- use app\api\model\StoreModel;
- use app\common\until\Until;
- use think\captcha\Captcha;
- use think\Db;
- use think\Exception;
- use think\Request;
- class Admin extends BaseController {
- /**
- * @OA\GET(path="/api/Admin/index",
- * tags={"管理员管理"},
- * summary="管理员列表",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="ineger",default="1")),
- * @OA\Parameter(name="pageSize", in="query", description="页尺寸", @OA\Schema(type="integer",default="10")),
- * @OA\Parameter(name="status", in="query", description="状态 1正常 2删除", @OA\Schema(type="integer",default="1")),
- * @OA\Parameter(name="name", in="query", description="名称", @OA\Schema(type="string")),
- * @OA\Parameter(name="mobile", in="query", description="手机号", @OA\Schema(type="string")),
- * @OA\Parameter(name="roleId", in="query", description="角色id", @OA\Schema(type="ineger")),
- * @OA\RequestBody(
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function index() {
- $input = request()->get();
- $model = new AdminModel();
- $model->setPage($input['page'] ?? 1);
- $model->setPageSize($input['pageSize'] ?? 10);
- $where = [];
- if (!empty($input['status'])) {
- $where[] = ['a.status', '=', $model::NORMAL];
- }
- if (!empty($input['name'])) {
- $where[] = ['a.name', 'like', "%{$input['name']}%"];
- }
- if (!empty($input['mobile'])) {
- $where[] = ['a.mobile', 'like', "%{$input['mobile']}%"];
- }
- if (!empty($input['roleId'])) {
- $where[] = ['a.role_id', '=', "{$input['roleId']}"];
- }
- $model->setWhere($where);
- $data = $model->getAdminList();
- Until::output($data);
- }
- /**
- * @OA\Post(path="/api/Admin/save",
- * tags={"管理员管理"},
- * summary="保存管理员信息",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\RequestBody(
- * @OA\MediaType(
- * mediaType="multipart/form-data",
- * @OA\Schema(
- * @OA\Property(description="管理员名称", property="name", type="string", default="jack"),
- * @OA\Property(description="登入账号", property="account", type="string", default="admin01"),
- * @OA\Property(description="登入密码", property="password", type="string", default="123465"),
- * @OA\Property(description="手机号", property="mobile", type="string", default="12367897654"),
- * @OA\Property(description="角色id-单选", property="roleId", type="integer", default="1"),
- * @OA\Property(description="集团id-多选", property="groupIds", type="string", default="1,2"),
- * @OA\Property(description="门店id-多选", property="storeIds", type="string", default="1,2"),
- * @OA\Property(description="mac地址", property="macAdress", type="string", default="1,2"),
- * @OA\Property(description="管理员id", property="id", type="string", default="0"),
- * required={"name","account","mobile","roleId","groupIds","storeIds"})
- * )
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function save() {
- $input = Until::getInput();
- $rule = [
- 'name|管理员名称' => 'require',
- 'account|登入账号' => 'require',
- 'mobile|手机号' => 'require',
- 'roleId|角色id' => 'require',
- 'storeIds|门店id' => 'require',
- 'groupIds|集团id' => 'require',
- ];
- Until::check($rule, $input);
- $model = new AdminModel();
- if (!empty($input['id'])) {
- $id = (int)$input['id'];
- try {
- $model->startTrans();
- $model::where(['id' => $id])->update([
- 'name' => $input['name'],
- 'account' => $input['account'],
- 'role_id' => $input['roleId'],
- 'mobile' => $input['mobile'],
- 'status' => $input['status'] ?? 1,
- ]);
- $model->saveStoreRole($input['storeIds'], $id, true);
- $model->saveGroupRole($input['groupIds'], $id,true);
- }catch (\Exception $e){
- $model->rollback();
- throw new ApiException($e->getMessage());
- }
- } else {
- if (empty($input['password'])) {
- throw new ApiException('密码不为空');
- }
- try {
- $model->startTrans();
- $id = $model->insertGetId([
- 'name' => $input['name'],
- 'account' => $input['account'],
- 'role_id' => $input['roleId'],
- 'password' => md5($input['password'] . '-Bjx14Nb3Le9ghOmM'),
- 'mobile' => $input['mobile'],
- 'status' => $input['status'] ?? 1,
- 'mac_address' => $input['macAddress'] ?? ''
- ]);
- $model->saveStoreRole($input['storeIds'], (int)$id);
- $model->saveGroupRole($input['groupIds'], (int)$id);
- $model->commit();
- } catch (Exception $e) {
- $model->rollback();
- throw new ApiException($e->getMessage());
- }
- }
- $where[] = ['a.id', '=', (int)$id];
- $model->setWhere($where);
- $info = $model->getAdminInfo();
- Until::output(['info' => $info]);
- }
- /**
- * @OA\Post(path="/api/Admin/login",
- * tags={"管理员管理"},
- * summary="管理员登入",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\RequestBody(
- * @OA\MediaType(
- * mediaType="multipart/form-data",
- * @OA\Schema(
- * @OA\Property(description="登入账号", property="account", type="string", default="admin"),
- * @OA\Property(description="登入密码", property="password", type="string", default="123456"),
- * @OA\Property(description="验证码", property="code", type="string", default="akjs"),
- * required={"account","password","code"})
- * )
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function login() {
- $input = Until::getInput();
- $rule = [
- 'account|用户名' => 'require',
- 'password|内容' => 'require',
- 'code|验证码' => 'require'
- ];
- Until::check($rule, $input);
- if( !captcha_check($input['code'] )) {
- // 验证失败
- throw new ApiException('验证码错误');
- }
- $model = (new AdminModel());
- $where[] = ['a.account', '=', $input['account']];
- $where[] = ['a.password', '=', $input['password']];
- $model->setWhere($where);
- $info = $model->getAdminInfo();
- if (empty($info)) {
- throw new ApiException('账号或密码错误');
- }
- $tokenService = new \app\common\until\Token();
- $token = $tokenService->getToken($info['id'],'',true);
- Until::output(['token' => $token, 'info' => $info]);
- }
- public function logout() {
- Until::output(['name' => 'tom']);
- }
- /**
- * @OA\GET(path="/api/Admin/read",
- * tags={"管理员管理"},
- * summary="查看管理员个人信息",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\Parameter(name="id", in="query", description="管理员id", @OA\Schema(type="ineger")),
- * @OA\RequestBody(
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function read($id) {
- $model = new AdminModel();
- $where[] = ['a.id', '=', (int)$id];
- $model->setWhere($where);
- $info = $model->getAdminInfo();
- $storeList = $model->getStoreList();
- // var_dump($storeList);
- $info['storeList'] = $storeList;
- Until::output(['info' => $info]);
- }
- /**
- * 保存更新的资源
- *
- * @param \think\Request $request
- * @param int $id
- * @return \think\Response
- */
- public function update(Request $request, $id) {
- //
- }
- /**
- * @OA\GET(path="/api/Admin/delete",
- * tags={"管理员管理"},
- * summary="删除管理员信息",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\Parameter(name="id", in="query", description="管理员id", @OA\Schema(type="ineger",default="1")),
- * @OA\Parameter(name="status", in="query", description="1正常 2删除", @OA\Schema(type="ineger",default="1")),
- * @OA\RequestBody(
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function delete($id,$status) {
- $model = new AdminModel();
- $where[] = ['id', '=', (int)$id];
- $data = ['status' => (int)$status];
- $isSuccess = $model::where($where)->update($data);
- Until::output(['isSuccess' => $isSuccess]);
- }
- /**
- * @OA\GET(path="/api/Admin/verifyImg",
- * tags={"管理员管理"},
- * summary="生成验证码",
- * @OA\RequestBody(
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function verifyImg() {
- $captcha = new Captcha();
- return $captcha->entry();
- }
- /**
- * @OA\GET(path="/api/Admin/menu",
- * tags={"管理员管理"},
- * summary="菜单权限",
- * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
- * @OA\RequestBody(
- * ),
- * @OA\Response(response="200", description="请求成功")
- * )
- */
- public function menu() {
- $model = new AdminModel();
- $info = $model->getMenuList($this->adminId);
- Until::output(['info' => $info]);
- }
- }
|