UserController.php 11 KB

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