UserController.php 9.4 KB

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