UserController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use App\Consts\ErrorConst;
  4. use App\Libs\ApiResponse;
  5. use App\Libs\TikTok\Kernel\Exceptions\Exception;
  6. use App\Libs\Utils;
  7. use App\Services\Book\BookService;
  8. use App\Services\OpenApi\OpenService;
  9. use App\Services\Report\ReportService;
  10. use App\Services\User\UserService;
  11. use App\Transformer\User\UserTransformer;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Routing\Controller as BaseController;
  14. use App\Exceptions\ApiException;
  15. use GuzzleHttp\Exception\GuzzleException;
  16. use Illuminate\Support\Facades\Validator;
  17. class UserController extends BaseController
  18. {
  19. use ApiResponse;
  20. protected $userService;
  21. protected $openService;
  22. protected $bookService;
  23. protected $reportService;
  24. public function __construct(
  25. UserService $userService,
  26. OpenService $openService,
  27. BookService $bookService,
  28. ReportService $reportService
  29. )
  30. {
  31. $this->userService = $userService;
  32. $this->openService = $openService;
  33. $this->bookService = $bookService;
  34. $this->reportService = $reportService;
  35. }
  36. /**
  37. * 登录接口
  38. *
  39. * @param Request $request
  40. * @return mixed
  41. * @throws ApiException
  42. * @throws GuzzleException
  43. */
  44. public function login(Request $request)
  45. {
  46. $params = $request->all();
  47. $sendOrderId = (int)getProp($params, 'send_order_id');
  48. $code = trim(getProp($params, 'code'));
  49. $anonymousCode = trim(getProp($params, 'anonymous_code'));
  50. if (empty($code)) {
  51. Utils::throwError(ErrorConst::PARAM_ERROR_CODE);
  52. }
  53. dLog('login')->info('params', $params);
  54. // 授权
  55. $user = $this->openService->getInstance()->code2Session($sendOrderId, $code, $anonymousCode);
  56. // 绑定邀请码
  57. try {
  58. $this->userService->bindUser($params);
  59. } catch (\Exception $e) {
  60. dLog('exception')->info('login-bind-fail', [
  61. 'params' => $params,
  62. 'user' => $user,
  63. 'exception_msg' => $e->getMessage()
  64. ]);
  65. }
  66. // 上报注册
  67. try {
  68. $this->reportService->reportRegister($user, $params);
  69. } catch (\Exception $e) {
  70. dLog('exception')->info('login-report-register-fail', [
  71. 'params' => $params,
  72. 'user' => $user,
  73. 'exception_msg' => $e->getMessage()
  74. ]);
  75. }
  76. return $this->success($user);
  77. }
  78. /**
  79. * 我的书架
  80. *
  81. * @param Request $request
  82. * @return mixed
  83. */
  84. public function shelfBooks(Request $request)
  85. {
  86. $data = $request->all();
  87. $result = $this->userService->getShelfBooks($data);
  88. return $this->success($result, [new UserTransformer(), 'newBuildUserShelfBooks']);
  89. }
  90. /**
  91. * 添加书架
  92. *
  93. * @param Request $request
  94. * @return mixed
  95. */
  96. public function addShelf(Request $request)
  97. {
  98. $data = $request->all();
  99. $result = $this->userService->addShelf($data);
  100. return $this->success(['success' => $result ? 1 : 0]);
  101. }
  102. /**
  103. * 删除书架
  104. *
  105. * @param Request $request
  106. * @return mixed
  107. */
  108. public function deleteShelf(Request $request)
  109. {
  110. $data = $request->all();
  111. $result = $this->userService->batchDeleteShelf($data);
  112. return $this->success(['success' => $result ? 1 : 0]);
  113. }
  114. /**
  115. * 获取用户阅读记录
  116. *
  117. * @param Request $request
  118. * @return mixed
  119. */
  120. public function recentBooks(Request $request)
  121. {
  122. $data = $request->all();
  123. $result = $this->userService->getRecentBooks($data);
  124. return $this->success(['list' => $result]);
  125. }
  126. /**
  127. * 用户信息
  128. *
  129. * @param Request $request
  130. * @return mixed
  131. */
  132. public function userInfo(Request $request)
  133. {
  134. $data = $request->all();
  135. $result = $this->userService->getUserInfo($data);
  136. return $this->success($result);
  137. }
  138. /**
  139. * 绑定用户
  140. *
  141. * @param Request $request
  142. * @return mixed
  143. */
  144. public function bindUser(Request $request)
  145. {
  146. $data = $request->all();
  147. $result = $this->userService->bindUser($data);
  148. return $this->success(['success' => $result ? 1 : 0]);
  149. }
  150. /**
  151. * 福利页
  152. *
  153. * @param Request $request
  154. * @return mixed
  155. */
  156. public function welfare(Request $request)
  157. {
  158. $data = $request->all();
  159. $result = $this->userService->welfare($data);
  160. return $this->success($result);
  161. }
  162. /**
  163. * 我的收益
  164. *
  165. * @param Request $request
  166. * @return mixed
  167. */
  168. public function earnings(Request $request)
  169. {
  170. $data = $request->all();
  171. $result = $this->userService->earnings($data);
  172. return $this->success($result, [new UserTransformer(), 'newBuildEarnings']);
  173. }
  174. /**
  175. * 提现档位
  176. *
  177. * @param Request $request
  178. * @return mixed
  179. */
  180. public function withdrawConfigs(Request $request)
  181. {
  182. $data = $request->all();
  183. $result = $this->userService->withdrawConfigs($data);
  184. return $this->success($result);
  185. }
  186. /**
  187. * 提现
  188. *
  189. * @param Request $request
  190. * @return mixed
  191. */
  192. public function withdraw(Request $request)
  193. {
  194. $data = $request->all();
  195. $validator = Validator::make($data, [
  196. 'withdraw_type' => 'required|in:WECHAT,ZHIFUBAO',
  197. 'amount' => 'required|numeric',
  198. 'alipay_username' => 'required_if:withdraw_type,ZHIFUBAO',
  199. 'alipay_account' => 'required_if:withdraw_type,ZHIFUBAO',
  200. ], [
  201. 'withdraw_type.required' => '请选择提现方式',
  202. 'withdraw_type.in' => '提现方式不正确',
  203. 'amount.required' => '请填写提现额度',
  204. 'amount.numeric' => '提现额度格式不正确',
  205. 'alipay_username.required_if' => '请填写用户名',
  206. 'alipay_account.required_if' => '请填写手机号或邮箱',
  207. ]);
  208. if ($validator->fails()) {
  209. Utils::throwError('1002:' . $validator->errors()->all()[0]);
  210. }
  211. $result = $this->userService->withdraw($data);
  212. return $this->success(['success' => $result ? 1 : 0]);
  213. }
  214. /**
  215. * 当前阅读
  216. *
  217. * @return mixed
  218. */
  219. public function currentBook()
  220. {
  221. $result = $this->bookService->getCurrentBook();
  222. return $this->success($result, [new UserTransformer(), 'newBuildCurrentBook']);
  223. }
  224. /**
  225. * 同步用户数据
  226. *
  227. * @param Request $request
  228. * @return mixed
  229. * @throws ApiException
  230. */
  231. public function syncUserInfo(Request $request)
  232. {
  233. $iv = $request->get('iv', '');
  234. $signature = $request->get('signature', '');
  235. $encryptedData = $request->get('encryptedData', '');
  236. $rawData = $request->get('rawData', '');
  237. $userInfo = $request->get('userInfo', []);
  238. $result = $this->userService->syncUserInfo($iv, $signature, $encryptedData, $rawData, $userInfo);
  239. return $this->success(compact('result'));
  240. }
  241. /**
  242. * 绑定派单链接
  243. *
  244. * @param Request $request
  245. * @return mixed
  246. */
  247. public function bindSendOrder(Request $request)
  248. {
  249. $data = $request->all();
  250. $result = $this->userService->bindSendOrder($data);
  251. return $this->success(['success' => $result ? 1 : 0]);
  252. }
  253. }