Admin.php 10 KB

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