Admin.php 8.9 KB

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