Admin.php 9.9 KB

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