UserController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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['price']) && $sign_today['reward'] = $sign_today['price'];
  143. }
  144. $result = [
  145. 'sign_status'=>$sign_status,
  146. 'sign_result'=>$sign_result,
  147. 'sign_today'=>$sign_today
  148. ];
  149. return response()->success($result);
  150. }
  151. public function getCoupons(Request $request){
  152. $activity_id = $request->input('activity_id');
  153. if(!DB::table('discount_coupons')->where('uid',$this->uid)->where('activity_id',$activity_id)->count()){
  154. DB::table('discount_coupons')->insert([
  155. 'uid'=>$this->uid,
  156. 'activity_id'=>$activity_id,
  157. 'created_at'=>date('Y-m-d H:i:s'),
  158. 'updated_at'=>date('Y-m-d H:i:s')
  159. ]);
  160. }
  161. return response()->success();
  162. }
  163. public function sign(Request $request){
  164. //sign_days
  165. //$day = date('Y-m-d');
  166. $sign = UserSignService::isSign($this->uid);
  167. $sign_count = ReadRecordService::getSignCount($this->uid);
  168. if($sign){
  169. //如果已经签过到
  170. if(!$sign_count){
  171. ReadRecordService::setSignCount($this->uid,1);
  172. ReadRecordService::setSignDay($this->uid);
  173. $sign_count = 1;
  174. }
  175. $fee = 30;
  176. if($sign_count>=3){
  177. $fee = 50;
  178. }
  179. $data = ['sign_days'=>$sign_count,'sign_reard'=>$fee,'status'=>1];
  180. return response()->success($data);
  181. }
  182. //还没有签到
  183. $status = UserSignService::signToday($this->uid);
  184. $sign_count = ReadRecordService::getSignCount($this->uid);
  185. $data = ['sign_days'=>$sign_count,'sign_reard'=>$status,'status'=>0];
  186. return response()->success($data);
  187. }
  188. public function recordShare(Request $request){
  189. if(!$this->checkUid()){
  190. return response()->error('NOT_LOGIN');
  191. }
  192. $uid = $this->uid;
  193. $distribution_channel_id = $this->distribution_channel_id;
  194. $type = 'click';
  195. $bid = $request->get('bid');
  196. $cid = $request->get('cid',0);
  197. if(!$bid){
  198. return response()->error('PARAM_ERROR');
  199. }
  200. !is_numeric($bid) && $bid = Hashids::decode($bid)[0];
  201. WapReaderPageFissionService::createV2($uid,$bid,$distribution_channel_id,$type,0,$cid);
  202. $user = $this->_user_info;
  203. $data = [];
  204. if(in_array($distribution_channel_id,[2,14,211]) && $user){
  205. $data['username'] = $user->nickname;
  206. $data['head_img'] = $user->head_img;
  207. $url = 'https://'._domain().'/detail?fromtype=readershare&id='.Hashids::encode($bid).'&fromflag='.$uid;
  208. //$url = str_replace('http://', $h5_scheme . '://', url()->current() . '?' . http_build_query($params));
  209. $data['share_url'] = $url;
  210. }
  211. return response()->success($data);
  212. }
  213. /**
  214. * @apiVersion 1.0.0
  215. * @apiDescription 用户点击广告统计
  216. * @api {post} user/advisitstat 用户点击广告统计
  217. * @apiGroup User
  218. * @apiName signRecord
  219. * @apiParam {Int} bid bid
  220. * @apiParam {Int} cid cid
  221. * @apiParam {Int} type type类型(CLICK|UNLOCK)
  222. * @apiSuccess {int} code 状态码
  223. * @apiSuccess {String} msg 信息
  224. * @apiSuccess {object} data 结果集
  225. * @apiParam {page} page
  226. * @apiSuccessExample {json} Success-Response:
  227. *
  228. * {
  229. * code: 0,
  230. * msg: "",
  231. * data: {
  232. * {
  233. * reward: 50,
  234. * sign_time: "2018-03-20 13:43:11"
  235. * },
  236. * {
  237. * reward: 50,
  238. * sign_time: "2018-01-18 16:22:33"
  239. * },
  240. * }
  241. * }
  242. */
  243. public function adVisitStat(Request $request){
  244. $bid = $request->get('bid');
  245. $cid = $request->get('cid',0);
  246. $type = $request->get('type');
  247. if(!$bid || !$type){
  248. return response()->error('PARAM_ERROR');
  249. }
  250. !is_numeric($bid) && $bid = Hashids::decode($bid)[0];
  251. AdVisitStatService::create($this->uid,$bid,$cid,$type);
  252. return response()->success();
  253. }
  254. public function urgeUpdate(Request $request){
  255. $bid = $request->get('bid');
  256. if(!$bid){
  257. return response()->error('PARAM_ERROR');
  258. }
  259. $bid = Hashids::decode($bid)[0];
  260. $result = BookUrgeUpdateService::UrgeRecord($this->uid,$bid);
  261. if($result && !$result->id){
  262. BookUrgeUpdateService::UrgeUpdate($this->uid,$bid,$result->updated_at);
  263. }
  264. return response()->success();
  265. }
  266. function logout()
  267. {
  268. setcookie(env('COOKIE_AUTH_WEB_WECHAT'), '', -1);
  269. setcookie('u', '', -1);
  270. return response('logout');
  271. }
  272. function test_add_user_login_cookie(Request $request)
  273. {
  274. $uid = $request->get('uid');
  275. \Log::info('test_add_user_login_cookie:'.$uid);
  276. Cookie::queue(env('COOKIE_AUTH_WEB_WECHAT'), $uid, env('U_COOKIE_EXPIRE'), null, null, false, false);
  277. return response('add_cookie:'.$uid);
  278. }
  279. }