1. <?php
    2. class Controller_Activity_Christ extends Controller_Base
    3. {
    4. private $_openId = null;
    5. private $_wechatUnionId = null;
    6. public function init()
    7. {
    8. parent::init();
    9. if(!Dg_Helper_Common::isWeixin() || Dg_Helper_Common::isWeixin()){
    10. //微信跳转到登录页面
    11. // 获取openid , 本地数据库中的用户数据
    12. $this->_openId = Hnb_Session::getInstance()->getSessionData('weixin_open_id');
    13. $this->_wechatUnionId = Hnb_Session::getInstance()->getSessionData('weixin_union_id');
    14. $nowUrl = "http://m.hinabian.com/activity_christ/index.html";
    15. if (!$this->_openId && !$this->_wechatUnionId) {
    16. $jumpUrl = 'http://www.hinabian.com/weixin/snsBaseForMActivity1000?login_referrer=' . urlencode($nowUrl);
    17. header("Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . WEIXIN_WEB_APP_ID . "&redirect_uri=" . urlencode($jumpUrl). "&response_type=code&scope=snsapi_base&state=1#wechat_redirec");
    18. exit();
    19. }
    20. } /*else {
    21. header("Content-type: text/html; charset=utf-8");
    22. echo "微信登录失败!<a href='http://m.hinabian.com/activity_christ/index.html'>点击重试</a>";
    23. }*/
    24. $this->assign('title',"摇滚圣诞节,high到海那边");
    25. $this->assign("share_title" , "我在海那边摇滚圣诞,一大波圣诞礼物来袭,一起来摇滚吧!");
    26. $this->assign("share_friend_title" , "我在海那边摇滚圣诞,一大波圣诞礼物来袭,一起来摇滚吧!");
    27. $this->assign("share_friend_desc" , "我在海那边摇滚圣诞,一大波圣诞礼物来袭,一起来摇滚吧!");
    28. $this->assign("share_url" , "http://m.hinabian.com/activity_christ/index.html?ss=1212");
    29. }
    30. private function _top()
    31. {
    32. $arr_max = Hnb_Model_Activity_Christ::getInstance()->selectMax();
    33. $arr_return = Dg_Weixin::getInstance()->getUserInfo(WEIXIN_WEB_APP_ID , WEIXIN_WEB_APP_SECRET , $arr_max['f_wechat_open_id']);
    34. if(!isset($arr_return['data']['nickname']))
    35. {
    36. $arr_return['data'] = $arr_max;
    37. } else {
    38. $arr_return['data']['f_headimgurl'] = $arr_return['data']['headimgurl'];
    39. }
    40. return $arr_return;
    41. }
    42. // 初始化网页授权
    43. private function _initSns()
    44. {
    45. $nowUrl = "http://m.hinabian.com/activity_christ/index.html";
    46. $jumpUrl = 'http://www.hinabian.com/weixin/snsInfo?login_referrer=' . urlencode($nowUrl);
    47. header("Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . WEIXIN_WEB_APP_ID . "&redirect_uri=" . urlencode($jumpUrl). "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirec");
    48. }
    49. public function indexAction()
    50. {
    51. $arr_visitor = Dg_Weixin::getInstance()->getUserInfo(WEIXIN_WEB_APP_ID , WEIXIN_WEB_APP_SECRET , $this->_openId);
    52. $arr_sns_visitor = unserialize(Hnb_Session::getInstance()->getSessionData('weixin_info'));
    53. // 通过公众号关注接口获取的数据错误时,那么就进行用户授权模式
    54. if(!isset($arr_visitor['data']['nickname']) && !$arr_sns_visitor && Dg_Helper_Common::isWeixin())
    55. {
    56. $this->_initSns();
    57. }
    58. $arr_wxUserInfo = $this->_top();
    59. $this->assign("share_img" , $arr_wxUserInfo['data']['f_headimgurl']);
    60. $this->assign('arr_wxUserInfo' , $arr_wxUserInfo);
    61. $this->assign("mod" , 'christ-index');
    62. $this->display("activity");
    63. }
    64. // 获取微信用户资料
    65. private function _getWxUserInfo()
    66. {
    67. $arr_visitor = Dg_Weixin::getInstance()->getUserInfo(WEIXIN_WEB_APP_ID , WEIXIN_WEB_APP_SECRET , $this->_openId);
    68. $arr_sns_visitor['data'] = unserialize(Hnb_Session::getInstance()->getSessionData('weixin_info'));
    69. return isset($arr_visitor['data']['nickname']) ? $arr_visitor : $arr_sns_visitor;
    70. }
    71. // 记录用户
    72. public function recAction()
    73. {
    74. $count = Dg_Helper_UrlParam::request('count' , '');
    75. $circle = Dg_Helper_UrlParam::request('circle' , '');
    76. $sessionKey = Hnb_Session::getInstance()->getSid();
    77. $arr_wxUserInfo = $this->_getWxUserInfo();
    78. $arr_row = array(
    79. 'f_no' => $count,
    80. //'f_circle' => $circle,
    81. 'f_name' => $arr_wxUserInfo['data']['nickname'],
    82. 'f_session_key' => $sessionKey,
    83. 'f_wechat_open_id' => $this->_openId,
    84. 'f_headimgurl' => $arr_wxUserInfo['data']['headimgurl'],
    85. 'f_ctime' => date('Y-m-d H:i:s')
    86. );
    87. $id = Hnb_Model_Activity_Christ::getInstance()->rec($arr_row);
    88. $arr_row = Hnb_Model_Activity_Christ::getInstance()->getRow(array('f_id' => $id));
    89. // On tree
    90. $arr_top = $this->_top();
    91. $return = $this->_logic($this->_openId , $id);
    92. $before = Hnb_Model_Activity_Christ::getInstance()->getGtNum($arr_row['f_no']);
    93. $arr_return = Dg_Helper_Common::initReturn($id ? NO_ERROR : ERROR_COMMON , $id ? NO_ERROR : ERROR_COMMON , $id && $return['data'] ? $return['data'] : array('f_id' => false , 'headimgurl' => $arr_top['data']['f_headimgurl'] , 'had_win' => $return['had_win'] , 'no_win' => $return['no_win'] , 'num' => $arr_row['f_no'] , 'before' => $before));
    94. $this->end($arr_return);
    95. }
    96. private function _logic($wechat_open_id , $id)
    97. {
    98. $arr_row = Hnb_Model_Activity_Christ::getInstance()->getRow(array('f_wechat_open_id' => $wechat_open_id));
    99. //$arr_return = false;
    100. $arr_return = array('had_win' => false , 'no_win' => false , 'data' => false);
    101. if(!$arr_row)
    102. {
    103. // 计算当前的用户获奖的概率
    104. $arr_row = Hnb_Model_Activity_Christ::getInstance()->getRow(array('f_id' => $id));
    105. // 随机计算
    106. //$award_id = $this->_award();
    107. $award_id = $this->_award100();
    108. //$award_id = rand(2 , 4); // 测试使用
    109. // 中奖的
    110. if($award_id)
    111. {
    112. // 查询奖品的信息
    113. $arr_return['data'] = Hnb_Model_Activity_Christ::getInstance()->getOneGift($award_id)[0];
    114. // 将该奖品放入中奖池子
    115. $a = Hnb_Model_Activity_Christ::getInstance()->updateRow(array('f_wechat_open_id' => $this->_openId));
    116. $arr_row['award_id'] = $award_id;
    117. $logPath = DOC_ROOT . '/log/activity/' . date('Y-m-d') . '-awards.log';
    118. Dg_Helper_Log::getInstance()->init($logPath);
    119. Dg_Helper_Log::getInstance()->info($arr_row);
    120. } else {
    121. $arr_return['no_win'] = true;
    122. }
    123. } else {
    124. $arr_return['had_win'] = true;
    125. }
    126. return $arr_return;
    127. }
    128. public function _award100()
    129. {
    130. return false;
    131. $ret = $this->_limitNum(rand(1 , 100));
    132. if($ret)
    133. {
    134. $arr_row = array(
    135. 'f_wechat_open_id' => $this->_openId,
    136. 'f_session_key' => Hnb_Session::getInstance()->getSid(),
    137. 'f_award_id' => $ret,
    138. 'f_ctime' => date('Y-m-d H:i:s')
    139. );
    140. Hnb_Model_Activity_Christ::getInstance()->insertWin($arr_row);
    141. }
    142. return $ret;
    143. }
    144. private function _award()
    145. {
    146. $percent = mt_rand() / mt_getrandmax();
    147. // 8 , 9 , 10
    148. // Hnb
    149. if($percent < 0.01212)
    150. {
    151. $no = array(11);
    152. }
    153. if($percent < 0.01212)
    154. {
    155. $no = array(11);
    156. }
    157. // lvpeng
    158. if($percent < 0.38787)
    159. {
    160. $no = array(8 , 9 , 10);
    161. }
    162. // lvpeng
    163. if($percent < 0.377)
    164. {
    165. $no = array(8 , 9 , 10);
    166. }
    167. // lvpeng
    168. if($percent < 0.34444)
    169. {
    170. $no = array(8 , 9 , 10);
    171. }
    172. // lvpeng
    173. if($percent < 0.25)
    174. {
    175. $no = array(8 , 9 , 10);
    176. }
    177. // dada
    178. if($percent < 0.48485)
    179. {
    180. $no = array(1);
    181. }
    182. // qiubao
    183. if($percent < 0.00485)
    184. {
    185. $no = array(2);
    186. }
    187. // qiubao
    188. if($percent < 0.90)
    189. {
    190. $no = array(2);
    191. }
    192. // jumi
    193. if($percent < 0.11271)
    194. {
    195. $no = array(5 , 6 , 7);
    196. }
    197. if($percent < 0.18)
    198. {
    199. $no = array(5 , 6 , 7);
    200. }
    201. if($percent < 0.38)
    202. {
    203. $no = array(5 , 6 , 7);
    204. }
    205. if($percent < 0.74)
    206. {
    207. $no = array(5 , 6 , 7);
    208. }
    209. $r = false;
    210. if($no)
    211. {
    212. if(count($no) > 1)
    213. {
    214. $rand = rand(1 ,11);
    215. if(in_array($rand , $no))
    216. {
    217. $r = $rand;
    218. } else {
    219. return false;
    220. }
    221. } else {
    222. $r = $no[0];
    223. }
    224. // 奖品数量比较大的brand
    225. $arr_id = array(9 , 10 , 5 , 6 , 7);
    226. // 如果奖品消耗完了,立即返回 false
    227. $this->_limitNum($r);
    228. if(in_array($r , $arr_id))
    229. {
    230. $arr_row = array(
    231. 'f_wechat_open_id' => $this->_openId,
    232. 'f_session_key' => Hnb_Session::getInstance()->getSid(),
    233. 'f_award_id' => $r,
    234. 'f_ctime' => date('Y-m-d H:i:s')
    235. );
    236. Hnb_Model_Activity_Christ::getInstance()->insertWin($arr_row);
    237. } else {
    238. return 0;
    239. }
    240. }
    241. return $r;
    242. }
    243. // 限制数量
    244. private function _limitNum($id)
    245. {
    246. $arr_nums = array(
    247. 1 => 2000,
    248. 2 => 20,
    249. 3 => 5,
    250. 4 => 30,
    251. 5 => 30,
    252. 6 => 200,
    253. 7 => 200,
    254. 8 => 100,
    255. 9 => 500,
    256. 10 => 1000,
    257. 11 => 50
    258. );
    259. $arr_awNums = Hnb_Model_Activity_Christ::getInstance()->getNumOfAw();
    260. $arr_awNums = array_column($arr_awNums , 'tt' , 'id');
    261. if($arr_awNums[$id] >= $arr_nums[$id])
    262. {
    263. return false;
    264. }
    265. }
    266. // 规则
    267. public function ruleAction()
    268. {
    269. $this->assign("share_img" , "http://cache.hinabian.com/mobile/images/activity/christ/meman.png");
    270. $this->assign("mod" , "christ-rule");
    271. $this->display("activity");
    272. }
    273. public function focusAction()
    274. {
    275. $arr_allRows = Hnb_Model_Activity_Christ::getInstance()->getAll(array('f_name' => true , 'f_headimgurl' => true , 'f_no' => true));
    276. // 每个wechat_open_id取最大值的f_no
    277. //$arr_focus = array_slice($arr_allRows , 0 , 10);
    278. $arr_focus = array();
    279. $wechat_open_id = null;
    280. foreach($arr_allRows as $k => $u)
    281. {
    282. // 分组
    283. $arr_focus[$u['f_wechat_open_id']][] = $u;
    284. }
    285. $arr_return = [];
    286. foreach($arr_focus as $rows)
    287. {
    288. usort($rows , array($this , '_cmp'));
    289. $arr_return[] = $rows[0];
    290. }
    291. if(count($arr_return) > 1)
    292. {
    293. usort($arr_return , array($this , '_cmp'));
    294. }
    295. $this->assign("share_img" , "http://cache.hinabian.com/mobile/images/activity/christ/meman.png");
    296. $this->assign('arr_focus' , $arr_return);
    297. $this->assign("mod" , "christ-focus");
    298. $this->display("activity");
    299. }
    300. public function ssAction()
    301. {
    302. $name = Dg_Helper_UrlParam::request('name' , '');
    303. $mobile = Dg_Helper_UrlParam::request('mobile' , '');
    304. $addr = Dg_Helper_UrlParam::request('addr' , '');
    305. $id = Dg_Helper_UrlParam::request('id' , '');
    306. $validate = new Dg_Helper_Validate();
    307. $validate->addRule('name' , array('required' => true));
    308. $validate->addRule('mobile' , array('required' => true));
    309. if($id != 4)
    310. {
    311. $validate->addRule('addr' , array('required' => true));
    312. }
    313. $this->runCheck($validate);
    314. $arr_row = array(
    315. 'f_name' => $name,
    316. 'f_mobile' => $mobile,
    317. 'f_addr' => $addr,
    318. 'f_award_id' => $id,
    319. 'f_wechat_open_id' => $this->_openId,
    320. 'f_ctime' => date('Y-m-d H:i:s')
    321. );
    322. $id = Hnb_Model_Activity_Christ::getInstance()->insertAward($arr_row);
    323. $this->end(Dg_Helper_Common::initReturn($id ? NO_ERROR : ERROR_COMMON , $id ? NO_ERROR : ERROR_COMMON , $id ? $id : '您的网络有点慢,请稍后再试。'));
    324. }
    325. private function _cmp($a , $b)
    326. {
    327. if($a['f_no'] == $b['f_no'])
    328. {
    329. return 0;
    330. }
    331. return ($a['f_no'] > $b['f_no']) ? -1 : 1;
    332. }
    333. public function testAction()
    334. {
    335. $arr_allGifts = Hnb_Model_Activity_Christ::getInstance()->getAllGifts();
    336. $this->assign('arr_allGifts' , $arr_allGifts);
    337. $this->assign('mod' , 'christ-test');
    338. $this->display('activity');
    339. }
    340. public function ttAction()
    341. {
    342. $arr_wechatOpenIds= array(
    343. 'oBOvWtyThe9Mc7-hAWSfT48BGbU4',
    344. 'oBOvWt-AHcqbdd84YVV-SXUQzbFU',
    345. 'oBOvWt4asEQla9H5Yld2ynTMMOzU',
    346. 'oBOvWtwZI7FJUvTdjpo7833su-sA',
    347. 'oBOvWt2Tu1m8Lq5opkzwONl0I90s',
    348. 'oBOvWtxLzYSbfYA6IjsrDbkAtpBM'
    349. );
    350. $arr_return = [];
    351. foreach($arr_wechatOpenIds as $id)
    352. {
    353. $arr_return[] = Dg_Weixin::getInstance()->getUserInfo(WEIXIN_WEB_APP_ID , WEIXIN_WEB_APP_SECRET , $id);
    354. }
    355. print_r($arr_return);
    356. }
    357. public function getaumAction()
    358. {
    359. $results = Hnb_Model_Activity_Christ::getInstance()->selectAwIdNum(3);
    360. var_dump($results);
    361. }
    362. }