UserController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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\Services\QappUserService;
  12. use App\Modules\User\Services\ReadRecordService;
  13. use App\Modules\User\Services\SignService;
  14. use App\Modules\User\Services\UserService;
  15. use App\Modules\User\Services\UserSignService;
  16. use App\Modules\UserTask\Services\UserTaskService;
  17. use Illuminate\Http\Request;
  18. use App\Modules\Book\Models\BookConfig;
  19. use Redis;
  20. class UserController extends BaseController
  21. {
  22. /**
  23. * @apiDefine User 用户
  24. */
  25. /**
  26. * @apiVersion 1.0.0
  27. * @apiDescription 获取用户信息
  28. * @api {GET} userinfo 获取用户信息
  29. * @apiHeader {String} [Authorization] token
  30. * @apiGroup User
  31. * @apiName index
  32. * @apiSuccess {Number} id 用户ID.
  33. * @apiSuccess {String} openid 微信openid.
  34. * @apiSuccess {String} unionid 微信unionid.
  35. * @apiSuccess {Number} distribution_channel_id 分销渠道ID.
  36. * @apiSuccess {String} province 省份.
  37. * @apiSuccess {String} city 城市.
  38. * @apiSuccess {String} country 国家.
  39. * @apiSuccess {String} headimgurl 头像地址.
  40. * @apiSuccess {Number} send_order_id 派单ID.
  41. * @apiSuccess {Number=0,1} sex 性别.
  42. * @apiSuccess {String} balance 书币余额.
  43. * @apiSuccess {Int} is_vip 是否vip
  44. * @apiSuccess {String} vip_days 364天.
  45. * @apiSuccess {String} phone 手机号.
  46. * @apiSuccessExample {json} Success-Response:
  47. *
  48. * {
  49. * "code": 0,
  50. * "msg": "",
  51. * "data": {
  52. * "id": 56,
  53. * "openid": "sdfs34ssdfdsf",
  54. * "unionid": "SDFSD3343S",
  55. * "distribution_channel_id": 1212,
  56. * "province": "浙江省",
  57. * "city": "杭州",
  58. * "country": "中国",
  59. * "headimgurl": "http://..",
  60. * "send_order_id": 323,
  61. * "balance": 8956,
  62. * "register_time": "2017-12-12 12:12:12",
  63. * "phone": "12312435",
  64. * }
  65. * }
  66. */
  67. public function index()
  68. {
  69. $data = $this->user_info;
  70. if (!$data->head_img) {
  71. $data->head_img = 'https://yueduyun.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/img/defaulthead.png';
  72. }
  73. $data['is_vip'] = 0;
  74. $data['vip_days'] = 0;
  75. $data['book_sex'] = 0;
  76. $data['phone'] = $this->phone;
  77. $year_record = YearOrderService::getRecord($this->uid);
  78. if ($year_record) {
  79. $data['is_vip'] = 1;
  80. $time = strtotime($year_record['end_time']) - time();
  81. if ($time >= 86400) {
  82. $data['vip_days'] = floor($time / 86400) . '天';
  83. } elseif ($time > 3600) {
  84. $data['vip_days'] = floor($time / 3600) . '小时';
  85. } elseif ($time > 60) {
  86. $data['vip_days'] = floor($time / 60) . '分钟';
  87. } else {
  88. $data['vip_days'] = $time . '秒';
  89. }
  90. }
  91. $data['pay_mode_default'] = 'weixin';
  92. $data['is_check'] = $data->distribution_channel_id == 9487 ? true : false;
  93. $user = (new QappUserService())->getQAppUserByUid($data['id']);
  94. if(!$user || $user->status == 0){
  95. \Log::info('user_log_off_user_info:uid:'.$user->uid);
  96. $data['balance'] = 0;
  97. $data['charge_balance'] = 0;
  98. $data['reward_balance'] = 0;
  99. $data['is_vip'] = 0;
  100. $data['vip_days'] = 0;
  101. }else{
  102. $send_order_id = isset($user->send_order_id) ? $user->send_order_id : isset($this->send_order_id) ? $this->send_order_id : 0;
  103. if($send_order_id){
  104. $send_order = SendOrder::find($send_order_id);
  105. $book = Book::leftjoin('book_categories','book_categories.id','books.category_id')
  106. ->where('books.id',$send_order->book_id)
  107. ->select('book_categories.pid')
  108. ->first();
  109. $data['book_sex'] = (isset($book->pid) && $book->pid == 1) ? $book->pid : 0;
  110. }
  111. }
  112. // $data['is_check'] = !$this->phone;
  113. \Log::info('user_info:'.$this->uid.' data:'.json_encode($data));
  114. return response()->success($data);
  115. }
  116. /**
  117. * @apiVersion 1.0.0
  118. * @apiDescription 用户签到
  119. * @api {GET} sign 用户签到
  120. * @apiHeader {String} [Authorization] token
  121. * @apiGroup User
  122. * @apiName sign
  123. * @apiSuccess {Double} fee 签到奖励
  124. * @apiSuccess {Number} days 签到天数
  125. * @apiSuccess {Array} day_list 签到列表
  126. * @apiSuccessExample {json} Success-Response:
  127. *
  128. * {
  129. * "code": 0,
  130. * "msg": "",
  131. * "data": {
  132. * "fee": 30,
  133. * "days": 1
  134. * "day_list": []
  135. * }
  136. * }
  137. */
  138. public function sign()
  139. {
  140. $result = UserSignService::sign($this->uid, date('Y-m-d'));
  141. if ($result) {
  142. return response()->success($result);
  143. }
  144. return response()->error('QAPP_SYS_ERROR');
  145. }
  146. /**
  147. * @apiVersion 1.0.0
  148. * @apiDescription 发送验证码
  149. * @api {POST} user/sendCode 发送验证码
  150. * @apiHeader {String} [Authorization] token
  151. * @apiGroup User
  152. * @apiName sendCode
  153. * @apiParam {String} phone 手机号
  154. * @apiSuccessExample {json} Success-Response:
  155. *
  156. * {
  157. * "code": 0,
  158. * "msg": "",
  159. * "data": {}
  160. * }
  161. */
  162. public function sendCode(Request $request)
  163. {
  164. $phone = $request->post('phone');
  165. if ($phone) {
  166. $code = random_int(1000, 9999);
  167. Redis::setex('quser_code:' . $phone, 120, $code);
  168. AliSMS::send($phone, 'quickapp_user_bind_phone', ['code' => $code]);
  169. return response()->success();
  170. } else {
  171. return response()->error('WAP_BIND_PHONE_EXIST');
  172. }
  173. }
  174. /**
  175. * @apiVersion 1.0.0
  176. * @apiDescription 绑定手机号
  177. * @api {POST} user/bindPhone 绑定手机号
  178. * @apiHeader {String} [Authorization] token
  179. * @apiGroup User
  180. * @apiName bindPhone
  181. * @apiParam {String} phone 手机号
  182. * @apiParam {String} code 验证码
  183. * @apiSuccessExample {json} Success-Response:
  184. *
  185. * {
  186. * "code": 0,
  187. * "msg": "",
  188. * "data": {}
  189. * }
  190. */
  191. public function bindPhone(Request $request)
  192. {
  193. $code = $request->post('code');
  194. $phone = $request->post('phone');
  195. $version = $request->post('version', '1.0');
  196. $old = Redis::get('quser_code:' . $phone);
  197. if ($old && $old == $code) {
  198. Redis::del('quser_code:' . $phone);
  199. if (!$this->phone) {
  200. $result = (new QappUserService)->bindPhone($this->uid, $phone, $version);
  201. if ($result) {
  202. return response()->success();
  203. } else {
  204. return response()->error('WAP_BIND_PHONE_EXIST');
  205. }
  206. } else {
  207. return response()->success();
  208. }
  209. } else {
  210. return response()->error('WAP_SEND_CODE_ERROR');
  211. }
  212. }
  213. /**
  214. * @apiVersion 1.0.0
  215. * @apiDescription 用户签到记录
  216. * @api {GET} user/sign_record 用户签到记录
  217. * @apiGroup User
  218. * @apiName signRecord
  219. * @apiParam {String} date 查询日期每个月一号
  220. * @apiSuccess {int} code 状态码
  221. * @apiSuccess {String} msg 信息
  222. * @apiSuccess {object} data 结果集
  223. * @apiSuccess {sign_status} data.sign_status .
  224. * @apiSuccess {sign_result} data.sign_result .
  225. * @apiSuccess {sign_today} data.sign_today .
  226. * @apiParam {page} page
  227. * @apiSuccessExample {json} Success-Response:
  228. *
  229. * {
  230. * code: 0,
  231. * msg: "",
  232. * data: {
  233. * "sign_status": true,
  234. * "sign_result": {
  235. * "list": [
  236. * {
  237. * "reward": 30,
  238. * "sign_time": "2019-11-01 14:20:30"
  239. * }
  240. * ],
  241. * "meta": {
  242. * "total": 1,
  243. * "per_page": 15,
  244. * "current_page": 1,
  245. * "last_page": 1,
  246. * "next_page_url": "",
  247. * "prev_page_url": ""
  248. * }
  249. * },
  250. * "sign_today": {
  251. * "uid": 162261523,
  252. * "price": 50,
  253. * "day": "2019-11-01",
  254. * "sign_time": "2019-11-01 09:04:43",
  255. * "created_at": "2019-11-01 09:04:43",
  256. * "updated_at": "2019-11-01 09:04:43",
  257. * "reward": 50
  258. * }
  259. * }
  260. */
  261. public function signRecord(Request $request)
  262. {
  263. $month = $request->get('date', date('Y-m-01'));
  264. $page_size = $request->get('page_size', 15);
  265. $sign_result = paginationTransform(new SignRecordTransformer(), UserSignService::getUserSignRecord($this->uid, $month, $page_size));
  266. $sign_status = UserSignService::isSign($this->uid);
  267. $sign_today = [];
  268. if ($sign_status) {
  269. $sign_today = ReadRecordService::getByField($this->uid, 'sign_info');
  270. if ($sign_today) $sign_today = json_decode($sign_today, 1);
  271. isset($sign_today['sign_time']) && $sign_today['sign_time'] = date('Y-m-d H:i:s', $sign_today['sign_time']);
  272. isset($sign_today['sign_at']) && $sign_today['sign_time'] = $sign_today['sign_at'];
  273. isset($sign_today['price']) && $sign_today['reward'] = $sign_today['price'];
  274. }
  275. $result = [
  276. 'sign_status' => $sign_status,
  277. 'sign_result' => $sign_result,
  278. 'sign_today' => $sign_today
  279. ];
  280. return response()->success($result);
  281. }
  282. /**
  283. * @apiVersion 1.0.0
  284. * @apiDescription 催更
  285. * @api {POST} user/urgeUpdate 催更
  286. * @apiHeader {String} [Authorization] token
  287. * @apiGroup User
  288. * @apiName urgeUpdate
  289. * @apiParam {String} bid 书号
  290. * @apiSuccessExample {json} Success-Response:
  291. *
  292. * {
  293. * "code": 0,
  294. * "msg": "",
  295. * "data": {}
  296. * }
  297. */
  298. public function urgeUpdate(Request $request)
  299. {
  300. $bid = $request->input('bid');
  301. if (empty($bid)) {
  302. return response()->error('PARAM_ERROR');
  303. }
  304. $bid = BookService::decodeBidStatic($bid);
  305. $result = BookUrgeUpdateService::UrgeRecord($this->uid, $bid);
  306. if ($result && !$result->id) {
  307. BookUrgeUpdateService::UrgeUpdate($this->uid, $bid, $result->updated_at);
  308. }
  309. return response()->success();
  310. }
  311. /**
  312. * @apiVersion 1.0.0
  313. * @apiDescription 更新派单ID
  314. * @api {GET} user/setSendOrder 更新派单ID
  315. * @apiHeader {String} [Authorization] token
  316. * @apiGroup User
  317. * @apiName setSendOrder
  318. * @apiParam {Int} send_order_id 派单ID
  319. * @apiSuccessExample {json} Success-Response:
  320. *
  321. * {
  322. * "code": 0,
  323. * "msg": "",
  324. * "data": {}
  325. */
  326. public function setSendOrder(Request $request)
  327. {
  328. $send_order_id = $request->get('send_order_id', 0);
  329. if ($send_order_id) {
  330. UserService::setUserSendOrder($this->uid, $send_order_id);
  331. }
  332. return response()->success();
  333. }
  334. /**
  335. * 加桌
  336. */
  337. public function addDesktop(Request $request)
  338. {
  339. $status = $request->get('status');
  340. if (is_numeric($status)) {
  341. UserService::qappAddDesktop($this->uid, $status);
  342. }
  343. }
  344. /**
  345. * 获取任务奖励
  346. */
  347. public function getUserTaskReward(int $id)
  348. {
  349. $service = new UserTaskService($this->uid);
  350. $result = $service->getTaskReward($id);
  351. if ($result == 1) {
  352. return response()->success(['is_sign' => true]);
  353. } else if ($result == -1) {
  354. return response()->error('REWARD_GOTTEN_ERROR');
  355. } else if ($result == 0) {
  356. return response()->error('NO_REWARD');
  357. }
  358. }
  359. /**
  360. * 任务中心
  361. */
  362. public function taskList()
  363. {
  364. $service = new UserTaskService($this->uid);
  365. $new_user_tasks = $service->findNewUserTaskList();
  366. $date_tasks = $service->findDateUserTaskList();
  367. return response()->success(compact('new_user_tasks', 'date_tasks'));
  368. }
  369. /**
  370. * 注销用户
  371. * @return mixed
  372. */
  373. public function logOff()
  374. {
  375. $service = new QappUserService();
  376. $res = $service->getLogOff($this->uid);
  377. \Log::info('user_log_off:uid:'.$this->uid.' res:'.$res);
  378. if($res){
  379. return response()->success();
  380. }
  381. return response()->error('QAPP_SYS_ERROR');
  382. }
  383. /**
  384. * 新版签到信息
  385. */
  386. public function findSignInfo()
  387. {
  388. $service = new SignService($this->uid);
  389. return response()->success($service->getSignInfo());
  390. }
  391. /**
  392. * 新版签到
  393. */
  394. public function newSign()
  395. {
  396. $service = new SignService($this->uid);
  397. $service->sign();
  398. return response()->success();
  399. }
  400. }