Collect2Logic.class.php 29 KB

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