UserController.php 12 KB

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