Admin.php 11 KB

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