UserController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. namespace App\Http\Controllers\Wap\User;
  3. use App\Libs\AliSMS;
  4. use App\Modules\Book\Services\BookConfigService;
  5. use App\Modules\Book\Services\BookUrgeUpdateService;
  6. use App\Modules\Book\Services\SignBookService;
  7. use App\Modules\Statistic\Services\AdVisitStatService;
  8. use App\Modules\Subscribe\Services\OrderService;
  9. use App\Modules\User\Services\ForceGuidePersonAccountService;
  10. use App\Modules\User\Services\ReadRecordService;
  11. use App\Modules\User\Services\UserBindPhoneService;
  12. use App\Modules\User\Services\UserWealthyOperateRecordService;
  13. use App\Modules\User\Services\WapReaderPageFissionService;
  14. use Illuminate\Http\Request;
  15. use App\Http\Controllers\Wap\BaseController;
  16. use App\Modules\Subscribe\Services\YearOrderService;
  17. use App\Modules\User\Services\UserService;
  18. use App\Modules\User\Services\UserRandSignService;
  19. use App\Modules\User\Services\UserSignService;
  20. use App\Http\Controllers\Wap\User\Transformers\SignRecordTransformer;
  21. use Log;
  22. use Redis;
  23. use Cookie;
  24. use DB;
  25. use Hashids;
  26. class UserController extends BaseController
  27. {
  28. /**
  29. * @apiDefine User 用户
  30. */
  31. /**
  32. * @apiVersion 1.0.0
  33. * @apiDescription 获取用户信息
  34. * @api {GET} userinfo 获取用户信息
  35. * @apiGroup User
  36. * @apiName index
  37. * @apiSuccess {Number} id 用户ID.
  38. * @apiSuccess {String} openid 微信openid.
  39. * @apiSuccess {String} unionid 微信unionid.
  40. * @apiSuccess {Number} distribution_channel_id 分销渠道ID.
  41. * @apiSuccess {String} province 省份.
  42. * @apiSuccess {String} city 城市.
  43. * @apiSuccess {String} country 国家.
  44. * @apiSuccess {String} headimgurl 头像地址.
  45. * @apiSuccess {Number} send_order_id 派单ID.
  46. * @apiSuccess {Number=0,1} sex 性别.
  47. * @apiSuccess {String} balance 书币余额.
  48. * @apiSuccess {Int} is_vip 是否vip
  49. * @apiSuccess {String} vip_days 364天.
  50. * @apiSuccessExample {json} Success-Response:
  51. *
  52. * {
  53. * "code": 0,
  54. * "msg": "",
  55. * "data": {
  56. * "id": 56,
  57. * "openid": "sdfs34ssdfdsf",
  58. * "unionid": "SDFSD3343S",
  59. * "distribution_channel_id": 1212,
  60. * "province": "浙江省",
  61. * "city": "杭州",
  62. * "country": "中国",
  63. * "headimgurl": "http://.."
  64. * "send_order_id": 323
  65. * "balance": 8956
  66. * "register_time": "2017-12-12 12:12:12"
  67. * }
  68. * }
  69. */
  70. public function index(){
  71. if(!$this->checkUid()){
  72. return response()->error('NOT_LOGIN');
  73. }
  74. $data = UserService::getById($this->uid);
  75. $bind_info = UserBindPhoneService::bindInfo($this->uid);
  76. if($bind_info){
  77. $data['bind'] = 1;
  78. $data['bind_phone'] = $bind_info->phone;
  79. }else{
  80. $data['bind'] = 0;
  81. $data['bind_phone'] = '';
  82. }
  83. $data['is_vip'] = 0;
  84. $data['vip_days'] = 0;
  85. $year_record = YearOrderService::getRecord($this->uid);
  86. if($year_record){
  87. $data['is_vip'] = 1;
  88. $time = strtotime($year_record['end_time'])-time();
  89. if($time>=86400){
  90. $data['vip_days'] = floor($time/86400).'天';
  91. }elseif ($time>3600) {
  92. $data['vip_days'] = floor($time/3600).'小时';
  93. }elseif ($time>60) {
  94. $data['vip_days'] = floor($time/60).'分钟';
  95. }else{
  96. $data['vip_days'] = $time.'秒';
  97. }
  98. }
  99. $data['is_all_life'] = 0;
  100. $activity_id = env('FOREVER_ACTIVITY_ID');
  101. if($activity_id){
  102. if(OrderService::userIsParticipateActivity($this->uid,$activity_id)){
  103. $data['is_vip'] = 0 ;
  104. $data['vip_days'] = '99年';
  105. $data['is_all_life'] = 1;
  106. }
  107. }
  108. return response()->success($data);
  109. }
  110. /**
  111. * @apiVersion 1.0.0
  112. * @apiDescription 用户签到记录
  113. * @api {GET} user/sign_record 用户签到记录
  114. * @apiGroup User
  115. * @apiName signRecord
  116. * @apiSuccess {int} code 状态码
  117. * @apiSuccess {String} msg 信息
  118. * @apiSuccess {object} data 结果集
  119. * @apiSuccess {reward} data.reward 奖励金额.
  120. * @apiSuccess {sign_time} data.sign_time 签到时间.
  121. * @apiParam {page} page
  122. * @apiSuccessExample {json} Success-Response:
  123. *
  124. * {
  125. * code: 0,
  126. * msg: "",
  127. * data: {
  128. * list: [
  129. * {
  130. * reward: 50,
  131. * sign_time: "2018-03-20 13:43:11"
  132. * },
  133. * {
  134. * reward: 50,
  135. * sign_time: "2018-01-18 16:22:33"
  136. * },
  137. * ],
  138. * meta: {
  139. * total: 12,
  140. * per_page: 15,
  141. * current_page: 1,
  142. * last_page: 1,
  143. * next_page_url: "",
  144. * prev_page_url: ""
  145. * }
  146. * }
  147. * }
  148. */
  149. public function signRecord(Request $request){
  150. $sign_result = paginationTransform(new SignRecordTransformer(),UserSignService::getUserSignRecord($this->uid));
  151. $sign_status = UserSignService::isSign($this->uid);
  152. $sign_today = [];
  153. if($sign_status){
  154. $sign_today = ReadRecordService::getByField($this->uid,'sign_info');
  155. if($sign_today) $sign_today = json_decode($sign_today,1);
  156. isset($sign_today['sign_time']) && $sign_today['sign_time'] = date('Y-m-d H:i:s',$sign_today['sign_time']);
  157. isset($sign_today['sign_at']) && $sign_today['sign_time'] = $sign_today['sign_at'];
  158. isset($sign_today['price']) && $sign_today['reward'] = $sign_today['price'];
  159. }
  160. $result = [
  161. 'sign_status'=>$sign_status,
  162. 'sign_result'=>$sign_result,
  163. 'sign_today'=>$sign_today
  164. ];
  165. return response()->success($result);
  166. }
  167. public function getCoupons(Request $request){
  168. $activity_id = $request->input('activity_id');
  169. if(!DB::table('discount_coupons')->where('uid',$this->uid)->where('activity_id',$activity_id)->count()){
  170. DB::table('discount_coupons')->insert([
  171. 'uid'=>$this->uid,
  172. 'activity_id'=>$activity_id,
  173. 'created_at'=>date('Y-m-d H:i:s'),
  174. 'updated_at'=>date('Y-m-d H:i:s')
  175. ]);
  176. }
  177. return response()->success();
  178. }
  179. public function sign(Request $request){
  180. //sign_days
  181. //$day = date('Y-m-d');
  182. $sign = UserSignService::isSign($this->uid);
  183. $sign_count = ReadRecordService::getSignCount($this->uid);
  184. if($sign){
  185. //如果已经签过到
  186. if(!$sign_count){
  187. ReadRecordService::setSignCount($this->uid,1);
  188. ReadRecordService::setSignDay($this->uid);
  189. $sign_count = 1;
  190. }
  191. $fee = 30;
  192. if($sign_count>=3){
  193. $fee = 50;
  194. }
  195. $data = ['sign_days'=>$sign_count,'sign_reard'=>$fee,'status'=>1];
  196. return response()->success($data);
  197. }
  198. //还没有签到
  199. $status = UserSignService::signToday($this->uid);
  200. $sign_count = ReadRecordService::getSignCount($this->uid);
  201. $data = ['sign_days'=>$sign_count,'sign_reard'=>$status,'status'=>0];
  202. return response()->success($data);
  203. }
  204. /**
  205. * @return string
  206. */
  207. public function signi()
  208. {
  209. /*if(in_array($this->distribution_channel_id,explode(',',redisEnv('NEW_SIGN_CHANNELS','')))){
  210. $version = UserSignService::getUserSignVersion($this->uid);
  211. }else{
  212. $version = 'v1';
  213. }*/
  214. $version = UserSignService::getUserSignVersion($this->uid);
  215. if($version == 'v1'){
  216. $page = 'wap.sign';
  217. }else{
  218. $page = 'wap.signv2';
  219. }
  220. $book1= $book2 = null;
  221. if($version == 'v2' ){
  222. $sex = $this->_user_info->sex;
  223. $sex = $sex?$sex:2;
  224. $book1 = BookConfigService::getRandomOneHighQualityBook($sex);
  225. $book1->url = sprintf('/reader?bid=%s&cid=%s&source=wechatmsg&fromtype=sign_recommend',Hashids::encode($book1->bid),$book1->first_cid);
  226. $book2 = SignBookService::getRandomBook($sex);
  227. \Log::info($book2);
  228. foreach ($book2 as $item){
  229. $item->url = sprintf('/reader?bid=%s&cid=%s&source=wechatmsg&fromtype=sign_recommend',Hashids::encode($item->bid),$item->first_cid);
  230. }
  231. }
  232. $tomorrow_pool = $fee_pool = [0,30,50,120,50,50,50,150];
  233. $day = [0,'一','二','三','四','五','六','七'];
  234. list($sign,$sign_count) = ReadRecordService::getByMultiField($this->uid,'sign_day','sign_counts');
  235. if ($sign && $sign == date('Y-m-d')) {
  236. if($version == 'v1'){
  237. $fee = $sign_count>=3 ? 50:30;
  238. }else{
  239. if ($sign_count % 7 == 1 && $sign_count<=7) {
  240. $fee = 30;
  241. } elseif ($sign_count % 7 == 3) {
  242. $fee = 120;
  243. } elseif ($sign_count % 7 == 0) {
  244. $fee = 150;
  245. } else {
  246. $fee = 50;
  247. }
  248. }
  249. if($sign_count >=8){
  250. $fee_pool[1] = 50;
  251. }
  252. $tomorrow = (($sign_count+1) % 7 == 0? 7:($sign_count+1) % 7);
  253. if($sign_count+1 >=8){
  254. $tomorrow_pool[1] = 50;
  255. }
  256. //签过到了
  257. $data = ['sign_count'=>$sign_count,'fee'=>$fee,'fee_pool'=>$fee_pool,'day'=>$day,'book1'=>$book1,'book2'=>$book2,'tomorrow_fee'=>$tomorrow_pool[$tomorrow]];
  258. return view($page,$data);
  259. }
  260. $fee = UserSignService::signToday($this->uid,$version);
  261. if(!$fee){
  262. return response()->error('WAP_SYS_ERROR');
  263. }
  264. $sign_count = ReadRecordService::getSignCount($this->uid);
  265. if($sign_count >=8){
  266. $fee_pool[1] = 50;
  267. }
  268. if($sign_count+1 >=8){
  269. $tomorrow_pool[1] = 50;
  270. }
  271. $tomorrow = (($sign_count+1) % 7 == 0? 7:($sign_count+1) % 7);
  272. $data = ['sign_count'=>$sign_count,'fee'=>$fee,'fee_pool'=>$fee_pool,'day'=>$day,'book1'=>$book1,'book2'=>$book2,'tomorrow_fee'=>$tomorrow_pool[$tomorrow]];
  273. return view($page,$data);
  274. }
  275. public function recordShare(Request $request){
  276. if(!$this->checkUid()){
  277. return response()->error('NOT_LOGIN');
  278. }
  279. $uid = $this->uid;
  280. $distribution_channel_id = $this->distribution_channel_id;
  281. $type = 'click';
  282. $bid = $request->get('bid');
  283. $cid = $request->get('cid',0);
  284. if(!$bid){
  285. return response()->error('PARAM_ERROR');
  286. }
  287. !is_numeric($bid) && $bid = Hashids::decode($bid)[0];
  288. WapReaderPageFissionService::createV2($uid,$bid,$distribution_channel_id,$type,0,$cid);
  289. $user = $this->_user_info;
  290. $data = [];
  291. if(in_array($distribution_channel_id,[2,14,211]) && $user){
  292. $data['username'] = $user->nickname;
  293. $data['head_img'] = $user->head_img;
  294. $url = 'https://'._domain().'/detail?fromtype=readershare&id='.Hashids::encode($bid).'&fromflag='.$uid;
  295. //$url = str_replace('http://', $h5_scheme . '://', url()->current() . '?' . http_build_query($params));
  296. $data['share_url'] = $url;
  297. }
  298. return response()->success($data);
  299. }
  300. /**
  301. * @apiVersion 1.0.0
  302. * @apiDescription 用户点击广告统计
  303. * @api {post} user/advisitstat 用户点击广告统计
  304. * @apiGroup User
  305. * @apiName signRecord
  306. * @apiParam {Int} bid bid
  307. * @apiParam {Int} cid cid
  308. * @apiParam {Int} type type类型(CLICK|UNLOCK)
  309. * @apiSuccess {int} code 状态码
  310. * @apiSuccess {String} msg 信息
  311. * @apiSuccess {object} data 结果集
  312. * @apiParam {page} page
  313. * @apiSuccessExample {json} Success-Response:
  314. *
  315. * {
  316. * code: 0,
  317. * msg: "",
  318. * data: {
  319. * {
  320. * reward: 50,
  321. * sign_time: "2018-03-20 13:43:11"
  322. * },
  323. * {
  324. * reward: 50,
  325. * sign_time: "2018-01-18 16:22:33"
  326. * },
  327. * }
  328. * }
  329. */
  330. public function adVisitStat(Request $request){
  331. $bid = $request->get('bid');
  332. $cid = $request->get('cid',0);
  333. $type = $request->get('type');
  334. if(!$bid || !$type){
  335. return response()->error('PARAM_ERROR');
  336. }
  337. !is_numeric($bid) && $bid = Hashids::decode($bid)[0];
  338. AdVisitStatService::create($this->uid,$bid,$cid,$type);
  339. return response()->success();
  340. }
  341. public function urgeUpdate(Request $request){
  342. $bid = $request->get('bid');
  343. if(!$bid){
  344. return response()->error('PARAM_ERROR');
  345. }
  346. $bid = Hashids::decode($bid)[0];
  347. $result = BookUrgeUpdateService::UrgeRecord($this->uid,$bid);
  348. if($result && !$result->id){
  349. BookUrgeUpdateService::UrgeUpdate($this->uid,$bid,$result->updated_at);
  350. }
  351. return response()->success();
  352. }
  353. function logout(Request $request,$channel_id,$domain)
  354. {
  355. //echo $channel_id.'----';
  356. //echo $domain;
  357. $domains = ['zhuishuyun','66kshu','iycdm','leyuee'];
  358. setcookie(env('COOKIE_AUTH_WEB_WECHAT'), '', -1);
  359. setcookie('u', '', -1);
  360. setcookie('force_show_qrcode', '', -1);
  361. setcookie('sub_random_num', '', -1);
  362. setcookie('cpc_ad_status', '', -1);
  363. //return response('logout');
  364. $param = $request->except('_url');
  365. $url_format = '%s://site%s.%s.com/logout?%s';
  366. if(in_array($domain,$domains)){
  367. $i = 0;
  368. foreach ($domains as $k=>$v){
  369. if($v == $domain){
  370. $i = $k;
  371. }
  372. }
  373. if(isset($domains[$i+1])){
  374. $link = sprintf($url_format,env('PROTOCOL'),$channel_id,$domains[$i+1],http_build_query($param)) ;
  375. return redirect()->to($link);
  376. }
  377. }
  378. return view('help.logout');
  379. }
  380. function setOrderDelCookie(Request $request){
  381. $param = $request->except('_url');
  382. $type = isset($param['type']) ?$param['type']:'set';
  383. foreach ($param as $k=>$v){
  384. if($k == 'type') continue;
  385. if($type == 'set'){
  386. Cookie::queue($k, $v, 1000, null, null, false, false);
  387. }else{
  388. setcookie($k, '', -1);
  389. }
  390. }
  391. return response('ok');
  392. }
  393. function test_add_user_login_cookie(Request $request)
  394. {
  395. $uid = $request->get('uid');
  396. \Log::info('test_add_user_login_cookie:'.$uid);
  397. Cookie::queue(env('COOKIE_AUTH_WEB_WECHAT'), $uid, env('U_COOKIE_EXPIRE'), null, null, false, false);
  398. return response('add_cookie:'.$uid);
  399. }
  400. //日常随机签到
  401. function day_rand_sign(Request $request)
  402. {
  403. $uid = $this->uid;
  404. $day = date('Y-m-d');
  405. $hasSigned = UserRandSignService::hasSigned(compact('uid','day'));
  406. $fee = 0;
  407. if(!$hasSigned)
  408. {
  409. $fee = mt_rand(5,15);
  410. UserRandSignService::sign(compact('uid','day','fee'));
  411. }
  412. return view('wap.rand_sign',compact('hasSigned','fee'));
  413. }
  414. public function sendCode(Request $request){
  415. $phone = $request->post('phone');
  416. $code = random_int(1000,9999);
  417. Redis::setex('code:'.$phone,'120',$code);
  418. AliSMS::send($phone, 'paid_user_bind_phone', ['code'=>$code]);
  419. return response()->success();
  420. }
  421. public function bindPhone(Request $request){
  422. $code = $request->post('code');
  423. $phone = $request->post('phone');
  424. $old = Redis::get('code:'.$phone);
  425. if($old && $old == $code){
  426. $user_info = UserService::getById($this->uid);
  427. if(!$user_info){
  428. return response()->error();
  429. }
  430. try{
  431. $result = UserBindPhoneService::bind($this->uid,$user_info->openid,$phone);
  432. if($result == 0){
  433. UserService::addBalance($this->uid,100,0,100);
  434. UserWealthyOperateRecordService::create($this->uid,100,$this->distribution_channel_id,'bind_phone');
  435. }
  436. if($result == -1){
  437. return response()->error('WAP_BIND_PHONE_EXIST');
  438. }
  439. if($result == -2){
  440. return response()->error('WAP_SEND_OPENID_EXIST');
  441. }
  442. }catch (\Exception $e){}
  443. return response()->success();
  444. }else{
  445. return response()->error('WAP_SEND_CODE_ERROR');
  446. }
  447. }
  448. public function bindPhoneView(Request $request){
  449. $from = 'personal';
  450. $order = '';
  451. $url = '/personal';
  452. return view('pay.order.bindPhone', compact('order', 'url','from'));
  453. }
  454. public function guidePersonalAccount(Request $request){
  455. $bid = $request->get('bid');
  456. $cid = $request->get('cid');
  457. $prev_cid = $request->get('prev_cid');
  458. $book_name = $request->get('book_name');
  459. $bid_no = Hashids::decode($bid)[0];
  460. $env_config = redisEnvMulti('FORCE_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE','FORCE_GUIDE_PERSONAL_ACCOUNT_MAX_UV',
  461. 'FORCE_GUIDE_PERSONAL_ACCOUNT_ID','FORCE_GUIDE_PERSONAL_ACCOUNT_MAX_EVERY_UV');
  462. $img = empty($env_config[0])?'https://cdn-novel.iycdm.com/h5/personal_account/chenchen.jpg':$env_config[0];
  463. $max = empty($env_config[1])?100:$env_config[1];
  464. $one_loop_max = empty($env_config[3])?10:$env_config[3];
  465. $now_id = (int)$env_config[2];
  466. $uv = Redis::scard('force_guide_personal_uv');
  467. $personal_info = DB::table('personal_account_list')->where('id',$now_id)->select('count')->first();
  468. $total_max = $personal_info->count+$uv;
  469. //Log::info('$total_max is: '.);
  470. if($uv >= $one_loop_max || $total_max>=$max){
  471. if($total_max>=$max){
  472. DB::table('personal_account_list')->where('id',$now_id)->update([
  473. 'status'=>2,
  474. 'count'=>$total_max,
  475. 'updated_at'=>date('Y-m-d H:i:s')
  476. ]);
  477. }else{
  478. DB::table('personal_account_list')->where('id',$now_id)->increment('count',$uv,[
  479. 'updated_at'=>date('Y-m-d H:i:s')
  480. ]);
  481. }
  482. $account = DB::table('personal_account_list')
  483. ->where('is_enable',1)
  484. ->whereIn('status',[1,0])
  485. ->select('id','url')
  486. ->where('count','<',$max)
  487. ->where('group','FORCE')
  488. ->orderBy('count','asc')
  489. ->orderBy('id')
  490. ->first();
  491. Redis::del('force_guide_personal_uv');
  492. if($account){
  493. DB::table('personal_account_list')->where('id',$account->id)->update([
  494. 'status'=>1,
  495. 'updated_at'=>date('Y-m-d H:i:s')
  496. ]);
  497. $img = $account->url;
  498. $now_id = $account->id;
  499. Redis::Hmset('env','FORCE_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE',$account->url,'FORCE_GUIDE_PERSONAL_ACCOUNT_ID',$account->id);
  500. }else{
  501. Redis::Hmset('env','FORCE_GUIDE_PERSONAL_ACCOUNT_OURS_QRCODE','','FORCE_GUIDE_PERSONAL_ACCOUNT_ID','');
  502. }
  503. }
  504. Redis::sadd('force_guide_personal_uv',$this->uid);
  505. DB::table('ad_pdd')->insert([
  506. 'uid'=>$this->uid,
  507. 'img'=>'FORCE_GUIDE_PERSONAL_ACCOUNT_'.$now_id,
  508. 'date'=>date('Y-m-d'),
  509. 'created_at'=>date('Y-m-d H:i:s'),
  510. 'updated_at'=>date('Y-m-d H:i:s')
  511. ]);
  512. ForceGuidePersonAccountService::create($this->uid,$bid_no,$cid);
  513. $link['next'] = sprintf('/reader?bid=%s&cid=%s',$bid,$cid);
  514. $link['prev'] = sprintf('/reader?bid=%s&cid=%s',$bid,$prev_cid);
  515. $link['catalog'] = sprintf('/catalog?id=%s',$bid);
  516. return view('jump.forceGuidePersonalAccount',['img'=>$img,'link'=>$link,'title'=>$book_name]);
  517. }
  518. }