LookTvLogic.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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 LookTvLogic 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. $act_id = I('act_id',0,'intval');
  31. $act_name = I('act_code');
  32. if($act_id){
  33. $where['id'] = $act_id;
  34. }else{
  35. $where['activity_code'] = $act_name;
  36. }
  37. if(!$activityinfo = M('activity_v2')->where($where)->find()){
  38. $this->responseError('act_id 或 act_code 参数非法');
  39. }
  40. $this->act_id = $activityinfo['id'];
  41. $this->activityinfo = $activityinfo;
  42. //检查vip鉴权方式
  43. $this->vip_level = I('is_vip') >= 1 ? 1 : 0;
  44. }
  45. /**
  46. * 获取活动状态
  47. * @return [type] [description]
  48. */
  49. public function getActStatus()
  50. {
  51. //开始时间结束时间未作限制
  52. if($this->activityinfo['is_enable'] == 1){
  53. $this->responseSuccess(1, "请求成功!");
  54. }else{
  55. $this->responseSuccess(-1, "请求成功!");
  56. }
  57. }
  58. /**
  59. * 提交用户资料
  60. * @return [type] [description]
  61. */
  62. public function setUserInfo()
  63. {
  64. $phone = trim(I('phone'));
  65. if (empty($phone)) {
  66. $this->responseError('参数错误!');
  67. }
  68. //检测手机号码是否正确
  69. if (!preg_match('/^1[345789]\d{9}$/ims', $phone)) {
  70. $this->responseError('手机号填写有误!');
  71. }
  72. //更新用户活动手机号码
  73. $where = array(
  74. 'uid'=>$this->uid,
  75. 'act_id'=>$this->act_id,
  76. );
  77. if ($id = M('activity_user_v2')->where($where)->getField('id')){
  78. $update['user_phone']= $phone;
  79. $update['updated_at']= date('Y-m-d H:i:s');
  80. if(M('activity_user_v2')->where(['id'=>$id])->save($update)===false){
  81. Writelog(M()->getLastSql(),'sql','prize');
  82. $this->responseError("更新失败!");
  83. }
  84. }else{
  85. $insert_data = array(
  86. 'uid'=>$this->uid,
  87. 'act_id'=>$this->act_id,
  88. 'user_phone'=>$phone,
  89. 'created_at'=>date('Y-m-d H:i:s'),
  90. 'updated_at'=>date('Y-m-d H:i:s')
  91. );
  92. if(!M('activity_user_v2')->add($insert_data)){
  93. Writelog(M()->getLastSql(),'sql','prize');
  94. $this->responseError("登记失败!");
  95. }
  96. }
  97. $this->responseSuccess(0, "登记成功!");
  98. }
  99. /**
  100. * 提交用户资料
  101. * @return [type] [description]
  102. */
  103. public function setUserInfoByphone()
  104. {
  105. $phone = trim(I('phone'));
  106. $address = trim(I('address'));
  107. $receiver = trim(I('receiver'));
  108. if (!($phone && $address && $receiver)) {
  109. $this->responseError('参数错误!');
  110. }
  111. //检测手机号码是否正确
  112. if (!preg_match('/^1[345789]\d{9}$/ims', $phone)) {
  113. $this->responseError('手机号填写有误!');
  114. }
  115. //更新用户活动手机号码
  116. $where = array(
  117. 'uid'=>$this->uid,
  118. 'act_id'=>$this->act_id,
  119. );
  120. if ($id = M('activity_user_v2')->where($where)->getField('id')){
  121. $update['user_phone']= $phone;
  122. $update['address'] = $address;
  123. $update['receiver'] = $receiver;
  124. $update['updated_at'] = date('Y-m-d H:i:s');
  125. if(M('activity_user_v2')->where(['id'=>$id])->save($update)===false){
  126. Writelog(M()->getLastSql(),'sql','prize');
  127. $this->responseError("更新失败!");
  128. }
  129. }else{
  130. $insert_data = array(
  131. 'uid'=>$this->uid,
  132. 'act_id'=>$this->act_id,
  133. 'user_phone'=>$phone,
  134. 'address'=>$address,
  135. 'receiver'=>$receiver,
  136. 'created_at'=>date('Y-m-d H:i:s'),
  137. 'updated_at'=>date('Y-m-d H:i:s'),
  138. );
  139. if(!M('activity_user_v2')->add($insert_data)){
  140. Writelog(M()->getLastSql(),'sql','prize');
  141. $this->responseError("登记失败!");
  142. }
  143. }
  144. $this->responseSuccess(0, "登记成功!");
  145. }
  146. //获取网页二维码
  147. public function getQrcode()
  148. {
  149. $this->responseSuccess($this->createQrcode());
  150. }
  151. /**
  152. * 活动规则说明
  153. * @return [type] [description]
  154. */
  155. public function getActivityInfo()
  156. {
  157. //获取中奖信息
  158. $data['rank'] = $this->prizelog('localhost');
  159. unset($data['act']['prize_list']);
  160. unset($data['act']['prize_rule']);
  161. //获取当前用户中奖信息
  162. $map['act_id'] = $this->act_id;
  163. $map['uid'] = $this->uid;
  164. $map['rule_id'] = array("neq", 0);
  165. //$data['my_prizes'] = M('prize_log_v2')->field('prize_id,prize_name')->where($map)->select();
  166. $my_prizes = M('prize_log_v2')->field('created_at,prize_id')->where($map)->select() ? : array();
  167. if($my_prizes){
  168. foreach ($my_prizes as $k=>$value) {
  169. $my_prizes[$k]['prize_name'] = $prizes[$value['prize_id']]['prize_name'];
  170. $data['my_prizes'][] = ['prize_id'=>$value['prize_id'],'prize_name'=>$prizes[$value['prize_id']]['prize_name']];
  171. }
  172. }
  173. $data['collects'] = $this->_getUserCollect();
  174. //获取用户剩余中奖次数
  175. $data['remain_chance'] = $this->_getUserChance();
  176. $this->responseSuccess($data);
  177. }
  178. //集物
  179. public function collect()
  180. {
  181. if(I('run')!='complate'){
  182. $rand = rand(1,2);
  183. if($rand != 1){
  184. $this->responseError('未抽中!', -1);
  185. }
  186. }
  187. $type = I('type');
  188. $lists = ['gx'=>1,'yy'=>2,'cy'=>3,'ds'=>4,'kx'=>5];
  189. $collect_id = $lists[$type] ?: 0 ;
  190. if(empty($collect_id)){
  191. $this->responseError('type 类型错误!', -1);
  192. }
  193. $where = array(
  194. 'act_id' =>$this->act_id,
  195. 'uid' =>$this->uid,
  196. 'collect_id'=>$collect_id
  197. );
  198. //判断是否收集到了此卡
  199. if(M('activity_collect')->where($where)->find()){
  200. $this->responseError('您已获得过该卡片,不可重复获取哟!', -1);
  201. }
  202. // echo $collect_id;die;
  203. $collect_list = json_decode($this->activityinfo['collect_list'],TRUE);
  204. $collect_list = array_column($collect_list,null,'collect_id');
  205. $results[] = array(
  206. 'collect_id'=>$collect_list[$collect_id]['collect_id'],
  207. 'collect_name'=>$collect_list[$collect_id]['collect_name'],
  208. 'collect_img'=>$collect_list[$collect_id]['collect_img'] ? __MY_URL__.$collect_list[$collect_id]['collect_img'] : '',
  209. );
  210. //写入集物表
  211. foreach ($results as $row) {
  212. $insert[] = array(
  213. 'act_id'=>$this->act_id,
  214. 'uid' =>$this->uid,
  215. 'collect_id'=>$row['collect_id'],
  216. 'operate' =>1,
  217. 'date' =>date('Ymd'),
  218. 'created_at'=>date('Y-m-d H:i:s')
  219. );
  220. }
  221. M('activity_collect')->addAll($insert);
  222. $return_data = ['collect_status'=>'yes','collect_list'=>$results];
  223. $this->responseSuccess($return_data);
  224. }
  225. //获取集物信息
  226. //集物
  227. public function getUserCollects()
  228. {
  229. $collects = $this->_getUserCollect();
  230. if($collects){
  231. $this->responseSuccess($collects);
  232. }else{
  233. $this->responseError('没有抽奖记录!', -3, ['error'=>'error']);
  234. }
  235. }
  236. private function _getUserCollect()
  237. {
  238. //写入集物表
  239. $where = array(
  240. 'act_id'=>$this->act_id,
  241. 'uid' =>$this->uid,
  242. );
  243. $collects = M('activity_collect')->where($where)->group('collect_id')->getField('collect_id,sum(operate) as num');
  244. $collects = $collects ? : [];
  245. // echo M()->getLastSql();
  246. $allCollects = json_decode($this->activityinfo['collect_list'],TRUE);
  247. if(!$allCollects){
  248. $this->responseError('没有集物列表!', -3, ['error'=>'error']);
  249. }
  250. $collect_id_sum = $should_collect_sum = 0 ;
  251. foreach ($allCollects as $key => $row) {
  252. $return[$key] = array(
  253. 'collect_id'=>$row['collect_id'],
  254. 'collect_name'=>$row['collect_name'],
  255. 'collect_img'=>$row['collect_img'] ? __MY_URL__.$row['collect_img'] : '',
  256. 'num'=>$collects[$row['collect_id']] > 0 ? $collects[$row['collect_id']] : 0
  257. );
  258. if($collects[$row['collect_id']] > 0){
  259. $collect_id_sum += $row['collect_id'];
  260. }
  261. $should_collect_sum += $row['collect_id'];
  262. }
  263. if($collect_id_sum == $should_collect_sum){
  264. $collect['complate'][] = array(
  265. 'exchange_id'=>1,
  266. 'collect_ids'=>implode(',',[1,2,3,4,5])
  267. );
  268. // $collect['complate'][] = array(
  269. // 'exchange_id'=>2,
  270. // 'collect_ids'=>implode(',',[1,2,3,4,5,6,7,8,9])
  271. // );
  272. $collect['collect'] = $return;
  273. }else{
  274. $collect['complate'] = [];
  275. $collect['collect'] = $return;
  276. }
  277. return $collect;
  278. }
  279. /**
  280. * 获取虚拟道具卡
  281. * @param [type] $uid [description]
  282. * @return [type] [description]
  283. */
  284. public function getVitualCard()
  285. {
  286. if($daoju_name = trim(I('daoju_name'))){
  287. if(!in_array($daoju_name, ['reborn_card'])){
  288. $this->responseError('daoju_name 参数非法');
  289. }
  290. } else {
  291. $this->responseError('daoju_name 参数非法');
  292. }
  293. $remain = $this->_getVitualCard($daoju_name);
  294. $this->responseSuccess($remain,'剩余道具数量');
  295. }
  296. /**
  297. * 获取虚拟卡
  298. * @param [type] $uid [description]
  299. * @return [type] [description]
  300. */
  301. private function _getVitualCard($daoju_name)
  302. {
  303. $map['uid'] = $this->uid;
  304. $map['act_id'] = $this->act_id;
  305. $map['date'] = date('Ymd');
  306. $map['name'] = $daoju_name;
  307. $daoju = M('activity_daoju')->field('operate,count(*) as total')->where($map)->group('operate')->select();
  308. $daoju = array_column($daoju, 'total', 'operate');
  309. $count = $daoju['add'] - $daoju['reduce'];
  310. if($daoju_name == 'reborn_card'){
  311. if($this->vip_level == 2){
  312. $remain_chance = 2 + $count;
  313. }elseif($this->vip_level ==1){
  314. $remain_chance = 1 + $count;
  315. }else{
  316. $remain_chance = 0 + $count;
  317. }
  318. }
  319. return $remain_chance > 0 ? $remain_chance : 0;
  320. }
  321. /**
  322. * 获取用户当日抽奖次数
  323. * @param [type] $uid [description]
  324. * @return [type] [description]
  325. */
  326. private function _getUserChance()
  327. {
  328. $map['uid'] = $this->uid;
  329. $map['act_id'] = $this->act_id;
  330. $map['date'] = date('Ymd');
  331. $chance = M('activity_take_part')->where($map)->getField('number') ? : 0;
  332. // var_dump($chance);die;
  333. if($this->activityinfo['is_test'] ==1){
  334. if(I('is_test') ==1){
  335. return $remain_chance = 1000 - $chance;
  336. }
  337. }
  338. if($this->vip_level >=1){
  339. $remain_chance = ($this->vip_level * $this->activityinfo['vip_chance']) - $chance;
  340. }else{
  341. $remain_chance = $this->activityinfo['novip_chance'] - $chance;
  342. }
  343. return $remain_chance > 0 ? $remain_chance : 0;
  344. }
  345. /**
  346. * 获取用户当日抽奖次数
  347. * @param [type] $uid [description]
  348. * @return [type] [description]
  349. */
  350. private function _useUserChance()
  351. {
  352. $map['uid'] = $this->uid;
  353. $map['act_id'] = $this->act_id;
  354. $map['date'] = date('Ymd');
  355. $data = M('activity_take_part')->where($map)->find();
  356. // var_dump($chance);die;
  357. if($data){
  358. $data['number'] += 1;
  359. $bool = M('activity_take_part')->save($data);
  360. }else{
  361. $map['number'] = 1;
  362. $map['created_at'] = date('Y-m-d H:i:s');
  363. $bool = M('activity_take_part')->add($map);
  364. }
  365. if(!$bool){
  366. $this->responseError('服务异常',-1);
  367. }
  368. }
  369. /**
  370. * 使用虚拟道具
  371. * @param [type] $uid [description]
  372. * @return [type] [description]
  373. */
  374. public function useVitualCard()
  375. {
  376. $remain_reborn_card = $this->_getVitualCard('reborn_card');
  377. if($remain_reborn_card >= 1){
  378. $insert = array(
  379. 'uid' => $this->uid,
  380. 'act_id' => $this->act_id,
  381. 'date' => date('Ymd'),
  382. 'operate' => 'reduce',
  383. 'name' => 'reborn_card'
  384. );
  385. if(M('activity_daoju')->add($insert)){
  386. $this->responseSuccess();
  387. }else{
  388. $this->responseError('网络异常!', -1);
  389. }
  390. }else{
  391. $this->responseError('您没有复活卡拉!', -3);
  392. }
  393. }
  394. /**
  395. * 获取用户剩余抽奖次数
  396. * @return [type] [description]
  397. */
  398. public function getPrizeNum()
  399. {
  400. $remain_chance = $this->_getUserChance();
  401. $this->responseSuccess($remain_chance);
  402. }
  403. /**
  404. * 所有中奖记录查询
  405. */
  406. public function prizelog($model = ''){
  407. $error[] = ['error' => 'error'];
  408. $where['pl.act_id'] = $this->act_id;
  409. $where['pl.prize_status'] = 1;
  410. $data = M('prize_log_v2')->alias('pl')
  411. ->join(array('left join '.C("DB_PREFIX").'activity_user_v2 as iu on iu.uid = pl.uid and iu.act_id = '.$this->act_id))
  412. ->where($where)
  413. ->order('pl.id desc')
  414. ->field('pl.prize_id,pl.created_at,pl.uid,iu.user_phone')
  415. ->select();
  416. $prizes = json_decode($this->activityinfo['prize_list'],TRUE);
  417. foreach ($prizes as $value) {
  418. $prize_list[$value['prize_id']] = $value;
  419. }
  420. if($data){
  421. foreach ($data as $key=>$value) {
  422. $data[$key]['prize_name'] = $prize_list[$value['prize_id']]['prize_name'];
  423. $data[$key]['prize_img'] = __MY_URL__.$prize_list[$value['prize_id']]['prize_img'];
  424. }
  425. }
  426. //增加虚拟中奖信息
  427. $default_data = $this->getPrizeDefault();
  428. if($data || $default_data){
  429. if($data){
  430. foreach ($data as $k=>$v){
  431. if($v['user_phone']){
  432. $data[$k]['user_phone'] = substr_replace($v['user_phone'],'****',3,4);
  433. }else {
  434. $data[$k]['user_phone'] = substr_replace($v['uid'],'****',6);
  435. }
  436. unset($data[$k]['uid']);
  437. }
  438. }
  439. if($data && $default_data){
  440. $data = array_merge($default_data,$data);
  441. }else{
  442. $data = $data ? : $default_data;
  443. }
  444. array_multisort(array_column($data,'created_at'),SORT_DESC,$data);
  445. if($model == 'localhost'){
  446. return $data;
  447. }
  448. $this->responseSuccess($data, '查询成功!');
  449. }else{
  450. if($model == 'localhost'){
  451. return array();
  452. }
  453. $this->responseError('没有抽奖记录!', -3, $error);
  454. }
  455. }
  456. /**
  457. * 我的中奖记录查询
  458. */
  459. public function userPrizelog(){
  460. $error[] = ['error' => 'error'];
  461. //获取用户
  462. if($user_phone = M('activity_user_v2')->where(['uid'=>$this->uid,'act_id'=>$this->act_id])->getField('user_phone')){
  463. $phone_staus = 1;//填过
  464. }else{
  465. $phone_staus = 2;//填过
  466. }
  467. $where = array();
  468. $where['pl.prize_status'] = ['gt',0];
  469. $where['pl.uid'] = $this->uid;
  470. $where['pl.act_id'] = $this->act_id;
  471. $data = M('prize_log_v2')->alias('pl')
  472. ->where($where)
  473. ->order('pl.id desc')
  474. ->field('pl.prize_id,pl.created_at')
  475. ->select();
  476. // echo M()->getLastSql();die;
  477. $prizes = json_decode($this->activityinfo['prize_list'],TRUE);
  478. foreach ($prizes as $value) {
  479. $prize_list[$value['prize_id']] = $value;
  480. }
  481. $flag = $data ? 1 : 0;
  482. if($flag){
  483. if($data){
  484. foreach ($data as $k => $v){
  485. $data[$k]['prize_name'] = $prize_list[$v['prize_id']]['prize_name'];
  486. $data[$k]['prize_object'] = 'real';
  487. $data[$k]['show_image'] = $prize_list[$v['prize_id']]['prize_img'] ? __MY_URL__.$prize_list[$v['prize_id']]['prize_img']:null;
  488. unset($data[$k]['prize_id']);
  489. }
  490. }
  491. $return_data = array(
  492. 'my_prizes'=>$data,
  493. 'user_phone'=>['phone_status'=>$phone_staus,'user_phone'=>$user_phone ? :''],
  494. 'prize_status'=>$data ? 'real' : 'vitual'
  495. );
  496. $this->responseSuccess($return_data, '查询成功!');
  497. }else{
  498. $this->responseError('没有抽奖记录!', -3, $error);
  499. }
  500. }
  501. /**
  502. * 抽奖
  503. */
  504. public function prize(){
  505. $error[] = ['error' => 'error'];
  506. //查看是否抽过奖
  507. $where = [
  508. 'act_id'=>$this->act_id,
  509. 'uid'=>$this->uid,
  510. 'prize_status'=>['gt',0]
  511. ];
  512. if($prize_status=M('prize_log_v2')->where($where)->getField('prize_status')){
  513. //$this->responseError('您已经兑换过奖品啦!', -1);
  514. if($prize_status == 1){
  515. $res = array();
  516. $res['yes']['prize_name'] = '玩具大礼包'; //中奖项
  517. $res['yes']['prize_status'] = 'real';
  518. $res['yes']['prize_img'] = '';
  519. }else{
  520. $res = array();
  521. $res['yes']['prize_name'] = '荣誉勋章'; //中奖项
  522. $res['yes']['prize_status'] = 'vitual';
  523. $res['yes']['prize_img'] = '';
  524. }
  525. $this->responseSuccess($res, '抽奖成功!');
  526. }
  527. //判断是否有完成的集合
  528. $result = $this->_getUserCollect();
  529. if(!$result['complate']){
  530. $this->responseError('没有完成的集合!', -3, $error);
  531. }
  532. // $exchanges = array_column($result['complate'], null, 'exchange_id');
  533. // $collect_ids = $exchanges[1]['collect_ids'];
  534. // $collect_arr = explode(',', $collect_ids);
  535. // //消耗兑换卡
  536. // foreach ($collect_arr as $key => $value) {
  537. // $datalist[] = ['act_id'=>$this->act_id,'uid'=>$this->uid,'collect_id'=>$value,'operate'=>-1,'date'=>date('Ymd'),'created_at'=>date('Y-m-d H:i:s')];
  538. // }
  539. // M('activity_collect')->addAll($datalist);
  540. //开始抽奖
  541. $prize_data = $this->_get_prize();
  542. unset($prize_data['yes']['rule_id']);
  543. $this->responseSuccess($prize_data, '抽奖成功!');
  544. }
  545. private function _get_prize() {
  546. $result = $this->getPrizeSetDeatil();
  547. $this->_writePrizeLog($result);
  548. $res = array();
  549. $res['yes']['prize_name'] = $result['prize_name']; //中奖项
  550. $res['yes']['prize_status'] = $result['prize_object'] ? : 'none';
  551. $res['yes']['prize_img'] = $result['prize_img'] ? __MY_URL__.$result['prize_img'] : '';
  552. $res['yes']['rule_id'] = $result['rule_id'];
  553. return $res;
  554. }
  555. /**
  556. * @param desc 写入抽奖日志
  557. * @param type $result
  558. */
  559. private function _writePrizeLog(&$result) {
  560. $log_data['uid'] = $this->uid;
  561. $log_data['prize_id'] = $result['prize_id'];
  562. $log_data['is_vip'] = $this->vip_level;
  563. $log_data['act_id'] = $this->act_id;
  564. $log_data['created_at'] = date('Y-m-d H:i:s');
  565. $log_data['date'] = date('Ymd');
  566. $log_data['rule_id'] = $result['rule_id'];
  567. $log_data['prize_status'] = $result['prize_object'] == 'real' ? 1:2;
  568. if(!M('prize_log_v2')->add($log_data)){
  569. Writelog(M()->getLastSql(),'sql','activity');
  570. $this->responseError('系统繁忙抽奖失败', -1);
  571. }
  572. }
  573. /**
  574. * 返回中奖的信息
  575. */
  576. private function getPrizeSetDeatil()
  577. {
  578. $prize_list = json_decode($this->activityinfo['prize_list'],TRUE);//获取奖项设置
  579. if($prize_list){
  580. foreach ($prize_list as $row) {
  581. $limit_sales[$row['prize_id']] = $row;
  582. if($row['prize_level']== 0){
  583. $impossible_prize = ['prize_id'=>$row['prize_id'],'prize_name'=>$row['prize_name'],'rule_id'=>0,'prize_img'=>'','prize_status'=>0];
  584. }
  585. }
  586. }
  587. //获取所有规则
  588. $rules = json_decode($this->activityinfo['prize_rule'],TRUE);
  589. if(!$rules){
  590. return $impossible_prize;
  591. }
  592. $swithc_no = '';
  593. foreach ($rules as $value) {
  594. if($value['rule_status'] == 0){
  595. $swithc_no = $swithc_no. 0;
  596. continue;
  597. }
  598. //1.检查是否符合身份
  599. if(FALSE === $this->_checkRuleRole($value['rule_role'])){
  600. $swithc_no = $swithc_no. 1;
  601. continue;
  602. }
  603. //2.检查中奖日期设置
  604. if(FALSE === $this->_checkRuleDate($value['rule_date'])){
  605. $swithc_no = $swithc_no. 2;
  606. continue;
  607. }
  608. //3.检查时段设置
  609. if(FALSE === $this->_checkRuleHour($value['rule_hour'])){
  610. $swithc_no = $swithc_no. 3;
  611. continue;
  612. }
  613. //4.概率摇奖
  614. if(FALSE === $this->_getRandNum($value['rule_probability'])){
  615. $swithc_no = $swithc_no. 4;
  616. continue;
  617. }
  618. //5.查询是否超出当前规则下的出奖数
  619. if(FALSE === $this->_checkCurrentRuleSale($value)){
  620. $swithc_no = $swithc_no. 5;
  621. continue;
  622. }
  623. //6.查询是否超出奖品总数
  624. if(FALSE === $this->_checkGolabRule($value,$limit_sales)){
  625. $swithc_no = $swithc_no. 6;
  626. continue;
  627. }
  628. Writelog('uid: '.$this->uid.'跳出关卡: '.$swithc_no,'switch','activityv2');
  629. $prize = $limit_sales[$value['rule_prize_id']];
  630. $prize['rule_id'] = $value['rule_id'];
  631. return $prize;
  632. }
  633. Writelog('uid: '.$this->uid.'跳出关卡: '.$swithc_no,'switch','activityv2');
  634. return $impossible_prize;
  635. }
  636. /**
  637. * 获取转盘信息
  638. */
  639. public function prizeData(){
  640. $error[] = ['error' => 'error'];
  641. $data = array();
  642. $data['userp']['prize_num'] = $this->_getUserChance();
  643. $data['userp']['user_phone'] = $this->userinfo['user_phone'] ? :$this->userinfo['uid'];
  644. //转盘信息
  645. $turn_data = json_decode($this->activityinfo['prize_list'],TRUE);
  646. if($turn_data){
  647. foreach ($turn_data as $k => $v){
  648. if($v['prize_status'] == 0){
  649. unset($turn_data[$k]);
  650. continue;
  651. }
  652. if($v['prize_img']){
  653. $turn_data[$k]['prize_img'] = __MY_URL__.$v['prize_img'];
  654. }
  655. unset($turn_data[$k]['prize_num']);
  656. unset($turn_data[$k]['prize_level']);
  657. unset($turn_data[$k]['prize_status']);
  658. }
  659. $data['prize_data'] = $turn_data;
  660. }else{
  661. $this->responseError('获取失败!', -1, $error);
  662. }
  663. $this->responseSuccess($data, '查询成功!');
  664. }
  665. private function getPrizeDefault()
  666. {
  667. $map['pd.act_id'] = $this->act_id;
  668. $prizes = json_decode($this->activityinfo['prize_list'],true);
  669. foreach ($prizes as $key=>$v){
  670. $prize_list[$v['prize_id']] = $v;
  671. }
  672. // var_dump($prize_list);die;
  673. $data = M('prize_default_v2')->where(['act_id'=>$this->act_id])->select();
  674. if($data){
  675. foreach ($data as $value) {
  676. $return_data[] = array(
  677. 'created_at'=>$value['prize_date'],
  678. 'prize_name'=>$prize_list[$value['prize_id']]['prize_name'],
  679. 'prize_img'=>$prize_list[$value['prize_id']]['prize_img'] ? __MY_URL__.$prize_list[$value['prize_id']]['prize_img']:'',
  680. 'user_phone'=>substr_replace($value['phone'],'****',3,4),
  681. );
  682. }
  683. }
  684. return $return_data;
  685. }
  686. /**
  687. * @desc 检查中奖规则关于身份设置
  688. */
  689. private function _checkRuleRole($rule_role)
  690. {
  691. $role = $this->vip_level ? '会员' : '普通用户';
  692. if(FALSE !== strpos($rule_role, '不限')){
  693. return TRUE;
  694. }
  695. if($rule_role != '虚拟用户'){
  696. if(in_array($role, explode(',', $rule_role))){
  697. return TRUE;
  698. }
  699. }
  700. return FALSE;
  701. }
  702. /**
  703. * @desc 检查中奖规则关于日期
  704. */
  705. private function _checkRuleDate($rule_date)
  706. {
  707. if(FALSE !== strpos($rule_date,'每日')){
  708. return TRUE;
  709. }
  710. //时间段
  711. if(strpos($rule_date,'至')){
  712. $date = explode('至', $rule_date);
  713. if((time()>=strtotime($date[0])) && time()< strtotime($date[1])){
  714. return TRUE;
  715. }else{
  716. return FALSE;
  717. }
  718. }else{
  719. if(date('Y-m-d') == $rule_date){
  720. return TRUE;
  721. }else{
  722. return FALSE;
  723. }
  724. }
  725. }
  726. /**
  727. * @desc 检查中奖规则关于时间段
  728. */
  729. private function _checkRuleHour($rule_hour)
  730. {
  731. $hour = explode('至', $rule_hour);
  732. if((date('H:i')>= trim($hour[0])) && (date('H:i')< trim($hour[1]))){
  733. return TRUE;
  734. }else{
  735. return FALSE;
  736. }
  737. }
  738. /**
  739. * @desc 概率摇奖
  740. * @param type $rule_probability
  741. * @return boolean
  742. */
  743. private function _getRandNum($rule_probability)
  744. {
  745. if($rule_probability == 0){
  746. return FALSE;
  747. }
  748. $tmp = explode('/', $rule_probability);
  749. $rand_array = range(1, $tmp[1]);
  750. shuffle($rand_array);
  751. $get_randnum = array_slice($rand_array, 0, $tmp[0]);
  752. Writelog('uid: '.$this->uid.'抽签: '.json_encode($get_randnum),'switch','activityv2');
  753. if(in_array($tmp[0], $get_randnum)){
  754. return TRUE;
  755. }else{
  756. return FALSE;
  757. }
  758. }
  759. /**
  760. * @desc 查询符合当前规则
  761. */
  762. private function _checkCurrentRuleSale(&$rule)
  763. {
  764. //查询当前规则下的中奖数,如果为-1则无限制
  765. if($rule['rule_num'] == -1){
  766. return TRUE;
  767. }
  768. if((FALSE!==strpos($rule['rule_date'],'至')) && ($rule['rule_cycle']=='total')){
  769. $rule_date = explode('至', $rule['rule_date']);
  770. $where['rule_id'] = $rule['rule_id'];
  771. $where['act_id'] = $this->act_id;
  772. $where['date'] = ['between',[date('Ymd', strtotime($rule_date[0])),date('Ymd', strtotime($rule_date[1]))]];
  773. $countNum = M('Prize_log_v2')->where($where)->count();
  774. Writelog('当前1:'.$countNum.'规则:'.$rule['rule_num'],'info','activityv2');
  775. return $countNum >= $rule['rule_num'] ? FALSE : TRUE;
  776. }else{
  777. $where['date'] = date('Ymd');
  778. $where['rule_id'] = $rule['rule_id'];
  779. $where['act_id'] = $this->act_id;
  780. $countNum = M('Prize_log_v2')->where($where)->count();
  781. Writelog('当前2:'.$countNum.'规则:'.$rule['rule_num'],'info','activityv2');
  782. // echo M()->getLastSql();die;
  783. return $countNum >= $rule['rule_num'] ? FALSE : TRUE;
  784. }
  785. }
  786. /**
  787. * @desc 不能超过奖品总数
  788. */
  789. private function _checkGolabRule(&$rule,&$limit_sales)
  790. {
  791. if($limit_sales[$rule['rule_prize_id']]['prize_num'] == -1){
  792. return TRUE;
  793. }
  794. $countNum = M('Prize_log_v2')->where(['prize_id'=>$rule['rule_prize_id'],'act_id'=>$this->act_id])->count();
  795. // echo M()->getLastSql();die;
  796. Writelog('当前3:'.$countNum.'总量:'.$limit_sales[$rule['rule_prize_id']]['prize_num'],'info','activityv2');
  797. return $countNum >= $limit_sales[$rule['rule_prize_id']]['prize_num'] ? FALSE : TRUE;
  798. }
  799. }