EventsController.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. <?php
  2. namespace Cas\Controller;
  3. use KIF\Core\Request;
  4. use KIF\Cookie;
  5. use Cas\Module\User;
  6. use KIF\Core\Config;
  7. use KIF\String\String;
  8. use Cas\Dao\Platform;
  9. use Cas\Dao\LotteryEvents;
  10. use KIF\Math\Math;
  11. use Cas\Module\Lottery;
  12. use Cas\Module\LotteryLog;
  13. use Cas\Dao\LotteryUserFeedback;
  14. use Cas\Dao\LotteryData;
  15. use Cas\Dao\UserInfo;
  16. use KIF\Dao\SqlHelper;
  17. use Cas\Dao\Focus;
  18. use Cas\Dao\LotteryUserExpress;
  19. use Cas\Dao\LotteryUserHelp;
  20. use Cas\Dao\LotteryUserHelpLog;
  21. /**
  22. * 活动页面的控制器
  23. *
  24. * @author lihuanchun
  25. *
  26. */
  27. class EventsController extends Controller {
  28. private $userCookieName = "USER_INFO";
  29. private $key;
  30. public function __construct() {
  31. $this->key = Config::getInstance ()->get ( 'passport_sign_key' );
  32. // 初始化一个本站用户
  33. $cookieInfo = Cookie::get ( $this->userCookieName );
  34. if (! $cookieInfo) {
  35. $objMUser = new User ();
  36. $uid = $objMUser->initUser ();
  37. $userinfo = $objMUser->get ( $uid );
  38. $userinfo ['sign'] = $this->createSign ( $userinfo );
  39. $json_data = json_encode ( $userinfo, true );
  40. Cookie::set ( $this->userCookieName, $json_data, 60 * 60 * 24 * 29, '.' . $_SERVER ['HTTP_HOST'] );
  41. }
  42. // 平台信息
  43. $objPlatform = new Platform ();
  44. $plat_form_id = $this->getPlatFormId ();
  45. $platform = $objPlatform->get ( $plat_form_id );
  46. $this->setOutput ( 'platform', $platform );
  47. // 列表页URL
  48. $this->setOutput ( 'listUrl', Request::schemeDomain () . '/list/' . $platform ['url'] );
  49. // 我的礼品URL
  50. $this->setOutput ( 'giftsUrl', Request::schemeDomain () . '/list/' . $platform ['url'] . '/member/gifts' );
  51. }
  52. /**
  53. * 获取当前用户
  54. */
  55. public function getRunTimeUid() {
  56. $cookieInfo = html_entity_decode ( Cookie::get ( $this->userCookieName ) );
  57. $cookieInfo = json_decode ( $cookieInfo, true );
  58. return $cookieInfo ['id'];
  59. }
  60. /**
  61. * 获取活动基本信息
  62. */
  63. public function info($info) {
  64. $this->objMLottery = new Lottery();
  65. # 剩余天数
  66. $TIME = time();
  67. $rday = ($info['end_time'] - $TIME) / (24 * 60 * 60);
  68. $info['rday'] = ceil($rday);
  69. # 参与人数
  70. $objMLotteryLog = new LotteryLog ( null, $info['id'] );
  71. $info['user_total'] = $objMLotteryLog->getUserParticipateLogNumGroupByUid ();
  72. # 阅读人数
  73. # 获取登录地址
  74. $url = Request::g('url');
  75. $plat_form_id = $this->getPlatFormId();
  76. $thisUrl = $this->objMLottery -> getEventsUrl($info['type'],$info['create_time'],$url);
  77. $loginUrl = $this -> getLoginUrl($thisUrl);
  78. $info['thisUrl'] = $thisUrl;
  79. $this->setOutput('loginUrl', $loginUrl);
  80. # 判断是否需要登录
  81. $ckLogin = $this->ckLogin();
  82. $this->setOutput('ckLogin', $ckLogin);
  83. # 获取用户信息
  84. $objUserData = new UserInfo();
  85. $uid = $this->getRunTimeUid();
  86. $userData = $objUserData -> get($uid);
  87. $userData['headimgurl'] = $this->getUserHead($userData);
  88. $this->setOutput('uid', $uid);
  89. $this->setOutput('userInfo', $userData);
  90. $source_user = Request::g('source_user');
  91. if (!$source_user) {
  92. $source_user = $uid;
  93. }
  94. $this->setOutput('source_user', $source_user);
  95. if ($source_user != $uid) {
  96. $userData = $objUserData -> get($source_user);
  97. $userData['headimgurl'] = $this->getUserHead($userData);
  98. }
  99. if (!$userData['nickname']) {
  100. $userData['nickname'] = '福利社网友';
  101. }
  102. $this->setOutput('userSourceInfo', $userData);
  103. $info['end'] = 1; //未过期
  104. if ($TIME > $info['end_time']) {
  105. $info['end'] = 2;//已过期
  106. }
  107. # 抽奖、优惠券、邀请、试用
  108. $type = array(1,4,6,7);
  109. if (in_array($info['type'], $type)) {
  110. $info['prize'] = 1;
  111. //$info['authorize'] = true; //强制登录
  112. } else {
  113. $info['top_data'] = 1;
  114. }
  115. # 检查当前活动是否需要登录
  116. $info['login'] = 1;
  117. $authorize = $info['authorize'];
  118. if ($authorize) {
  119. if($ckLogin==true){
  120. $info['login'] = 2;//需要登录
  121. //self::redirect($loginUrl);
  122. }
  123. }
  124. # 如果是试用,判断评价
  125. $condition = array('events_id' => $info['id'],'audit' => 2, 'uid' => $uid,'scratch_receive' => LotteryData::EVENT_SCRATCH_RECEIVE_TRUE);
  126. $offset = 0;
  127. $size = 1;
  128. $order = 'id desc';
  129. $userData = $this->objMLottery->getOnesLotteryData($condition);
  130. if ($userData) {
  131. $userData['feedback'] = 1;//不需要填写反馈
  132. $userData['url'] = base64_encode($info['thisUrl']);
  133. }
  134. if ($info['type'] == 4) {
  135. $info['feedback'] = 1;
  136. if ($userData) {
  137. $objLotteryUserFeedback = new LotteryUserFeedback();
  138. if (isset($userData['express']) && $userData['express']) {
  139. $userData['feedback'] = 2;//未填写反馈
  140. $where = array();
  141. $where['uid'] = $userData['uid'];
  142. $where['events_id'] = $userData['run_time_data']['events']['id'];
  143. $where['status'] = 1;
  144. $ids = $objLotteryUserFeedback->findIdsBy ( $where, 1, $order = ' id desc');
  145. if ($ids) {
  146. $userData['feedback'] = 3;//已填写反馈
  147. }
  148. } else {
  149. $userData['feedback'] = 4;//未填写收货地址
  150. }
  151. }
  152. }
  153. $order = '`index` desc, id desc';
  154. $limit = '0,5';
  155. $condition = array ('platform_id' => $plat_form_id);
  156. $objFocus = new Focus ();
  157. $ids = $objFocus->findIdsBy ( $condition, $limit, $order );
  158. $focusData = $objFocus->gets ( $ids );
  159. $ad = false;
  160. if (isset($this->platform['ad'])) {
  161. $ad = $this->platform['ad'];
  162. }
  163. $this->setOutput('ad', $ad);
  164. $this->setOutput('focusData', $focusData);
  165. $this->setOutput('userData', $userData);
  166. $this->setOutput('info', $info);
  167. $type = Request::g('type');
  168. if ($type == 'share') {
  169. # 分享
  170. $this->share($info, $plat_form_id, $source_user);
  171. } elseif ($type == 'prize') {
  172. # 获得福利
  173. $this->prize($info, $plat_form_id, 10, false);
  174. } elseif ($type == 'feedback') {
  175. # 获得评价信息
  176. $this->feedback($info, $plat_form_id, 10, false);
  177. }
  178. $this->setOutput('type', $type);
  179. $ajax = Request::g('ajax');
  180. if ($ajax == 'ajax') {
  181. $this->tpl = 'ajax_events';
  182. $this->ajax_success_exit($this->render(true));
  183. die;
  184. }
  185. }
  186. public function share($eventsData, $plat_form_id, $uid)
  187. {
  188. $events_id = $eventsData['id'];
  189. $condition = array (
  190. 'events_id' => $events_id,
  191. 'uid' => $uid,
  192. );
  193. $objLotteryUserHelp = new LotteryUserHelp();
  194. $info = $objLotteryUserHelp->fetchOne ( $condition);
  195. if (!$info) {
  196. $id = $objLotteryUserHelp->add ( $condition);
  197. } else {
  198. $id = $info['id'];
  199. }
  200. if ($id) {
  201. $objLotteryUserHelpLog = new LotteryUserHelpLog();
  202. $condition = array (
  203. 'events_id' => $events_id,
  204. 'help_id' => $id,
  205. );
  206. $total_num = $objLotteryUserHelpLog->totals ( $condition );
  207. $ids = $objLotteryUserHelpLog->findIdsBy ( $condition, '0,50', 'id desc' );
  208. $helpData = $objLotteryUserHelpLog->gets ( $ids );
  209. if ($helpData) {
  210. foreach ($helpData as $k => $v) {
  211. $helpData[$k]['headimgurl'] = $this->getUserHead($v['uid']);
  212. }
  213. }
  214. $session = $this->shareSession($id, $uid);
  215. $this->setOutput('help_session', $session);
  216. $this->setOutput('help_id', $id);
  217. $this->setOutput('helpData', $helpData);
  218. $this->setOutput('helpDataTotal', $total_num);
  219. }
  220. }
  221. public function shareSession($id, $uid, $source_session = false)
  222. {
  223. $key = 'onepage2999';
  224. $session = md5($id . '_' . $key . '_' . $uid);
  225. if ($source_session) {
  226. if ($source_session == $session) {
  227. return true;
  228. } else {
  229. return false;
  230. }
  231. } else {
  232. return $session;
  233. }
  234. }
  235. public function feedback($eventsData, $plat_form_id, $limit = 10, $uid = false)
  236. {
  237. $objFeedBack = new LotteryUserFeedback ();
  238. $page = Request::varGetInt ( 'page', 1 );
  239. $size = $limit;
  240. $offset = ($page - 1) * $size;
  241. $limit = "{$offset},{$size}";
  242. $order = ' id desc';
  243. $condition = array (
  244. 'events_id' => $eventsData['id'],
  245. 'status' => 1,
  246. );
  247. $ids = $objFeedBack->findIdsBy ( $condition, $limit, $order );
  248. $data = $objFeedBack->gets ( $ids );
  249. $total_num = $objFeedBack->totals ( $condition );
  250. $uids = array();
  251. if ($data) {
  252. foreach ($data as $k => $v) {
  253. $uids[] = $v['uid'];
  254. }
  255. # 通过UIDs 获取用户信息
  256. $objMUser = new User();
  257. $users = $objMUser -> gets($uids);
  258. }
  259. if ($data) {
  260. $data = $this->userData($data, $eventsData);
  261. }
  262. $this->setOutput('fdData', $data);
  263. $this->setOutput('fdDataTotals', $total_num);
  264. }
  265. public function prize($eventsData, $plat_form_id, $limit = 10, $uid = false)
  266. {
  267. $condition = array('audit' => 2, 'platform_id' => SqlHelper::addCompareOperator('&', $plat_form_id), 'scratch_receive' => LotteryData::EVENT_SCRATCH_RECEIVE_TRUE);
  268. if (isset($eventsData['id'])) {
  269. $condition['events_id'] = $eventsData['id'];
  270. }
  271. if ($uid) {
  272. $condition['uid'] = $uid;
  273. }
  274. $totals = $this->objMLottery -> getLotteryDataListNum($condition);
  275. $page = Request::varGetInt ( 'page', 1 );
  276. $size = $limit;
  277. $offset = ($page - 1) * $size;
  278. $order = 'update_time desc';
  279. $data = $this->objMLottery->getLotteryDataList($condition, "{$offset},{$size}", $order);
  280. if ($data) {
  281. $data = $this->userData($data, $eventsData);
  282. $this->setOutput('userEventsData', $data);
  283. $this->setOutput('userEventsDataTotals', $totals);
  284. }
  285. }
  286. public function userData($data, $eventsData)
  287. {
  288. $uids = array();
  289. foreach ($data as $k => $v) {
  290. $uids[] = $v['uid'];
  291. }
  292. # 通过UIDs 获取用户信息
  293. $objMUser = new User();
  294. $users = $objMUser -> gets($uids);
  295. $objLotteryUserExpress = new LotteryUserExpress();
  296. $condition = array(
  297. 'uid' => $uids
  298. );
  299. $tmpUserExpIds = $objLotteryUserExpress->findIdsBy($condition);
  300. $userExpData = $objLotteryUserExpress -> gets($tmpUserExpIds);
  301. $newUserExpData = array();
  302. foreach($userExpData as $k => $v){
  303. $newUserExpData[$v['uid']] = $v;
  304. }
  305. $this->setOutput('newUserExpData', $newUserExpData);
  306. //138****1234 的用户 2018.4.20 15:10 获得 某某某某活动 奖品。
  307. foreach ($data as $k => $v) {
  308. if (isset($v['pic'])) {
  309. $data[$k]['pic'] = explode(',', $v['pic']);
  310. }
  311. $type = isset($eventsData['type']) ? $eventsData['type'] : $eventsData[$v['events_id']]['type'];
  312. $data[$k]['user'] = array();
  313. $data[$k]['user']['cdate'] = date('Y.m.d H:i', isset($v['winners_time']) ? $v['winners_time'] : $v['create_time']);
  314. $data[$k]['user']['events'] = isset($eventsData['events_name']) ? $eventsData['events_name'] : $eventsData[$v['events_id']]['events_name'];
  315. $data[$k]['user']['headimgurl'] = $this->getUserHead($users[$v['uid']]);
  316. $data[$k]['user']['ext'] = '';
  317. if ($type == 6) {
  318. $data[$k]['user']['ext'] = '邀请';
  319. } elseif ($type == 4) {
  320. $data[$k]['user']['ext'] = '试用资格';
  321. } elseif ($type == 7) {
  322. $data[$k]['user']['ext'] = '优惠券';
  323. } elseif ($type == 1) {
  324. $data[$k]['user']['ext'] = '奖品';
  325. }
  326. if (isset($v['express']['phone']) && $v['express']['phone']) {
  327. $data[$k]['user']['mobile'] = substr_replace($v['express']['phone'],'****',3,4);
  328. } elseif (isset($newUserExpData[$v['uid']]['phone']) && $newUserExpData[$v['uid']]['phone']) {
  329. $data[$k]['user']['mobile'] = substr_replace($newUserExpData[$v['uid']]['phone'],'****',3,4);
  330. } else {
  331. unset($data[$k]);
  332. }
  333. }
  334. return $data;
  335. }
  336. /**
  337. * 获取头像
  338. */
  339. public function getUserHead($user)
  340. {
  341. if (!is_array($user)) {
  342. $objUserData = new UserInfo();
  343. $user = $objUserData -> get($user);
  344. }
  345. if (isset($user['headimgurl']) && $user['headimgurl']) {
  346. $result = $user['headimgurl'];
  347. } else {
  348. $result = 'http://news.mydrivers.com/Img/20110518/04481549.png';
  349. }
  350. return $result;
  351. }
  352. /**
  353. * 判断当前用户是否已经登录
  354. * true 需要
  355. * false 不需要
  356. */
  357. public function ckLogin() {
  358. $cookieInfo = Cookie::get ( $this->userCookieName );
  359. $cookieInfo = html_entity_decode ( $cookieInfo );
  360. $userInfo = json_decode ( $cookieInfo, true );
  361. /*
  362. * if(!$this->checkSign($userInfo['sign'] , $userInfo)){ return true; }
  363. */
  364. if ($userInfo ['is_new'] == 0) {
  365. return true;
  366. }
  367. return false;
  368. }
  369. /**
  370. * 获取登录地址
  371. * $thisUrl : 活动URL地址
  372. */
  373. public function getLoginUrl($thisUrl) {
  374. // 1.拼接一个 referer 地址 
  375. $cookieInfo = html_entity_decode ( Cookie::get ( $this->userCookieName ) );
  376. $cookieInfo = json_decode ( $cookieInfo, true );
  377. $uid = $cookieInfo ['id'];
  378. $args = array (
  379. 'cas_uid' => $uid,
  380. 'cas_return_url' => urlencode ( $thisUrl ) // 绑定用户后的跳转地址
  381. );
  382. $args ['sign'] = $this->createSign ( $args );
  383. $refererUrl = urlencode ( String::jointUrl ( Request::schemeDomain () . '/SetUser/', $args ) ); // passport 返回统一着陆页
  384. // 2. 拼接passport请求地址
  385. $passport_url = Config::getInstance ()->get ( 'passport_url' );
  386. $passport_url .= '&referer=' . $refererUrl;
  387. $arr = $this->convertUrlQuery ( $passport_url );
  388. $arr ['sign'] = $this->createSign ( $arr );
  389. $passport_url .= '&sign=' . $arr ['sign'];
  390. $passport_url .= '&cas_uid=' . $uid;
  391. $this->setOutput('applogin', 2);
  392. $weixin = $this->checkIsFromWeixin();
  393. if (!$weixin) {
  394. $tokenid = Request::g('tokenid');
  395. if (!$tokenid) {
  396. $tokenid = Request::g('tokenId');
  397. }
  398. if ($tokenid) {
  399. # app中已登陆
  400. $passport_url .= '&tokenid=' . $tokenid;
  401. } else {
  402. # app中未登陆
  403. $this->setOutput('applogin', 1);
  404. }
  405. }
  406. return $passport_url;
  407. }
  408. public function convertUrlQuery($url) {
  409. $arr = parse_url ( $url );
  410. $query = $arr ['query'];
  411. $queryParts = explode ( '&', $query );
  412. $params = array ();
  413. foreach ( $queryParts as $param ) {
  414. $item = explode ( '=', $param );
  415. $params [$item [0]] = $item [1];
  416. }
  417. return $params;
  418. }
  419. /**
  420. * 通过活动创建时间,返回活动ID
  421. */
  422. public function getEventsCreateTimeToId() {
  423. $create_time = Request::g ( 'create_time' );
  424. $create_time = strtotime ( $create_time );
  425. $objEvents = new LotteryEvents ();
  426. $ids = $objEvents->findIdsBy ( array (
  427. 'create_time' => $create_time
  428. ) );
  429. return array_pop ( $ids );
  430. }
  431. /**
  432. * 活动相关页面获取 plat_form_id 方法
  433. */
  434. public function getPlatFormId() {
  435. $url = Request::g ( 'url' );
  436. $objDPlatform = new Platform ();
  437. $data = $objDPlatform->getsAll ();
  438. foreach ( $data as $key => $d ) {
  439. if ($d ['url'] == $url) {
  440. $plat_form_id = $key;
  441. $this->platform = $d;
  442. }
  443. }
  444. if (empty ( $plat_form_id )) {
  445. $plat_form_id = Cookie::get ( 'plat_form_id' );
  446. } else {
  447. Cookie::set ( 'plat_form_id', $plat_form_id, 60 * 60 * 24 * 29, '.' . $_SERVER ['HTTP_HOST'] );
  448. }
  449. return $plat_form_id;
  450. }
  451. /**
  452. * 生成请求参数的sign
  453. *
  454. * @param array $params
  455. * @return String
  456. */
  457. public function createSign(array $params) {
  458. if ($params && is_array ( $params )) {
  459. ksort ( $params );
  460. $str = $this->key;
  461. foreach ( $params as $key => $value ) {
  462. if ($key != 'sign') {
  463. $str .= $key . $value;
  464. }
  465. }
  466. return strtoupper ( md5 ( $str ) );
  467. }
  468. return '';
  469. }
  470. /**
  471. * 指定的sign值是否正确
  472. *
  473. * @param String $sign
  474. * 给定需要验证的sign值
  475. * @param Array $params
  476. * 用来计算sign值的参数集合
  477. * @return Boolean
  478. */
  479. public function checkSign($sign, $params = null) {
  480. $sign2 = $this->createSign ( $params );
  481. return ($sign2 == $sign);
  482. }
  483. public function checkIsFromWeixin($title) {
  484. $state = false;
  485. $user_agent = $_SERVER ['HTTP_USER_AGENT'];
  486. if (preg_match ( '/MicroMessenger/i', $user_agent )) {
  487. $state = true;
  488. }
  489. return $state;
  490. }
  491. /**
  492. *
  493. * @param string $title
  494. */
  495. public function checkIsFromMobile($title) {
  496. return true;
  497. if (Request::get ( 'debugs' ) == 'rabin') {
  498. return true;
  499. }
  500. // PC 端显示二维码页
  501. $isMobile = false;
  502. $user_agent = $_SERVER ['HTTP_USER_AGENT'];
  503. /*
  504. if (preg_match ( '/MicroMessenger|Mobile/i', $user_agent )) {
  505. $isMobile = true;
  506. }
  507. */
  508. if (preg_match ( '/MicroMessenger/i', $user_agent )) {
  509. $isMobile = true;
  510. }
  511. if (! $isMobile) {
  512. include_once Config::getInstance ()->get ( 'App_Path' ) . DS . 'include/phpqrcode/phpqrcode.php';
  513. $IMG_UPLOAD_PATH = Config::getInstance ()->get ( 'upload.path' ) . DS . 'qrcode';
  514. $IMG_UPLOAD_URL = Config::getInstance ()->get ( 'upload.url' ) . DS . 'qrcode';
  515. if (! file_exists ( $IMG_UPLOAD_PATH )) {
  516. if (! mkdir ( $IMG_UPLOAD_PATH, 0775, true )) {
  517. self::fail_exit ( '创建二维码失败,原因:图片目录不存在' );
  518. }
  519. }
  520. $url = Request::url ();
  521. $fileName = Math::md5_16 ( $url ) . '.png';
  522. $save_path = $IMG_UPLOAD_PATH . DS . $fileName;
  523. $save_url = $IMG_UPLOAD_URL . DS . $fileName;
  524. if (! file_exists ( $save_path )) {
  525. \QRcode::png ( $url, $save_path, 3, 6 );
  526. }
  527. $this->tpl = 'pc_qrcode';
  528. $this->setOutput ( 'qrcode', $save_url );
  529. $this->setOutput ( 'title', $title );
  530. $this->setOutput('user_agent', $user_agent);
  531. exit ( $this->render () );
  532. }
  533. }
  534. }