UserController.php 9.6 KB

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