UserController.php 8.9 KB

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