IndexController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /**
  3. * 应用首页接口,后面可能根据不同版本独立
  4. */
  5. class IndexController extends ApiBaseController {
  6. public function beforeAction($action){
  7. // $weixin_use = array('staticSource');
  8. // if(Yii::app()->getRequest()->getParam("request_from") == 'weixin' && in_array($action->id,$weixin_use)){
  9. // return true;
  10. // }
  11. //todo user first login
  12. return $this->verify();
  13. }
  14. public function actionV3(){
  15. //根据自定义参数生成缓存key
  16. $user_id = Yii::app()->request->getParam('user_id','');
  17. $device_id = Yii::app()->request->getParam('device_id','0');
  18. $city_info = json_decode(Yii::app()->request->getParam('city_info'),true);
  19. $province = isset($city_info['province'])?$city_info['province']:'';
  20. $position_city_info = json_decode(Yii::app()->request->getParam('position_city_info'),true);
  21. $position_province = isset($position_city_info['province'])?$position_city_info['province']:'';
  22. $get_new = Yii::app()->request->getParam('get_new',0);
  23. $z_User = new ZUser();
  24. $host = ENVIRONMENT=='product'?'www.wozhua.mobi':'wwwtest.wozhua.mobi';
  25. $f_host = ENVIRONMENT=='product'?'f.wozhua.mobi':'ftest.wozhua.mobi';
  26. $user = $z_User->idExist($user_id);
  27. //$new_user_coupons = 0;
  28. $no_recommond_groups = Yii::app()->params['no_recommond_groups'];
  29. //登陆用户根据上次访问时间,关注圈子,页数,当前时间生成唯一key
  30. if($user){
  31. $add_score = $this->today_first_login($user_id);
  32. $last_visit_hours = CommonFn::get_user_last_visit_hours($user['last_visit_time']);
  33. $group_ids = $user['groups'];
  34. $sort_group = array();
  35. foreach ($group_ids as $key => $value) {
  36. $sort_group[] = (string)$value;
  37. }
  38. sort($sort_group);
  39. $stamp = '3';
  40. foreach ($sort_group as $key => $value) {
  41. $stamp.=$value;
  42. }
  43. $cache_key = md5($stamp.$last_visit_hours.$get_new);
  44. //未登录用户根据页数,默认获取推荐关注圈子七天内帖子
  45. }elseif($device_id){
  46. $cache = new ARedisCache();
  47. $key = 'guest_last_visit_time_'.$device_id;
  48. $guest_last_visit_time = $cache->get($key);
  49. if($guest_last_visit_time){
  50. $last_visit_hours = CommonFn::get_user_last_visit_hours($guest_last_visit_time);
  51. }else{
  52. $last_visit_hours = 168;
  53. }
  54. $cache->set($key,time());
  55. //未登录用户获取获取推荐的圈子
  56. $default_follow_group = Service::factory('VariableService')->getVariable('default_follow_group');
  57. $groups = explode(',',trim($default_follow_group,','));
  58. if(isset($city_info['province'])){
  59. $criteria = new EMongoCriteria();
  60. $criteria->status('==',1);
  61. $criteria->addCond('city_info.province','==',$city_info['province']);
  62. $cursor = Group::model()->findAll($criteria);
  63. foreach ($cursor as $value) {
  64. $groups[] = (string)$value->_id;
  65. }
  66. }
  67. $stamp = '3';
  68. foreach ($groups as $key => $value) {
  69. if(!CommonFn::isMongoId($value)){
  70. unset($groups[$key]);
  71. continue;
  72. }
  73. $groups[$key] = new MongoId($value);
  74. $stamp.=$value;
  75. }
  76. $group_ids = array_values($groups);
  77. $cache_key = md5($stamp.$last_visit_hours.$get_new);
  78. }else{
  79. $default_follow_group = Service::factory('VariableService')->getVariable('default_follow_group');
  80. $groups = explode(',',trim($default_follow_group,','));
  81. $stamp = '3';
  82. foreach ($groups as $key => $value) {
  83. if(!CommonFn::isMongoId($value)){
  84. unset($groups[$key]);
  85. continue;
  86. }
  87. $groups[$key] = new MongoId($value);
  88. $stamp.=$value;
  89. }
  90. $group_ids = array_values($groups);
  91. $last_visit_hours = 168;
  92. $cache_key = md5($stamp.$last_visit_hours.$get_new);
  93. }
  94. foreach ($group_ids as $key => $value) {
  95. if(in_array((string)$value,$no_recommond_groups)){
  96. unset($group_ids[$key]);
  97. }
  98. }
  99. $group_ids = array_values($group_ids);
  100. $cache = new ARedisCache();
  101. $cache_data = $cache->get($cache_key);
  102. if($cache_data && !YII_DEBUG){
  103. $cache_res = unserialize($cache_data);
  104. $data = $cache_res['data'];
  105. }else{
  106. //按钮链接
  107. $link_button[] = array(
  108. 'title'=>'买宠物',
  109. 'link_text'=>'有保障/大礼包',
  110. 'icon_url'=>'http://7oxep6.com1.z0.glb.clouddn.com/huotijiaoyi.png',
  111. 'link'=>'http://'.$host.'/o2o/web/index/deal?need_header=0'
  112. );
  113. $link_button[] = array(
  114. 'title'=>'问专家',
  115. 'link_text'=>'看病/训犬/养护',
  116. 'icon_url'=>'http://7oxep6.com1.z0.glb.clouddn.com/doctorhij.png',
  117. 'link'=>'http://'.$host.'/group/54a0fa1f0eb9fb17308b47a6'
  118. );
  119. $data['link_button'] = $link_button;
  120. //活体交易部分
  121. $deal['type'] = 'deal';
  122. $deal['title'] = '放心、有保障的宠物交易';
  123. $deal['more'] = 'http://'.$host.'/o2o/web/index/deal?need_header=0';
  124. //todo pettype
  125. $new_pets = DealPet::getNewPet();
  126. foreach ($new_pets as $key => $value) {
  127. $new_pets[$key]['url'] = 'http://'.$host.'/o2o/web/index/deal?need_header=0/#!/detail/'.$value['id'];
  128. }
  129. $deal['pets'] = $new_pets;
  130. $data['modules'][] = $deal;
  131. $res_topics = array();
  132. $conditions = array(
  133. 'group'=>array('in',$group_ids),
  134. 'status'=>array('==',1),
  135. 'time'=>array('>=',time()-3600*$last_visit_hours)
  136. );
  137. //最新最热帖排序规则
  138. $order = $get_new?['_id'=>'desc']:['like_count'=>'desc'];
  139. $model = new Topic();
  140. $pagedata = CommonFn::getPagedata($model,1,20,$conditions,$order,false);
  141. $topics = $pagedata['res'];
  142. foreach($topics as $topic){
  143. $_topic = $model->parseRow($topic,array('id','content','city_info','city_info_str','city_topic_str','time','time_str','group','visit_count','reply_count','fav_count','like_count','user','pics','voice','video','last_post_time','last_post_time_str'));
  144. if($user){
  145. $z_like = new ZLike();
  146. $like = $z_like->getLikeByLikeObj((string)$user['_id'],$_topic['id']);
  147. if(empty($like)){
  148. $_topic['is_liked'] = false;
  149. }else{
  150. $_topic['is_liked'] = true;
  151. }
  152. }else{
  153. $_topic['is_liked'] = false;
  154. }
  155. $res_topics[] = $_topic;
  156. }
  157. $data['topics'] = $res_topics;
  158. $cache_data = array();
  159. $cache_data['data'] = $data;
  160. $cache->set($cache_key,serialize($cache_data),3600);
  161. }
  162. //首页轮播图
  163. $province_tag = isset($city_info['province'])?$city_info['province']:'no';
  164. $slide = Slide::getIndexSlide($province_tag);
  165. $data['slide'] = $slide;
  166. if(isset($add_score['status'])){
  167. $score_info['score_change'] = $add_score['score'];
  168. $score_info['current_score'] = $add_score['current_score'];
  169. $score_info['score_type'] = '签到';
  170. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$data,303,$score_info);
  171. }else{
  172. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$data,200,array('last_visit_hours' =>$last_visit_hours));
  173. }
  174. }
  175. public function actionV4(){
  176. //根据自定义参数生成缓存key
  177. $user_id = Yii::app()->request->getParam('user_id','');
  178. $device_id = Yii::app()->request->getParam('device_id','0');
  179. $page = intval(Yii::app()->request->getParam('page',1));
  180. $city_info = json_decode(Yii::app()->request->getParam('city_info'),true);
  181. $province = isset($city_info['province'])?$city_info['province']:'';
  182. $position_city_info = json_decode(Yii::app()->request->getParam('position_city_info'),true);
  183. $position_province = isset($position_city_info['province'])?$position_city_info['province']:'';
  184. $get_new = Yii::app()->request->getParam('get_new',0);
  185. $z_User = new ZUser();
  186. $host = ENVIRONMENT=='product'?'www.wozhua.mobi':'wwwtest.wozhua.mobi';
  187. $f_host = ENVIRONMENT=='product'?'f.wozhua.mobi':'ftest.wozhua.mobi';
  188. $user = $z_User->idExist($user_id);
  189. //$new_user_coupons = 0;
  190. $no_recommond_groups = Yii::app()->params['no_recommond_groups'];
  191. //登陆用户根据上次访问时间,关注圈子,页数,当前时间生成唯一key
  192. if($user){
  193. $add_score = $this->today_first_login($user_id);
  194. $last_visit_hours = CommonFn::get_user_last_visit_hours($user['last_visit_time']);
  195. $group_ids = $user['groups'];
  196. $sort_group = array();
  197. foreach ($group_ids as $key => $value) {
  198. $sort_group[] = (string)$value;
  199. }
  200. sort($sort_group);
  201. $stamp = '3';
  202. foreach ($sort_group as $key => $value) {
  203. $stamp.=$value;
  204. }
  205. $cache_key = md5($stamp.$last_visit_hours.$get_new);
  206. //未登录用户根据页数,默认获取推荐关注圈子七天内帖子
  207. }elseif($device_id){
  208. $cache = new ARedisCache();
  209. $key = 'guest_last_visit_time_'.$device_id;
  210. $guest_last_visit_time = $cache->get($key);
  211. if($guest_last_visit_time){
  212. $last_visit_hours = CommonFn::get_user_last_visit_hours($guest_last_visit_time);
  213. }else{
  214. $last_visit_hours = 168;
  215. }
  216. $cache->set($key,time());
  217. //未登录用户获取获取推荐的圈子
  218. $default_follow_group = Service::factory('VariableService')->getVariable('default_follow_group');
  219. $groups = explode(',',trim($default_follow_group,','));
  220. if(isset($city_info['province'])){
  221. $criteria = new EMongoCriteria();
  222. $criteria->status('==',1);
  223. $criteria->addCond('city_info.province','==',$city_info['province']);
  224. $cursor = Group::model()->findAll($criteria);
  225. foreach ($cursor as $value) {
  226. $groups[] = (string)$value->_id;
  227. }
  228. }
  229. $stamp = '3';
  230. foreach ($groups as $key => $value) {
  231. if(!CommonFn::isMongoId($value)){
  232. unset($groups[$key]);
  233. continue;
  234. }
  235. $groups[$key] = new MongoId($value);
  236. $stamp.=$value;
  237. }
  238. $group_ids = array_values($groups);
  239. $cache_key = md5($stamp.$last_visit_hours.$get_new);
  240. }else{
  241. $default_follow_group = Service::factory('VariableService')->getVariable('default_follow_group');
  242. $groups = explode(',',trim($default_follow_group,','));
  243. $stamp = '3';
  244. foreach ($groups as $key => $value) {
  245. if(!CommonFn::isMongoId($value)){
  246. unset($groups[$key]);
  247. continue;
  248. }
  249. $groups[$key] = new MongoId($value);
  250. $stamp.=$value;
  251. }
  252. $group_ids = array_values($groups);
  253. $last_visit_hours = 168;
  254. $cache_key = md5($stamp.$last_visit_hours.$get_new);
  255. }
  256. foreach ($group_ids as $key => $value) {
  257. if(in_array((string)$value,$no_recommond_groups)){
  258. unset($group_ids[$key]);
  259. }
  260. }
  261. $group_ids = array_values($group_ids);
  262. $cache = new ARedisCache();
  263. $cache_data = $cache->get($cache_key.'v4');
  264. if($cache_data && !YII_DEBUG){
  265. $cache_res = unserialize($cache_data);
  266. $data = $cache_res['data'];
  267. }else{
  268. //按钮链接
  269. $link_button[] = array(
  270. 'title'=>'买宠物',
  271. 'link_text'=>'有保障/大礼包',
  272. 'icon_url'=>'http://7oxep6.com1.z0.glb.clouddn.com/huotijiaoyi.png',
  273. 'link'=>'http://'.$host.'/o2o/web/index/deal?need_header=0'
  274. );
  275. $link_button[] = array(
  276. 'title'=>'问专家',
  277. 'link_text'=>'看病/训犬/养护',
  278. 'icon_url'=>'http://7oxep6.com1.z0.glb.clouddn.com/doctorhij.png',
  279. 'link'=>'http://'.$host.'/group/54a0fa1f0eb9fb17308b47a6'
  280. );
  281. $data['link_button'] = $link_button;
  282. //活体交易部分
  283. $deal['type'] = 'deal';
  284. $deal['title'] = '放心、有保障的宠物交易';
  285. $deal['more'] = 'http://'.$host.'/o2o/web/index/deal?need_header=0';
  286. //todo pettype
  287. $new_pets = DealPet::getNewPet();
  288. foreach ($new_pets as $key => $value) {
  289. $new_pets[$key]['url'] = 'http://'.$host.'/o2o/web/index/deal?need_header=0/#!/detail/'.$value['id'];
  290. }
  291. $deal['pets'] = $new_pets;
  292. $data['modules'][] = $deal;
  293. $res_topics = array();
  294. $conditions = array(
  295. 'group'=>array('in',$group_ids),
  296. 'status'=>array('==',1),
  297. 'time'=>array('>=',time()-3600*$last_visit_hours)
  298. );
  299. //最新最热帖排序规则
  300. $order = $get_new?['_id'=>'desc']:['like_count'=>'desc'];
  301. $model = new Topic();
  302. $pagedata = CommonFn::getPagedata($model,1,200,$conditions,$order,false);
  303. $topics = $pagedata['res'];
  304. foreach($topics as $topic){
  305. $res_topics[] = $topic['_id'];
  306. }
  307. $data['topics'] = $res_topics;
  308. $cache_data = array();
  309. $cache_data['data'] = $data;
  310. $cache->set($cache_key.'v4',serialize($cache_data),3600);
  311. }
  312. $res_topics = array();
  313. $topics = $data['topics'];
  314. $skip = 20*$page-20;
  315. if($skip+20 >= count($topics)){
  316. $has_more = 0;
  317. }else{
  318. $has_more = 1;
  319. }
  320. $topics_ids = array_slice($topics,$skip,20);
  321. $model = new Topic();
  322. foreach ($topics_ids as $key => $topic_id) {
  323. $_topic = $model->parseRow(Topic::get(new MongoId($topic_id)),array('id','content','city_info','city_info_str','city_topic_str','time','time_str','group','visit_count','reply_count','fav_count','like_count','user','pics','voice','video','last_post_time','last_post_time_str'));
  324. if($user){
  325. $z_like = new ZLike();
  326. $like = $z_like->getLikeByLikeObj((string)$user['_id'],$_topic['id']);
  327. if(empty($like)){
  328. $_topic['is_liked'] = false;
  329. }else{
  330. $_topic['is_liked'] = true;
  331. }
  332. }else{
  333. $_topic['is_liked'] = false;
  334. }
  335. $res_topics[] = $_topic;
  336. }
  337. $data['topics'] = $res_topics;
  338. //首页轮播图
  339. $province_tag = isset($city_info['province'])?$city_info['province']:'no';
  340. $slide = Slide::getIndexSlide($province_tag);
  341. $data['slide'] = $slide;
  342. if(isset($add_score['status'])){
  343. $score_info['score_change'] = $add_score['score'];
  344. $score_info['current_score'] = $add_score['current_score'];
  345. $score_info['score_type'] = '签到';
  346. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$data,303,$score_info);
  347. }else{
  348. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$data,200,array('has_more'=>$has_more,'last_visit_hours' =>$last_visit_hours));
  349. }
  350. }
  351. }