UserController.php 19 KB

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