UserController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. namespace App\Http\Controllers\Wap\User;
  3. use App\Modules\Book\Services\BookConfigService;
  4. use App\Modules\Book\Services\BookUrgeUpdateService;
  5. use App\Modules\Book\Services\SignBookService;
  6. use App\Modules\Statistic\Services\AdVisitStatService;
  7. use App\Modules\Subscribe\Services\OrderService;
  8. use App\Modules\User\Services\ReadRecordService;
  9. use App\Modules\User\Services\WapReaderPageFissionService;
  10. use Illuminate\Http\Request;
  11. use App\Http\Controllers\Wap\BaseController;
  12. use App\Modules\Subscribe\Services\YearOrderService;
  13. use App\Modules\User\Services\UserService;
  14. use App\Modules\User\Services\UserRandSignService;
  15. use App\Modules\User\Services\UserSignService;
  16. use App\Http\Controllers\Wap\User\Transformers\SignRecordTransformer;
  17. use Log;
  18. use Redis;
  19. use Cookie;
  20. use DB;
  21. use Hashids;
  22. class UserController extends BaseController
  23. {
  24. /**
  25. * @apiDefine User 用户
  26. */
  27. /**
  28. * @apiVersion 1.0.0
  29. * @apiDescription 获取用户信息
  30. * @api {GET} userinfo 获取用户信息
  31. * @apiGroup User
  32. * @apiName index
  33. * @apiSuccess {Number} id 用户ID.
  34. * @apiSuccess {String} openid 微信openid.
  35. * @apiSuccess {String} unionid 微信unionid.
  36. * @apiSuccess {Number} distribution_channel_id 分销渠道ID.
  37. * @apiSuccess {String} province 省份.
  38. * @apiSuccess {String} city 城市.
  39. * @apiSuccess {String} country 国家.
  40. * @apiSuccess {String} headimgurl 头像地址.
  41. * @apiSuccess {Number} send_order_id 派单ID.
  42. * @apiSuccess {Number=0,1} sex 性别.
  43. * @apiSuccess {String} balance 书币余额.
  44. * @apiSuccess {Int} is_vip 是否vip
  45. * @apiSuccess {String} vip_days 364天.
  46. * @apiSuccessExample {json} Success-Response:
  47. *
  48. * {
  49. * "code": 0,
  50. * "msg": "",
  51. * "data": {
  52. * "id": 56,
  53. * "openid": "sdfs34ssdfdsf",
  54. * "unionid": "SDFSD3343S",
  55. * "distribution_channel_id": 1212,
  56. * "province": "浙江省",
  57. * "city": "杭州",
  58. * "country": "中国",
  59. * "headimgurl": "http://.."
  60. * "send_order_id": 323
  61. * "balance": 8956
  62. * "register_time": "2017-12-12 12:12:12"
  63. * }
  64. * }
  65. */
  66. public function index(){
  67. if(!$this->checkUid()){
  68. return response()->error('NOT_LOGIN');
  69. }
  70. $data = UserService::getById($this->uid);
  71. $data['is_vip'] = 0;
  72. $data['vip_days'] = 0;
  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['is_all_life'] = 0;
  88. $activity_id = env('FOREVER_ACTIVITY_ID');
  89. if($activity_id){
  90. if(OrderService::userIsParticipateActivity($this->uid,$activity_id)){
  91. $data['is_vip'] = 0 ;
  92. $data['vip_days'] = '99年';
  93. $data['is_all_life'] = 1;
  94. }
  95. }
  96. return response()->success($data);
  97. }
  98. /**
  99. * @apiVersion 1.0.0
  100. * @apiDescription 用户签到记录
  101. * @api {GET} user/sign_record 用户签到记录
  102. * @apiGroup User
  103. * @apiName signRecord
  104. * @apiSuccess {int} code 状态码
  105. * @apiSuccess {String} msg 信息
  106. * @apiSuccess {object} data 结果集
  107. * @apiSuccess {reward} data.reward 奖励金额.
  108. * @apiSuccess {sign_time} data.sign_time 签到时间.
  109. * @apiParam {page} page
  110. * @apiSuccessExample {json} Success-Response:
  111. *
  112. * {
  113. * code: 0,
  114. * msg: "",
  115. * data: {
  116. * list: [
  117. * {
  118. * reward: 50,
  119. * sign_time: "2018-03-20 13:43:11"
  120. * },
  121. * {
  122. * reward: 50,
  123. * sign_time: "2018-01-18 16:22:33"
  124. * },
  125. * ],
  126. * meta: {
  127. * total: 12,
  128. * per_page: 15,
  129. * current_page: 1,
  130. * last_page: 1,
  131. * next_page_url: "",
  132. * prev_page_url: ""
  133. * }
  134. * }
  135. * }
  136. */
  137. public function signRecord(Request $request){
  138. $sign_result = paginationTransform(new SignRecordTransformer(),UserSignService::getUserSignRecord($this->uid));
  139. $sign_status = UserSignService::isSign($this->uid);
  140. $sign_today = [];
  141. if($sign_status){
  142. $sign_today = ReadRecordService::getByField($this->uid,'sign_info');
  143. if($sign_today) $sign_today = json_decode($sign_today,1);
  144. isset($sign_today['sign_time']) && $sign_today['sign_time'] = date('Y-m-d H:i:s',$sign_today['sign_time']);
  145. isset($sign_today['sign_at']) && $sign_today['sign_time'] = $sign_today['sign_at'];
  146. isset($sign_today['price']) && $sign_today['reward'] = $sign_today['price'];
  147. }
  148. $result = [
  149. 'sign_status'=>$sign_status,
  150. 'sign_result'=>$sign_result,
  151. 'sign_today'=>$sign_today
  152. ];
  153. return response()->success($result);
  154. }
  155. public function getCoupons(Request $request){
  156. $activity_id = $request->input('activity_id');
  157. if(!DB::table('discount_coupons')->where('uid',$this->uid)->where('activity_id',$activity_id)->count()){
  158. DB::table('discount_coupons')->insert([
  159. 'uid'=>$this->uid,
  160. 'activity_id'=>$activity_id,
  161. 'created_at'=>date('Y-m-d H:i:s'),
  162. 'updated_at'=>date('Y-m-d H:i:s')
  163. ]);
  164. }
  165. return response()->success();
  166. }
  167. public function sign(Request $request){
  168. //sign_days
  169. //$day = date('Y-m-d');
  170. $sign = UserSignService::isSign($this->uid);
  171. $sign_count = ReadRecordService::getSignCount($this->uid);
  172. if($sign){
  173. //如果已经签过到
  174. if(!$sign_count){
  175. ReadRecordService::setSignCount($this->uid,1);
  176. ReadRecordService::setSignDay($this->uid);
  177. $sign_count = 1;
  178. }
  179. $fee = 30;
  180. if($sign_count>=3){
  181. $fee = 50;
  182. }
  183. $data = ['sign_days'=>$sign_count,'sign_reard'=>$fee,'status'=>1];
  184. return response()->success($data);
  185. }
  186. //还没有签到
  187. $status = UserSignService::signToday($this->uid);
  188. $sign_count = ReadRecordService::getSignCount($this->uid);
  189. $data = ['sign_days'=>$sign_count,'sign_reard'=>$status,'status'=>0];
  190. return response()->success($data);
  191. }
  192. /**
  193. * @return string
  194. */
  195. public function signi()
  196. {
  197. /*if(in_array($this->distribution_channel_id,explode(',',redisEnv('NEW_SIGN_CHANNELS','')))){
  198. $version = UserSignService::getUserSignVersion($this->uid);
  199. }else{
  200. $version = 'v1';
  201. }*/
  202. $version = UserSignService::getUserSignVersion($this->uid);
  203. if($version == 'v1'){
  204. $page = 'wap.sign';
  205. }else{
  206. $page = 'wap.signv2';
  207. }
  208. $book1= $book2 = null;
  209. if($version == 'v2' ){
  210. $sex = $this->_user_info->sex;
  211. $sex = $sex?$sex:2;
  212. $book1 = BookConfigService::getRandomOneHighQualityBook($sex);
  213. $book1->url = sprintf('/reader?bid=%s&cid=%s&source=wechatmsg&fromtype=sign_recommend',Hashids::encode($book1->bid),$book1->first_cid);
  214. $book2 = SignBookService::getRandomBook($sex);
  215. \Log::info($book2);
  216. foreach ($book2 as $item){
  217. $item->url = sprintf('/reader?bid=%s&cid=%s&source=wechatmsg&fromtype=sign_recommend',Hashids::encode($item->bid),$item->first_cid);
  218. }
  219. }
  220. $tomorrow_pool = $fee_pool = [0,30,50,120,50,50,50,150];
  221. $day = [0,'一','二','三','四','五','六','七'];
  222. list($sign,$sign_count) = ReadRecordService::getByMultiField($this->uid,'sign_day','sign_counts');
  223. if ($sign && $sign == date('Y-m-d')) {
  224. if($version == 'v1'){
  225. $fee = $sign_count>=3 ? 50:30;
  226. }else{
  227. if ($sign_count % 7 == 1 && $sign_count<=7) {
  228. $fee = 30;
  229. } elseif ($sign_count % 7 == 3) {
  230. $fee = 120;
  231. } elseif ($sign_count % 7 == 0) {
  232. $fee = 150;
  233. } else {
  234. $fee = 50;
  235. }
  236. }
  237. if($sign_count >=8){
  238. $fee_pool[1] = 50;
  239. }
  240. $tomorrow = (($sign_count+1) % 7 == 0? 7:($sign_count+1) % 7);
  241. if($sign_count+1 >=8){
  242. $tomorrow_pool[1] = 50;
  243. }
  244. //签过到了
  245. $data = ['sign_count'=>$sign_count,'fee'=>$fee,'fee_pool'=>$fee_pool,'day'=>$day,'book1'=>$book1,'book2'=>$book2,'tomorrow_fee'=>$tomorrow_pool[$tomorrow]];
  246. return view($page,$data);
  247. }
  248. $fee = UserSignService::signToday($this->uid,$version);
  249. if(!$fee){
  250. return response()->error('WAP_SYS_ERROR');
  251. }
  252. $sign_count = ReadRecordService::getSignCount($this->uid);
  253. if($sign_count >=8){
  254. $fee_pool[1] = 50;
  255. }
  256. if($sign_count+1 >=8){
  257. $tomorrow_pool[1] = 50;
  258. }
  259. $tomorrow = (($sign_count+1) % 7 == 0? 7:($sign_count+1) % 7);
  260. $data = ['sign_count'=>$sign_count,'fee'=>$fee,'fee_pool'=>$fee_pool,'day'=>$day,'book1'=>$book1,'book2'=>$book2,'tomorrow_fee'=>$tomorrow_pool[$tomorrow]];
  261. return view($page,$data);
  262. }
  263. public function recordShare(Request $request){
  264. if(!$this->checkUid()){
  265. return response()->error('NOT_LOGIN');
  266. }
  267. $uid = $this->uid;
  268. $distribution_channel_id = $this->distribution_channel_id;
  269. $type = 'click';
  270. $bid = $request->get('bid');
  271. $cid = $request->get('cid',0);
  272. if(!$bid){
  273. return response()->error('PARAM_ERROR');
  274. }
  275. !is_numeric($bid) && $bid = Hashids::decode($bid)[0];
  276. WapReaderPageFissionService::createV2($uid,$bid,$distribution_channel_id,$type,0,$cid);
  277. $user = $this->_user_info;
  278. $data = [];
  279. if(in_array($distribution_channel_id,[2,14,211]) && $user){
  280. $data['username'] = $user->nickname;
  281. $data['head_img'] = $user->head_img;
  282. $url = 'https://'._domain().'/detail?fromtype=readershare&id='.Hashids::encode($bid).'&fromflag='.$uid;
  283. //$url = str_replace('http://', $h5_scheme . '://', url()->current() . '?' . http_build_query($params));
  284. $data['share_url'] = $url;
  285. }
  286. return response()->success($data);
  287. }
  288. /**
  289. * @apiVersion 1.0.0
  290. * @apiDescription 用户点击广告统计
  291. * @api {post} user/advisitstat 用户点击广告统计
  292. * @apiGroup User
  293. * @apiName signRecord
  294. * @apiParam {Int} bid bid
  295. * @apiParam {Int} cid cid
  296. * @apiParam {Int} type type类型(CLICK|UNLOCK)
  297. * @apiSuccess {int} code 状态码
  298. * @apiSuccess {String} msg 信息
  299. * @apiSuccess {object} data 结果集
  300. * @apiParam {page} page
  301. * @apiSuccessExample {json} Success-Response:
  302. *
  303. * {
  304. * code: 0,
  305. * msg: "",
  306. * data: {
  307. * {
  308. * reward: 50,
  309. * sign_time: "2018-03-20 13:43:11"
  310. * },
  311. * {
  312. * reward: 50,
  313. * sign_time: "2018-01-18 16:22:33"
  314. * },
  315. * }
  316. * }
  317. */
  318. public function adVisitStat(Request $request){
  319. $bid = $request->get('bid');
  320. $cid = $request->get('cid',0);
  321. $type = $request->get('type');
  322. if(!$bid || !$type){
  323. return response()->error('PARAM_ERROR');
  324. }
  325. !is_numeric($bid) && $bid = Hashids::decode($bid)[0];
  326. AdVisitStatService::create($this->uid,$bid,$cid,$type);
  327. return response()->success();
  328. }
  329. public function urgeUpdate(Request $request){
  330. $bid = $request->get('bid');
  331. if(!$bid){
  332. return response()->error('PARAM_ERROR');
  333. }
  334. $bid = Hashids::decode($bid)[0];
  335. $result = BookUrgeUpdateService::UrgeRecord($this->uid,$bid);
  336. if($result && !$result->id){
  337. BookUrgeUpdateService::UrgeUpdate($this->uid,$bid,$result->updated_at);
  338. }
  339. return response()->success();
  340. }
  341. function logout(Request $request,$channel_id,$domain)
  342. {
  343. //echo $channel_id.'----';
  344. //echo $domain;
  345. $domains = ['zhuishuyun','66kshu','iycdm','leyuee'];
  346. setcookie(env('COOKIE_AUTH_WEB_WECHAT'), '', -1);
  347. setcookie('u', '', -1);
  348. setcookie('force_show_qrcode', '', -1);
  349. setcookie('sub_random_num', '', -1);
  350. setcookie('cpc_ad_status', '', -1);
  351. //return response('logout');
  352. $param = $request->except('_url');
  353. $url_format = '%s://site%s.%s.com/logout?%s';
  354. if(in_array($domain,$domains)){
  355. $i = 0;
  356. foreach ($domains as $k=>$v){
  357. if($v == $domain){
  358. $i = $k;
  359. }
  360. }
  361. if(isset($domains[$i+1])){
  362. $link = sprintf($url_format,env('PROTOCOL'),$channel_id,$domains[$i+1],http_build_query($param)) ;
  363. return redirect()->to($link);
  364. }
  365. }
  366. return view('help.logout');
  367. }
  368. function setOrderDelCookie(Request $request){
  369. $param = $request->except('_url');
  370. $type = isset($param['type']) ?$param['type']:'set';
  371. foreach ($param as $k=>$v){
  372. if($k == 'type') continue;
  373. if($type == 'set'){
  374. Cookie::queue($k, $v, 1000, null, null, false, false);
  375. }else{
  376. setcookie($k, '', -1);
  377. }
  378. }
  379. return response('ok');
  380. }
  381. function test_add_user_login_cookie(Request $request)
  382. {
  383. $uid = $request->get('uid');
  384. \Log::info('test_add_user_login_cookie:'.$uid);
  385. Cookie::queue(env('COOKIE_AUTH_WEB_WECHAT'), $uid, env('U_COOKIE_EXPIRE'), null, null, false, false);
  386. return response('add_cookie:'.$uid);
  387. }
  388. //日常随机签到
  389. function day_rand_sign(Request $request)
  390. {
  391. $uid = $this->uid;
  392. $day = date('Y-m-d');
  393. $hasSigned = UserRandSignService::hasSigned(compact('uid','day'));
  394. $fee = 0;
  395. if(!$hasSigned)
  396. {
  397. $fee = mt_rand(5,15);
  398. UserRandSignService::sign(compact('uid','day','fee'));
  399. }
  400. return view('wap.rand_sign',compact('hasSigned','fee'));
  401. }
  402. private function bindTelephoneStatus(){
  403. }
  404. }