123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- <?php
- namespace App\Http\Controllers\QuickApp\User;
- use App\Http\Controllers\QuickApp\BaseController;
- use App\Http\Controllers\QuickApp\User\Transformers\SignRecordTransformer;
- use App\Libs\AliSMS;
- use App\Modules\Book\Models\Book;
- use App\Modules\Book\Services\BookService;
- use App\Modules\Book\Services\BookUrgeUpdateService;
- use App\Modules\SendOrder\Models\SendOrder;
- use App\Modules\Subscribe\Services\YearOrderService;
- use App\Modules\User\Models\QappPackage;
- use App\Modules\User\Services\QappUserService;
- use App\Modules\User\Services\ReadRecordService;
- use App\Modules\User\Services\SignService;
- use App\Modules\User\Services\UserService;
- use App\Modules\User\Services\UserSignService;
- use App\Modules\UserTask\Services\UserTaskService;
- use Illuminate\Http\Request;
- use App\Modules\Book\Models\BookConfig;
- use Redis;
- class UserController extends BaseController
- {
- /**
- * @apiDefine User 用户
- */
- /**
- * @apiVersion 1.0.0
- * @apiDescription 获取用户信息
- * @api {GET} userinfo 获取用户信息
- * @apiHeader {String} [Authorization] token
- * @apiGroup User
- * @apiName index
- * @apiSuccess {Number} id 用户ID.
- * @apiSuccess {String} openid 微信openid.
- * @apiSuccess {String} unionid 微信unionid.
- * @apiSuccess {Number} distribution_channel_id 分销渠道ID.
- * @apiSuccess {String} province 省份.
- * @apiSuccess {String} city 城市.
- * @apiSuccess {String} country 国家.
- * @apiSuccess {String} headimgurl 头像地址.
- * @apiSuccess {Number} send_order_id 派单ID.
- * @apiSuccess {Number=0,1} sex 性别.
- * @apiSuccess {String} balance 书币余额.
- * @apiSuccess {Int} is_vip 是否vip
- * @apiSuccess {String} vip_days 364天.
- * @apiSuccess {String} phone 手机号.
- * @apiSuccessExample {json} Success-Response:
- *
- * {
- * "code": 0,
- * "msg": "",
- * "data": {
- * "id": 56,
- * "openid": "sdfs34ssdfdsf",
- * "unionid": "SDFSD3343S",
- * "distribution_channel_id": 1212,
- * "province": "浙江省",
- * "city": "杭州",
- * "country": "中国",
- * "headimgurl": "http://..",
- * "send_order_id": 323,
- * "balance": 8956,
- * "register_time": "2017-12-12 12:12:12",
- * "phone": "12312435",
- * }
- * }
- */
- public function index(Request $request)
- {
- $package = $request->header('x-package', '');
- $data = $this->user_info;
- if (!$data->head_img) {
- $data->head_img = 'https://yueduyun.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/img/defaulthead.png';
- }
- $home_page = 'index';
- if (!empty($package)) {
- $home_page = QappPackage::where('package', $package)->value('home_page');
- }
- //快应用首页默认展示
- $data['home_page'] = empty($home_page) ? 'index' : $home_page;
- $data['is_vip'] = 0;
- $data['vip_days'] = 0;
- $data['book_sex'] = 1;
- $data['phone'] = $this->phone;
- $year_record = YearOrderService::getRecord($this->uid);
- if ($year_record) {
- $data['is_vip'] = 1;
- $time = strtotime($year_record['end_time']) - time();
- if ($time >= 86400) {
- $data['vip_days'] = floor($time / 86400) . '天';
- } elseif ($time > 3600) {
- $data['vip_days'] = floor($time / 3600) . '小时';
- } elseif ($time > 60) {
- $data['vip_days'] = floor($time / 60) . '分钟';
- } else {
- $data['vip_days'] = $time . '秒';
- }
- }
- $data['pay_mode_default'] = 'weixin';
- $data['is_check'] = $data->distribution_channel_id == 9487 ? true : false;
- $user = (new QappUserService())->getQAppUserByUid($data['id']);
- if (!$user || $user->status == 0) {
- \Log::info('user_log_off_user_info:uid:' . $user->uid);
- $data['balance'] = 0;
- $data['charge_balance'] = 0;
- $data['reward_balance'] = 0;
- $data['is_vip'] = 0;
- $data['vip_days'] = 0;
- } else {
- $send_order_id = !empty($this->send_order_id) ? $this->send_order_id : !empty($user->send_order_id) ? $user->send_order_id : 0;
- if ($send_order_id) {
- $send_order = SendOrder::find($send_order_id);
- if ($send_order) {
- $book = Book::leftjoin('book_categories', 'book_categories.id', 'books.category_id')
- ->where('books.id', $send_order->book_id)
- ->select('book_categories.pid')
- ->first();
- $data['book_sex'] = (isset($book->pid) && $book->pid == 2) ? 0 : 1;
- }
- }
- }
- // $data['is_check'] = !$this->phone;
- \Log::info('user_info:' . $this->uid . ' data:' . json_encode($data));
- return response()->success($data);
- }
- /**
- * @apiVersion 1.0.0
- * @apiDescription 用户签到
- * @api {GET} sign 用户签到
- * @apiHeader {String} [Authorization] token
- * @apiGroup User
- * @apiName sign
- * @apiSuccess {Double} fee 签到奖励
- * @apiSuccess {Number} days 签到天数
- * @apiSuccess {Array} day_list 签到列表
- * @apiSuccessExample {json} Success-Response:
- *
- * {
- * "code": 0,
- * "msg": "",
- * "data": {
- * "fee": 30,
- * "days": 1
- * "day_list": []
- * }
- * }
- */
- public function sign()
- {
- $result = UserSignService::sign($this->uid, date('Y-m-d'));
- if ($result) {
- return response()->success($result);
- }
- return response()->error('QAPP_SYS_ERROR');
- }
- /**
- * @apiVersion 1.0.0
- * @apiDescription 发送验证码
- * @api {POST} user/sendCode 发送验证码
- * @apiHeader {String} [Authorization] token
- * @apiGroup User
- * @apiName sendCode
- * @apiParam {String} phone 手机号
- * @apiSuccessExample {json} Success-Response:
- *
- * {
- * "code": 0,
- * "msg": "",
- * "data": {}
- * }
- */
- public function sendCode(Request $request)
- {
- $phone = $request->post('phone');
- if ($phone) {
- $code = random_int(1000, 9999);
- Redis::setex('quser_code:' . $phone, 120, $code);
- AliSMS::send($phone, 'quickapp_user_bind_phone', ['code' => $code]);
- return response()->success();
- } else {
- return response()->error('WAP_BIND_PHONE_EXIST');
- }
- }
- /**
- * @apiVersion 1.0.0
- * @apiDescription 绑定手机号
- * @api {POST} user/bindPhone 绑定手机号
- * @apiHeader {String} [Authorization] token
- * @apiGroup User
- * @apiName bindPhone
- * @apiParam {String} phone 手机号
- * @apiParam {String} code 验证码
- * @apiSuccessExample {json} Success-Response:
- *
- * {
- * "code": 0,
- * "msg": "",
- * "data": {}
- * }
- */
- public function bindPhone(Request $request)
- {
- $code = $request->post('code');
- $phone = $request->post('phone');
- $version = $request->post('version', '1.0');
- $old = Redis::get('quser_code:' . $phone);
- if ($old && $old == $code) {
- Redis::del('quser_code:' . $phone);
- if (!$this->phone) {
- $result = (new QappUserService)->bindPhone($this->uid, $phone, $version);
- if ($result) {
- return response()->success();
- } else {
- return response()->error('WAP_BIND_PHONE_EXIST');
- }
- } else {
- return response()->success();
- }
- } else {
- return response()->error('WAP_SEND_CODE_ERROR');
- }
- }
- /**
- * @apiVersion 1.0.0
- * @apiDescription 用户签到记录
- * @api {GET} user/sign_record 用户签到记录
- * @apiGroup User
- * @apiName signRecord
- * @apiParam {String} date 查询日期每个月一号
- * @apiSuccess {int} code 状态码
- * @apiSuccess {String} msg 信息
- * @apiSuccess {object} data 结果集
- * @apiSuccess {sign_status} data.sign_status .
- * @apiSuccess {sign_result} data.sign_result .
- * @apiSuccess {sign_today} data.sign_today .
- * @apiParam {page} page
- * @apiSuccessExample {json} Success-Response:
- *
- * {
- * code: 0,
- * msg: "",
- * data: {
- * "sign_status": true,
- * "sign_result": {
- * "list": [
- * {
- * "reward": 30,
- * "sign_time": "2019-11-01 14:20:30"
- * }
- * ],
- * "meta": {
- * "total": 1,
- * "per_page": 15,
- * "current_page": 1,
- * "last_page": 1,
- * "next_page_url": "",
- * "prev_page_url": ""
- * }
- * },
- * "sign_today": {
- * "uid": 162261523,
- * "price": 50,
- * "day": "2019-11-01",
- * "sign_time": "2019-11-01 09:04:43",
- * "created_at": "2019-11-01 09:04:43",
- * "updated_at": "2019-11-01 09:04:43",
- * "reward": 50
- * }
- * }
- */
- public function signRecord(Request $request)
- {
- $month = $request->get('date', date('Y-m-01'));
- $page_size = $request->get('page_size', 15);
- $sign_result = paginationTransform(new SignRecordTransformer(), UserSignService::getUserSignRecord($this->uid, $month, $page_size));
- $sign_status = UserSignService::isSign($this->uid);
- $sign_today = [];
- if ($sign_status) {
- $sign_today = ReadRecordService::getByField($this->uid, 'sign_info');
- if ($sign_today) $sign_today = json_decode($sign_today, 1);
- isset($sign_today['sign_time']) && $sign_today['sign_time'] = date('Y-m-d H:i:s', $sign_today['sign_time']);
- isset($sign_today['sign_at']) && $sign_today['sign_time'] = $sign_today['sign_at'];
- isset($sign_today['price']) && $sign_today['reward'] = $sign_today['price'];
- }
- $result = [
- 'sign_status' => $sign_status,
- 'sign_result' => $sign_result,
- 'sign_today' => $sign_today
- ];
- return response()->success($result);
- }
- /**
- * @apiVersion 1.0.0
- * @apiDescription 催更
- * @api {POST} user/urgeUpdate 催更
- * @apiHeader {String} [Authorization] token
- * @apiGroup User
- * @apiName urgeUpdate
- * @apiParam {String} bid 书号
- * @apiSuccessExample {json} Success-Response:
- *
- * {
- * "code": 0,
- * "msg": "",
- * "data": {}
- * }
- */
- public function urgeUpdate(Request $request)
- {
- $bid = $request->input('bid');
- if (empty($bid)) {
- return response()->error('PARAM_ERROR');
- }
- $bid = BookService::decodeBidStatic($bid);
- $result = BookUrgeUpdateService::UrgeRecord($this->uid, $bid);
- if ($result && !$result->id) {
- BookUrgeUpdateService::UrgeUpdate($this->uid, $bid, $result->updated_at);
- }
- return response()->success();
- }
- /**
- * @apiVersion 1.0.0
- * @apiDescription 更新派单ID
- * @api {GET} user/setSendOrder 更新派单ID
- * @apiHeader {String} [Authorization] token
- * @apiGroup User
- * @apiName setSendOrder
- * @apiParam {Int} send_order_id 派单ID
- * @apiSuccessExample {json} Success-Response:
- *
- * {
- * "code": 0,
- * "msg": "",
- * "data": {}
- */
- public function setSendOrder(Request $request)
- {
- $send_order_id = $request->get('send_order_id', 0);
- if ($send_order_id) {
- UserService::setUserSendOrder($this->uid, $send_order_id);
- }
- return response()->success();
- }
- /**
- * 加桌
- */
- public function addDesktop(Request $request)
- {
- $status = $request->get('status');
- if (is_numeric($status)) {
- UserService::qappAddDesktop($this->uid, $status);
- }
- }
- /**
- * 获取任务奖励
- */
- public function getUserTaskReward(int $id)
- {
- $service = new UserTaskService($this->uid);
- $result = $service->getTaskReward($id);
- if ($result == 1) {
- return response()->success(['is_sign' => true]);
- } else if ($result == -1) {
- return response()->error('REWARD_GOTTEN_ERROR');
- } else if ($result == 0) {
- return response()->error('NO_REWARD');
- }
- }
- /**
- * 任务中心
- */
- public function taskList()
- {
- $list = $this->getTaskList();
- return response()->success();
- }
- /**
- * 注销用户
- * @return mixed
- */
- public function logOff()
- {
- $service = new QappUserService();
- $res = $service->getLogOff($this->uid);
- \Log::info('user_log_off:uid:' . $this->uid . ' res:' . $res);
- if ($res) {
- return response()->success();
- }
- return response()->error('QAPP_SYS_ERROR');
- }
- /**
- * 新版签到信息
- */
- public function findSignInfo()
- {
- $service = new SignService($this->uid);
- $reward_list = $this->get_sign_reward_list($this->distribution_channel_id);
- $service->setRewardList($reward_list);
- return response()->success($service->getSignInfo());
- }
- /**
- * 新版签到
- */
- public function newSign()
- {
- $service = new SignService($this->uid);
- $reward_list = $this->get_sign_reward_list($this->distribution_channel_id);
- $service->setRewardList($reward_list);
- $service->sign();
- return response()->success();
- }
- /**
- * 奖励
- * name: get_sign_reward_list
- * @param $distribution_channel_id
- * @return int[]
- * date 2022/10/11 11:56
- */
- private function get_sign_reward_list($distribution_channel_id)
- {
- $list = [30, 35, 40, 45, 50, 55, 60];
- // 琥珀阅读签到奖励独立
- if ($distribution_channel_id == 14903) {
- $list = [30, 88, 188, 30, 88, 188, 188];
- }
- return $list;
- }
- /**
- * 是否有可领取的任务包括签到
- * name: hasPendingTask
- * date 2023/02/22 14:15
- */
- public function hasPendingTask(Request $request)
- {
- $show = true;
- try {
- $type = $request->input('type', "home");
- $service = new SignService($this->uid);
- $has_sign_today = $service->hasSignToday();
- if ($has_sign_today) {
- // 签到找查看是否有可领取的任务
- $show = $this->checkTask($type);
- }
- } catch (\Exception $exception) {
- $show = true;
- }
- return response()->success(['has_pend_task' => $show]);
- }
- /**
- * 是否有可以领取的任务
- * name: checkTask
- * @return bool
- * date 2023/02/22 15:11
- */
- private function checkTask($type= "home")
- {
- $list = $this->getTaskList();
- $task = $list['new_user_tasks'] ?? [];
- $show = $this->checkTaskItem($task,$type);
- if ($show){
- return $show;
- }
- $task = $list['date_tasks'] ?? [];
- return $this->checkTaskItem($task);
- }
- /**
- * 获取任务列表
- * name: getTaskList
- * @return array
- * date 2023/02/22 15:10
- */
- private function getTaskList()
- {
- $service = new UserTaskService($this->uid);
- $new_user_tasks = $service->findNewUserTaskList();
- $date_tasks = $service->findDateUserTaskList();
- // 琥珀阅读去除加桌任务
- if ($this->distribution_channel_id == 14903 && $new_user_tasks) {
- foreach ($new_user_tasks as $key => $val) {
- if ($val['code'] == "add_desk") {
- unset($new_user_tasks[$key]);
- }
- }
- unset($val);
- }
- $new_user_tasks = array_values($new_user_tasks->toArray());
- return compact('new_user_tasks', 'date_tasks');
- }
- /**
- * 检测是否有可以领取项
- * name: checkTaskItem
- * @param $task
- * @return bool
- * date 2023/02/22 15:10
- */
- private function checkTaskItem($task,$type)
- {
- $show = false;
- foreach ($task as $value){
- if ($type == 'home'){
- if (isset($value['status']) && $value['status'] < 2){
- $show = true;
- break ;
- }
- }else if ($type == 'recharge'){
- if (isset($value['status']) && $value['status'] == 1){
- $show = true;
- break ;
- }
- }
- }
- return $show;
- }
- }
|