UserController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\User;
  3. use App\Http\Controllers\QuickApp\BaseController;
  4. use App\Http\Controllers\QuickApp\User\Transformers\SignRecordTransformer;
  5. use App\Libs\AliSMS;
  6. use App\Modules\Book\Models\Book;
  7. use App\Modules\Book\Services\BookService;
  8. use App\Modules\Book\Services\BookUrgeUpdateService;
  9. use App\Modules\SendOrder\Models\SendOrder;
  10. use App\Modules\Subscribe\Services\YearOrderService;
  11. use App\Modules\User\Models\QappPackage;
  12. use App\Modules\User\Services\QappUserService;
  13. use App\Modules\User\Services\ReadRecordService;
  14. use App\Modules\User\Services\SignService;
  15. use App\Modules\User\Services\UserService;
  16. use App\Modules\User\Services\UserSignService;
  17. use App\Modules\UserTask\Services\UserTaskService;
  18. use Illuminate\Http\Request;
  19. use App\Modules\Book\Models\BookConfig;
  20. use Redis;
  21. class UserController extends BaseController
  22. {
  23. /**
  24. * @apiDefine User 用户
  25. */
  26. /**
  27. * @apiVersion 1.0.0
  28. * @apiDescription 获取用户信息
  29. * @api {GET} userinfo 获取用户信息
  30. * @apiHeader {String} [Authorization] token
  31. * @apiGroup User
  32. * @apiName index
  33. * @apiSuccess {Number} id 用户ID.
  34. * @apiSuccess {String} openid 微信openid.
  35. * @apiSuccess {String} unionid 微信unionid.
  36. * @apiSuccess {Number} distribution_channel_id 分销渠道ID.
  37. * @apiSuccess {String} province 省份.
  38. * @apiSuccess {String} city 城市.
  39. * @apiSuccess {String} country 国家.
  40. * @apiSuccess {String} headimgurl 头像地址.
  41. * @apiSuccess {Number} send_order_id 派单ID.
  42. * @apiSuccess {Number=0,1} sex 性别.
  43. * @apiSuccess {String} balance 书币余额.
  44. * @apiSuccess {Int} is_vip 是否vip
  45. * @apiSuccess {String} vip_days 364天.
  46. * @apiSuccess {String} phone 手机号.
  47. * @apiSuccessExample {json} Success-Response:
  48. *
  49. * {
  50. * "code": 0,
  51. * "msg": "",
  52. * "data": {
  53. * "id": 56,
  54. * "openid": "sdfs34ssdfdsf",
  55. * "unionid": "SDFSD3343S",
  56. * "distribution_channel_id": 1212,
  57. * "province": "浙江省",
  58. * "city": "杭州",
  59. * "country": "中国",
  60. * "headimgurl": "http://..",
  61. * "send_order_id": 323,
  62. * "balance": 8956,
  63. * "register_time": "2017-12-12 12:12:12",
  64. * "phone": "12312435",
  65. * }
  66. * }
  67. */
  68. public function index(Request $request)
  69. {
  70. $package = $request->header('x-package', '');
  71. $data = $this->user_info;
  72. if (!$data->head_img) {
  73. $data->head_img = 'https://yueduyun.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/img/defaulthead.png';
  74. }
  75. $home_page = 'index';
  76. if (!empty($package)) {
  77. $home_page = QappPackage::where('package', $package)->value('home_page');
  78. }
  79. //快应用首页默认展示
  80. $data['home_page'] = empty($home_page) ? 'index' : $home_page;
  81. $data['is_vip'] = 0;
  82. $data['vip_days'] = 0;
  83. $data['book_sex'] = 1;
  84. $data['phone'] = $this->phone;
  85. $year_record = YearOrderService::getRecord($this->uid);
  86. if ($year_record) {
  87. $data['is_vip'] = 1;
  88. $time = strtotime($year_record['end_time']) - time();
  89. if ($time >= 86400) {
  90. $data['vip_days'] = floor($time / 86400) . '天';
  91. } elseif ($time > 3600) {
  92. $data['vip_days'] = floor($time / 3600) . '小时';
  93. } elseif ($time > 60) {
  94. $data['vip_days'] = floor($time / 60) . '分钟';
  95. } else {
  96. $data['vip_days'] = $time . '秒';
  97. }
  98. }
  99. $data['pay_mode_default'] = 'weixin';
  100. $data['is_check'] = $data->distribution_channel_id == 9487 ? true : false;
  101. $user = (new QappUserService())->getQAppUserByUid($data['id']);
  102. if (!$user || $user->status == 0) {
  103. \Log::info('user_log_off_user_info:uid:' . $user->uid);
  104. $data['balance'] = 0;
  105. $data['charge_balance'] = 0;
  106. $data['reward_balance'] = 0;
  107. $data['is_vip'] = 0;
  108. $data['vip_days'] = 0;
  109. } else {
  110. $send_order_id = !empty($this->send_order_id) ? $this->send_order_id : !empty($user->send_order_id) ? $user->send_order_id : 0;
  111. if ($send_order_id) {
  112. $send_order = SendOrder::find($send_order_id);
  113. if ($send_order) {
  114. $book = Book::leftjoin('book_categories', 'book_categories.id', 'books.category_id')
  115. ->where('books.id', $send_order->book_id)
  116. ->select('book_categories.pid')
  117. ->first();
  118. $data['book_sex'] = (isset($book->pid) && $book->pid == 2) ? 0 : 1;
  119. }
  120. }
  121. }
  122. // $data['is_check'] = !$this->phone;
  123. \Log::info('user_info:' . $this->uid . ' data:' . json_encode($data));
  124. return response()->success($data);
  125. }
  126. /**
  127. * @apiVersion 1.0.0
  128. * @apiDescription 用户签到
  129. * @api {GET} sign 用户签到
  130. * @apiHeader {String} [Authorization] token
  131. * @apiGroup User
  132. * @apiName sign
  133. * @apiSuccess {Double} fee 签到奖励
  134. * @apiSuccess {Number} days 签到天数
  135. * @apiSuccess {Array} day_list 签到列表
  136. * @apiSuccessExample {json} Success-Response:
  137. *
  138. * {
  139. * "code": 0,
  140. * "msg": "",
  141. * "data": {
  142. * "fee": 30,
  143. * "days": 1
  144. * "day_list": []
  145. * }
  146. * }
  147. */
  148. public function sign()
  149. {
  150. $result = UserSignService::sign($this->uid, date('Y-m-d'));
  151. if ($result) {
  152. return response()->success($result);
  153. }
  154. return response()->error('QAPP_SYS_ERROR');
  155. }
  156. /**
  157. * @apiVersion 1.0.0
  158. * @apiDescription 发送验证码
  159. * @api {POST} user/sendCode 发送验证码
  160. * @apiHeader {String} [Authorization] token
  161. * @apiGroup User
  162. * @apiName sendCode
  163. * @apiParam {String} phone 手机号
  164. * @apiSuccessExample {json} Success-Response:
  165. *
  166. * {
  167. * "code": 0,
  168. * "msg": "",
  169. * "data": {}
  170. * }
  171. */
  172. public function sendCode(Request $request)
  173. {
  174. $phone = $request->post('phone');
  175. if ($phone) {
  176. $code = random_int(1000, 9999);
  177. Redis::setex('quser_code:' . $phone, 120, $code);
  178. AliSMS::send($phone, 'quickapp_user_bind_phone', ['code' => $code]);
  179. return response()->success();
  180. } else {
  181. return response()->error('WAP_BIND_PHONE_EXIST');
  182. }
  183. }
  184. /**
  185. * @apiVersion 1.0.0
  186. * @apiDescription 绑定手机号
  187. * @api {POST} user/bindPhone 绑定手机号
  188. * @apiHeader {String} [Authorization] token
  189. * @apiGroup User
  190. * @apiName bindPhone
  191. * @apiParam {String} phone 手机号
  192. * @apiParam {String} code 验证码
  193. * @apiSuccessExample {json} Success-Response:
  194. *
  195. * {
  196. * "code": 0,
  197. * "msg": "",
  198. * "data": {}
  199. * }
  200. */
  201. public function bindPhone(Request $request)
  202. {
  203. $code = $request->post('code');
  204. $phone = $request->post('phone');
  205. $version = $request->post('version', '1.0');
  206. $old = Redis::get('quser_code:' . $phone);
  207. if ($old && $old == $code) {
  208. Redis::del('quser_code:' . $phone);
  209. if (!$this->phone) {
  210. $result = (new QappUserService)->bindPhone($this->uid, $phone, $version);
  211. if ($result) {
  212. return response()->success();
  213. } else {
  214. return response()->error('WAP_BIND_PHONE_EXIST');
  215. }
  216. } else {
  217. return response()->success();
  218. }
  219. } else {
  220. return response()->error('WAP_SEND_CODE_ERROR');
  221. }
  222. }
  223. /**
  224. * @apiVersion 1.0.0
  225. * @apiDescription 用户签到记录
  226. * @api {GET} user/sign_record 用户签到记录
  227. * @apiGroup User
  228. * @apiName signRecord
  229. * @apiParam {String} date 查询日期每个月一号
  230. * @apiSuccess {int} code 状态码
  231. * @apiSuccess {String} msg 信息
  232. * @apiSuccess {object} data 结果集
  233. * @apiSuccess {sign_status} data.sign_status .
  234. * @apiSuccess {sign_result} data.sign_result .
  235. * @apiSuccess {sign_today} data.sign_today .
  236. * @apiParam {page} page
  237. * @apiSuccessExample {json} Success-Response:
  238. *
  239. * {
  240. * code: 0,
  241. * msg: "",
  242. * data: {
  243. * "sign_status": true,
  244. * "sign_result": {
  245. * "list": [
  246. * {
  247. * "reward": 30,
  248. * "sign_time": "2019-11-01 14:20:30"
  249. * }
  250. * ],
  251. * "meta": {
  252. * "total": 1,
  253. * "per_page": 15,
  254. * "current_page": 1,
  255. * "last_page": 1,
  256. * "next_page_url": "",
  257. * "prev_page_url": ""
  258. * }
  259. * },
  260. * "sign_today": {
  261. * "uid": 162261523,
  262. * "price": 50,
  263. * "day": "2019-11-01",
  264. * "sign_time": "2019-11-01 09:04:43",
  265. * "created_at": "2019-11-01 09:04:43",
  266. * "updated_at": "2019-11-01 09:04:43",
  267. * "reward": 50
  268. * }
  269. * }
  270. */
  271. public function signRecord(Request $request)
  272. {
  273. $month = $request->get('date', date('Y-m-01'));
  274. $page_size = $request->get('page_size', 15);
  275. $sign_result = paginationTransform(new SignRecordTransformer(), UserSignService::getUserSignRecord($this->uid, $month, $page_size));
  276. $sign_status = UserSignService::isSign($this->uid);
  277. $sign_today = [];
  278. if ($sign_status) {
  279. $sign_today = ReadRecordService::getByField($this->uid, 'sign_info');
  280. if ($sign_today) $sign_today = json_decode($sign_today, 1);
  281. isset($sign_today['sign_time']) && $sign_today['sign_time'] = date('Y-m-d H:i:s', $sign_today['sign_time']);
  282. isset($sign_today['sign_at']) && $sign_today['sign_time'] = $sign_today['sign_at'];
  283. isset($sign_today['price']) && $sign_today['reward'] = $sign_today['price'];
  284. }
  285. $result = [
  286. 'sign_status' => $sign_status,
  287. 'sign_result' => $sign_result,
  288. 'sign_today' => $sign_today
  289. ];
  290. return response()->success($result);
  291. }
  292. /**
  293. * @apiVersion 1.0.0
  294. * @apiDescription 催更
  295. * @api {POST} user/urgeUpdate 催更
  296. * @apiHeader {String} [Authorization] token
  297. * @apiGroup User
  298. * @apiName urgeUpdate
  299. * @apiParam {String} bid 书号
  300. * @apiSuccessExample {json} Success-Response:
  301. *
  302. * {
  303. * "code": 0,
  304. * "msg": "",
  305. * "data": {}
  306. * }
  307. */
  308. public function urgeUpdate(Request $request)
  309. {
  310. $bid = $request->input('bid');
  311. if (empty($bid)) {
  312. return response()->error('PARAM_ERROR');
  313. }
  314. $bid = BookService::decodeBidStatic($bid);
  315. $result = BookUrgeUpdateService::UrgeRecord($this->uid, $bid);
  316. if ($result && !$result->id) {
  317. BookUrgeUpdateService::UrgeUpdate($this->uid, $bid, $result->updated_at);
  318. }
  319. return response()->success();
  320. }
  321. /**
  322. * @apiVersion 1.0.0
  323. * @apiDescription 更新派单ID
  324. * @api {GET} user/setSendOrder 更新派单ID
  325. * @apiHeader {String} [Authorization] token
  326. * @apiGroup User
  327. * @apiName setSendOrder
  328. * @apiParam {Int} send_order_id 派单ID
  329. * @apiSuccessExample {json} Success-Response:
  330. *
  331. * {
  332. * "code": 0,
  333. * "msg": "",
  334. * "data": {}
  335. */
  336. public function setSendOrder(Request $request)
  337. {
  338. $send_order_id = $request->get('send_order_id', 0);
  339. if ($send_order_id) {
  340. UserService::setUserSendOrder($this->uid, $send_order_id);
  341. }
  342. return response()->success();
  343. }
  344. /**
  345. * 加桌
  346. */
  347. public function addDesktop(Request $request)
  348. {
  349. $status = $request->get('status');
  350. if (is_numeric($status)) {
  351. UserService::qappAddDesktop($this->uid, $status);
  352. }
  353. }
  354. /**
  355. * 获取任务奖励
  356. */
  357. public function getUserTaskReward(int $id)
  358. {
  359. $service = new UserTaskService($this->uid);
  360. $result = $service->getTaskReward($id);
  361. if ($result == 1) {
  362. return response()->success(['is_sign' => true]);
  363. } else if ($result == -1) {
  364. return response()->error('REWARD_GOTTEN_ERROR');
  365. } else if ($result == 0) {
  366. return response()->error('NO_REWARD');
  367. }
  368. }
  369. /**
  370. * 任务中心
  371. */
  372. public function taskList()
  373. {
  374. $list = $this->getTaskList();
  375. return response()->success();
  376. }
  377. /**
  378. * 注销用户
  379. * @return mixed
  380. */
  381. public function logOff()
  382. {
  383. $service = new QappUserService();
  384. $res = $service->getLogOff($this->uid);
  385. \Log::info('user_log_off:uid:' . $this->uid . ' res:' . $res);
  386. if ($res) {
  387. return response()->success();
  388. }
  389. return response()->error('QAPP_SYS_ERROR');
  390. }
  391. /**
  392. * 新版签到信息
  393. */
  394. public function findSignInfo()
  395. {
  396. $service = new SignService($this->uid);
  397. $reward_list = $this->get_sign_reward_list($this->distribution_channel_id);
  398. $service->setRewardList($reward_list);
  399. return response()->success($service->getSignInfo());
  400. }
  401. /**
  402. * 新版签到
  403. */
  404. public function newSign()
  405. {
  406. $service = new SignService($this->uid);
  407. $reward_list = $this->get_sign_reward_list($this->distribution_channel_id);
  408. $service->setRewardList($reward_list);
  409. $service->sign();
  410. return response()->success();
  411. }
  412. /**
  413. * 奖励
  414. * name: get_sign_reward_list
  415. * @param $distribution_channel_id
  416. * @return int[]
  417. * date 2022/10/11 11:56
  418. */
  419. private function get_sign_reward_list($distribution_channel_id)
  420. {
  421. $list = [30, 35, 40, 45, 50, 55, 60];
  422. // 琥珀阅读签到奖励独立
  423. if ($distribution_channel_id == 14903) {
  424. $list = [30, 88, 188, 30, 88, 188, 188];
  425. }
  426. return $list;
  427. }
  428. /**
  429. * 是否有可领取的任务包括签到
  430. * name: hasPendingTask
  431. * date 2023/02/22 14:15
  432. */
  433. public function hasPendingTask(Request $request)
  434. {
  435. $show = true;
  436. try {
  437. $type = $request->input('type', "home");
  438. $service = new SignService($this->uid);
  439. $has_sign_today = $service->hasSignToday();
  440. if ($has_sign_today) {
  441. // 签到找查看是否有可领取的任务
  442. $show = $this->checkTask($type);
  443. }
  444. } catch (\Exception $exception) {
  445. $show = true;
  446. }
  447. return response()->success(['has_pend_task' => $show]);
  448. }
  449. /**
  450. * 是否有可以领取的任务
  451. * name: checkTask
  452. * @return bool
  453. * date 2023/02/22 15:11
  454. */
  455. private function checkTask($type= "home")
  456. {
  457. $list = $this->getTaskList();
  458. $task = $list['new_user_tasks'] ?? [];
  459. $show = $this->checkTaskItem($task,$type);
  460. if ($show){
  461. return $show;
  462. }
  463. $task = $list['date_tasks'] ?? [];
  464. return $this->checkTaskItem($task,$type);
  465. }
  466. /**
  467. * 获取任务列表
  468. * name: getTaskList
  469. * @return array
  470. * date 2023/02/22 15:10
  471. */
  472. private function getTaskList()
  473. {
  474. $service = new UserTaskService($this->uid);
  475. $new_user_tasks = $service->findNewUserTaskList();
  476. $date_tasks = $service->findDateUserTaskList();
  477. // 琥珀阅读去除加桌任务
  478. if ($this->distribution_channel_id == 14903 && $new_user_tasks) {
  479. foreach ($new_user_tasks as $key => $val) {
  480. if ($val['code'] == "add_desk") {
  481. unset($new_user_tasks[$key]);
  482. }
  483. }
  484. unset($val);
  485. }
  486. $new_user_tasks = array_values($new_user_tasks->toArray());
  487. return compact('new_user_tasks', 'date_tasks');
  488. }
  489. /**
  490. * 检测是否有可以领取项
  491. * name: checkTaskItem
  492. * @param $task
  493. * @return bool
  494. * date 2023/02/22 15:10
  495. */
  496. private function checkTaskItem($task,$type)
  497. {
  498. $show = false;
  499. foreach ($task as $value){
  500. if ($type == 'home'){
  501. if (isset($value['status']) && $value['status'] < 2){
  502. $show = true;
  503. break ;
  504. }
  505. }else if ($type == 'recharge'){
  506. if (isset($value['status']) && $value['status'] == 1){
  507. $show = true;
  508. break ;
  509. }
  510. }
  511. }
  512. return $show;
  513. }
  514. }