MengbaoLogic.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. <?php
  2. /**
  3. * 佩奇拼图活动(由萌鸡快跑活动改写)
  4. * @author xusong
  5. * @version 0.0.1
  6. * @datetime 2019.07.25
  7. */
  8. namespace Home\Logic\Activityv2;
  9. class MengbaoLogic extends BaseLogic
  10. {
  11. private $act_id;
  12. private $uid;
  13. private $userinfo;
  14. private $activityinfo;
  15. private $vip_level;
  16. /**
  17. * [_initialize 前置操作-继承公共前置方法]
  18. * @author Devil
  19. * @blog http://gong.gg/
  20. * @version 0.0.1
  21. * @datetime 2016-12-03T12:39:08+0800
  22. */
  23. public function _initialize() {
  24. // 调用父类前置方法
  25. parent::_initialize();
  26. $this->uid = trim(I('uid'));
  27. if(!$this->userinfo = M('iptv_users')->where(['uid'=>$this->uid])->find()){
  28. $this->responseError('uid 参数非法');
  29. }
  30. $this->act_id = (int)I('act_id');
  31. if(!$this->activityinfo = M('Activity_v2')->where(['id'=>$this->act_id])->find()){
  32. $this->responseError('act_id 参数非法');
  33. }
  34. //检查vip鉴权方式
  35. $this->vip_level = I('is_vip') >= 0 ? I('is_vip') : 0;
  36. }
  37. public function getMengbaoList()
  38. {
  39. $page = I('page',1);
  40. $limit = I('limit',8);
  41. $count = M('activity_mengbao')->where(['is_show'=>1])->count();
  42. $order = I('order','id');
  43. if($order == 'id'){
  44. $order = 'id asc';
  45. }else{
  46. $order = 'score desc , id asc';
  47. }
  48. $maxPage = ceil($count/$limit);
  49. $error = ['error'=>'error'];
  50. if( ($page < 1) || ($page > $maxPage)){
  51. $this->responseError('没有更多萌宝资料!', -3, $error);
  52. }
  53. $data = M('activity_mengbao')->field('id as mengbao_id,name,age,image,media_id,media_num,score')->where(['is_show'=>1])->order($order)->limit(($page-1)*$limit,$limit)->select();
  54. foreach ($data as $key=>$value) {
  55. $data[$key]['image'] = __MY_URL__.$value['image'];
  56. }
  57. $this->responseSuccess($data,'萌宝资料');
  58. }
  59. /**
  60. * 获取活动状态
  61. * @return [type] [description]
  62. */
  63. public function getActStatus()
  64. {
  65. //开始时间结束时间未作限制
  66. if($this->activityinfo['is_enable'] == 1){
  67. $this->responseSuccess(1, "请求成功!");
  68. }else{
  69. $this->responseSuccess(-1, "请求成功!");
  70. }
  71. }
  72. /**
  73. * 提交用户资料
  74. * @return [type] [description]
  75. */
  76. public function setUserInfo()
  77. {
  78. $phone = trim(I('phone'));
  79. if (empty($phone)) {
  80. $this->responseError('参数错误!');
  81. }
  82. //检测手机号码是否正确
  83. if (!preg_match('/^1[345789]\d{9}$/ims', $phone)) {
  84. $this->responseError('手机号填写有误!');
  85. }
  86. //更新用户活动手机号码
  87. $where = array(
  88. 'uid'=>$this->uid,
  89. 'act_id'=>$this->act_id,
  90. );
  91. if ($id = M('activity_user_v2')->where($where)->getField('id')){
  92. $update['user_phone']= $phone;
  93. $update['updated_at']= date('Y-m-d H:i:s');
  94. if(M('activity_user_v2')->where(['id'=>$id])->save($update)===false){
  95. Writelog(M()->getLastSql(),'sql','prize');
  96. $this->responseError("更新失败!");
  97. }
  98. }else{
  99. $insert_data = array(
  100. 'uid'=>$this->uid,
  101. 'act_id'=>$this->act_id,
  102. 'user_phone'=>$phone,
  103. 'created_at'=>date('Y-m-d H:i:s'),
  104. 'updated_at'=>date('Y-m-d H:i:s')
  105. );
  106. if(!M('activity_user_v2')->add($insert_data)){
  107. Writelog(M()->getLastSql(),'sql','prize');
  108. $this->responseError("登记失败!");
  109. }
  110. }
  111. $this->responseSuccess(0, "登记成功!");
  112. }
  113. public function vote()
  114. {
  115. $score = I('score',1);
  116. $mengbao_id = I('mengbao_id');
  117. $remain_vote_chance = $this->_getVoteChance();
  118. if(($remain_chance = $remain_vote_chance - $score) < 0){
  119. $this->responseError('您没有足够的投票次数,投票失败');
  120. }
  121. //更新萌宝获得总票数
  122. M('activity_mengbao')->where(['id'=>$mengbao_id])->setInc('score',$score);
  123. if($this->vip_level==2){
  124. //已投票数
  125. $vip_vote = M('activity_vip_vote')->where(['uid'=>$this->uid,'act_id'=>$this->act_id])->field('id,num')->find();
  126. $alrady_vip_vote = $vip_vote['num'] ? : 0;
  127. //剩余数
  128. $remain_vip_vote = 200 - $alrady_vip_vote;
  129. $diff = $remain_vip_vote - $score;
  130. if($diff >= 0){
  131. M('activity_vip_vote')->where(['id'=>$vip_vote['id']])->setInc('num',$diff);
  132. }else{
  133. M('activity_vip_vote')->where(['id'=>$vip_vote['id']])->save(['num'=>200]);
  134. }
  135. }
  136. //插入
  137. $insert_data = [
  138. 'act_id'=>$this->act_id,
  139. 'uid' =>$this->uid,
  140. 'num' =>$score,
  141. 'mengbao_id'=>$mengbao_id,
  142. 'created_at'=>date('Y-m-d H:i:s'),
  143. 'date' =>date('Ymd')
  144. ];
  145. M('activity_vote')->add($insert_data);
  146. $this->responseSuccess(['num'=>$remain_chance],'投票成功,去参与抽奖吧');
  147. }
  148. /**ascii码从小到大排序
  149. * @param array $params
  150. * @return bool|string
  151. */
  152. function asc_sort($params = array())
  153. {
  154. if (!empty($params)) {
  155. $p = ksort($params);
  156. if ($p) {
  157. $str = '';
  158. foreach ($params as $k => $val) {
  159. $str .= $k . '=' . $val . '&';
  160. }
  161. $strs = rtrim($str, '&');
  162. return $strs;
  163. }
  164. }
  165. return false;
  166. }
  167. public function checkYearVip(){
  168. $uid = $this->uid;
  169. $ip = get_client_ip();
  170. $mac = I('mac');
  171. $version = I('version');
  172. $params = [
  173. 'userid'=>$uid,
  174. 'skuid'=>'30441',
  175. 'stime'=>'20200116000000',
  176. 'etime'=>'20201231000000',
  177. 'business_id'=>'1',
  178. 'ip'=>$ip,
  179. 'mac'=>$mac
  180. ];
  181. if(empty($uid) || empty($mac) || empty($version)){
  182. $this->responseError('检查uid mac version 参数',-1);
  183. }
  184. $data_json = json_encode($params,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  185. $asc_sort_arr = [
  186. 'version'=>$version,
  187. 'data'=>$data_json
  188. ];
  189. $asc_sort = $this->asc_sort($asc_sort_arr);
  190. $secret_key = '82dawe3ihz!@~5d32s(*%aaa(#aa&jy$';
  191. $params_string = strtolower($asc_sort.'&secret_key='.$secret_key);
  192. $sign = sha1($params_string);
  193. $request_string = 'sign='.$sign.'&version='.$version.'&data='.$data_json;
  194. //测试地址
  195. $url = 'http://10.2.204.141:8081/cms/ever_active_product';
  196. $ch = curl_init($url);
  197. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  198. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  199. curl_setopt($ch, CURLOPT_POSTFIELDS, $request_string); //设置提交的字符串
  200. $output = curl_exec($ch);
  201. $error_num = curl_errno($ch);
  202. curl_close($ch);
  203. if($error_num){
  204. Writelog($error_num,'error_num','curl_error');
  205. $this->responseError('服务异常', -1);
  206. }
  207. // $output = '{"msg":"是活动内订购","err":"0","status":"1111"}';
  208. $return = json_decode($output,true);
  209. if($return['status']== '0000'){
  210. $this->responseSuccess(['status'=>0],'is year_vip');
  211. }else{
  212. $this->responseSuccess(['status'=>2],'is not year_vip');
  213. }
  214. }
  215. /**
  216. * 获取剩余投票次数
  217. */
  218. public function _getVoteChance()
  219. {
  220. if($this->vip_level >= 1){
  221. $vote = 10;
  222. }else{
  223. $vote = 5;
  224. }
  225. $add = 0;
  226. if($this->vip_level ==2){
  227. $num = M('activity_vip_vote')->where(['uid'=>$this->uid,'act_id'=>$this->act_id])->getField('num');
  228. if($num === null){
  229. $insert = [
  230. 'act_id'=>$this->act_id,
  231. 'uid'=>$this->uid,
  232. 'num'=>0,
  233. 'created_at'=>date('Y-m-d H:i:s')
  234. ];
  235. M('activity_vip_vote')->add($insert);
  236. }
  237. $add = 200 - $num;
  238. }
  239. $vote += $add;
  240. //查看当前已投票次数
  241. $alreay_num = M('activity_vote')->where(['date'=>date('Ymd'),'uid'=>$this->uid,'act_id'=>$this->act_id])->sum('num');
  242. //查看当前应有票数
  243. if($vote <= $alreay_num){
  244. return 0;
  245. }
  246. return $vote > $alreay_num ? $vote - $alreay_num : 0;
  247. }
  248. /**
  249. * 获取剩余投票次数
  250. */
  251. public function getVoteChance()
  252. {
  253. $data['num'] = $this->_getVoteChance();
  254. $this->responseSuccess($data,'投票剩余次数');
  255. }
  256. /**
  257. * 提交用户资料
  258. * @return [type] [description]
  259. */
  260. public function setUserInfoByphone()
  261. {
  262. $phone = trim(I('phone'));
  263. $address = trim(I('address'));
  264. $receiver = trim(I('receiver'));
  265. if (!($phone && $address && $receiver)) {
  266. $this->responseError('参数错误!');
  267. }
  268. //检测手机号码是否正确
  269. if (!preg_match('/^1[345789]\d{9}$/ims', $phone)) {
  270. $this->responseError('手机号填写有误!');
  271. }
  272. //更新用户活动手机号码
  273. $where = array(
  274. 'uid'=>$this->uid,
  275. 'act_id'=>$this->act_id,
  276. );
  277. if ($id = M('activity_user_v2')->where($where)->getField('id')){
  278. $update['user_phone']= $phone;
  279. $update['address'] = $address;
  280. $update['receiver'] = $receiver;
  281. $update['updated_at'] = date('Y-m-d H:i:s');
  282. if(M('activity_user_v2')->where(['id'=>$id])->save($update)===false){
  283. Writelog(M()->getLastSql(),'sql','prize');
  284. $this->responseError("更新失败!");
  285. }
  286. }else{
  287. $insert_data = array(
  288. 'uid'=>$this->uid,
  289. 'act_id'=>$this->act_id,
  290. 'user_phone'=>$phone,
  291. 'address'=>$address,
  292. 'receiver'=>$receiver,
  293. 'created_at'=>date('Y-m-d H:i:s'),
  294. 'updated_at'=>date('Y-m-d H:i:s'),
  295. );
  296. if(!M('activity_user_v2')->add($insert_data)){
  297. Writelog(M()->getLastSql(),'sql','prize');
  298. $this->responseError("登记失败!");
  299. }
  300. }
  301. $this->responseSuccess(0, "登记成功!");
  302. }
  303. /**
  304. * 活动规则说明
  305. * @return [type] [description]
  306. */
  307. public function getActivityInfo()
  308. {
  309. // //获取活动规则信息
  310. $data['act'] = $this->activityinfo;
  311. $data['act']['info'] = explode('######', $this->activityinfo['introduce']);
  312. $data['act']['info'][0] = mb_substr(htmlspecialchars_decode($data['act']['info'][0]), 0, -3) ;
  313. $data['act']['info'][1] = mb_substr(htmlspecialchars_decode($data['act']['info'][1]), 4);
  314. //获取当前活动的手机号码
  315. $user_phone = M('activity_user_v2')->where(['act_id'=>$this->act_id,'uid'=>$this->uid])->getField('user_phone');
  316. if ($user_phone) {
  317. $data['user']['user_phone'] = $user_phone;
  318. }
  319. //获取奖品信息
  320. $prizes_tmp = json_decode($this->activityinfo['prize_list'],TRUE);
  321. foreach ($prizes_tmp as $key => $v) {
  322. $prizes[$v['prize_id']] = $v;
  323. if($v['prize_status'] == 0 || $v['prize_status'] == -1){
  324. continue;
  325. }
  326. $prize_list[] = ['id'=>$v['prize_id'],'prize_name'=>$v['prize_name'],'prize_img'=>$v['prize_img']];
  327. }
  328. // var_dump($prizes);die;
  329. $data['prizes'] = $prize_list;
  330. //获取中奖信息
  331. $data['rank'] = $this->prizelog('localhost');
  332. unset($data['act']['prize_list']);
  333. unset($data['act']['prize_rule']);
  334. //获取当前用户中奖信息
  335. $map['act_id'] = $this->act_id;
  336. $map['uid'] = $this->uid;
  337. $map['rule_id'] = array("neq", 0);
  338. //$data['my_prizes'] = M('prize_log_v2')->field('prize_id,prize_name')->where($map)->select();
  339. $my_prizes = M('prize_log_v2')->field('created_at,prize_id')->where($map)->select() ? : array();
  340. if($my_prizes){
  341. foreach ($my_prizes as $k=>$value) {
  342. $my_prizes[$k]['prize_name'] = $prizes[$value['prize_id']]['prize_name'];
  343. $data['my_prizes'][] = ['prize_id'=>$value['prize_id'],'prize_name'=>$prizes[$value['prize_id']]['prize_name']];
  344. }
  345. }
  346. //获取用户剩余中奖次数
  347. $data['remain_chance'] = $this->_getUserChance();
  348. $this->responseSuccess($data);
  349. }
  350. /**
  351. * 获取用户当日抽奖次数
  352. * @param [type] $uid [description]
  353. * @return [type] [description]
  354. */
  355. public function _getUserChance()
  356. {
  357. $map['uid'] = $this->uid;
  358. $map['act_id'] = $this->act_id;
  359. $map['date'] = date('Ymd');
  360. $chance = M('prize_log_v2')->where($map)->count();
  361. if(($this->activityinfo['is_test'] == 1) && (I('is_test') == 1)){
  362. return 1;
  363. }
  364. if($chance){
  365. return 0;
  366. }
  367. //是否参与了投票
  368. $bool = M('activity_vote')->where([$map])->find();
  369. if($bool){
  370. return 1;
  371. }else{
  372. return -1;
  373. }
  374. }
  375. /**
  376. * 获取用户剩余抽奖次数
  377. * @return [type] [description]
  378. */
  379. public function getPrizeNum()
  380. {
  381. $remain_chance = $this->_getUserChance();
  382. if($remain_chance == 0){
  383. $this->responseSuccess(['num'=>0],'您今天的抽奖次数用完了');
  384. }
  385. if($remain_chance == 1){
  386. $this->responseSuccess(['num'=>1],'您今天的剩余次数');
  387. }
  388. if($remain_chance == -1){
  389. $this->responseSuccess(['num'=>-1],'参与投票,即可抽奖');
  390. }
  391. }
  392. /**
  393. * 所有中奖记录查询
  394. */
  395. public function prizelog($model = ''){
  396. $error[] = ['error' => 'error'];
  397. $where['pl.act_id'] = $this->act_id;
  398. $where['pl.prize_status'] = 1;
  399. $data = M('prize_log_v2')->alias('pl')
  400. ->join(array('left join '.C("DB_PREFIX").'activity_user_v2 as iu on iu.uid = pl.uid and iu.act_id = '.$this->act_id))
  401. ->where($where)
  402. ->order('pl.id desc')
  403. ->field('pl.prize_id,pl.created_at,pl.uid,iu.user_phone')
  404. ->select();
  405. $prizes = json_decode($this->activityinfo['prize_list'],TRUE);
  406. foreach ($prizes as $value) {
  407. $prize_list[$value['prize_id']] = $value;
  408. }
  409. if($data){
  410. foreach ($data as $key=>$value) {
  411. $data[$key]['prize_name'] = $prize_list[$value['prize_id']]['prize_name'];
  412. $data[$key]['prize_img'] = __MY_URL__.$prize_list[$value['prize_id']]['prize_img'];
  413. }
  414. }
  415. //增加虚拟中奖信息
  416. $default_data = $this->getPrizeDefault();
  417. if($data || $default_data){
  418. if($data){
  419. foreach ($data as $k=>$v){
  420. if($v['user_phone']){
  421. $data[$k]['user_phone'] = substr_replace($v['user_phone'],'****',3,4);
  422. }else {
  423. $data[$k]['user_phone'] = substr_replace($v['uid'],'****',6);
  424. }
  425. unset($data[$k]['uid']);
  426. }
  427. }
  428. if($data && $default_data){
  429. $data = array_merge($default_data,$data);
  430. }else{
  431. $data = $data ? : $default_data;
  432. }
  433. array_multisort(array_column($data,'created_at'),SORT_DESC,$data);
  434. if($model == 'localhost'){
  435. return $data;
  436. }
  437. $this->responseSuccess($data, '查询成功!');
  438. }else{
  439. if($model == 'localhost'){
  440. return array();
  441. }
  442. $this->responseError('没有抽奖记录!', -3, $error);
  443. }
  444. }
  445. /**
  446. * 我的中奖记录查询
  447. */
  448. public function userPrizelog(){
  449. $error[] = ['error' => 'error'];
  450. //获取用户
  451. if($user_phone = M('activity_user_v2')->where(['uid'=>$this->uid,'act_id'=>$this->act_id])->getField('user_phone')){
  452. $phone_staus = 1;//填过
  453. }else{
  454. $phone_staus = 2;//填过
  455. }
  456. $where = array();
  457. $where['pl.prize_status'] = 1;
  458. $where['pl.uid'] = $this->uid;
  459. $where['pl.act_id'] = $this->act_id;
  460. $data = M('prize_log_v2')->alias('pl')
  461. ->where($where)
  462. ->order('pl.id desc')
  463. ->field('pl.prize_id,pl.created_at')
  464. ->select();
  465. // echo M()->getLastSql();die;
  466. $prizes = json_decode($this->activityinfo['prize_list'],TRUE);
  467. foreach ($prizes as $value) {
  468. $prize_list[$value['prize_id']] = $value;
  469. }
  470. $flag = $data ? 1 : 0;
  471. if($flag){
  472. if($data){
  473. foreach ($data as $k => $v){
  474. $data[$k]['prize_name'] = $prize_list[$v['prize_id']]['prize_name'];
  475. $data[$k]['prize_object'] = 'real';
  476. $data[$k]['show_image'] = $prize_list[$v['prize_id']]['prize_img'] ? __MY_URL__.$prize_list[$v['prize_id']]['prize_img']:null;
  477. unset($data[$k]['prize_id']);
  478. }
  479. }
  480. $return_data = array(
  481. 'my_prizes'=>$data,
  482. 'user_phone'=>['phone_status'=>$phone_staus,'user_phone'=>$user_phone ? :''],
  483. 'prize_status'=>$data ? 'real' : 'vitual'
  484. );
  485. $this->responseSuccess($return_data, '查询成功!');
  486. }else{
  487. $this->responseError('没有抽奖记录!', -3, $error);
  488. }
  489. }
  490. /**
  491. * 抽奖
  492. */
  493. public function prize(){
  494. $error[] = ['error' => 'error'];
  495. $chance = $this->_getUserChance();
  496. if($chance == 0){
  497. $this->responseError('您今天的抽奖次数用完了');
  498. }
  499. if($chance == -1){
  500. $this->responseSuccess('参与投票,即可抽奖');
  501. }
  502. //开始抽奖
  503. $prize_data = $this->_get_prize();
  504. unset($prize_data['yes']['rule_id']);
  505. $this->responseSuccess($prize_data, '抽奖成功!');
  506. }
  507. private function _get_prize() {
  508. $result = $this->getPrizeSetDeatil();
  509. // var_dump($result);die;
  510. $this->_writePrizeLog($result);
  511. $res = array();
  512. $res['yes']['prize_name'] = $result['prize_name']; //中奖项
  513. $res['yes']['prize_status'] = $result['prize_object'] ? : 'none';
  514. $res['yes']['prize_img'] = $result['prize_img'] ? __MY_URL__.$result['prize_img'] : '';
  515. $res['yes']['rule_id'] = $result['rule_id'];
  516. return $res;
  517. }
  518. /**
  519. * @param desc 写入抽奖日志
  520. * @param type $result
  521. */
  522. private function _writePrizeLog(&$result) {
  523. $log_data['uid'] = $this->uid;
  524. $log_data['prize_id'] = $result['prize_id'];
  525. $log_data['is_vip'] = $this->vip_level;
  526. $log_data['act_id'] = $this->act_id;
  527. $log_data['created_at'] = date('Y-m-d H:i:s');
  528. $log_data['date'] = date('Ymd');
  529. $log_data['rule_id'] = $result['rule_id'];
  530. $log_data['prize_status'] = $result['prize_status'];
  531. if(!M('prize_log_v2')->add($log_data)){
  532. Writelog(M()->getLastSql(),'sql','activity');
  533. $this->responseError('系统繁忙抽奖失败', -1);
  534. }
  535. }
  536. /**
  537. * 返回中奖的信息
  538. */
  539. private function getPrizeSetDeatil()
  540. {
  541. $prize_list = json_decode($this->activityinfo['prize_list'],TRUE);//获取奖项设置
  542. if($prize_list){
  543. foreach ($prize_list as $row) {
  544. $limit_sales[$row['prize_id']] = $row;
  545. if($row['prize_level']== 0){
  546. $impossible_prize = ['prize_id'=>$row['prize_id'],'prize_name'=>$row['prize_name'],'rule_id'=>0,'prize_img'=>'','prize_status'=>0];
  547. }
  548. }
  549. }
  550. //获取所有规则
  551. $rules = json_decode($this->activityinfo['prize_rule'],TRUE);
  552. if(!$rules){
  553. return $impossible_prize;
  554. }
  555. if(I('run')!='complate'){
  556. return $impossible_prize;
  557. }
  558. //如果中了一次实物奖就不允许中第二次
  559. $map = array(
  560. 'act_id'=> $this->act_id,
  561. 'uid'=> $this->uid,
  562. 'prize_status'=>1,
  563. );
  564. if(M('Prize_log_v2')->where($map)->find()){
  565. return $impossible_prize;
  566. }
  567. $swithc_no = '';
  568. foreach ($rules as $value) {
  569. if($value['rule_status'] == 0){
  570. $swithc_no = $swithc_no. 0;
  571. continue;
  572. }
  573. //1.检查是否符合身份
  574. if(FALSE === $this->_checkRuleRole($value['rule_role'])){
  575. $swithc_no = $swithc_no. 1;
  576. continue;
  577. }
  578. //2.检查中奖日期设置
  579. if(FALSE === $this->_checkRuleDate($value['rule_date'])){
  580. $swithc_no = $swithc_no. 2;
  581. continue;
  582. }
  583. //3.检查时段设置
  584. if(FALSE === $this->_checkRuleHour($value['rule_hour'])){
  585. $swithc_no = $swithc_no. 3;
  586. continue;
  587. }
  588. //4.概率摇奖
  589. if(FALSE === $this->_getRandNum($value['rule_probability'])){
  590. $swithc_no = $swithc_no. 4;
  591. continue;
  592. }
  593. //5.查询是否超出当前规则下的出奖数
  594. if(FALSE === $this->_checkCurrentRuleSale($value)){
  595. $swithc_no = $swithc_no. 5;
  596. continue;
  597. }
  598. //6.查询是否超出奖品总数
  599. if(FALSE === $this->_checkGolabRule($value,$limit_sales)){
  600. $swithc_no = $swithc_no. 6;
  601. continue;
  602. }
  603. //8.如果中了一次实物奖就不允许中第二次
  604. if('real'== $limit_sales[$value['rule_prize_id']]['prize_object']){
  605. if(M('Prize_log_v2')->where(['act_id'=>$this->act_id,'uid'=>$this->uid,'prize_status'=>1])->find()){
  606. $swithc_no = $swithc_no. 8;
  607. continue;
  608. }
  609. }
  610. Writelog('uid: '.$this->uid.'跳出关卡: '.$swithc_no,'switch','activityv2');
  611. $prize = $limit_sales[$value['rule_prize_id']];
  612. $prize['rule_id'] = $value['rule_id'];
  613. $prize['prize_status'] = 1;
  614. return $prize;
  615. }
  616. Writelog('uid: '.$this->uid.'跳出关卡: '.$swithc_no,'switch','activityv2');
  617. return $impossible_prize;
  618. }
  619. /**
  620. * 获取转盘信息
  621. */
  622. public function prizeData(){
  623. $error[] = ['error' => 'error'];
  624. $data = array();
  625. $data['userp']['prize_num'] = $this->_getUserChance();
  626. $data['userp']['user_phone'] = $this->userinfo['user_phone'] ? :$this->userinfo['uid'];
  627. //转盘信息
  628. $turn_data = json_decode($this->activityinfo['prize_list'],TRUE);
  629. if($turn_data){
  630. foreach ($turn_data as $k => $v){
  631. if($v['prize_status'] == 0){
  632. unset($turn_data[$k]);
  633. continue;
  634. }
  635. if($v['prize_img']){
  636. $turn_data[$k]['prize_img'] = __MY_URL__.$v['prize_img'];
  637. }
  638. unset($turn_data[$k]['prize_num']);
  639. unset($turn_data[$k]['prize_level']);
  640. unset($turn_data[$k]['prize_status']);
  641. }
  642. $data['prize_data'] = $turn_data;
  643. }else{
  644. $this->responseError('获取失败!', -1, $error);
  645. }
  646. $this->responseSuccess($data, '查询成功!');
  647. }
  648. private function getPrizeDefault()
  649. {
  650. $map['pd.act_id'] = $this->act_id;
  651. $prizes = json_decode($this->activityinfo['prize_list'],true);
  652. foreach ($prizes as $key=>$v){
  653. $prize_list[$v['prize_id']] = $v;
  654. }
  655. // var_dump($prize_list);die;
  656. $data = M('prize_default_v2')->where(['act_id'=>$this->act_id])->select();
  657. if($data){
  658. foreach ($data as $value) {
  659. $return_data[] = array(
  660. 'created_at'=>$value['prize_date'],
  661. 'prize_name'=>$prize_list[$value['prize_id']]['prize_name'],
  662. 'prize_img'=>$prize_list[$value['prize_id']]['prize_img'] ? __MY_URL__.$prize_list[$value['prize_id']]['prize_img']:'',
  663. 'user_phone'=>substr_replace($value['phone'],'****',3,4),
  664. );
  665. }
  666. }
  667. return $return_data;
  668. }
  669. /**
  670. * @desc 检查中奖规则关于身份设置
  671. */
  672. private function _checkRuleRole($rule_role)
  673. {
  674. $role = $this->vip_level ? '会员' : '普通用户';
  675. if(FALSE !== strpos($rule_role, '不限')){
  676. return TRUE;
  677. }
  678. if($rule_role != '虚拟用户'){
  679. if(in_array($role, explode(',', $rule_role))){
  680. return TRUE;
  681. }
  682. }
  683. return FALSE;
  684. }
  685. /**
  686. * @desc 检查中奖规则关于日期
  687. */
  688. private function _checkRuleDate($rule_date)
  689. {
  690. if(FALSE !== strpos($rule_date,'每日')){
  691. return TRUE;
  692. }
  693. //时间段
  694. if(strpos($rule_date,'至')){
  695. $date = explode('至', $rule_date);
  696. if((time()>=strtotime($date[0])) && time()< strtotime($date[1])){
  697. return TRUE;
  698. }else{
  699. return FALSE;
  700. }
  701. }else{
  702. if(date('Y-m-d') == $rule_date){
  703. return TRUE;
  704. }else{
  705. return FALSE;
  706. }
  707. }
  708. }
  709. /**
  710. * @desc 检查中奖规则关于时间段
  711. */
  712. private function _checkRuleHour($rule_hour)
  713. {
  714. $hour = explode('至', $rule_hour);
  715. if((date('H:i')>= trim($hour[0])) && (date('H:i')< trim($hour[1]))){
  716. return TRUE;
  717. }else{
  718. return FALSE;
  719. }
  720. }
  721. /**
  722. * @desc 概率摇奖
  723. * @param type $rule_probability
  724. * @return boolean
  725. */
  726. private function _getRandNum($rule_probability)
  727. {
  728. if($rule_probability == 0){
  729. return FALSE;
  730. }
  731. $tmp = explode('/', $rule_probability);
  732. $rand_array = range(1, $tmp[1]);
  733. shuffle($rand_array);
  734. $get_randnum = array_slice($rand_array, 0, $tmp[0]);
  735. Writelog('uid: '.$this->uid.'抽签: '.json_encode($get_randnum),'switch','activityv2');
  736. if(in_array($tmp[0], $get_randnum)){
  737. return TRUE;
  738. }else{
  739. return FALSE;
  740. }
  741. }
  742. /**
  743. * @desc 查询符合当前规则
  744. */
  745. private function _checkCurrentRuleSale(&$rule)
  746. {
  747. //查询当前规则下的中奖数,如果为-1则无限制
  748. if($rule['rule_num'] == -1){
  749. return TRUE;
  750. }
  751. if((FALSE!==strpos($rule['rule_date'],'至')) && ($rule['rule_cycle']=='total')){
  752. $rule_date = explode('至', $rule['rule_date']);
  753. $where['rule_id'] = $rule['rule_id'];
  754. $where['act_id'] = $this->act_id;
  755. $where['date'] = ['between',[date('Ymd', strtotime($rule_date[0])),date('Ymd', strtotime($rule_date[1]))]];
  756. $countNum = M('Prize_log_v2')->where($where)->count();
  757. Writelog('当前1:'.$countNum.'规则:'.$rule['rule_num'],'info','activityv2');
  758. return $countNum >= $rule['rule_num'] ? FALSE : TRUE;
  759. }else{
  760. $where['date'] = date('Ymd');
  761. $where['rule_id'] = $rule['rule_id'];
  762. $where['act_id'] = $this->act_id;
  763. $countNum = M('Prize_log_v2')->where($where)->count();
  764. Writelog('当前2:'.$countNum.'规则:'.$rule['rule_num'],'info','activityv2');
  765. // echo M()->getLastSql();die;
  766. return $countNum >= $rule['rule_num'] ? FALSE : TRUE;
  767. }
  768. }
  769. /**
  770. * @desc 不能超过奖品总数
  771. */
  772. private function _checkGolabRule(&$rule,&$limit_sales)
  773. {
  774. if($limit_sales[$rule['rule_prize_id']]['prize_num'] == -1){
  775. return TRUE;
  776. }
  777. $countNum = M('Prize_log_v2')->where(['prize_id'=>$rule['rule_prize_id'],'act_id'=>$this->act_id])->count();
  778. // echo M()->getLastSql();die;
  779. Writelog('当前3:'.$countNum.'总量:'.$limit_sales[$rule['rule_prize_id']]['prize_num'],'info','activityv2');
  780. return $countNum >= $limit_sales[$rule['rule_prize_id']]['prize_num'] ? FALSE : TRUE;
  781. }
  782. }