UserController.php 13 KB

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