EventsController.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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_4";
  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. if (isset($info['base_num']) && $info['base_num']) {
  73. $info['user_total'] += $info['base_num'];
  74. }
  75. # 获取登录地址
  76. $url = Request::g('url');
  77. $plat_form_id = $this->getPlatFormId();
  78. $thisUrl = $this->objMLottery -> getEventsUrl($info['type'],$info['create_time'],$url);
  79. $loginUrl = $this -> getLoginUrl($thisUrl);
  80. $info['thisUrl'] = $thisUrl;
  81. $this->thisUrl = $thisUrl;
  82. $this->setOutput('thisUrl', $thisUrl);
  83. $this->setOutput('loginUrl', $loginUrl);
  84. # 判断是否需要登录
  85. $ckLogin = $this->ckLogin();
  86. $this->setOutput('ckLogin', $ckLogin);
  87. # 获取用户信息
  88. $objUserData = new UserInfo();
  89. $uid = $this->getRunTimeUid();
  90. $userData = $objUserData -> get($uid);
  91. $userData['headimgurl'] = $this->getUserHead($userData);
  92. $this->setOutput('uid', $uid);
  93. $this->setOutput('userInfo', $userData);
  94. $source_user = Request::g('source_user');
  95. if (!$source_user) {
  96. $source_user = $uid;
  97. }
  98. $this->setOutput('source_user', $source_user);
  99. if ($source_user != $uid) {
  100. $userData = $objUserData -> get($source_user);
  101. $userData['headimgurl'] = $this->getUserHead($userData);
  102. }
  103. if (!$userData['nickname']) {
  104. $userData['nickname'] = '福利社网友';
  105. }
  106. $this->setOutput('userSourceInfo', $userData);
  107. $info['end'] = 1; //未过期
  108. if ($TIME > $info['end_time']) {
  109. $info['end'] = 2;//已过期
  110. }
  111. # 抽奖、优惠券、邀请、试用
  112. $type = array(1,4,6,7);
  113. if (in_array($info['type'], $type)) {
  114. $info['prize'] = 1;
  115. //$info['authorize'] = true; //强制登录
  116. } else {
  117. $info['top_data'] = 1;
  118. }
  119. # 检查当前活动是否需要登录
  120. $info['login'] = 1;
  121. $authorize = $info['authorize'];
  122. if ($authorize) {
  123. if($ckLogin==true){
  124. $info['login'] = 2;//需要登录
  125. $weixin = $this->checkIsFromWeixin();
  126. if (!$weixin && $this->applogin == 2) {
  127. self::redirect($loginUrl);
  128. }
  129. //self::redirect($loginUrl);
  130. }
  131. }
  132. # 如果是试用,判断评价
  133. $condition = array('events_id' => $info['id'],'audit' => 2, 'uid' => $uid,'scratch_receive' => LotteryData::EVENT_SCRATCH_RECEIVE_TRUE);
  134. $offset = 0;
  135. $size = 1;
  136. $order = 'id desc';
  137. $userData = $this->objMLottery->getOnesLotteryData($condition);
  138. if ($userData) {
  139. $userData['feedback'] = 1;//不需要填写反馈
  140. $userData['url'] = base64_encode($info['thisUrl']);
  141. }
  142. if ($info['type'] == 4) {
  143. $info['feedback'] = 1;
  144. if ($userData) {
  145. $objLotteryUserFeedback = new LotteryUserFeedback();
  146. $userData['feedback'] = 2;//未填写反馈
  147. $where = array();
  148. $where['uid'] = $userData['uid'];
  149. $where['events_id'] = $userData['run_time_data']['events']['id'];
  150. $where['status'] = 1;
  151. $ids = $objLotteryUserFeedback->findIdsBy ( $where, 1, $order = ' id desc');
  152. if ($ids) {
  153. $userData['feedback'] = 3;//已填写反馈
  154. }
  155. /*
  156. if (isset($userData['express']) && $userData['express']) {
  157. $userData['feedback'] = 2;//未填写反馈
  158. $where = array();
  159. $where['uid'] = $userData['uid'];
  160. $where['events_id'] = $userData['run_time_data']['events']['id'];
  161. $where['status'] = 1;
  162. $ids = $objLotteryUserFeedback->findIdsBy ( $where, 1, $order = ' id desc');
  163. if ($ids) {
  164. $userData['feedback'] = 3;//已填写反馈
  165. }
  166. } else {
  167. $userData['feedback'] = 4;//未填写收货地址
  168. }
  169. */
  170. }
  171. }
  172. if ($info['type'] == 1) {
  173. $info['feedback'] = 1;
  174. if ($userData) {
  175. $objLotteryUserFeedback = new LotteryUserFeedback();
  176. if (isset($userData['express']) && $userData['express']) {
  177. } else {
  178. $userData['feedback'] = 4;//未填写收货地址
  179. }
  180. }
  181. }
  182. $order = '`index` desc, id desc';
  183. $limit = '0,5';
  184. $condition = array ('platform_id' => $plat_form_id);
  185. $objFocus = new Focus ();
  186. $ids = $objFocus->findIdsBy ( $condition, $limit, $order );
  187. $focusData = $objFocus->gets ( $ids );
  188. $ad = false;
  189. if (isset($this->platform['ad'])) {
  190. $ad = $this->platform['ad'];
  191. }
  192. $this->setOutput('ad', $ad);
  193. $this->setOutput('focusData', $focusData);
  194. $this->setOutput('userData', $userData);
  195. $this->setOutput('info', $info);
  196. $type = Request::g('type');
  197. if ($type == 'share') {
  198. # 分享
  199. $this->share($info, $plat_form_id, $source_user);
  200. } elseif ($type == 'prize') {
  201. # 获得福利
  202. $this->prize($info, $plat_form_id, 10, false);
  203. } elseif ($type == 'feedback') {
  204. # 获得评价信息
  205. $this->feedback($info, $plat_form_id, 10, false);
  206. }
  207. $this->setOutput('type', $type);
  208. $ajax = Request::g('ajax');
  209. if ($ajax == 'ajax') {
  210. $this->tpl = 'ajax_events';
  211. $this->ajax_success_exit($this->render(true));
  212. die;
  213. }
  214. }
  215. public function share($eventsData, $plat_form_id, $uid)
  216. {
  217. $events_id = $eventsData['id'];
  218. $condition = array (
  219. 'events_id' => $events_id,
  220. 'uid' => $uid,
  221. );
  222. $objLotteryUserHelp = new LotteryUserHelp();
  223. $info = $objLotteryUserHelp->fetchOne ( $condition);
  224. if (!$info) {
  225. $id = $objLotteryUserHelp->add ( $condition);
  226. } else {
  227. $id = $info['id'];
  228. }
  229. if ($id) {
  230. $objLotteryUserHelpLog = new LotteryUserHelpLog();
  231. $condition = array (
  232. 'events_id' => $events_id,
  233. 'help_id' => $id,
  234. );
  235. $total_num = $objLotteryUserHelpLog->totals ( $condition );
  236. $ids = $objLotteryUserHelpLog->findIdsBy ( $condition, '0,50', 'id desc' );
  237. $helpData = $objLotteryUserHelpLog->gets ( $ids );
  238. if ($helpData) {
  239. foreach ($helpData as $k => $v) {
  240. $helpData[$k]['headimgurl'] = $this->getUserHead($v['uid']);
  241. }
  242. }
  243. $session = $this->shareSession($id, $uid);
  244. $this->setOutput('help_session', $session);
  245. $this->setOutput('help_id', $id);
  246. $this->setOutput('helpData', $helpData);
  247. $this->setOutput('helpDataTotal', $total_num);
  248. # 获取登录地址
  249. $thisUrl = $this->thisUrl . '/share?events_id='.$events_id.'&help_id='.$id.'&help_session='.$session.'&source_user=' . $uid;
  250. $loginUrl = $this -> getLoginUrl($thisUrl);
  251. $this->setOutput('loginUrl', $loginUrl);
  252. }
  253. }
  254. public function shareSession($id, $uid, $source_session = false)
  255. {
  256. $key = 'onepage2999';
  257. $session = md5($id . '_' . $key . '_' . $uid);
  258. if ($source_session) {
  259. if ($source_session == $session) {
  260. return true;
  261. } else {
  262. return false;
  263. }
  264. } else {
  265. return $session;
  266. }
  267. }
  268. public function feedback($eventsData, $plat_form_id, $limit = 10, $uid = false)
  269. {
  270. $objFeedBack = new LotteryUserFeedback ();
  271. $page = Request::varGetInt ( 'page', 1 );
  272. $size = $limit;
  273. $offset = ($page - 1) * $size;
  274. $limit = "{$offset},{$size}";
  275. $order = ' id desc';
  276. $condition = array (
  277. 'events_id' => $eventsData['id'],
  278. 'status' => 1,
  279. );
  280. $ids = $objFeedBack->findIdsBy ( $condition, $limit, $order );
  281. $data = $objFeedBack->gets ( $ids );
  282. $total_num = $objFeedBack->totals ( $condition );
  283. $uids = array();
  284. if ($data) {
  285. foreach ($data as $k => $v) {
  286. $uids[] = $v['uid'];
  287. }
  288. # 通过UIDs 获取用户信息
  289. $objMUser = new User();
  290. $users = $objMUser -> gets($uids);
  291. }
  292. if ($data) {
  293. $data = $this->userData($data, $eventsData);
  294. }
  295. $this->setOutput('fdData', $data);
  296. $this->setOutput('fdDataTotals', $total_num);
  297. }
  298. public function prize($eventsData, $plat_form_id, $limit = 10, $uid = false)
  299. {
  300. $condition = array('audit' => 2, 'platform_id' => SqlHelper::addCompareOperator('&', $plat_form_id), 'scratch_receive' => LotteryData::EVENT_SCRATCH_RECEIVE_TRUE);
  301. if (isset($eventsData['id'])) {
  302. $condition['events_id'] = $eventsData['id'];
  303. unset($condition['platform_id']);
  304. unset($condition['scratch_receive']);
  305. }
  306. if ($uid) {
  307. $condition['uid'] = $uid;
  308. }
  309. $totals = $this->objMLottery -> getLotteryDataListNum($condition);
  310. $page = Request::varGetInt ( 'page', 1 );
  311. $size = $limit;
  312. $offset = ($page - 1) * $size;
  313. $order = 'update_time desc';
  314. $data = $this->objMLottery->getLotteryDataList($condition, "{$offset},{$size}", $order);
  315. if ($data) {
  316. $data = $this->userData($data, $eventsData);
  317. $this->setOutput('userEventsData', $data);
  318. $this->setOutput('userEventsDataTotals', $totals);
  319. }
  320. }
  321. public function userData($data, $eventsData)
  322. {
  323. $t = Request::g('a');
  324. if ($t == 1) {
  325. print_r($data);
  326. }
  327. $uids = array();
  328. foreach ($data as $k => $v) {
  329. $uids[] = $v['uid'];
  330. }
  331. # 通过UIDs 获取用户信息
  332. $objMUser = new User();
  333. $users = $objMUser -> gets($uids);
  334. $objLotteryUserExpress = new LotteryUserExpress();
  335. $condition = array(
  336. 'uid' => $uids
  337. );
  338. $tmpUserExpIds = $objLotteryUserExpress->findIdsBy($condition);
  339. $userExpData = $objLotteryUserExpress -> gets($tmpUserExpIds);
  340. $newUserExpData = array();
  341. foreach($userExpData as $k => $v){
  342. $newUserExpData[$v['uid']] = $v;
  343. }
  344. if ($t == 1) {
  345. print_r($newUserExpData);
  346. }
  347. $this->setOutput('newUserExpData', $newUserExpData);
  348. //138****1234 的用户 2018.4.20 15:10 获得 某某某某活动 奖品。
  349. $objEvents = new LotteryEvents ();
  350. foreach ($data as $k => $v) {
  351. if (isset($v['pic'])) {
  352. $data[$k]['pic'] = explode(',', $v['pic']);
  353. }
  354. if ($v['events_id']) {
  355. $d = $objEvents->findIdsBy(array('id' => $v['events_id']));
  356. $t = $objEvents -> gets($d);
  357. $eventsData = array_pop($t);
  358. }
  359. $type = isset($eventsData['type']) ? $eventsData['type'] : $eventsData[$v['events_id']]['type'];
  360. $data[$k]['user'] = array();
  361. $data[$k]['user']['cdate'] = date('Y.m.d H:i', isset($v['winners_time']) ? $v['winners_time'] : $v['create_time']);
  362. $data[$k]['user']['events'] = isset($eventsData['events_name']) ? $eventsData['events_name'] : $eventsData[$v['events_id']]['events_name'];
  363. $data[$k]['user']['headimgurl'] = $this->getUserHead($users[$v['uid']]);
  364. $data[$k]['user']['ext'] = '';
  365. if ($type == 6) {
  366. $data[$k]['user']['ext'] = '邀请';
  367. } elseif ($type == 4) {
  368. $data[$k]['user']['ext'] = '试用资格';
  369. } elseif ($type == 7) {
  370. $data[$k]['user']['ext'] = '优惠券';
  371. } elseif ($type == 1) {
  372. $data[$k]['user']['ext'] = '奖品';
  373. }
  374. if (isset($v['express']['phone']) && $v['express']['phone']) {
  375. $data[$k]['user']['mobile'] = substr_replace($v['express']['phone'],'****',3,4);
  376. } elseif (isset($newUserExpData[$v['uid']]['phone']) && $newUserExpData[$v['uid']]['phone']) {
  377. $data[$k]['user']['mobile'] = substr_replace($newUserExpData[$v['uid']]['phone'],'****',3,4);
  378. } else {
  379. unset($data[$k]);
  380. }
  381. }
  382. return $data;
  383. }
  384. /**
  385. * 获取头像
  386. */
  387. public function getUserHead($user)
  388. {
  389. if (!is_array($user)) {
  390. $objUserData = new UserInfo();
  391. $user = $objUserData -> get($user);
  392. }
  393. if (isset($user['headimgurl']) && $user['headimgurl']) {
  394. $result = $user['headimgurl'];
  395. } else {
  396. $result = 'http://news.mydrivers.com/Img/20110518/04481549.png';
  397. }
  398. return $result;
  399. }
  400. /**
  401. * 判断当前用户是否已经登录
  402. * true 需要
  403. * false 不需要
  404. */
  405. public function ckLogin() {
  406. $cookieInfo = Cookie::get ( $this->userCookieName );
  407. $cookieInfo = html_entity_decode ( $cookieInfo );
  408. $userInfo = json_decode ( $cookieInfo, true );
  409. /*
  410. * if(!$this->checkSign($userInfo['sign'] , $userInfo)){ return true; }
  411. */
  412. if ($userInfo ['is_new'] == 0) {
  413. return true;
  414. }
  415. return false;
  416. }
  417. /**
  418. * 获取登录地址
  419. * $thisUrl : 活动URL地址
  420. */
  421. public function getLoginUrl($thisUrl) {
  422. $type = Request::g('type');
  423. if ($type == 'share' && !strstr($thisUrl, 'share')) {
  424. $thisUrl .= '/share';
  425. }
  426. // 1.拼接一个 referer 地址 
  427. $cookieInfo = html_entity_decode ( Cookie::get ( $this->userCookieName ) );
  428. $cookieInfo = json_decode ( $cookieInfo, true );
  429. $uid = $cookieInfo ['id'];
  430. $args = array (
  431. 'cas_uid' => $uid,
  432. 'cas_return_url' => urlencode ( $thisUrl ) // 绑定用户后的跳转地址
  433. );
  434. $args ['sign'] = $this->createSign ( $args );
  435. $refererUrl = urlencode ( String::jointUrl ( Request::schemeDomain () . '/SetUser/', $args ) ); // passport 返回统一着陆页
  436. // 2. 拼接passport请求地址
  437. $passport_url = Config::getInstance ()->get ( 'passport_url' );
  438. $passport_third_url = Config::getInstance ()->get ( 'passport_third_url' );
  439. $weixin = $this->checkIsFromWeixin();
  440. if ($passport_third_url && !$weixin) {
  441. $passport_url = $passport_third_url;
  442. }
  443. $passport_url .= '&referer=' . $refererUrl;
  444. $arr = $this->convertUrlQuery ( $passport_url );
  445. $arr ['sign'] = $this->createSign ( $arr );
  446. $passport_url .= '&sign=' . $arr ['sign'];
  447. $passport_url .= '&cas_uid=' . $uid;
  448. $this->setOutput('applogin', 2);
  449. $this->applogin = 2;
  450. if (!$weixin) {
  451. $tokenid = Request::g('tokenid');
  452. if (!$tokenid) {
  453. $tokenid = Request::g('tokenId');
  454. }
  455. if ($tokenid) {
  456. Cookie::set ( 'tokenid', $tokenid, 60 * 60 * 24 * 20 ,'.'.$_SERVER['HTTP_HOST']);
  457. }
  458. if (isset($_COOKIE['tokenid'])) {
  459. $tokenid = $_COOKIE['tokenid'];
  460. }
  461. if ($tokenid) {
  462. # 先检查有没有过期吧
  463. /*
  464. $config = Config::getInstance()->get('passport');
  465. $param['authorizedCode'] = 'fulishe';
  466. $param['tokenId'] = $tokenid;
  467. $data = $this->httpPost($config['url'], $param);
  468. $data = json_decode($data, true);
  469. if (isset($data['data']) && isset($data['data']['tokenId']) && $data['data']['tokenId']) {
  470. } else {
  471. # 已过期
  472. $this->setOutput('applogin', 1);
  473. $this->applogin = 1;
  474. return $passport_url;
  475. }
  476. */
  477. # app中已登陆
  478. $passport_url .= '&tokenid=' . $tokenid;
  479. } else {
  480. # app中未登陆
  481. $this->setOutput('applogin', 1);
  482. $this->applogin = 1;
  483. }
  484. #
  485. }
  486. return $passport_url;
  487. }
  488. private function httpPost($url, $param) {
  489. $curl = curl_init();
  490. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  491. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  492. curl_setopt($curl, CURLOPT_URL, $url);
  493. curl_setopt($curl, CURLOPT_POST, 1);
  494. curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
  495. $res = curl_exec($curl);
  496. curl_close($curl);
  497. return $res;
  498. }
  499. public function convertUrlQuery($url) {
  500. $arr = parse_url ( $url );
  501. $query = $arr ['query'];
  502. $queryParts = explode ( '&', $query );
  503. $params = array ();
  504. foreach ( $queryParts as $param ) {
  505. $item = explode ( '=', $param );
  506. $params [$item [0]] = $item [1];
  507. }
  508. return $params;
  509. }
  510. /**
  511. * 通过活动创建时间,返回活动ID
  512. */
  513. public function getEventsCreateTimeToId() {
  514. $create_time = Request::g ( 'create_time' );
  515. $create_time = strtotime ( $create_time );
  516. $objEvents = new LotteryEvents ();
  517. $ids = $objEvents->findIdsBy ( array (
  518. 'create_time' => $create_time
  519. ) );
  520. return array_pop ( $ids );
  521. }
  522. /**
  523. * 活动相关页面获取 plat_form_id 方法
  524. */
  525. public function getPlatFormId() {
  526. $url = Request::g ( 'url' );
  527. $objDPlatform = new Platform ();
  528. $data = $objDPlatform->getsAll ();
  529. foreach ( $data as $key => $d ) {
  530. if ($d ['url'] == $url) {
  531. $plat_form_id = $key;
  532. $this->platform = $d;
  533. }
  534. }
  535. if (empty ( $plat_form_id )) {
  536. $plat_form_id = Cookie::get ( 'plat_form_id' );
  537. } else {
  538. Cookie::set ( 'plat_form_id', $plat_form_id, 60 * 60 * 24 * 29, '.' . $_SERVER ['HTTP_HOST'] );
  539. }
  540. return $plat_form_id;
  541. }
  542. /**
  543. * 生成请求参数的sign
  544. *
  545. * @param array $params
  546. * @return String
  547. */
  548. public function createSign(array $params) {
  549. if ($params && is_array ( $params )) {
  550. ksort ( $params );
  551. $str = $this->key;
  552. foreach ( $params as $key => $value ) {
  553. if ($key != 'sign') {
  554. $str .= $key . $value;
  555. }
  556. }
  557. return strtoupper ( md5 ( $str ) );
  558. }
  559. return '';
  560. }
  561. /**
  562. * 指定的sign值是否正确
  563. *
  564. * @param String $sign
  565. * 给定需要验证的sign值
  566. * @param Array $params
  567. * 用来计算sign值的参数集合
  568. * @return Boolean
  569. */
  570. public function checkSign($sign, $params = null) {
  571. $sign2 = $this->createSign ( $params );
  572. return ($sign2 == $sign);
  573. }
  574. public function checkIsFromWeixin($title) {
  575. $state = false;
  576. $user_agent = $_SERVER ['HTTP_USER_AGENT'];
  577. if (preg_match ( '/MicroMessenger/i', $user_agent )) {
  578. $state = true;
  579. }
  580. return $state;
  581. }
  582. /**
  583. *
  584. * @param string $title
  585. */
  586. public function checkIsFromMobile($title) {
  587. //return true;
  588. if (Request::get ( 'debugs' ) == 'rabin') {
  589. return true;
  590. }
  591. // PC 端显示二维码页
  592. $isMobile = false;
  593. $user_agent = $_SERVER ['HTTP_USER_AGENT'];
  594. /*
  595. if (preg_match ( '/MicroMessenger|Mobile/i', $user_agent )) {
  596. $isMobile = true;
  597. }
  598. */
  599. if (preg_match ( '/MicroMessenger|Mobile/i', $user_agent )) {
  600. $isMobile = true;
  601. }
  602. if (! $isMobile) {
  603. include_once Config::getInstance ()->get ( 'App_Path' ) . DS . 'include/phpqrcode/phpqrcode.php';
  604. $IMG_UPLOAD_PATH = Config::getInstance ()->get ( 'upload.path' ) . DS . 'qrcode';
  605. $IMG_UPLOAD_URL = Config::getInstance ()->get ( 'upload.url' ) . DS . 'qrcode';
  606. if (! file_exists ( $IMG_UPLOAD_PATH )) {
  607. if (! mkdir ( $IMG_UPLOAD_PATH, 0775, true )) {
  608. self::fail_exit ( '创建二维码失败,原因:图片目录不存在' );
  609. }
  610. }
  611. $url = Request::url ();
  612. $fileName = Math::md5_16 ( $url ) . '.png';
  613. $save_path = $IMG_UPLOAD_PATH . DS . $fileName;
  614. $save_url = $IMG_UPLOAD_URL . DS . $fileName;
  615. if (! file_exists ( $save_path )) {
  616. \QRcode::png ( $url, $save_path, 3, 6 );
  617. }
  618. $this->tpl = 'pc_qrcode';
  619. $this->setOutput ( 'qrcode', $save_url );
  620. $this->setOutput ( 'title', $title );
  621. $this->setOutput('user_agent', $user_agent);
  622. exit ( $this->render () );
  623. }
  624. }
  625. }