MengbaoLogic.class.php 29 KB

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