EventsController.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. /**
  12. * 活动页面的控制器
  13. *
  14. * @author lihuanchun
  15. *
  16. */
  17. class EventsController extends Controller {
  18. private $userCookieName = "USER_INFO";
  19. private $key;
  20. public function __construct() {
  21. $this->key = Config::getInstance ()->get ( 'passport_sign_key' );
  22. // 初始化一个本站用户
  23. $cookieInfo = Cookie::get ( $this->userCookieName );
  24. if (! $cookieInfo) {
  25. $objMUser = new User ();
  26. $uid = $objMUser->initUser ();
  27. $userinfo = $objMUser->get ( $uid );
  28. $userinfo ['sign'] = $this->createSign ( $userinfo );
  29. $json_data = json_encode ( $userinfo, true );
  30. Cookie::set ( $this->userCookieName, $json_data, 60 * 60 * 24 * 29, '.' . $_SERVER ['HTTP_HOST'] );
  31. }
  32. // 平台信息
  33. $objPlatform = new Platform ();
  34. $plat_form_id = $this->getPlatFormId ();
  35. $platform = $objPlatform->get ( $plat_form_id );
  36. $this->setOutput ( 'platform', $platform );
  37. // 列表页URL
  38. $this->setOutput ( 'listUrl', Request::schemeDomain () . '/list/' . $platform ['url'] );
  39. // 我的礼品URL
  40. $this->setOutput ( 'giftsUrl', Request::schemeDomain () . '/list/' . $platform ['url'] . '/member/gifts' );
  41. }
  42. /**
  43. * 获取当前用户
  44. */
  45. public function getRunTimeUid() {
  46. $cookieInfo = html_entity_decode ( Cookie::get ( $this->userCookieName ) );
  47. $cookieInfo = json_decode ( $cookieInfo, true );
  48. return $cookieInfo ['id'];
  49. }
  50. /**
  51. * 判断当前用户是否已经登录
  52. * true 需要
  53. * false 不需要
  54. */
  55. public function ckLogin() {
  56. $cookieInfo = Cookie::get ( $this->userCookieName );
  57. $cookieInfo = html_entity_decode ( $cookieInfo );
  58. $userInfo = json_decode ( $cookieInfo, true );
  59. /*
  60. * if(!$this->checkSign($userInfo['sign'] , $userInfo)){ return true; }
  61. */
  62. if ($userInfo ['is_new'] == 0) {
  63. return true;
  64. }
  65. return false;
  66. }
  67. /**
  68. * 获取登录地址
  69. * $thisUrl : 活动URL地址
  70. */
  71. public function getLoginUrl($thisUrl) {
  72. // 1.拼接一个 referer 地址 
  73. $cookieInfo = html_entity_decode ( Cookie::get ( $this->userCookieName ) );
  74. $cookieInfo = json_decode ( $cookieInfo, true );
  75. $uid = $cookieInfo ['id'];
  76. $args = array (
  77. 'cas_uid' => $uid,
  78. 'cas_return_url' => urlencode ( $thisUrl ) // 绑定用户后的跳转地址
  79. );
  80. $args ['sign'] = $this->createSign ( $args );
  81. $refererUrl = urlencode ( String::jointUrl ( Request::schemeDomain () . '/SetUser/', $args ) ); // passport 返回统一着陆页
  82. // 2. 拼接passport请求地址
  83. $passport_url = Config::getInstance ()->get ( 'passport_url' );
  84. $passport_url .= '&referer=' . $refererUrl;
  85. $arr = $this->convertUrlQuery ( $passport_url );
  86. $arr ['sign'] = $this->createSign ( $arr );
  87. $passport_url .= '&sign=' . $arr ['sign'];
  88. return $passport_url;
  89. }
  90. public function convertUrlQuery($url) {
  91. $arr = parse_url ( $url );
  92. $query = $arr ['query'];
  93. $queryParts = explode ( '&', $query );
  94. $params = array ();
  95. foreach ( $queryParts as $param ) {
  96. $item = explode ( '=', $param );
  97. $params [$item [0]] = $item [1];
  98. }
  99. return $params;
  100. }
  101. /**
  102. * 通过活动创建时间,返回活动ID
  103. */
  104. public function getEventsCreateTimeToId() {
  105. $create_time = Request::g ( 'create_time' );
  106. $create_time = strtotime ( $create_time );
  107. $objEvents = new LotteryEvents ();
  108. $ids = $objEvents->findIdsBy ( array (
  109. 'create_time' => $create_time
  110. ) );
  111. return array_pop ( $ids );
  112. }
  113. /**
  114. * 活动相关页面获取 plat_form_id 方法
  115. */
  116. public function getPlatFormId() {
  117. $url = Request::g ( 'url' );
  118. $objDPlatform = new Platform ();
  119. $data = $objDPlatform->getsAll ();
  120. foreach ( $data as $key => $d ) {
  121. if ($d ['url'] == $url) {
  122. $plat_form_id = $key;
  123. }
  124. }
  125. if (empty ( $plat_form_id )) {
  126. $plat_form_id = Cookie::get ( 'plat_form_id' );
  127. } else {
  128. Cookie::set ( 'plat_form_id', $plat_form_id, 60 * 60 * 24 * 29, '.' . $_SERVER ['HTTP_HOST'] );
  129. }
  130. return $plat_form_id;
  131. }
  132. /**
  133. * 生成请求参数的sign
  134. *
  135. * @param array $params
  136. * @return String
  137. */
  138. public function createSign(array $params) {
  139. if ($params && is_array ( $params )) {
  140. ksort ( $params );
  141. $str = $this->key;
  142. foreach ( $params as $key => $value ) {
  143. if ($key != 'sign') {
  144. $str .= $key . $value;
  145. }
  146. }
  147. return strtoupper ( md5 ( $str ) );
  148. }
  149. return '';
  150. }
  151. /**
  152. * 指定的sign值是否正确
  153. *
  154. * @param String $sign
  155. * 给定需要验证的sign值
  156. * @param Array $params
  157. * 用来计算sign值的参数集合
  158. * @return Boolean
  159. */
  160. public function checkSign($sign, $params = null) {
  161. $sign2 = $this->createSign ( $params );
  162. return ($sign2 == $sign);
  163. }
  164. /**
  165. *
  166. * @param string $title
  167. */
  168. public function checkIsFromMobile($title) {
  169. return true;
  170. if (Request::get ( 'kif_debug' ) == '@kimiss') {
  171. return true;
  172. }
  173. // PC 端显示二维码页
  174. $isMobile = false;
  175. $user_agent = $_SERVER ['HTTP_USER_AGENT'];
  176. if (preg_match ( '/MicroMessenger|Mobile/i', $user_agent )) {
  177. $isMobile = true;
  178. }
  179. if (! $isMobile) {
  180. include_once Config::getInstance ()->get ( 'App_Path' ) . DS . 'include/phpqrcode/phpqrcode.php';
  181. $IMG_UPLOAD_PATH = Config::getInstance ()->get ( 'upload.path' ) . DS . 'qrcode';
  182. $IMG_UPLOAD_URL = Config::getInstance ()->get ( 'upload.url' ) . DS . 'qrcode';
  183. if (! file_exists ( $IMG_UPLOAD_PATH )) {
  184. if (! mkdir ( $IMG_UPLOAD_PATH, 0775, true )) {
  185. self::fail_exit ( '创建二维码失败,原因:图片目录不存在' );
  186. }
  187. }
  188. $url = Request::url ();
  189. $fileName = Math::md5_16 ( $url ) . '.png';
  190. $save_path = $IMG_UPLOAD_PATH . DS . $fileName;
  191. $save_url = $IMG_UPLOAD_URL . DS . $fileName;
  192. if (! file_exists ( $save_path )) {
  193. \QRcode::png ( $url, $save_path, 3, 6 );
  194. }
  195. $this->tpl = 'pc_qrcode';
  196. $this->setOutput ( 'qrcode', $save_url );
  197. $this->setOutput ( 'title', $title );
  198. $this->setOutput('user_agent', $user_agent);
  199. exit ( $this->render () );
  200. }
  201. }
  202. }