MengbaoLogic.class.php 29 KB

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