AuthController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Http\Controllers\Api\Base;
  11. use App\Http\Controllers\Controller;
  12. use App\Repositories\Enums\ModelStatusEnum;
  13. use App\Repositories\Enums\Navigation\TypeEnum;
  14. use App\Repositories\Enums\ResponseCodeEnum;
  15. use App\Repositories\Models\Base\Admin;
  16. use App\Repositories\Models\CMS\Setting;
  17. use App\Repositories\Models\Navigation\Category;
  18. use App\Repositories\Models\User\User;
  19. use App\Repositories\Transformers\User\UserTransformer;
  20. use Carbon\Carbon;
  21. use Illuminate\Http\Request;
  22. use Illuminate\Support\Facades\Auth;
  23. use Illuminate\Support\Facades\Cache;
  24. use Illuminate\Support\Facades\Hash;
  25. use Illuminate\Support\Facades\Log;
  26. use Illuminate\Support\Str;
  27. use Jiannei\Response\Laravel\Support\Facades\Response;
  28. use Spatie\Permission\Models\Role;
  29. class AuthController extends Controller
  30. {
  31. /**
  32. * Create a new controller instance.
  33. */
  34. public function __construct()
  35. {
  36. // $this->middleware('throttle:10,1', ['only' => ['login', 'me']]);
  37. }
  38. /**
  39. * 账号密码登录
  40. * Author: Mead
  41. */
  42. public function login(Request $request)
  43. {
  44. $this->validate($request, [
  45. 'username' => 'required',
  46. 'password' => 'required|min:6'
  47. ]);
  48. $credentials = request(['username', 'password']);
  49. $credentials['status'] = ModelStatusEnum::OK;
  50. if (!$token = auth()->guard('api')->attempt($credentials)) {
  51. return Response::fail(T('The account or password is incorrect.'), ResponseCodeEnum::SERVICE_LOGIN_ERROR);
  52. }
  53. $user = auth('api')->user();
  54. //判断个人是否有分类
  55. if (!Category::query()->where('user_id', $user->id)->status()->where('pid', 0)->exists()) {
  56. Category::query()->create([
  57. 'name' => '个人网站',
  58. 'pid' => 0,
  59. 'type' => TypeEnum::PERSON,
  60. 'slug' => Str::random(6),
  61. 'user_id' => $user->id,
  62. 'tier' => 0,
  63. 'logo' => 'fa-user-circle'
  64. ]);
  65. }
  66. $user = $user->only(['id', 'username', 'name', 'sex', 'role_id', 'class', 'mobile', 'organization_id']);
  67. return Response::success(compact('token', 'user'));
  68. }
  69. /**
  70. * 退出
  71. * @return mixed
  72. * Author: Mead
  73. */
  74. public function logout()
  75. {
  76. auth('api')->logout();
  77. return Response::noContent();
  78. }
  79. /**
  80. * 注册
  81. * Author: Mead
  82. */
  83. public function store(Request $request)
  84. {
  85. $this->validate($request, [
  86. 'name' => 'required',
  87. // 'turename' => 'required',
  88. 'mobile' => 'required',
  89. 'username' => 'required|min:8|unique:base_users,username',
  90. 'password' => 'required|min:6'
  91. ]);
  92. $data = $request->only(['turename', 'mobile', 'username', 'password']);
  93. $data['password'] = Hash::make($data['password']);
  94. User::query()->create($data);
  95. return Response::success(null);
  96. }
  97. /**
  98. * 用户
  99. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  100. * Author: Mead
  101. */
  102. public function me()
  103. {
  104. $me = (new UserTransformer())->transform(User::query()->find(login_user_id()));
  105. return Response::success($me);
  106. // return Response::success(1);
  107. }
  108. /**
  109. * 修改个人信息
  110. * @param Request $request
  111. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\Resource
  112. * @throws \Illuminate\Validation\ValidationException
  113. * Author: Mead
  114. */
  115. public function update(Request $request)
  116. {
  117. $this->validate($request, [
  118. 'name' => 'required',
  119. 'headimg' => 'sometimes',
  120. 'sex' => 'nullable',
  121. 'email' => 'nullable|email',
  122. 'class' => 'nullable',
  123. 'mobile' => 'nullable|mobile',
  124. 'intro' => 'nullable',
  125. 'personal_signature' => 'nullable',
  126. 'userrate' => 'nullable',
  127. ]);
  128. try {
  129. $data = $request->only(['turename', 'headimg', 'email', 'mobile', 'class', 'intro', 'personal_signature', 'userrate', 'name', 'sex']);
  130. $re = User::query()->where('id', login_user_id())->update($data);
  131. if ($re) {
  132. return Response::success(null);
  133. }
  134. return $this->errorFail();
  135. } catch (\Exception $e) {
  136. $this->errorStore($e);
  137. }
  138. }
  139. /**
  140. * 修改密码
  141. * @param Request $request
  142. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\Resource
  143. * @throws \Illuminate\Validation\ValidationException
  144. * Author: Mead
  145. */
  146. public function changePassword(Request $request)
  147. {
  148. $this->validate($request, [
  149. 'password' => 'required|min:6|confirmed',
  150. ]);
  151. $password = $request->get('password');
  152. $data['password'] = Hash::make($password);
  153. $re = User::query()->where('id', login_user_id())->update($data);
  154. if ($re) {
  155. return Response::success(null);
  156. }
  157. return $this->errorFail();
  158. }
  159. /**
  160. * 手机验证码
  161. * @param Request $request
  162. * Author: Mead
  163. */
  164. public function verificationCode(Request $request)
  165. {
  166. $this->validate($request, [
  167. 'mobile' => 'required|mobile',
  168. ]);
  169. $mobile = $request->get('mobile');
  170. $code = rand(1000, 9999);
  171. // try {
  172. // app('easy_sms')->send($mobile, [
  173. // 'template' => config('sms.template.verification_code'),
  174. // 'data' => [
  175. // 'code' => $code
  176. // ]
  177. // ]);
  178. //
  179. // } catch (\Exception $exception) {
  180. // Log::error($exception);
  181. // return Response::fail('发送失败');
  182. // }
  183. Cache::put("verification_code_{$mobile}", $code, Carbon::now()->addMinutes(5));
  184. return Response::success($code);
  185. }
  186. /**
  187. * 手机号登录
  188. * @param Request $request
  189. * Author: Mead
  190. */
  191. public function mobileLogin(Request $request)
  192. {
  193. $this->validate($request, [
  194. 'mobile' => 'required|mobile',
  195. 'code' => 'required|size:4',
  196. ]);
  197. $mobile = $request->get('mobile');
  198. $code = $request->get('code');
  199. // $cache_code = Cache::get("verification_code_{$mobile}", false);
  200. //
  201. // if ((int)$cache_code !== (int)$code) {
  202. // return Response::fail('验证码错误');
  203. // }
  204. $user = User::query()->where('mobile', $mobile)->with('organization')->select(['id', 'name', 'mobile', 'headimg', 'sex', 'organization_id', 'status'])->where('status', ModelStatusEnum::OK)->orderByDesc('id')->first();
  205. if (!$user) {
  206. return Response::fail('找不到该用户');
  207. // $user = User::query()->create([
  208. // 'name' => '圆圈导航-' . rand(99999, 10000),
  209. // 'mobile' => $mobile,
  210. // ]);
  211. }
  212. //判断个人是否有分类
  213. if (!Category::query()->where('user_id', $user->id)->status()->where('pid', 0)->exists()) {
  214. Category::query()->create([
  215. 'name' => '个人网站',
  216. 'pid' => 0,
  217. 'type' => TypeEnum::PERSON,
  218. 'slug' => Str::random(6),
  219. 'user_id' => $user->id,
  220. 'tier' => 0,
  221. 'logo' => 'fa-user-circle'
  222. ]);
  223. }
  224. $token = auth('api')->login($user);
  225. return Response::success(compact('token', 'user'));
  226. }
  227. /**
  228. * 小程序登录
  229. * Author: Mead
  230. */
  231. public function weappLogin(Request $request)
  232. {
  233. $this->validate($request, [
  234. 'appid' => 'required',
  235. 'code' => 'required',
  236. ]);
  237. $code = $request->code;
  238. $appid = $request->appid;
  239. if ($appid !== config('wechat.mini_program.default.app_id')) {
  240. return $this->error('app_id is error');
  241. }
  242. $response = [];
  243. try {
  244. // $miniProgram = Factory::miniProgram(config('wechat'));
  245. $miniProgram = app('wechat.mini_program');
  246. $data = $miniProgram->auth->session($code);
  247. } catch (\Exception $exception) {
  248. return $this->error('code 不正确,请刷新重试');
  249. }
  250. if (isset($data['errcode'])) {
  251. return $this->error('code 不正确,请刷新重试');
  252. }
  253. $user = User::query()->where('credential', $data['openid'])->first();
  254. //response参数
  255. $response['session_key'] = $data['session_key'];
  256. $is_new_user = false;
  257. if (!$user) {
  258. //注册用户
  259. $userInfo['name'] = config('site.userName', '用户') . '-' . rand(10000, 99999);
  260. $userInfo['credential'] = $data['openid'];
  261. $user = User::query()->create($userInfo);
  262. $is_new_user = true;
  263. }
  264. $token = auth('api')->login($user);
  265. $response['token'] = 'Bearer ' . $token;
  266. $response['exp'] = Carbon::now()->getTimestamp();
  267. $response['user'] = $user;
  268. $response['is_new_user'] = $is_new_user;
  269. return Response::success($response);
  270. }
  271. /**
  272. * 更新用户基本信息
  273. * User: Mead
  274. */
  275. public function weappUserInfoSync(Request $request)
  276. {
  277. $this->validate($request, [
  278. 'nickName' => 'required',
  279. 'gender' => 'required',
  280. ]);
  281. try {
  282. $user = auth('api')->user();
  283. User::query()->where('id', $user->id)->update([
  284. 'name' => $request->get('nickName', $user->nickname),
  285. 'sex' => $request->get('gender', 0),
  286. // 'country' => $request->get('country', 'China'),
  287. // 'province' => $request->get('province', 'Henan'),
  288. // 'city' => $request->get('city', 'Zhengzhou'),
  289. 'headimg' => $request->get('avatarUrl', config('filesystems.disks.qiniu.url') . '/logo.png') ?? config('filesystems.disks.qiniu.url') . "/logo.png",
  290. // 'language' => $request->get('language', 'zh_CN'),
  291. 'is_sync_info' => 1
  292. ]);
  293. $me = (new UserTransformer())->transform(User::query()->where('id', $user->id)->first());
  294. return Response::success($me);
  295. } catch (\Exception $exception) {
  296. return $this->error($exception);
  297. }
  298. }
  299. /**
  300. * 微信小程序绑定用户手机号
  301. * @param Request $request
  302. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\Resource|void
  303. * Author: Mead
  304. */
  305. public function weappBindMobile(Request $request)
  306. {
  307. $this->validate($request, [
  308. 'session_key' => 'required',
  309. 'iv' => 'required',
  310. 'encryptedData' => 'required',
  311. ]);
  312. try {
  313. //微信解析手机号
  314. $session = $request->get('session_key');
  315. $iv = $request->get('iv');
  316. $encryptedData = $request->get('encryptedData');
  317. if (!$iv) {
  318. return $this->error('授权失败');
  319. }
  320. $miniProgram = app('wechat.mini_program');
  321. $decryptedData = $miniProgram->encryptor->decryptData($session, $iv, $encryptedData);
  322. $mobile = $decryptedData['purePhoneNumber'];
  323. User::query()->where('id', login_user_id())->update([
  324. 'mobile' => $mobile,
  325. 'is_bind_mobile' => 1
  326. ]);
  327. return Response::success(null);
  328. } catch (\Exception $exception) {
  329. return $this->exception($exception);
  330. }
  331. }
  332. }