Admin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\api\controller;
  4. use app\api\BaseController;
  5. use app\api\exception\ApiException;
  6. use app\api\model\AdminModel;
  7. use app\api\model\GroupModel;
  8. use app\api\model\StoreModel;
  9. use app\common\until\Until;
  10. use think\captcha\Captcha;
  11. use think\Db;
  12. use think\Exception;
  13. use think\facade\Cache;
  14. use think\Request;
  15. class Admin extends BaseController {
  16. /**
  17. * @OA\GET(path="/api/Admin/index",
  18. * tags={"管理员管理"},
  19. * summary="管理员列表",
  20. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  21. * @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="ineger",default="1")),
  22. * @OA\Parameter(name="pageSize", in="query", description="页尺寸", @OA\Schema(type="integer",default="10")),
  23. * @OA\Parameter(name="status", in="query", description="状态 1正常 2删除", @OA\Schema(type="integer",default="1")),
  24. * @OA\Parameter(name="name", in="query", description="名称", @OA\Schema(type="string")),
  25. * @OA\Parameter(name="mobile", in="query", description="手机号", @OA\Schema(type="string")),
  26. * @OA\Parameter(name="roleId", in="query", description="角色id", @OA\Schema(type="ineger")),
  27. * @OA\RequestBody(
  28. * ),
  29. * @OA\Response(response="200", description="请求成功")
  30. * )
  31. */
  32. public function index() {
  33. $input = request()->get();
  34. $model = new AdminModel();
  35. $model->setPage($input['page'] ?? 1);
  36. $model->setPageSize($input['pageSize'] ?? 10);
  37. $where = [];
  38. if (!empty($input['status'])) {
  39. $where[] = ['a.status', '=', $model::NORMAL];
  40. }
  41. if (!empty($input['name'])) {
  42. $where[] = ['a.name', 'like', "%{$input['name']}%"];
  43. }
  44. if (!empty($input['mobile'])) {
  45. $where[] = ['a.mobile', 'like', "%{$input['mobile']}%"];
  46. }
  47. if (!empty($input['roleId'])) {
  48. $where[] = ['a.role_id', '=', "{$input['roleId']}"];
  49. }
  50. $model->setWhere($where);
  51. $data = $model->getAdminList();
  52. Until::output($data);
  53. }
  54. /**
  55. * @OA\Post(path="/api/Admin/save",
  56. * tags={"管理员管理"},
  57. * summary="保存管理员信息",
  58. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  59. * @OA\RequestBody(
  60. * @OA\MediaType(
  61. * mediaType="multipart/form-data",
  62. * @OA\Schema(
  63. * @OA\Property(description="管理员名称", property="name", type="string", default="jack"),
  64. * @OA\Property(description="登入账号", property="account", type="string", default="admin01"),
  65. * @OA\Property(description="登入密码", property="password", type="string", default="123465"),
  66. * @OA\Property(description="手机号", property="mobile", type="string", default="12367897654"),
  67. * @OA\Property(description="角色id-单选", property="roleId", type="integer", default="1"),
  68. * @OA\Property(description="集团id-多选", property="groupIds", type="string", default="1,2"),
  69. * @OA\Property(description="门店id-多选", property="storeIds", type="string", default="1,2"),
  70. * @OA\Property(description="mac地址", property="macAdress", type="string", default="1,2"),
  71. * @OA\Property(description="管理员id", property="id", type="string", default="0"),
  72. * required={"name","account","mobile","roleId","groupIds","storeIds"})
  73. * )
  74. * ),
  75. * @OA\Response(response="200", description="请求成功")
  76. * )
  77. */
  78. public function save() {
  79. $input = Until::getInput();
  80. $rule = [
  81. 'name|管理员名称' => 'require',
  82. 'account|登入账号' => 'require',
  83. 'mobile|手机号' => 'require',
  84. 'roleId|角色id' => 'require',
  85. 'storeIds|门店id' => 'require',
  86. 'groupIds|集团id' => 'require',
  87. ];
  88. Until::check($rule, $input);
  89. $model = new AdminModel();
  90. if (!empty($input['id'])) {
  91. $id = (int)$input['id'];
  92. try {
  93. $model->startTrans();
  94. $model::where(['id' => $id])->update([
  95. 'name' => $input['name'],
  96. 'account' => $input['account'],
  97. 'role_id' => $input['roleId'],
  98. 'mobile' => $input['mobile'],
  99. 'status' => $input['status'] ?? 1,
  100. ]);
  101. $model->saveStoreRole($input['storeIds'], $id, true);
  102. $model->saveGroupRole($input['groupIds'], $id,true);
  103. }catch (\Exception $e){
  104. $model->rollback();
  105. throw new ApiException($e->getMessage());
  106. }
  107. } else {
  108. if (empty($input['password'])) {
  109. throw new ApiException('密码不为空');
  110. }
  111. try {
  112. $model->startTrans();
  113. $id = $model->insertGetId([
  114. 'name' => $input['name'],
  115. 'account' => $input['account'],
  116. 'role_id' => $input['roleId'],
  117. 'password' => md5($input['password'] . '-Bjx14Nb3Le9ghOmM'),
  118. 'mobile' => $input['mobile'],
  119. 'status' => $input['status'] ?? 1,
  120. 'mac_address' => $input['macAddress'] ?? ''
  121. ]);
  122. $model->saveStoreRole($input['storeIds'], (int)$id);
  123. $model->saveGroupRole($input['groupIds'], (int)$id);
  124. $model->commit();
  125. } catch (Exception $e) {
  126. $model->rollback();
  127. throw new ApiException($e->getMessage());
  128. }
  129. }
  130. $where[] = ['a.id', '=', (int)$id];
  131. $model->setWhere($where);
  132. $info = $model->getAdminInfo();
  133. Until::output(['info' => $info]);
  134. }
  135. /**
  136. * @OA\Post(path="/api/Admin/login",
  137. * tags={"管理员管理"},
  138. * summary="管理员登入",
  139. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  140. * @OA\RequestBody(
  141. * @OA\MediaType(
  142. * mediaType="multipart/form-data",
  143. * @OA\Schema(
  144. * @OA\Property(description="登入账号", property="account", type="string", default="admin"),
  145. * @OA\Property(description="登入密码", property="password", type="string", default="123456"),
  146. * @OA\Property(description="验证码", property="code", type="string", default="akjs"),
  147. * @OA\Property(description="生成验证码时的唯一码", property="uniqueKey", type="string", default="sdafsdfsd"),
  148. * required={"account","password","code"})
  149. * )
  150. * ),
  151. * @OA\Response(response="200", description="请求成功")
  152. * )
  153. */
  154. public function login() {
  155. $input = Until::getInput();
  156. $rule = [
  157. 'account|用户名' => 'require',
  158. 'password|内容' => 'require',
  159. 'code|验证码' => 'require',
  160. 'uniqueKey|唯一码' => 'require'
  161. ];
  162. Until::check($rule, $input);
  163. if( strtolower($input['code']) != strtolower(Cache::get($input['uniqueKey']))) {
  164. // 验证失败
  165. throw new ApiException('验证码错误');
  166. }
  167. Cache::rm('uniqueKey');
  168. $model = (new AdminModel());
  169. $where[] = ['a.account', '=', $input['account']];
  170. $where[] = ['a.password', '=', $input['password']];
  171. $model->setWhere($where);
  172. $info = $model->getAdminInfo();
  173. if (empty($info)) {
  174. throw new ApiException('账号或密码错误');
  175. }
  176. $tokenService = new \app\common\until\Token();
  177. $token = $tokenService->getToken($info['id'],'',true);
  178. Until::output(['token' => $token, 'info' => $info]);
  179. }
  180. public function logout() {
  181. Until::output(['name' => 'tom']);
  182. }
  183. /**
  184. * @OA\GET(path="/api/Admin/read",
  185. * tags={"管理员管理"},
  186. * summary="查看管理员个人信息",
  187. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  188. * @OA\Parameter(name="id", in="query", description="管理员id", @OA\Schema(type="ineger")),
  189. * @OA\RequestBody(
  190. * ),
  191. * @OA\Response(response="200", description="请求成功")
  192. * )
  193. */
  194. public function read($id) {
  195. $model = new AdminModel();
  196. $where[] = ['a.id', '=', (int)$id];
  197. $model->setWhere($where);
  198. $info = $model->getAdminInfo();
  199. $storeList = $model->getStoreList();
  200. // var_dump($storeList);
  201. $info['storeList'] = $storeList;
  202. Until::output(['info' => $info]);
  203. }
  204. /**
  205. * 保存更新的资源
  206. *
  207. * @param \think\Request $request
  208. * @param int $id
  209. * @return \think\Response
  210. */
  211. public function update(Request $request, $id) {
  212. //
  213. }
  214. /**
  215. * @OA\GET(path="/api/Admin/delete",
  216. * tags={"管理员管理"},
  217. * summary="删除管理员信息",
  218. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  219. * @OA\Parameter(name="id", in="query", description="管理员id", @OA\Schema(type="ineger",default="1")),
  220. * @OA\Parameter(name="status", in="query", description="1正常 2删除", @OA\Schema(type="ineger",default="1")),
  221. * @OA\RequestBody(
  222. * ),
  223. * @OA\Response(response="200", description="请求成功")
  224. * )
  225. */
  226. public function delete($id,$status) {
  227. $model = new AdminModel();
  228. $where[] = ['id', '=', (int)$id];
  229. $data = ['status' => (int)$status];
  230. $isSuccess = $model::where($where)->update($data);
  231. Until::output(['isSuccess' => $isSuccess]);
  232. }
  233. /**
  234. * @OA\GET(path="/api/Admin/verifyImg",
  235. * tags={"管理员管理"},
  236. * summary="生成验证码",
  237. * @OA\Parameter(name="uniqueKey", in="query", description="唯一id", @OA\Schema(type="string",default="1121212")),
  238. * @OA\RequestBody(
  239. * ),
  240. * @OA\Response(response="200", description="请求成功")
  241. * )
  242. */
  243. public function verifyImg($uniqueKey) {
  244. $captcha = new Captcha();
  245. return $captcha->entry('',$uniqueKey);
  246. }
  247. /**
  248. * @OA\GET(path="/api/Admin/menu",
  249. * tags={"管理员管理"},
  250. * summary="菜单权限",
  251. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  252. * @OA\RequestBody(
  253. * ),
  254. * @OA\Response(response="200", description="请求成功")
  255. * )
  256. */
  257. public function menu() {
  258. $model = new AdminModel();
  259. $info = $model->getMenuList($this->adminId);
  260. Until::output(['info' => $info]);
  261. }
  262. }