UserController.php 14 KB

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