Collect2Logic.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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','activityv2');
  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','activityv2');
  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','activityv2');
  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','activityv2');
  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. //消减游戏次数
  178. $this->_useUserChance();
  179. $srand = mt_rand(0,8);
  180. if($srand == 0){
  181. $return_data = ['collect_status'=>'no','collect_list'=>''];
  182. $this->responseSuccess($return_data);
  183. }
  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 == 6){
  214. //四号卡片每个用户顶多给一个
  215. $where = array(
  216. 'act_id'=>$this->act_id,
  217. 'uid' =>$this->uid,
  218. 'collect_id'=>4,
  219. 'operate'=>1
  220. );
  221. if(M('activity_collect')->where($where)->find()){
  222. $rand = 0;
  223. }
  224. }
  225. if($rand == 0){
  226. $return_data = ['collect_status'=>'no','collect_list'=>''];
  227. $this->responseSuccess($return_data);
  228. }
  229. if($uid == 'U04599795' && $value['collect_id'] == 2){
  230. $return_data = ['collect_status'=>'no','collect_list'=>''];
  231. $this->responseSuccess($return_data);
  232. }
  233. if($uid == 'U00398811' && $value['collect_id'] == 3){
  234. $return_data = ['collect_status'=>'no','collect_list'=>''];
  235. $this->responseSuccess($return_data);
  236. }
  237. if($uid == 'U00419859' && $value['collect_id'] == 3){
  238. $return_data = ['collect_status'=>'no','collect_list'=>''];
  239. $this->responseSuccess($return_data);
  240. }
  241. if($uid == 'U00110045' && $value['collect_id'] == 7){
  242. $return_data = ['collect_status'=>'no','collect_list'=>''];
  243. $this->responseSuccess($return_data);
  244. }
  245. if($uid == '0108148565841340177' && $value['collect_id'] == 7){
  246. $return_data = ['collect_status'=>'no','collect_list'=>''];
  247. $this->responseSuccess($return_data);
  248. }
  249. $results[] = array(
  250. 'collect_id'=>$value['collect_id'],
  251. 'collect_name'=>$value['collect_name'],
  252. 'collect_img'=>$value['collect_img'] ? __MY_URL__.$value['collect_img'] : '',
  253. );
  254. //写入集物表
  255. foreach ($results as $row) {
  256. $insert[] = array(
  257. 'act_id'=>$this->act_id,
  258. 'uid' =>$this->uid,
  259. 'collect_id'=>$row['collect_id'],
  260. 'operate' =>1,
  261. 'date' =>date('Ymd'),
  262. 'created_at'=>date('Y-m-d H:i:s')
  263. );
  264. }
  265. M('activity_collect')->addAll($insert);
  266. $return_data = ['collect_status'=>'yes','collect_list'=>$results];
  267. $this->responseSuccess($return_data);
  268. }
  269. //获取集物信息
  270. //集物
  271. public function getUserCollects()
  272. {
  273. $collects = $this->_getUserCollect();
  274. if($collects){
  275. $this->responseSuccess($collects);
  276. }else{
  277. $this->responseError('没有抽奖记录!', -3, ['error'=>'error']);
  278. }
  279. }
  280. private function _getUserCollect()
  281. {
  282. //写入集物表
  283. $where = array(
  284. 'act_id'=>$this->act_id,
  285. 'uid' =>$this->uid,
  286. );
  287. $collects = M('activity_collect')->where($where)->group('collect_id')->getField('collect_id,sum(operate) as num');
  288. $collects = $collects ? : [];
  289. // echo M()->getLastSql();
  290. $allCollects = json_decode($this->activityinfo['collect_list'],TRUE);
  291. if(!$allCollects){
  292. $this->responseError('没有集物列表!', -3, ['error'=>'error']);
  293. }
  294. $collect_id_sum = $should_collect_sum = 0 ;
  295. foreach ($allCollects as $key => $row) {
  296. $return[$key] = array(
  297. 'collect_id'=>$row['collect_id'],
  298. 'collect_name'=>$row['collect_name'],
  299. 'collect_img'=>$row['collect_img'] ? __MY_URL__.$row['collect_img'] : '',
  300. 'num'=>$collects[$row['collect_id']] > 0 ? $collects[$row['collect_id']] : 0
  301. );
  302. if($collects[$row['collect_id']] > 0){
  303. $collect_id_sum += $row['collect_id'];
  304. }
  305. $should_collect_sum += $row['collect_id'];
  306. }
  307. if($collect_id_sum == $should_collect_sum){
  308. $collect['complate'][] = array(
  309. 'exchange_id'=>1,
  310. 'collect_ids'=>implode(',',[1,2,3,4,5,6,7])
  311. );
  312. // $collect['complate'][] = array(
  313. // 'exchange_id'=>2,
  314. // 'collect_ids'=>implode(',',[1,2,3,4,5,6,7,8,9])
  315. // );
  316. $collect['collect'] = $return;
  317. }else{
  318. $collect['complate'] = '';
  319. $collect['collect'] = $return;
  320. }
  321. return $collect;
  322. }
  323. /**
  324. * 获取虚拟道具卡
  325. * @param [type] $uid [description]
  326. * @return [type] [description]
  327. */
  328. public function getVitualCard()
  329. {
  330. if($daoju_name = trim(I('daoju_name'))){
  331. if(!in_array($daoju_name, ['reborn_card'])){
  332. $this->responseError('daoju_name 参数非法');
  333. }
  334. } else {
  335. $this->responseError('daoju_name 参数非法');
  336. }
  337. $remain = $this->_getVitualCard($daoju_name);
  338. $this->responseSuccess($remain,'剩余道具数量');
  339. }
  340. /**
  341. * 获取虚拟卡
  342. * @param [type] $uid [description]
  343. * @return [type] [description]
  344. */
  345. private function _getVitualCard($daoju_name)
  346. {
  347. $map['uid'] = $this->uid;
  348. $map['act_id'] = $this->act_id;
  349. $map['date'] = date('Ymd');
  350. $map['name'] = $daoju_name;
  351. $daoju = M('activity_daoju')->field('operate,count(*) as total')->where($map)->group('operate')->select();
  352. $daoju = array_column($daoju, 'total', 'operate');
  353. $count = $daoju['add'] - $daoju['reduce'];
  354. if($daoju_name == 'reborn_card'){
  355. if($this->vip_level == 2){
  356. $remain_chance = 2 + $count;
  357. }elseif($this->vip_level ==1){
  358. $remain_chance = 1 + $count;
  359. }else{
  360. $remain_chance = 0 + $count;
  361. }
  362. }
  363. return $remain_chance > 0 ? $remain_chance : 0;
  364. }
  365. /**
  366. * 获取用户当日抽奖次数
  367. * @param [type] $uid [description]
  368. * @return [type] [description]
  369. */
  370. private function _getUserChance()
  371. {
  372. $map['uid'] = $this->uid;
  373. $map['act_id'] = $this->act_id;
  374. $map['date'] = date('Ymd');
  375. $chance = M('activity_take_part')->where($map)->getField('number') ? : 0;
  376. // var_dump($chance);die;
  377. if($this->activityinfo['is_test'] ==1){
  378. if(I('is_test') ==1){
  379. return $remain_chance = 1000 - $chance;
  380. }
  381. }
  382. if($this->vip_level >=1){
  383. $remain_chance = ($this->vip_level * $this->activityinfo['vip_chance'] + $this->activityinfo['novip_chance'] - $chance);
  384. }else{
  385. $remain_chance = $this->activityinfo['novip_chance'] - $chance;
  386. }
  387. return $remain_chance > 0 ? $remain_chance : 0;
  388. }
  389. /**
  390. * 获取用户当日抽奖次数
  391. * @param [type] $uid [description]
  392. * @return [type] [description]
  393. */
  394. private function _useUserChance()
  395. {
  396. $map['uid'] = $this->uid;
  397. $map['act_id'] = $this->act_id;
  398. $map['date'] = date('Ymd');
  399. $data = M('activity_take_part')->where($map)->find();
  400. // var_dump($chance);die;
  401. if($data){
  402. $data['number'] += 1;
  403. $bool = M('activity_take_part')->save($data);
  404. }else{
  405. $map['number'] = 1;
  406. $map['created_at'] = date('Y-m-d H:i:s');
  407. $bool = M('activity_take_part')->add($map);
  408. }
  409. if(!$bool){
  410. $this->responseError('服务异常',-1);
  411. }
  412. }
  413. /**
  414. * 使用虚拟道具
  415. * @param [type] $uid [description]
  416. * @return [type] [description]
  417. */
  418. public function useVitualCard()
  419. {
  420. $remain_reborn_card = $this->_getVitualCard('reborn_card');
  421. if($remain_reborn_card >= 1){
  422. $insert = array(
  423. 'uid' => $this->uid,
  424. 'act_id' => $this->act_id,
  425. 'date' => date('Ymd'),
  426. 'operate' => 'reduce',
  427. 'name' => 'reborn_card'
  428. );
  429. if(M('activity_daoju')->add($insert)){
  430. $this->responseSuccess();
  431. }else{
  432. $this->responseError('网络异常!', -1);
  433. }
  434. }else{
  435. $this->responseError('您没有复活卡拉!', -3);
  436. }
  437. }
  438. /**
  439. * 获取用户剩余抽奖次数
  440. * @return [type] [description]
  441. */
  442. public function getPrizeNum()
  443. {
  444. $remain_chance = $this->_getUserChance();
  445. $this->responseSuccess($remain_chance);
  446. }
  447. /**
  448. * 所有中奖记录查询
  449. */
  450. public function prizelog($model = ''){
  451. $error[] = ['error' => 'error'];
  452. $where['act_id'] = $this->act_id;
  453. $data = M('activity_user_v2')->where($where)->field('id,prize_id,act_id,user_phone,created_at')->select();
  454. $prizes = json_decode($this->activityinfo['prize_list'],TRUE);
  455. foreach ($prizes as $value) {
  456. $prize_list[$value['prize_id']] = $value;
  457. }
  458. if($data){
  459. foreach ($data as $key=>$value) {
  460. $data[$key]['prize_name'] = $prize_list[$value['prize_id']]['prize_name'];
  461. $data[$key]['prize_img'] = __MY_URL__.$prize_list[$value['prize_id']]['prize_img'];
  462. }
  463. }
  464. //增加虚拟中奖信息
  465. $default_data = $this->getPrizeDefault();
  466. if($data || $default_data){
  467. if($data){
  468. foreach ($data as $k=>$v){
  469. if($v['user_phone']){
  470. $data[$k]['user_phone'] = substr_replace($v['user_phone'],'****',3,4);
  471. }else {
  472. $data[$k]['user_phone'] = substr_replace($v['uid'],'****',6);
  473. }
  474. unset($data[$k]['uid']);
  475. }
  476. }
  477. if($data && $default_data){
  478. $data = array_merge($default_data,$data);
  479. }else{
  480. $data = $data ? : $default_data;
  481. }
  482. array_multisort(array_column($data,'created_at'),SORT_DESC,$data);
  483. if($model == 'localhost'){
  484. return $data;
  485. }
  486. $this->responseSuccess($data, '查询成功!');
  487. }else{
  488. if($model == 'localhost'){
  489. return array();
  490. }
  491. $this->responseError('没有抽奖记录!', -3, $error);
  492. }
  493. }
  494. public function userPrizelog(){
  495. $error[] = ['error' => 'error'];
  496. //获取用户
  497. if($user_phone = M('activity_user_v2')->where(['uid'=>$this->uid,'act_id'=>$this->act_id])->getField('user_phone')){
  498. $phone_staus = 1;//填过
  499. }else{
  500. $phone_staus = 2;//填过
  501. }
  502. $where = array();
  503. $where['pl.prize_status'] = 1;
  504. $where['pl.uid'] = $this->uid;
  505. $where['pl.act_id'] = $this->act_id;
  506. $data = M('prize_log_v2')->alias('pl')
  507. ->where($where)
  508. ->order('pl.id desc')
  509. ->field('pl.prize_id,pl.created_at')
  510. ->select();
  511. // echo M()->getLastSql();die;
  512. $prizes = json_decode($this->activityinfo['prize_list'],TRUE);
  513. foreach ($prizes as $value) {
  514. $prize_list[$value['prize_id']] = $value;
  515. }
  516. $flag = $data ? 1 : 0;
  517. if($flag){
  518. if($data){
  519. foreach ($data as $k => $v){
  520. $data[$k]['prize_name'] = $prize_list[$v['prize_id']]['prize_name'];
  521. $data[$k]['prize_object'] = 'real';
  522. $data[$k]['show_image'] = $prize_list[$v['prize_id']]['prize_img'] ? __MY_URL__.$prize_list[$v['prize_id']]['prize_img']:null;
  523. unset($data[$k]['prize_id']);
  524. }
  525. }
  526. $return_data = array(
  527. 'my_prizes'=>$data,
  528. 'user_phone'=>['phone_status'=>$phone_staus,'user_phone'=>$user_phone ? :''],
  529. 'prize_status'=>$data ? 'real' : 'vitual'
  530. );
  531. $this->responseSuccess($return_data, '查询成功!');
  532. }else{
  533. $this->responseError('没有抽奖记录!', -3, $error);
  534. }
  535. }
  536. /**
  537. * 抽奖
  538. */
  539. public function prize(){
  540. $error[] = ['error' => 'error'];
  541. //判断是否有完成的集合
  542. $result = $this->_getUserCollect();
  543. if(!$result['complate']){
  544. $this->responseError('没有完成的集合!', -3, $error);
  545. }
  546. $exchanges = array_column($result['complate'], null, 'exchange_id');
  547. $collect_ids = $exchanges[1]['collect_ids'];
  548. $collect_arr = explode(',', $collect_ids);
  549. //消耗兑换卡
  550. foreach ($collect_arr as $key => $value) {
  551. $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')];
  552. }
  553. M('activity_collect')->addAll($datalist);
  554. //开始抽奖
  555. $prize_data = $this->_get_prize();
  556. unset($prize_data['yes']['rule_id']);
  557. $this->responseSuccess($prize_data, '抽奖成功!');
  558. }
  559. private function _get_prize() {
  560. $result = $this->getPrizeSetDeatil();
  561. // var_dump($result);die;
  562. $this->_writePrizeLog($result);
  563. $res = array();
  564. $res['yes']['prize_name'] = $result['prize_name']; //中奖项
  565. $res['yes']['prize_status'] = $result['prize_object'] ? : 'none';
  566. $res['yes']['prize_img'] = $result['prize_img'] ? __MY_URL__.$result['prize_img'] : '';
  567. $res['yes']['rule_id'] = $result['rule_id'];
  568. return $res;
  569. }
  570. /**
  571. * @param desc 写入抽奖日志
  572. * @param type $result
  573. */
  574. private function _writePrizeLog(&$result) {
  575. $log_data['uid'] = $this->uid;
  576. $log_data['prize_id'] = $result['prize_id'] ? :0;
  577. $log_data['is_vip'] = $this->vip_level;
  578. $log_data['act_id'] = $this->act_id;
  579. $log_data['created_at'] = date('Y-m-d H:i:s');
  580. $log_data['date'] = date('Ymd');
  581. $log_data['rule_id'] = $result['rule_id'] ? : 0;
  582. $log_data['prize_status'] = $result['prize_status'];
  583. if(!M('prize_log_v2')->add($log_data)){
  584. Writelog(M()->getLastSql(),'sql','activityv2');
  585. $this->responseError('系统繁忙抽奖失败', -1);
  586. }
  587. }
  588. /**
  589. * 返回中奖的信息
  590. */
  591. private function getPrizeSetDeatil()
  592. {
  593. $prize_list = json_decode($this->activityinfo['prize_list'],TRUE);//获取奖项设置
  594. if($prize_list){
  595. foreach ($prize_list as $row) {
  596. $limit_sales[$row['prize_id']] = $row;
  597. if($row['prize_level']== 0){
  598. $impossible_prize = ['prize_id'=>$row['prize_id'],'prize_name'=>$row['prize_name'],'rule_id'=>0,'prize_img'=>'','prize_status'=>0];
  599. }
  600. }
  601. }
  602. //获取所有规则
  603. $rules = json_decode($this->activityinfo['prize_rule'],TRUE);
  604. if(!$rules){
  605. return $impossible_prize;
  606. }
  607. if(I('run')!='complate'){
  608. return $impossible_prize;
  609. }
  610. $swithc_no = '';
  611. foreach ($rules as $value) {
  612. if($value['rule_status'] == 0){
  613. $swithc_no = $swithc_no. 0;
  614. continue;
  615. }
  616. //1.检查是否符合身份
  617. if(FALSE === $this->_checkRuleRole($value['rule_role'])){
  618. $swithc_no = $swithc_no. 1;
  619. continue;
  620. }
  621. //2.检查中奖日期设置
  622. if(FALSE === $this->_checkRuleDate($value['rule_date'])){
  623. $swithc_no = $swithc_no. 2;
  624. continue;
  625. }
  626. //3.检查时段设置
  627. if(FALSE === $this->_checkRuleHour($value['rule_hour'])){
  628. $swithc_no = $swithc_no. 3;
  629. continue;
  630. }
  631. //4.概率摇奖
  632. if(FALSE === $this->_getRandNum($value['rule_probability'])){
  633. $swithc_no = $swithc_no. 4;
  634. continue;
  635. }
  636. //5.查询是否超出当前规则下的出奖数
  637. if(FALSE === $this->_checkCurrentRuleSale($value)){
  638. $swithc_no = $swithc_no. 5;
  639. continue;
  640. }
  641. //6.查询是否超出奖品总数
  642. if(FALSE === $this->_checkGolabRule($value,$limit_sales)){
  643. $swithc_no = $swithc_no. 6;
  644. continue;
  645. }
  646. Writelog('uid: '.$this->uid.'跳出关卡: '.$swithc_no,'switch','activityv2');
  647. $prize = $limit_sales[$value['rule_prize_id']];
  648. $prize['rule_id'] = $value['rule_id'];
  649. $prize['prize_status'] = 1;
  650. return $prize;
  651. }
  652. Writelog('uid: '.$this->uid.'跳出关卡: '.$swithc_no,'switch','activityv2');
  653. return $impossible_prize;
  654. }
  655. /**
  656. * 获取转盘信息
  657. */
  658. public function prizeData(){
  659. $error[] = ['error' => 'error'];
  660. $data = array();
  661. $data['userp']['prize_num'] = $this->_getUserChance();
  662. $data['userp']['user_phone'] = $this->userinfo['user_phone'] ? :$this->userinfo['uid'];
  663. //转盘信息
  664. $turn_data = json_decode($this->activityinfo['prize_list'],TRUE);
  665. if($turn_data){
  666. foreach ($turn_data as $k => $v){
  667. if($v['prize_status'] == 0){
  668. unset($turn_data[$k]);
  669. continue;
  670. }
  671. if($v['prize_img']){
  672. $turn_data[$k]['prize_img'] = __MY_URL__.$v['prize_img'];
  673. }
  674. unset($turn_data[$k]['prize_num']);
  675. unset($turn_data[$k]['prize_level']);
  676. unset($turn_data[$k]['prize_status']);
  677. }
  678. $data['prize_data'] = $turn_data;
  679. }else{
  680. $this->responseError('获取失败!', -1, $error);
  681. }
  682. $this->responseSuccess($data, '查询成功!');
  683. }
  684. private function getPrizeDefault()
  685. {
  686. $map['pd.act_id'] = $this->act_id;
  687. $prizes = json_decode($this->activityinfo['prize_list'],true);
  688. foreach ($prizes as $key=>$v){
  689. $prize_list[$v['prize_id']] = $v;
  690. }
  691. // var_dump($prize_list);die;
  692. $data = M('prize_default_v2')->where(['act_id'=>$this->act_id])->select();
  693. if($data){
  694. foreach ($data as $value) {
  695. $return_data[] = array(
  696. 'created_at'=>$value['prize_date'],
  697. 'prize_name'=>$prize_list[$value['prize_id']]['prize_name'],
  698. 'prize_img'=>$prize_list[$value['prize_id']]['prize_img'] ? __MY_URL__.$prize_list[$value['prize_id']]['prize_img']:'',
  699. 'user_phone'=>substr_replace($value['phone'],'****',3,4),
  700. );
  701. }
  702. }
  703. return $return_data;
  704. }
  705. /**
  706. * @desc 检查中奖规则关于身份设置
  707. */
  708. private function _checkRuleRole($rule_role)
  709. {
  710. $role = $this->vip_level ? '会员' : '普通用户';
  711. if(FALSE !== strpos($rule_role, '不限')){
  712. return TRUE;
  713. }
  714. if($rule_role != '虚拟用户'){
  715. if(in_array($role, explode(',', $rule_role))){
  716. return TRUE;
  717. }
  718. }
  719. return FALSE;
  720. }
  721. /**
  722. * @desc 检查中奖规则关于日期
  723. */
  724. private function _checkRuleDate($rule_date)
  725. {
  726. if(FALSE !== strpos($rule_date,'每日')){
  727. return TRUE;
  728. }
  729. //时间段
  730. if(strpos($rule_date,'至')){
  731. $date = explode('至', $rule_date);
  732. if((time()>=strtotime($date[0])) && time()< strtotime($date[1])){
  733. return TRUE;
  734. }else{
  735. return FALSE;
  736. }
  737. }else{
  738. if(date('Y-m-d') == $rule_date){
  739. return TRUE;
  740. }else{
  741. return FALSE;
  742. }
  743. }
  744. }
  745. /**
  746. * @desc 检查中奖规则关于时间段
  747. */
  748. private function _checkRuleHour($rule_hour)
  749. {
  750. $hour = explode('至', $rule_hour);
  751. if((date('H:i')>= trim($hour[0])) && (date('H:i')< trim($hour[1]))){
  752. return TRUE;
  753. }else{
  754. return FALSE;
  755. }
  756. }
  757. /**
  758. * @desc 概率摇奖
  759. * @param type $rule_probability
  760. * @return boolean
  761. */
  762. private function _getRandNum($rule_probability)
  763. {
  764. if($rule_probability == 0){
  765. return FALSE;
  766. }
  767. $tmp = explode('/', $rule_probability);
  768. $rand_array = range(1, $tmp[1]);
  769. shuffle($rand_array);
  770. $get_randnum = array_slice($rand_array, 0, $tmp[0]);
  771. Writelog('uid: '.$this->uid.'抽签: '.json_encode($get_randnum),'switch','activityv2');
  772. if(in_array($tmp[0], $get_randnum)){
  773. return TRUE;
  774. }else{
  775. return FALSE;
  776. }
  777. }
  778. /**
  779. * @desc 查询符合当前规则
  780. */
  781. private function _checkCurrentRuleSale(&$rule)
  782. {
  783. //查询当前规则下的中奖数,如果为-1则无限制
  784. if($rule['rule_num'] == -1){
  785. return TRUE;
  786. }
  787. if((FALSE!==strpos($rule['rule_date'],'至')) && ($rule['rule_cycle']=='total')){
  788. $rule_date = explode('至', $rule['rule_date']);
  789. $where['rule_id'] = $rule['rule_id'];
  790. $where['act_id'] = $this->act_id;
  791. $where['date'] = ['between',[date('Ymd', strtotime($rule_date[0])),date('Ymd', strtotime($rule_date[1]))]];
  792. $countNum = M('Prize_log_v2')->where($where)->count();
  793. Writelog('当前1:'.$countNum.'规则:'.$rule['rule_num'],'info','activityv2');
  794. return $countNum >= $rule['rule_num'] ? FALSE : TRUE;
  795. }else{
  796. $where['date'] = date('Ymd');
  797. $where['rule_id'] = $rule['rule_id'];
  798. $where['act_id'] = $this->act_id;
  799. $countNum = M('Prize_log_v2')->where($where)->count();
  800. Writelog('当前2:'.$countNum.'规则:'.$rule['rule_num'],'info','activityv2');
  801. // echo M()->getLastSql();die;
  802. return $countNum >= $rule['rule_num'] ? FALSE : TRUE;
  803. }
  804. }
  805. /**
  806. * @desc 不能超过奖品总数
  807. */
  808. private function _checkGolabRule(&$rule,&$limit_sales)
  809. {
  810. if($limit_sales[$rule['rule_prize_id']]['prize_num'] == -1){
  811. return TRUE;
  812. }
  813. $countNum = M('Prize_log_v2')->where(['prize_id'=>$rule['rule_prize_id'],'act_id'=>$this->act_id])->count();
  814. // echo M()->getLastSql();die;
  815. Writelog('当前3:'.$countNum.'总量:'.$limit_sales[$rule['rule_prize_id']]['prize_num'],'info','activityv2');
  816. return $countNum >= $limit_sales[$rule['rule_prize_id']]['prize_num'] ? FALSE : TRUE;
  817. }
  818. }