UserController.php 12 KB

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