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. return '';
  73. // 1.拼接一个 referer 地址 
  74. $cookieInfo = html_entity_decode ( Cookie::get ( $this->userCookieName ) );
  75. $cookieInfo = json_decode ( $cookieInfo, true );
  76. $uid = $cookieInfo ['id'];
  77. $args = array (
  78. 'cas_uid' => $uid,
  79. 'cas_return_url' => urlencode ( $thisUrl ) // 绑定用户后的跳转地址
  80. );
  81. $args ['sign'] = $this->createSign ( $args );
  82. $refererUrl = urlencode ( String::jointUrl ( Request::schemeDomain () . '/SetUser/', $args ) ); // passport 返回统一着陆页
  83. // 2. 拼接passport请求地址
  84. $passport_url = Config::getInstance ()->get ( 'passport_url' );
  85. $passport_url .= '&referer=' . $refererUrl;
  86. $arr = $this->convertUrlQuery ( $passport_url );
  87. $arr ['sign'] = $this->createSign ( $arr );
  88. $passport_url .= '&sign=' . $arr ['sign'];
  89. return $passport_url;
  90. }
  91. public function convertUrlQuery($url) {
  92. $arr = parse_url ( $url );
  93. $query = $arr ['query'];
  94. $queryParts = explode ( '&', $query );
  95. $params = array ();
  96. foreach ( $queryParts as $param ) {
  97. $item = explode ( '=', $param );
  98. $params [$item [0]] = $item [1];
  99. }
  100. return $params;
  101. }
  102. /**
  103. * 通过活动创建时间,返回活动ID
  104. */
  105. public function getEventsCreateTimeToId() {
  106. $create_time = Request::g ( 'create_time' );
  107. $create_time = strtotime ( $create_time );
  108. $objEvents = new LotteryEvents ();
  109. $ids = $objEvents->findIdsBy ( array (
  110. 'create_time' => $create_time
  111. ) );
  112. return array_pop ( $ids );
  113. }
  114. /**
  115. * 活动相关页面获取 plat_form_id 方法
  116. */
  117. public function getPlatFormId() {
  118. $url = Request::g ( 'url' );
  119. $objDPlatform = new Platform ();
  120. $data = $objDPlatform->getsAll ();
  121. foreach ( $data as $key => $d ) {
  122. if ($d ['url'] == $url) {
  123. $plat_form_id = $key;
  124. }
  125. }
  126. if (empty ( $plat_form_id )) {
  127. $plat_form_id = Cookie::get ( 'plat_form_id' );
  128. } else {
  129. Cookie::set ( 'plat_form_id', $plat_form_id, 60 * 60 * 24 * 29, '.' . $_SERVER ['HTTP_HOST'] );
  130. }
  131. return $plat_form_id;
  132. }
  133. /**
  134. * 生成请求参数的sign
  135. *
  136. * @param array $params
  137. * @return String
  138. */
  139. public function createSign(array $params) {
  140. if ($params && is_array ( $params )) {
  141. ksort ( $params );
  142. $str = $this->key;
  143. foreach ( $params as $key => $value ) {
  144. if ($key != 'sign') {
  145. $str .= $key . $value;
  146. }
  147. }
  148. return strtoupper ( md5 ( $str ) );
  149. }
  150. return '';
  151. }
  152. /**
  153. * 指定的sign值是否正确
  154. *
  155. * @param String $sign
  156. * 给定需要验证的sign值
  157. * @param Array $params
  158. * 用来计算sign值的参数集合
  159. * @return Boolean
  160. */
  161. public function checkSign($sign, $params = null) {
  162. $sign2 = $this->createSign ( $params );
  163. return ($sign2 == $sign);
  164. }
  165. /**
  166. *
  167. * @param string $title
  168. */
  169. public function checkIsFromMobile($title) {
  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. }