Admin.php 9.5 KB

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