UserController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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\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. $version = $request->post('version', '1.0');
  172. $old = Redis::get('quser_code:' . $phone);
  173. if ($old && $old == $code) {
  174. Redis::del('quser_code:' . $phone);
  175. if (!$this->phone) {
  176. $result = (new QappUserService)->bindPhone($this->uid, $phone, $version);
  177. if ($result) {
  178. return response()->success();
  179. } else {
  180. return response()->error('WAP_BIND_PHONE_EXIST');
  181. }
  182. } else {
  183. return response()->success();
  184. }
  185. } else {
  186. return response()->error('WAP_SEND_CODE_ERROR');
  187. }
  188. }
  189. /**
  190. * @apiVersion 1.0.0
  191. * @apiDescription 用户签到记录
  192. * @api {GET} user/sign_record 用户签到记录
  193. * @apiGroup User
  194. * @apiName signRecord
  195. * @apiParam {String} date 查询日期每个月一号
  196. * @apiSuccess {int} code 状态码
  197. * @apiSuccess {String} msg 信息
  198. * @apiSuccess {object} data 结果集
  199. * @apiSuccess {sign_status} data.sign_status .
  200. * @apiSuccess {sign_result} data.sign_result .
  201. * @apiSuccess {sign_today} data.sign_today .
  202. * @apiParam {page} page
  203. * @apiSuccessExample {json} Success-Response:
  204. *
  205. * {
  206. * code: 0,
  207. * msg: "",
  208. * data: {
  209. * "sign_status": true,
  210. * "sign_result": {
  211. * "list": [
  212. * {
  213. * "reward": 30,
  214. * "sign_time": "2019-11-01 14:20:30"
  215. * }
  216. * ],
  217. * "meta": {
  218. * "total": 1,
  219. * "per_page": 15,
  220. * "current_page": 1,
  221. * "last_page": 1,
  222. * "next_page_url": "",
  223. * "prev_page_url": ""
  224. * }
  225. * },
  226. * "sign_today": {
  227. * "uid": 162261523,
  228. * "price": 50,
  229. * "day": "2019-11-01",
  230. * "sign_time": "2019-11-01 09:04:43",
  231. * "created_at": "2019-11-01 09:04:43",
  232. * "updated_at": "2019-11-01 09:04:43",
  233. * "reward": 50
  234. * }
  235. * }
  236. */
  237. public function signRecord(Request $request)
  238. {
  239. $month = $request->get('date', date('Y-m-01'));
  240. $page_size = $request->get('page_size', 15);
  241. $sign_result = paginationTransform(new SignRecordTransformer(), UserSignService::getUserSignRecord($this->uid, $month, $page_size));
  242. $sign_status = UserSignService::isSign($this->uid);
  243. $sign_today = [];
  244. if ($sign_status) {
  245. $sign_today = ReadRecordService::getByField($this->uid, 'sign_info');
  246. if ($sign_today) $sign_today = json_decode($sign_today, 1);
  247. isset($sign_today['sign_time']) && $sign_today['sign_time'] = date('Y-m-d H:i:s', $sign_today['sign_time']);
  248. isset($sign_today['sign_at']) && $sign_today['sign_time'] = $sign_today['sign_at'];
  249. isset($sign_today['price']) && $sign_today['reward'] = $sign_today['price'];
  250. }
  251. $result = [
  252. 'sign_status' => $sign_status,
  253. 'sign_result' => $sign_result,
  254. 'sign_today' => $sign_today
  255. ];
  256. return response()->success($result);
  257. }
  258. /**
  259. * @apiVersion 1.0.0
  260. * @apiDescription 催更
  261. * @api {POST} user/urgeUpdate 催更
  262. * @apiHeader {String} [Authorization] token
  263. * @apiGroup User
  264. * @apiName urgeUpdate
  265. * @apiParam {String} bid 书号
  266. * @apiSuccessExample {json} Success-Response:
  267. *
  268. * {
  269. * "code": 0,
  270. * "msg": "",
  271. * "data": {}
  272. * }
  273. */
  274. public function urgeUpdate(Request $request)
  275. {
  276. $bid = $request->input('bid');
  277. if (empty($bid)) {
  278. return response()->error('PARAM_ERROR');
  279. }
  280. $bid = BookService::decodeBidStatic($bid);
  281. $result = BookUrgeUpdateService::UrgeRecord($this->uid, $bid);
  282. if ($result && !$result->id) {
  283. BookUrgeUpdateService::UrgeUpdate($this->uid, $bid, $result->updated_at);
  284. }
  285. return response()->success();
  286. }
  287. /**
  288. * @apiVersion 1.0.0
  289. * @apiDescription 更新派单ID
  290. * @api {GET} user/setSendOrder 更新派单ID
  291. * @apiHeader {String} [Authorization] token
  292. * @apiGroup User
  293. * @apiName setSendOrder
  294. * @apiParam {Int} send_order_id 派单ID
  295. * @apiSuccessExample {json} Success-Response:
  296. *
  297. * {
  298. * "code": 0,
  299. * "msg": "",
  300. * "data": {}
  301. */
  302. public function setSendOrder(Request $request)
  303. {
  304. $send_order_id = $request->get('send_order_id', 0);
  305. if ($send_order_id) {
  306. UserService::setUserSendOrder($this->uid, $send_order_id);
  307. }
  308. return response()->success();
  309. }
  310. /**
  311. * 加桌
  312. */
  313. public function addDesktop(Request $request)
  314. {
  315. $status = $request->get('status');
  316. if (is_numeric($status)) {
  317. UserService::qappAddDesktop($this->uid, $status);
  318. }
  319. }
  320. /**
  321. * 获取任务奖励
  322. */
  323. public function getUserTaskReward(int $id)
  324. {
  325. $service = new UserTaskService($this->uid);
  326. $result = $service->getTaskReward($id);
  327. if ($result == 1) {
  328. return response()->success(['is_sign' => true]);
  329. } else if ($result == -1) {
  330. return response()->error('REWARD_GOTTEN_ERROR');
  331. } else if ($result == 0) {
  332. return response()->error('NO_REWARD');
  333. }
  334. }
  335. /**
  336. * 任务中心
  337. */
  338. public function taskList()
  339. {
  340. $service = new UserTaskService($this->uid);
  341. $new_user_tasks = $service->findNewUserTaskList();
  342. $date_tasks = $service->findDateUserTaskList();
  343. return response()->success(compact('new_user_tasks', 'date_tasks'));
  344. }
  345. /**
  346. * 新版签到信息
  347. */
  348. public function findSignInfo()
  349. {
  350. $service = new SignService($this->uid);
  351. return response()->success($service->getSignInfo());
  352. }
  353. /**
  354. * 新版签到
  355. */
  356. public function newSign()
  357. {
  358. $service = new SignService($this->uid);
  359. $service->sign();
  360. return response()->success();
  361. }
  362. }