UserController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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\WechatEncryptorMobileRequest;
  18. use App\Jobs\SendSMSCodeJob;
  19. use App\Models\FormId;
  20. use App\Models\InviteNewUsersConfig;
  21. use App\Models\User;
  22. use App\Repositories\InviteNewUserRepository;
  23. use App\Repositories\InviteNewUsersConfigRepository;
  24. use App\Repositories\InviteNewUsersGiveGiftLogRepository;
  25. use App\Repositories\UserRepository;
  26. use App\Repositories\WalletLogRepository;
  27. use App\Transformers\RegisterUsersTransformer;
  28. use App\Transformers\UserStatusTransformer;
  29. use App\Transformers\WalletLogTransformer;
  30. use Carbon\Carbon;
  31. use Illuminate\Support\Facades\Cache;
  32. use Illuminate\Support\Facades\DB;
  33. use Illuminate\Support\Facades\Log;
  34. use Intervention\Image\ImageManagerStatic as Image;
  35. use Laravel\Lumen\Http\Request;
  36. use Overtrue\EasySms\Exceptions\Exception;
  37. /**
  38. * 用户模块
  39. * Class UserController
  40. * @package App\Http\Controllers\V1
  41. */
  42. class UserController extends BaseController
  43. {
  44. /**
  45. * 实名认证
  46. * @param RealNameRequest $request
  47. * @return mixed
  48. * User: Mead
  49. */
  50. public function realNameAuthentication(RealNameRequest $request)
  51. {
  52. try {
  53. $card_id = $request->get('card_id');
  54. $name = $request->get('name');
  55. $age = idCard2age($card_id);
  56. $is_match_ride_age = ($age >= 16);
  57. User::where('id', $this->user->id)->update([
  58. 'card_id' => $card_id,
  59. 'truename' => $name,
  60. 'is_card_certified' => User::CARD_NO,
  61. 'is_match_ride_age' => $is_match_ride_age
  62. ]);
  63. if (!$is_match_ride_age) {
  64. return $this->errorNoValidation('不满16周岁禁止骑车!');
  65. }
  66. // $status = IdCardHandler::main($card_id, $name);
  67. // if ($status['code']) {
  68. // return $this->errorNoValidation($status['msg']);
  69. // }
  70. $user = User::where('id', $this->user->id)->update([
  71. 'is_card_certified' => User::CARD_OK
  72. ]);
  73. event(new CardCertifiedEvent($this->user()));
  74. } catch (\Exception $exception) {
  75. return $this->errorNoValidation($exception->getMessage());
  76. }
  77. return $this->response->array([
  78. 'is_card_certified' => !!$user
  79. ]);
  80. }
  81. public function bindWechatMobile(WechatEncryptorMobileRequest $request)
  82. {
  83. try {
  84. // $session = $request->get('session_key');
  85. $session = Cache::get('login:user:session_key:auth:' . $this->user->id);
  86. $iv = $request->get('iv');
  87. $encryptedData = $request->get('encryptedData');
  88. $miniProgram = app('wechat.mini_program');
  89. $decryptedData = $miniProgram->encryptor->decryptData($session, $iv, $encryptedData);
  90. $mobile = $decryptedData['purePhoneNumber'];
  91. $user = User::where('id', $this->user->id)->update([
  92. 'mobile' => $mobile,
  93. 'is_bind_mobile' => User::BIND_MOBILE_OK
  94. ]);
  95. return $this->response->array([
  96. 'is_bind_mobile' => !!$user
  97. ]);
  98. } catch (\Exception $exception) {
  99. return $this->errorNoValidation($exception->getMessage());
  100. }
  101. }
  102. /**
  103. * 绑定手机号
  104. * @param BindMobileRequest $request
  105. * User: Mead
  106. */
  107. public function bindMobile(BindMobileRequest $request)
  108. {
  109. try {
  110. $mobile = $request->get('mobile');
  111. $code = $request->get('code', false);
  112. $v_code = Cache::get("verification_code_{$mobile}", '');
  113. if (empty($v_code)) {
  114. return $this->errorNoValidation('验证码已过期');
  115. }
  116. if ((string)$v_code !== (string)$code) {
  117. return $this->errorNoValidation('验证码错误');
  118. }
  119. $user = User::where('id', $this->user->id)->update([
  120. 'mobile' => $mobile,
  121. 'is_bind_mobile' => User::BIND_MOBILE_OK
  122. ]);
  123. return $this->response->array([
  124. 'is_bind_mobile' => !!$user
  125. ]);
  126. } catch (\Exception $exception) {
  127. return $this->errorNoValidation($exception->getMessage());
  128. }
  129. }
  130. /**
  131. * 发送验证码
  132. * @param MobileCodeRequest $request
  133. * @return mixed
  134. * User: Mead
  135. */
  136. public function sendVerificationCode(MobileCodeRequest $request)
  137. {
  138. $mobile = $request->get('mobile');
  139. $type = $request->get('type');
  140. $code = rand(1000, 9999);
  141. if ($type == 2) {
  142. // 仅仅表示换绑手机号
  143. if ((bool)$this->user()->is_bind_mobile) {
  144. $verify_mobile = $this->user()->mobile ?? '';
  145. if ($verify_mobile == $mobile) return $this->errorNoValidation('与旧手机号码相同,无需更改');
  146. }
  147. $verify_user = User::query()->where('mobile', $mobile)->exists();
  148. if ($verify_user) return $this->errorNoValidation('您绑定得手机号已存在,如有疑问请致电客服');
  149. } elseif ($type == 3) {
  150. // 仅仅换账号登录时
  151. $verify_user = User::query()->where('mobile', $mobile)->exists();
  152. if (!$verify_user) return $this->errorNoValidation('您登录得手机号不存在,如有疑问请致电客服');
  153. }
  154. try {
  155. // $this->dispatch(new SendSMSCodeJob($mobile, $code));
  156. app('easy_sms')->send($mobile, [
  157. 'template' => '1251374',
  158. 'data' => [
  159. 'code' => $code
  160. ]
  161. ]);
  162. Cache::put("verification_code_{$mobile}", $code, Carbon::now()->addMinutes(5));
  163. return $this->response->array([
  164. 'is_send_sms' => true
  165. ]);
  166. } catch (Exception $exception) {
  167. $this->errorNoValidation($exception->getMessage());
  168. }
  169. }
  170. /**
  171. * 获取时间戳
  172. * @param UserRepository $userRepository
  173. * @return \Dingo\Api\Http\Response
  174. * User: Mead
  175. */
  176. public function userStatus(UserRepository $userRepository)
  177. {
  178. try {
  179. $user = $userRepository->byIdGetUserStatus($this->user->id);
  180. return $this->response->item($user, UserStatusTransformer::class);
  181. } catch (\Exception $exception) {
  182. return $this->errorNoValidation($exception->getMessage());
  183. }
  184. }
  185. /**
  186. * 存储用户的form_id
  187. * User: Mead
  188. */
  189. public function storeFormId(UserFormIdRequest $request)
  190. {
  191. try {
  192. $form_id = $request->get('form_id');
  193. FormId::create([
  194. 'user_id' => $this->user->id,
  195. 'form_id' => $form_id,
  196. 'timestamp' => (time() + 604800)
  197. ]);
  198. return $this->success();
  199. } catch (\Exception $exception) {
  200. return $this->errorNoValidation($exception->getMessage());
  201. }
  202. }
  203. /**
  204. * 用户钱包记录 userWallerLogList
  205. *
  206. * @param WalletLogRepository $walletLogRepository
  207. * @return \Dingo\Api\Http\Response|void
  208. * @author Fx
  209. *
  210. */
  211. public function userWallerLogList(WalletLogRepository $walletLogRepository)
  212. {
  213. try {
  214. $data = $walletLogRepository->byUserIdGetWalletLog($this->user->id);
  215. return $this->response->paginator($data, WalletLogTransformer::class);
  216. } catch (\Exception $exception) {
  217. return $this->errorNoValidation($exception->getMessage());
  218. }
  219. }
  220. public function getInviteWechatQrcode(ImageUploadHandler $imageUploadHandler)
  221. {
  222. try {
  223. $folder = storage_path('app/public/img/invite_new_users/qrcode/');
  224. $endfilename = 'qrcode.png';
  225. $user_id = $this->user->id;
  226. // $user_id = 212;
  227. $user_qrcode_file = $folder . $user_id . $endfilename;
  228. if (!file_exists($user_qrcode_file)) {
  229. if (!is_dir($folder . 'qrcode')) {
  230. mkdir($folder . 'qrcode');
  231. }
  232. if (!$imageUploadHandler->createWechatQrcode($folder, $user_id, $endfilename)) {
  233. return $this->errorNoValidation('系统错误,请稍后重试');
  234. }
  235. }
  236. return $this->response->array([
  237. 'path' => config('app.url') . '/storage/img/invite_new_users/qrcode/' . $user_id . $endfilename,
  238. ]);
  239. } catch (\Exception $e) {
  240. return $this->errorNoValidation($e->getMessage());
  241. }
  242. }
  243. public function getInvitePoster(InviteNewUsersConfigRepository $inviteNewUsersConfigRepository, ImageUploadHandler $imageUploadHandler)
  244. {
  245. try {
  246. $folder = storage_path('app/public/img/invite_new_users/');
  247. $endfilename = 'qrcode.png';
  248. $user_id = $this->user->id;
  249. $register_area_id = $this->user->register_area_id;
  250. $posterEndFileName = 'register_area_id_' . $register_area_id . '_user_id_' . $user_id . '_poster.png';
  251. $path = $folder . 'poster/' . $posterEndFileName;
  252. if (!is_dir($folder . 'poster')) {
  253. mkdir($folder . 'poster');
  254. }
  255. if (!file_exists($path)) {
  256. $user_qrcode_file = $folder . 'qrcode/' . $user_id . $endfilename;
  257. if (!file_exists($user_qrcode_file)) {
  258. if (!is_dir($folder . 'qrcode')) {
  259. mkdir($folder . 'qrcode');
  260. }
  261. if (!$imageUploadHandler->createWechatQrcode($folder . 'qrcode/', $user_id, $endfilename)) {
  262. return $this->errorNoValidation('系统错误,请稍后重试');
  263. }
  264. }
  265. $inviteConfig = $inviteNewUsersConfigRepository->getInviteNewUsersConfigByAreaId($register_area_id);
  266. if (empty($inviteConfig) || empty($inviteConfig->background_image)) return $this->errorNoValidation('系统错误,请稍后重试');
  267. $background_image = $inviteConfig->background_image;
  268. // Log::info($background_image);
  269. $img = Image::make($background_image);
  270. // Log::info(111);
  271. $watermark = Image::make($user_qrcode_file);
  272. $img->insert($watermark, 'bottom', 0, 635);
  273. $img->save($path);
  274. }
  275. return $this->response->array([
  276. 'path' => config('app.url') . '/storage/img/invite_new_users/poster/' . $posterEndFileName,
  277. ]);
  278. } catch (\Exception $e) {
  279. return $this->errorNoValidation($e->getMessage());
  280. }
  281. }
  282. public function getInviteNewUsersReward(InviteNewUsersRewardRequest $request, InviteNewUsersConfigRepository $inviteNewUsersConfigRepository, InviteNewUserRepository $inviteNewUserRepository, InviteNewUsersGiveGiftLogRepository $inviteNewUsersGiveGiftLogRepository)
  283. {
  284. try {
  285. $invite_new_users_configs_id = $request->get('invite_new_users_configs_id');
  286. $gift_type = $request->get('gift_type');
  287. $gift_id = $request->get('gift_id');
  288. $gift_num = $request->get('gift_num');
  289. $num = $request->get('num');
  290. $data = [
  291. 'user_id' => $this->user->id,
  292. 'invite_new_users_configs_id' => $invite_new_users_configs_id,
  293. 'gift_type' => $gift_type,
  294. 'gift_id' => $gift_id,
  295. 'gift_num' => $gift_num,
  296. ];
  297. $invite_new_users_configs = $inviteNewUsersConfigRepository->byIdGetModel($invite_new_users_configs_id);
  298. if (empty($invite_new_users_configs)) return $this->errorNoValidation('找不到该互动,或活动已过期');
  299. $is_repeat = $invite_new_users_configs->is_repeat;
  300. if ($is_repeat == InviteNewUsersConfig::REPEAT_NO) {
  301. // 不可重复
  302. if ($inviteNewUsersGiveGiftLogRepository->checkExist($data)) return $this->errorNoValidation('您已经领取过该礼物,不可重复领取');
  303. }
  304. $condition = $invite_new_users_configs->condition;
  305. $register_ids = $inviteNewUserRepository->isCondition($this->user->id, $condition, $num);
  306. if (!$register_ids) return $this->errorNoValidation('暂未达成条件,请稍后重试');
  307. // 达成条件
  308. $res = DB::transaction(function () use ($inviteNewUserRepository, $register_ids, $inviteNewUsersGiveGiftLogRepository, $data) {
  309. // 更新用户名额失效
  310. $inviteNewUserRepository->updateStatus($register_ids);
  311. // 送礼
  312. $logs = $inviteNewUsersGiveGiftLogRepository->logs($data);
  313. return $logs;
  314. });
  315. if ($res) {
  316. // 成功获得
  317. return $this->response->array(['status' => true]);
  318. } else {
  319. return $this->errorNoValidation('赠送失败');
  320. }
  321. } catch (\Exception $e) {
  322. return $this->errorNoValidation($e->getMessage());
  323. }
  324. }
  325. public function getInviteUsers(InviteNewUserRepository $inviteNewUserRepository)
  326. {
  327. try {
  328. $inviteUsers = $inviteNewUserRepository->getInviteUsers($this->user->id);
  329. return $this->response->collection($inviteUsers, RegisterUsersTransformer::class);
  330. } catch (\Exception $exception) {
  331. return $this->errorNoValidation($exception->getMessage());
  332. }
  333. }
  334. }