UserController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. $book = Book::leftjoin('book_categories','book_categories.id','books.category_id')
  114. ->where('books.id',$send_order->book_id)
  115. ->select('book_categories.pid')
  116. ->first();
  117. $data['book_sex'] = (isset($book->pid) && $book->pid == 2) ? 0 : 1;
  118. }
  119. }
  120. // $data['is_check'] = !$this->phone;
  121. \Log::info('user_info:'.$this->uid.' data:'.json_encode($data));
  122. return response()->success($data);
  123. }
  124. /**
  125. * @apiVersion 1.0.0
  126. * @apiDescription 用户签到
  127. * @api {GET} sign 用户签到
  128. * @apiHeader {String} [Authorization] token
  129. * @apiGroup User
  130. * @apiName sign
  131. * @apiSuccess {Double} fee 签到奖励
  132. * @apiSuccess {Number} days 签到天数
  133. * @apiSuccess {Array} day_list 签到列表
  134. * @apiSuccessExample {json} Success-Response:
  135. *
  136. * {
  137. * "code": 0,
  138. * "msg": "",
  139. * "data": {
  140. * "fee": 30,
  141. * "days": 1
  142. * "day_list": []
  143. * }
  144. * }
  145. */
  146. public function sign()
  147. {
  148. $result = UserSignService::sign($this->uid, date('Y-m-d'));
  149. if ($result) {
  150. return response()->success($result);
  151. }
  152. return response()->error('QAPP_SYS_ERROR');
  153. }
  154. /**
  155. * @apiVersion 1.0.0
  156. * @apiDescription 发送验证码
  157. * @api {POST} user/sendCode 发送验证码
  158. * @apiHeader {String} [Authorization] token
  159. * @apiGroup User
  160. * @apiName sendCode
  161. * @apiParam {String} phone 手机号
  162. * @apiSuccessExample {json} Success-Response:
  163. *
  164. * {
  165. * "code": 0,
  166. * "msg": "",
  167. * "data": {}
  168. * }
  169. */
  170. public function sendCode(Request $request)
  171. {
  172. $phone = $request->post('phone');
  173. if ($phone) {
  174. $code = random_int(1000, 9999);
  175. Redis::setex('quser_code:' . $phone, 120, $code);
  176. AliSMS::send($phone, 'quickapp_user_bind_phone', ['code' => $code]);
  177. return response()->success();
  178. } else {
  179. return response()->error('WAP_BIND_PHONE_EXIST');
  180. }
  181. }
  182. /**
  183. * @apiVersion 1.0.0
  184. * @apiDescription 绑定手机号
  185. * @api {POST} user/bindPhone 绑定手机号
  186. * @apiHeader {String} [Authorization] token
  187. * @apiGroup User
  188. * @apiName bindPhone
  189. * @apiParam {String} phone 手机号
  190. * @apiParam {String} code 验证码
  191. * @apiSuccessExample {json} Success-Response:
  192. *
  193. * {
  194. * "code": 0,
  195. * "msg": "",
  196. * "data": {}
  197. * }
  198. */
  199. public function bindPhone(Request $request)
  200. {
  201. $code = $request->post('code');
  202. $phone = $request->post('phone');
  203. $version = $request->post('version', '1.0');
  204. $old = Redis::get('quser_code:' . $phone);
  205. if ($old && $old == $code) {
  206. Redis::del('quser_code:' . $phone);
  207. if (!$this->phone) {
  208. $result = (new QappUserService)->bindPhone($this->uid, $phone, $version);
  209. if ($result) {
  210. return response()->success();
  211. } else {
  212. return response()->error('WAP_BIND_PHONE_EXIST');
  213. }
  214. } else {
  215. return response()->success();
  216. }
  217. } else {
  218. return response()->error('WAP_SEND_CODE_ERROR');
  219. }
  220. }
  221. /**
  222. * @apiVersion 1.0.0
  223. * @apiDescription 用户签到记录
  224. * @api {GET} user/sign_record 用户签到记录
  225. * @apiGroup User
  226. * @apiName signRecord
  227. * @apiParam {String} date 查询日期每个月一号
  228. * @apiSuccess {int} code 状态码
  229. * @apiSuccess {String} msg 信息
  230. * @apiSuccess {object} data 结果集
  231. * @apiSuccess {sign_status} data.sign_status .
  232. * @apiSuccess {sign_result} data.sign_result .
  233. * @apiSuccess {sign_today} data.sign_today .
  234. * @apiParam {page} page
  235. * @apiSuccessExample {json} Success-Response:
  236. *
  237. * {
  238. * code: 0,
  239. * msg: "",
  240. * data: {
  241. * "sign_status": true,
  242. * "sign_result": {
  243. * "list": [
  244. * {
  245. * "reward": 30,
  246. * "sign_time": "2019-11-01 14:20:30"
  247. * }
  248. * ],
  249. * "meta": {
  250. * "total": 1,
  251. * "per_page": 15,
  252. * "current_page": 1,
  253. * "last_page": 1,
  254. * "next_page_url": "",
  255. * "prev_page_url": ""
  256. * }
  257. * },
  258. * "sign_today": {
  259. * "uid": 162261523,
  260. * "price": 50,
  261. * "day": "2019-11-01",
  262. * "sign_time": "2019-11-01 09:04:43",
  263. * "created_at": "2019-11-01 09:04:43",
  264. * "updated_at": "2019-11-01 09:04:43",
  265. * "reward": 50
  266. * }
  267. * }
  268. */
  269. public function signRecord(Request $request)
  270. {
  271. $month = $request->get('date', date('Y-m-01'));
  272. $page_size = $request->get('page_size', 15);
  273. $sign_result = paginationTransform(new SignRecordTransformer(), UserSignService::getUserSignRecord($this->uid, $month, $page_size));
  274. $sign_status = UserSignService::isSign($this->uid);
  275. $sign_today = [];
  276. if ($sign_status) {
  277. $sign_today = ReadRecordService::getByField($this->uid, 'sign_info');
  278. if ($sign_today) $sign_today = json_decode($sign_today, 1);
  279. isset($sign_today['sign_time']) && $sign_today['sign_time'] = date('Y-m-d H:i:s', $sign_today['sign_time']);
  280. isset($sign_today['sign_at']) && $sign_today['sign_time'] = $sign_today['sign_at'];
  281. isset($sign_today['price']) && $sign_today['reward'] = $sign_today['price'];
  282. }
  283. $result = [
  284. 'sign_status' => $sign_status,
  285. 'sign_result' => $sign_result,
  286. 'sign_today' => $sign_today
  287. ];
  288. return response()->success($result);
  289. }
  290. /**
  291. * @apiVersion 1.0.0
  292. * @apiDescription 催更
  293. * @api {POST} user/urgeUpdate 催更
  294. * @apiHeader {String} [Authorization] token
  295. * @apiGroup User
  296. * @apiName urgeUpdate
  297. * @apiParam {String} bid 书号
  298. * @apiSuccessExample {json} Success-Response:
  299. *
  300. * {
  301. * "code": 0,
  302. * "msg": "",
  303. * "data": {}
  304. * }
  305. */
  306. public function urgeUpdate(Request $request)
  307. {
  308. $bid = $request->input('bid');
  309. if (empty($bid)) {
  310. return response()->error('PARAM_ERROR');
  311. }
  312. $bid = BookService::decodeBidStatic($bid);
  313. $result = BookUrgeUpdateService::UrgeRecord($this->uid, $bid);
  314. if ($result && !$result->id) {
  315. BookUrgeUpdateService::UrgeUpdate($this->uid, $bid, $result->updated_at);
  316. }
  317. return response()->success();
  318. }
  319. /**
  320. * @apiVersion 1.0.0
  321. * @apiDescription 更新派单ID
  322. * @api {GET} user/setSendOrder 更新派单ID
  323. * @apiHeader {String} [Authorization] token
  324. * @apiGroup User
  325. * @apiName setSendOrder
  326. * @apiParam {Int} send_order_id 派单ID
  327. * @apiSuccessExample {json} Success-Response:
  328. *
  329. * {
  330. * "code": 0,
  331. * "msg": "",
  332. * "data": {}
  333. */
  334. public function setSendOrder(Request $request)
  335. {
  336. $send_order_id = $request->get('send_order_id', 0);
  337. if ($send_order_id) {
  338. UserService::setUserSendOrder($this->uid, $send_order_id);
  339. }
  340. return response()->success();
  341. }
  342. /**
  343. * 加桌
  344. */
  345. public function addDesktop(Request $request)
  346. {
  347. $status = $request->get('status');
  348. if (is_numeric($status)) {
  349. UserService::qappAddDesktop($this->uid, $status);
  350. }
  351. }
  352. /**
  353. * 获取任务奖励
  354. */
  355. public function getUserTaskReward(int $id)
  356. {
  357. $service = new UserTaskService($this->uid);
  358. $result = $service->getTaskReward($id);
  359. if ($result == 1) {
  360. return response()->success(['is_sign' => true]);
  361. } else if ($result == -1) {
  362. return response()->error('REWARD_GOTTEN_ERROR');
  363. } else if ($result == 0) {
  364. return response()->error('NO_REWARD');
  365. }
  366. }
  367. /**
  368. * 任务中心
  369. */
  370. public function taskList()
  371. {
  372. $service = new UserTaskService($this->uid);
  373. $new_user_tasks = $service->findNewUserTaskList();
  374. $date_tasks = $service->findDateUserTaskList();
  375. return response()->success(compact('new_user_tasks', 'date_tasks'));
  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. return response()->success($service->getSignInfo());
  398. }
  399. /**
  400. * 新版签到
  401. */
  402. public function newSign()
  403. {
  404. $service = new SignService($this->uid);
  405. $service->sign();
  406. return response()->success();
  407. }
  408. }