UserController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/8/19
  6. * Time: 2:11 PM
  7. */
  8. namespace App\Http\Controllers\V1;
  9. use App\Events\CardCertifiedEvent;
  10. use App\Handlers\IdCardHandler;
  11. use App\Handlers\ImageUploadHandler;
  12. use App\Http\Requests\BindMobileRequest;
  13. use App\Http\Requests\InviteNewUsersRewardRequest;
  14. use App\Http\Requests\MobileCodeRequest;
  15. use App\Http\Requests\RealNameRequest;
  16. use App\Http\Requests\UserFormIdRequest;
  17. use App\Http\Requests\UserStudentAuthRequest;
  18. use App\Http\Requests\WechatEncryptorMobileRequest;
  19. use App\Jobs\SendSMSCodeJob;
  20. use App\Models\CardRidingUserBags;
  21. use App\Models\CouponsUserBag;
  22. use App\Models\FormId;
  23. use App\Models\InviteNewUsersConfig;
  24. use App\Models\Student;
  25. use App\Models\User;
  26. use App\Repositories\CouponRepository;
  27. use App\Repositories\InviteNewUserRepository;
  28. use App\Repositories\InviteNewUsersConfigRepository;
  29. use App\Repositories\InviteNewUsersGiveGiftLogRepository;
  30. use App\Repositories\UserRepository;
  31. use App\Repositories\WalletLogRepository;
  32. use App\Transformers\RegisterUsersTransformer;
  33. use App\Transformers\UserStatusTransformer;
  34. use App\Transformers\WalletLogTransformer;
  35. use Carbon\Carbon;
  36. use EasyWeChat\Factory;
  37. use Illuminate\Http\Request;
  38. use Illuminate\Support\Facades\Cache;
  39. use Illuminate\Support\Facades\DB;
  40. use Illuminate\Support\Facades\Log;
  41. use Intervention\Image\ImageManagerStatic as Image;
  42. use Overtrue\EasySms\Exceptions\Exception;
  43. /**
  44. * 用户模块
  45. * Class UserController
  46. * @package App\Http\Controllers\V1
  47. */
  48. class UserController extends BaseController
  49. {
  50. /**
  51. * 实名认证
  52. * @param RealNameRequest $request
  53. * @return mixed
  54. * User: Mead
  55. */
  56. public function realNameAuthentication(RealNameRequest $request)
  57. {
  58. try {
  59. $card_id = $request->get('card_id');
  60. $name = $request->get('name');
  61. $age = idCard2age($card_id);
  62. $is_match_ride_age = ($age >= 16);
  63. User::where('id', $this->user->id)->update([
  64. 'card_id' => $card_id,
  65. 'truename' => $name,
  66. 'is_card_certified' => User::CARD_NO,
  67. 'is_match_ride_age' => $is_match_ride_age
  68. ]);
  69. if (!$is_match_ride_age) {
  70. return $this->errorNoValidation('不满16周岁禁止骑车!');
  71. }
  72. $status = IdCardHandler::main($card_id, $name);
  73. if ($status['code']) {
  74. return $this->errorNoValidation($status['msg']);
  75. }
  76. $user = User::where('id', $this->user->id)->update([
  77. 'is_card_certified' => User::CARD_OK
  78. ]);
  79. event(new CardCertifiedEvent($this->user()));
  80. } catch (\Exception $exception) {
  81. return $this->exception($exception);
  82. }
  83. return $this->response->array([
  84. 'is_card_certified' => !!$user
  85. ]);
  86. }
  87. public function bindWechatMobile(WechatEncryptorMobileRequest $request)
  88. {
  89. try {
  90. $session = $request->get('session_key');
  91. $iv = $request->get('iv');
  92. $encryptedData = $request->get('encryptedData');
  93. $miniProgram = Factory::miniProgram(wechat_mini_config(self::$MERCHANT));
  94. $decryptedData = $miniProgram->encryptor->decryptData($session, $iv, $encryptedData);
  95. $mobile = $decryptedData['purePhoneNumber'];
  96. $user = User::where('id', $this->user->id)->update([
  97. 'mobile' => $mobile,
  98. 'is_bind_mobile' => User::BIND_MOBILE_OK
  99. ]);
  100. return $this->response->array([
  101. 'is_bind_mobile' => !!$user
  102. ]);
  103. } catch (\Exception $exception) {
  104. return $this->exception($exception);
  105. }
  106. }
  107. /**
  108. * 绑定手机号
  109. * @param BindMobileRequest $request
  110. * User: Mead
  111. */
  112. public function bindMobile(BindMobileRequest $request)
  113. {
  114. try {
  115. $mobile = $request->get('mobile');
  116. $code = $request->get('code', false);
  117. $v_code = Cache::get("verification_code_{$mobile}", '');
  118. if (empty($v_code)) {
  119. return $this->errorNoValidation('验证码已过期');
  120. }
  121. if ((string)$v_code !== (string)$code) {
  122. return $this->errorNoValidation('验证码错误');
  123. }
  124. if (User::where('mobile', $mobile)->where('merchant_id', self::$MERCHANT_ID)->count()) {
  125. return $this->errorNoValidation('手机号已存在');
  126. }
  127. $user = User::where('id', $this->user->id)->update([
  128. 'mobile' => $mobile,
  129. 'is_bind_mobile' => User::BIND_MOBILE_OK
  130. ]);
  131. return $this->response->array([
  132. 'is_bind_mobile' => !!$user
  133. ]);
  134. } catch (\Exception $exception) {
  135. return $this->exception($exception);
  136. }
  137. }
  138. /**
  139. * 发送验证码
  140. * @param MobileCodeRequest $request
  141. * @return mixed
  142. * User: Mead
  143. */
  144. public function sendVerificationCode(MobileCodeRequest $request)
  145. {
  146. $mobile = $request->get('mobile');
  147. $type = $request->get('type');
  148. $code = rand(1000, 9999);
  149. if ($type == 2) {
  150. // 仅仅表示换绑手机号
  151. if ((bool)$this->user()->is_bind_mobile) {
  152. $verify_mobile = $this->user()->mobile ?? '';
  153. if ($verify_mobile == $mobile) return $this->errorNoValidation('与旧手机号码相同,无需更改');
  154. }
  155. $verify_user = User::query()->merchant()->where('mobile', $mobile)->exists();
  156. if ($verify_user) return $this->errorNoValidation('您绑定得手机号已存在,如有疑问请致电客服');
  157. } elseif ($type == 3) {
  158. // 仅仅换账号登录时
  159. $verify_user = User::query()->merchant()->where('mobile', $mobile)->exists();
  160. if (!$verify_user) return $this->errorNoValidation('您登录得手机号不存在,如有疑问请致电客服');
  161. }
  162. try {
  163. // $this->dispatch(new SendSMSCodeJob($mobile, $code));
  164. app('easy_sms')->send($mobile, [
  165. 'template' => 'SMS_199330236',
  166. 'data' => [
  167. 'code' => $code
  168. ]
  169. ]);
  170. Cache::put("verification_code_{$mobile}", $code, Carbon::now()->addMinutes(5));
  171. return $this->response->array([
  172. 'is_send_sms' => true
  173. ]);
  174. } catch (Exception $exception) {
  175. return $this->exception($exception);
  176. }
  177. }
  178. /**
  179. * 获取时间戳
  180. * @param UserRepository $userRepository
  181. * @return
  182. * User: Mead
  183. */
  184. public function userStatus(UserRepository $userRepository)
  185. {
  186. try {
  187. $user = $userRepository->byIdGetUserStatus($this->user->id);
  188. return $this->response->item($user, UserStatusTransformer::class);
  189. } catch (\Exception $exception) {
  190. return $this->exception($exception);
  191. }
  192. }
  193. /**
  194. * 存储用户的form_id
  195. * User: Mead
  196. */
  197. public function storeFormId(UserFormIdRequest $request)
  198. {
  199. try {
  200. $form_id = $request->get('form_id');
  201. FormId::create([
  202. 'user_id' => $this->user->id,
  203. 'form_id' => $form_id,
  204. 'timestamp' => (time() + 604800)
  205. ]);
  206. return $this->success();
  207. } catch (\Exception $exception) {
  208. return $this->exception($exception);
  209. }
  210. }
  211. /**
  212. * 用户钱包记录 userWallerLogList
  213. *
  214. * @param WalletLogRepository $walletLogRepository
  215. * @return \Dingo\Api\Http\Response|void
  216. * @author Fx
  217. *
  218. */
  219. public function userWallerLogList(WalletLogRepository $walletLogRepository)
  220. {
  221. try {
  222. $data = $walletLogRepository->byUserIdGetWalletLog($this->user->id);
  223. return $this->response->paginator($data, WalletLogTransformer::class);
  224. } catch (\Exception $exception) {
  225. return $this->exception($exception);
  226. }
  227. }
  228. public function getInviteWechatQrcode(ImageUploadHandler $imageUploadHandler)
  229. {
  230. try {
  231. $folder = storage_path('app/public/img/invite_new_users/qrcode/');
  232. $endfilename = 'qrcode.png';
  233. $user_id = $this->user->id;
  234. // $user_id = 212;
  235. $user_qrcode_file = $folder . $user_id . $endfilename;
  236. if (!file_exists($user_qrcode_file)) {
  237. if (!is_dir($folder . 'qrcode')) {
  238. mkdir($folder . 'qrcode');
  239. }
  240. if (!$imageUploadHandler->createWechatQrcode($folder, $user_id, $endfilename)) {
  241. return $this->errorNoValidation('系统错误,请稍后重试');
  242. }
  243. }
  244. return $this->response->array([
  245. 'path' => config('app.url') . '/storage/img/invite_new_users/qrcode/' . $user_id . $endfilename,
  246. ]);
  247. } catch (\Exception $exception) {
  248. return $this->exception($exception);
  249. }
  250. }
  251. public function getInvitePoster(InviteNewUsersConfigRepository $inviteNewUsersConfigRepository, ImageUploadHandler $imageUploadHandler)
  252. {
  253. try {
  254. $folder = storage_path('app/public/img/invite_new_users/');
  255. $endfilename = 'qrcode.png';
  256. $user_id = $this->user->id;
  257. $register_area_id = $this->user->register_area_id;
  258. $posterEndFileName = 'register_area_id_' . $register_area_id . '_user_id_' . $user_id . '_poster.png';
  259. $path = $folder . 'poster/' . $posterEndFileName;
  260. if (!is_dir($folder . 'poster')) {
  261. mkdir($folder . 'poster');
  262. }
  263. if (!file_exists($path)) {
  264. $user_qrcode_file = $folder . 'qrcode/' . $user_id . $endfilename;
  265. if (!file_exists($user_qrcode_file)) {
  266. if (!is_dir($folder . 'qrcode')) {
  267. mkdir($folder . 'qrcode');
  268. }
  269. if (!$imageUploadHandler->createWechatQrcode($folder . 'qrcode/', $user_id, $endfilename)) {
  270. return $this->errorNoValidation('系统错误,请稍后重试');
  271. }
  272. }
  273. $inviteConfig = $inviteNewUsersConfigRepository->getInviteNewUsersConfigByAreaId($register_area_id);
  274. if (empty($inviteConfig) || empty($inviteConfig->background_image)) return $this->errorNoValidation('系统错误,请稍后重试');
  275. $background_image = $inviteConfig->background_image;
  276. // Log::info($background_image);
  277. $img = Image::make($background_image);
  278. // Log::info(111);
  279. $watermark = Image::make($user_qrcode_file);
  280. $img->insert($watermark, 'bottom', 0, 635);
  281. $img->save($path);
  282. }
  283. return $this->response->array([
  284. 'path' => config('app.url') . '/storage/img/invite_new_users/poster/' . $posterEndFileName,
  285. ]);
  286. } catch (\Exception $exception) {
  287. return $this->exception($exception);
  288. }
  289. }
  290. /**
  291. * @param InviteNewUsersRewardRequest $request
  292. * @param InviteNewUsersConfigRepository $inviteNewUsersConfigRepository
  293. * @param InviteNewUserRepository $inviteNewUserRepository
  294. * @param InviteNewUsersGiveGiftLogRepository $inviteNewUsersGiveGiftLogRepository
  295. * @return \Dingo\Api\Http\Response|void
  296. * Author: Mead
  297. */
  298. public function getInviteNewUsersReward(InviteNewUsersRewardRequest $request, InviteNewUsersConfigRepository $inviteNewUsersConfigRepository, InviteNewUserRepository $inviteNewUserRepository, InviteNewUsersGiveGiftLogRepository $inviteNewUsersGiveGiftLogRepository)
  299. {
  300. try {
  301. $invite_new_users_configs_id = $request->get('invite_new_users_configs_id');
  302. $gift_type = $request->get('gift_type');
  303. $gift_id = $request->get('gift_id');
  304. $gift_num = $request->get('gift_num');
  305. $num = $request->get('num');
  306. $data = [
  307. 'user_id' => $this->user->id,
  308. 'invite_new_users_configs_id' => $invite_new_users_configs_id,
  309. 'gift_type' => $gift_type,
  310. 'gift_id' => $gift_id,
  311. 'gift_num' => $gift_num,
  312. 'merchant_id' => self::$MERCHANT_ID
  313. ];
  314. $invite_new_users_configs = $inviteNewUsersConfigRepository->byIdGetModel($invite_new_users_configs_id);
  315. if (empty($invite_new_users_configs)) return $this->errorNoValidation('找不到该互动,或活动已过期');
  316. $is_repeat = $invite_new_users_configs->is_repeat;
  317. if ($is_repeat == InviteNewUsersConfig::REPEAT_NO) {
  318. // 不可重复
  319. if ($inviteNewUsersGiveGiftLogRepository->checkExist($data)) return $this->errorNoValidation('您已经领取过该礼物,不可重复领取');
  320. }
  321. $condition = $invite_new_users_configs->condition;
  322. $register_ids = $inviteNewUserRepository->isCondition($this->user->id, $condition, $num);
  323. if (!$register_ids) return $this->errorNoValidation('暂未达成条件,请稍后重试');
  324. // 达成条件
  325. $res = DB::transaction(function () use ($inviteNewUserRepository, $register_ids, $inviteNewUsersGiveGiftLogRepository, $data) {
  326. // 更新用户名额失效
  327. $inviteNewUserRepository->updateStatus($register_ids);
  328. // 送礼
  329. $logs = $inviteNewUsersGiveGiftLogRepository->logs($data);
  330. return $logs;
  331. });
  332. if ($res) {
  333. // 成功获得
  334. return $this->response->array(['status' => true]);
  335. } else {
  336. return $this->errorNoValidation('赠送失败');
  337. }
  338. } catch (\Exception $exception) {
  339. return $this->exception($exception);
  340. }
  341. }
  342. /**
  343. *
  344. * @param InviteNewUserRepository $inviteNewUserRepository
  345. * @return \Dingo\Api\Http\Response|void
  346. * Author: Mead\
  347. */
  348. public function getInviteUsers(InviteNewUserRepository $inviteNewUserRepository)
  349. {
  350. try {
  351. $inviteUsers = $inviteNewUserRepository->getInviteUsers($this->user->id);
  352. return $this->response->collection($inviteUsers, RegisterUsersTransformer::class);
  353. } catch (\Exception $exception) {
  354. return $this->exception($exception);
  355. }
  356. }
  357. /**
  358. * 学生或者老师实名认证
  359. * Author: Mead
  360. */
  361. public function students(UserStudentAuthRequest $studentAuthRequest)
  362. {
  363. $data = $studentAuthRequest->all();
  364. $user_id = $this->user->id;
  365. $auth = Student::where('user_id', $user_id)->first();
  366. $re = false;
  367. if ($auth) {
  368. //已经认证
  369. if ((int)$auth->auth_status === Student::AUTH_STATUS_OK) {
  370. return $this->error('您已经认证,请勿重复认证');
  371. }
  372. $re = $auth->fill([
  373. 'user_id' => $user_id,
  374. 'name' => $data['name'],
  375. 'school' => $data['school'],
  376. 'student_no' => $data['no'],
  377. 'type' => $data['type'],
  378. 'year' => $data['year'],
  379. 'imgs' => $data['imgs'],
  380. 'auth_status' => Student::AUTH_STATUS_WAIT,
  381. ]);
  382. } else {
  383. $re = Student::create([
  384. 'user_id' => $user_id,
  385. 'name' => $data['name'],
  386. 'school' => $data['school'],
  387. 'student_no' => $data['no'],
  388. 'type' => $data['type'],
  389. 'year' => $data['year'],
  390. 'imgs' => $data['imgs'],
  391. 'auth_status' => Student::AUTH_STATUS_WAIT,
  392. 'merchant_id' => self::$MERCHANT_ID
  393. ]);
  394. }
  395. if ($re) {
  396. return $this->success();
  397. }
  398. return $this->error('认证失败');
  399. }
  400. /**
  401. * 新用户邀请
  402. * @param Request $request
  403. * Author: Mead
  404. */
  405. public function newUserCoupons(Request $request, CouponRepository $couponRepository)
  406. {
  407. $ids = $request->get('ids', false);
  408. $user = $this->user;
  409. //判断是否领取
  410. if ($user->is_new_user_coupons === User::IS_NEW_USER_COUPONS_NO) {
  411. return $this->error('您已经领取,请勿重复领取');
  412. }
  413. //发放福利
  414. $coupons = $couponRepository->byIdsGetNewUserCoupons($ids);
  415. foreach ($coupons as $coupon) {
  416. CouponsUserBag::addCoupon($coupon, $user, $coupon->give_count);
  417. }
  418. User::where('id', $user->id)->update([
  419. 'is_new_user_coupons' => User::IS_NEW_USER_COUPONS_NO
  420. ]);
  421. return $this->success();
  422. }
  423. }