Admin.php 11 KB

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