UserController.php 13 KB

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