Admin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\BaseService;
  6. use App\Services\JwtService;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Validator;
  11. use App\Models\Admin as AdminModel;
  12. use Exception;
  13. use Illuminate\Validation\ValidationException;
  14. use function Symfony\Component\HttpFoundation\Session\Storage\Handler\commit;
  15. use App\Services\AdminService;
  16. use Illuminate\Validation\Rule;
  17. /**
  18. * @apiDefine result
  19. * @apiSuccess (成功) {Number} code=0 错误代码 0-请求成功 详见 <a href="javascript:;" onclick="toMenu('Error','GetGeterrorcode')">错误代码</a>
  20. * @apiSuccess (成功) {Number} timestamp 服务器时间戳
  21. * @apiSuccess (成功) {String} msg 错误信息 OK为成功
  22. * @apiSuccess (成功) {Array} [data] 数据 若code!=0 则为错误数据,code=101009 该值为验证失败的详情
  23. *
  24. *
  25. */
  26. /**
  27. * @apiDefine header
  28. * @apiHeader {String} Authorization "Bearer "+ token
  29. *
  30. */
  31. /**
  32. * @api {get} /getErrorCode 错误代码
  33. * @apiGroup Error
  34. * @apiSampleRequest off
  35. * @apiDescription 下面列出一些常见的错误代码:
  36. * | code | 说明 |
  37. * |---------|-----------------------------------------------------------------------------------|
  38. * |-1 | 未知错误,联系开发人员 |
  39. * |0 | OK 请求成功 |
  40. * |101001 | 用户不存在 |
  41. * |101002 | 密码错误 |
  42. * |101003 | 验证码错误 |
  43. * |101004 | 验证码已过期 |
  44. * |101005 | 密码不一致 |
  45. * |101006 | 用户名已存在,请直接登录 |
  46. * |101007 | 邮箱已存在,请直接登录 |
  47. * |101008 | 用户名错误 |
  48. * |101009 | 参数验证失败,具体错误信息见 data |
  49. * |101010 | 系统错误 |
  50. * |101011 | 没有登录,请检查登录状态 |
  51. * |101012 | 禁止收藏自己 |
  52. * |101013 | 先填写基本信息 |
  53. * |101014 | 请求地址不存在,请检查请求地址是否正确 |
  54. * |101015 | 上传的头像必须是正方形的,如果用户所选的图片不是方形的,请裁剪后上传 |
  55. * |101016 | 没有匹配到合适的对象 |
  56. * |101017 | 可收藏数达到最大值,完善资料可获取更多数量 |
  57. * |101018 | 发送失败 |
  58. * |101019 | 手机号码不正确 |
  59. * |101020 | 帖子不存在 |
  60. * |101021 | 文件上传错误 |
  61. * |101022 | 邀请码错误 |
  62. * |101023 | 用户已在其他设备登录 |
  63. * |101024 | 剩余抽奖次数不足 |
  64. * |101025 | 地址数量最多10条 |
  65. * |101026 | post请求错误 |
  66. * |101027 | IM 错误 |
  67. * |101028 | 手机号已存在或已绑定其他账号,请直接登录或绑定其它手机号 |
  68. * |101029 | 谷歌登录错误 |
  69. * |101030 | 聊天余额不足 |
  70. * |101031 | 钱包余额不足 |
  71. * |101032 | Facebook 错误 |
  72. * |101033 | 资料验证失败,请检查当前是否是待验证状态 |
  73. *
  74. * @apiVersion 1.0.0
  75. */
  76. class Admin extends Controller
  77. {
  78. protected $jwtService;
  79. public function __construct(JwtService $jwtService)
  80. {
  81. $this->jwtService = $jwtService;
  82. }
  83. /**
  84. * @api {post} /admin/setPassword 修改密码
  85. * @apiGroup 管理员
  86. * @apiUse result
  87. * @apiUse header
  88. * @apiVersion 1.0.0
  89. *
  90. * @apiParam {string} oldPassword 旧密码
  91. * @apiParam {string} password 新密码
  92. * @apiParam {string} password_confirmation 确认密码
  93. *
  94. */
  95. public function setPassword()
  96. {
  97. DB::beginTransaction();
  98. try {
  99. request()->validate([
  100. 'oldPassword' => ['required', 'string', 'min:1'],
  101. 'password' => ['required', 'string', 'min:8', 'max:20', 'confirmed'],
  102. ]);
  103. $user = request()->user;
  104. $oldPassword = request()->input('oldPassword', '');
  105. $password = request()->input('password', '');
  106. if (!password_verify($oldPassword, $user->password)) {
  107. throw new Exception('', HttpStatus::PASSWORDS_ERROR);
  108. }
  109. $user->password = password_hash($password, PASSWORD_DEFAULT);
  110. $user->save();
  111. DB::commit();
  112. } catch (ValidationException $e) {
  113. DB::rollBack();
  114. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  115. } catch (Exception $e) {
  116. DB::rollBack();
  117. return $this->error(intval($e->getCode()));
  118. }
  119. return $this->success();
  120. }
  121. public function logout()
  122. {
  123. Auth::logout();
  124. session()->regenerateToken();
  125. return $this->success();
  126. }
  127. /**
  128. * @api {post} /admin/login 登录
  129. * @apiGroup 管理员
  130. * @apiUse result
  131. * @apiVersion 1.0.0
  132. * @apiParam {string} username
  133. * @apiParam {string} password
  134. *
  135. * @apiSuccess (成功) data
  136. * @apiSuccess (成功) data.token
  137. */
  138. function login()
  139. {
  140. try {
  141. $username = request()->input('username');
  142. $password = request()->input('password');
  143. $user = AdminModel::login($username, $password);
  144. $token = $this->jwtService->generateToken($user);
  145. Cache::put("user_{$user->getId()}_jwt", $token);
  146. } catch (Exception $e) {
  147. return $this->error(intval($e->getCode()));
  148. }
  149. $data = [
  150. 'token' => "Bearer $token",
  151. 'userInfo' => $user
  152. ];
  153. return $this->success($data);
  154. }
  155. function test()
  156. {
  157. return $this->success('ok');
  158. }
  159. /**
  160. * @api {get} /admin/index 人员列表
  161. * @apiGroup 管理员
  162. *
  163. * @apiUse result
  164. * @apiUse header
  165. * @apiVersion 1.0.0
  166. *
  167. * @apiParam {int} [page=1]
  168. * @apiParam {int} [limit=10]
  169. * @apiParam {string} [username] 账号
  170. * @apiParam {string} [nickname] 昵称
  171. *
  172. * @apiSuccess (data) {Object} data
  173. * @apiSuccess (data) {int} data.total 数量
  174. * @apiSuccess (data) {Object[]} data.data 列表
  175. * @apiSuccess (data) {int} data.data.id
  176. * @apiSuccess (data) {string} data.data.username 账号
  177. * @apiSuccess (data) {string} data.data.nickname 昵称
  178. * @apiSuccess (data) {array} data.data.roles_ids 账号的角色
  179. * @apiSuccess (data) {array} data.data.roles_names 账号的角色名称
  180. * @apiSuccess (data) {string} data.data.updated_at
  181. * @apiSuccess (data) {string} data.data.created_at
  182. */
  183. public function index()
  184. {
  185. // try {
  186. request()->validate([
  187. 'username' => ['nullable', 'string'],
  188. 'nickname' => ['nullable', 'string'],
  189. ]);
  190. $search = request()->all();
  191. $result = AdminService::paginate($search);
  192. // } catch (ValidationException $e) {
  193. // return $this->error(HttpStatus::VALIDATION_FAILED, '', $e->errors());
  194. // } catch (Exception $e) {
  195. // return $this->error(intval($e->getCode()));
  196. // }
  197. return $this->success($result);
  198. }
  199. /**
  200. * @api {post} /admin/submit 修改账号
  201. * @apiGroup 管理员
  202. *
  203. * @apiUse result
  204. * @apiUse header
  205. * @apiVersion 1.0.0
  206. *
  207. * @apiParam {int} id 角色ID
  208. * @apiParam {string} username 账号
  209. * @apiParam {string} nickname 昵称
  210. * @apiParam {string} password 密码
  211. * @apiParam {array} roles_ids 账号角色
  212. */
  213. public function store()
  214. {
  215. try {
  216. $id = request()->input('id');
  217. $validator = [
  218. 'username' => 'required|string|min:5|max:50|alpha_dash|unique:admin,username',
  219. 'nickname' => 'required|string|max:100',
  220. 'password' => ['nullable', 'string', 'min:6', 'max:20'],
  221. 'cellphone' => ['required', 'string'],
  222. 'email' => ['required', 'email'],
  223. 'remarks' => ['nullable', 'string'],
  224. 'roles_ids' => ['nullable', 'array'],
  225. 'id' => ['nullable', 'integer', 'min:1'],
  226. ];
  227. if (!empty($id)) {
  228. if ($id == 1) {
  229. unset($validator['username'], $validator['password'],$validator['roles_ids']);
  230. } else {
  231. $validator['roles_ids'] = ['required', 'array', 'min:1'];
  232. $validator['roles_ids.*'] = ['required', 'integer', 'min:1'];
  233. $validator['username'] = ['required', 'string', 'max:50', 'alpha_dash',
  234. Rule::unique('admin', 'username')->ignore($id), // 忽略当前 ID
  235. ];
  236. }
  237. }
  238. $params = request()->validate($validator);
  239. if ($id == 1) $params['roles_ids'] = [];
  240. $ret = AdminService::submit($params);
  241. if ($ret['code'] == BaseService::NOT) {
  242. throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  243. }
  244. } catch (ValidationException $e) {
  245. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  246. } catch (Exception $e) {
  247. return $this->error($e->getCode(), $e->getMessage());
  248. }
  249. return $this->success();
  250. }
  251. /**
  252. * @api {post} /admin/delete 删除账号
  253. * @apiGroup 管理员
  254. *
  255. * @apiUse result
  256. * @apiUse header
  257. * @apiVersion 1.0.0
  258. *
  259. * @apiParam {int} id 角色ID
  260. */
  261. public function destroy()
  262. {
  263. $id = request()->post('id');
  264. if ($id == 1) {
  265. return $this->error(HttpStatus::CUSTOM_ERROR, '超级管理员禁止操作');
  266. }
  267. // 示例:通过 ID 删除菜单
  268. $info = AdminService::findOne(['id' => $id]);
  269. if (!$info) {
  270. return $this->error(0, '账号不存在');
  271. }
  272. $info->delete();
  273. return $this->success([], '删除成功');
  274. }
  275. }