MengbaoLogic.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 = $score;
  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. /**ascii码从小到大排序
  153. * @param array $params
  154. * @return bool|string
  155. */
  156. function asc_sort($params = array())
  157. {
  158. if (!empty($params)) {
  159. $p = ksort($params);
  160. if ($p) {
  161. $str = '';
  162. foreach ($params as $k => $val) {
  163. $str .= $k . '=' . $val . '&';
  164. }
  165. $strs = rtrim($str, '&');
  166. return $strs;
  167. }
  168. }
  169. return false;
  170. }
  171. public function checkYearVip(){
  172. $uid = $this->uid;
  173. $ip = get_client_ip();
  174. $mac = I('mac');
  175. $version = I('version');
  176. $params = [
  177. 'userid'=>$uid,
  178. 'skuid'=>'30441',
  179. 'stime'=>'20200116000000',
  180. 'etime'=>'20201231000000',
  181. 'business_id'=>'1',
  182. 'ip'=>$ip,
  183. 'mac'=>$mac
  184. ];
  185. if(empty($uid) || empty($mac) || empty($version)){
  186. $this->responseError('检查uid mac version 参数',-1);
  187. }
  188. $data_json = json_encode($params,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  189. $asc_sort_arr = [
  190. 'version'=>$version,
  191. 'data'=>$data_json
  192. ];
  193. $asc_sort = $this->asc_sort($asc_sort_arr);
  194. $secret_key = '82dawe3ihz!@~5d32s(*%aaa(#aa&jy$';
  195. $params_string = strtolower($asc_sort.'&secret_key='.$secret_key);
  196. $sign = sha1($params_string);
  197. $request_string = 'sign='.$sign.'&version='.$version.'&data='.$data_json;
  198. //测试地址
  199. $url = 'http://10.2.204.141:8081/cms/ever_active_product';
  200. $ch = curl_init($url);
  201. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  202. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  203. curl_setopt($ch, CURLOPT_POSTFIELDS, $request_string); //设置提交的字符串
  204. $output = curl_exec($ch);
  205. $error_num = curl_errno($ch);
  206. curl_close($ch);
  207. if($error_num){
  208. Writelog($error_num,'error_num','curl_error');
  209. $this->responseError('服务异常', -1);
  210. }
  211. // $output = '{"msg":"是活动内订购","err":"0","status":"1111"}';
  212. $return = json_decode($output,true);
  213. if($return['status']== '0000'){
  214. $this->responseSuccess(['status'=>0],'is year_vip');
  215. }else{
  216. $this->responseSuccess(['status'=>2],'is not year_vip');
  217. }
  218. }
  219. /**
  220. * 获取剩余投票次数
  221. */
  222. public function _getVoteChance()
  223. {
  224. if($this->vip_level >= 1){
  225. $vote = 10;
  226. }else{
  227. $vote = 5;
  228. }
  229. $add = 0;
  230. if($this->vip_level ==2){
  231. $num = M('activity_vip_vote')->where(['uid'=>$this->uid,'act_id'=>$this->act_id])->getField('num');
  232. if($num === null){
  233. $insert = [
  234. 'act_id'=>$this->act_id,
  235. 'uid'=>$this->uid,
  236. 'num'=>0,
  237. 'created_at'=>date('Y-m-d H:i:s')
  238. ];
  239. M('activity_vip_vote')->add($insert);
  240. }
  241. $add = 200 - $num;
  242. }
  243. $vote += $add;
  244. //查看当前已投票次数
  245. $alreay_num = M('activity_vote')->where(['date'=>date('Ymd'),'uid'=>$this->uid,'act_id'=>$this->act_id])->sum('num');
  246. //查看当前应有票数
  247. if($vote <= $alreay_num){
  248. return 0;
  249. }
  250. return $vote > $alreay_num ? $vote - $alreay_num : 0;
  251. }
  252. /**
  253. * 获取剩余投票次数
  254. */
  255. public function getVoteChance()
  256. {
  257. $data['num'] = $this->_getVoteChance();
  258. $this->responseSuccess($data,'投票剩余次数');
  259. }
  260. /**
  261. * 提交用户资料
  262. * @return [type] [description]
  263. */
  264. public function setUserInfoByphone()
  265. {
  266. $phone = trim(I('phone'));
  267. $address = trim(I('address'));
  268. $receiver = trim(I('receiver'));
  269. if (!($phone && $address && $receiver)) {
  270. $this->responseError('参数错误!');
  271. }
  272. //检测手机号码是否正确
  273. if (!preg_match('/^1[345789]\d{9}$/ims', $phone)) {
  274. $this->responseError('手机号填写有误!');
  275. }
  276. //更新用户活动手机号码
  277. $where = array(
  278. 'uid'=>$this->uid,
  279. 'act_id'=>$this->act_id,
  280. );
  281. if ($id = M('activity_user_v2')->where($where)->getField('id')){
  282. $update['user_phone']= $phone;
  283. $update['address'] = $address;
  284. $update['receiver'] = $receiver;
  285. $update['updated_at'] = date('Y-m-d H:i:s');
  286. if(M('activity_user_v2')->where(['id'=>$id])->save($update)===false){
  287. Writelog(M()->getLastSql(),'sql','prize');
  288. $this->responseError("更新失败!");
  289. }
  290. }else{
  291. $insert_data = array(
  292. 'uid'=>$this->uid,
  293. 'act_id'=>$this->act_id,
  294. 'user_phone'=>$phone,
  295. 'address'=>$address,
  296. 'receiver'=>$receiver,
  297. 'created_at'=>date('Y-m-d H:i:s'),
  298. 'updated_at'=>date('Y-m-d H:i:s'),
  299. );
  300. if(!M('activity_user_v2')->add($insert_data)){
  301. Writelog(M()->getLastSql(),'sql','prize');
  302. $this->responseError("登记失败!");
  303. }
  304. }
  305. $this->responseSuccess(0, "登记成功!");
  306. }
  307. /**
  308. * 活动规则说明
  309. * @return [type] [description]
  310. */
  311. public function getActivityInfo()
  312. {
  313. // //获取活动规则信息
  314. $data['act'] = $this->activityinfo;
  315. $data['act']['info'] = explode('######', $this->activityinfo['introduce']);
  316. $data['act']['info'][0] = mb_substr(htmlspecialchars_decode($data['act']['info'][0]), 0, -3) ;
  317. $data['act']['info'][1] = mb_substr(htmlspecialchars_decode($data['act']['info'][1]), 4);
  318. //获取当前活动的手机号码
  319. $user_phone = M('activity_user_v2')->where(['act_id'=>$this->act_id,'uid'=>$this->uid])->getField('user_phone');
  320. if ($user_phone) {
  321. $data['user']['user_phone'] = $user_phone;
  322. }
  323. //获取奖品信息
  324. $prizes_tmp = json_decode($this->activityinfo['prize_list'],TRUE);
  325. foreach ($prizes_tmp as $key => $v) {
  326. $prizes[$v['prize_id']] = $v;
  327. if($v['prize_status'] == 0 || $v['prize_status'] == -1){
  328. continue;
  329. }
  330. $prize_list[] = ['id'=>$v['prize_id'],'prize_name'=>$v['prize_name'],'prize_img'=>$v['prize_img']];
  331. }
  332. // var_dump($prizes);die;
  333. $data['prizes'] = $prize_list;
  334. //获取中奖信息
  335. $data['rank'] = $this->prizelog('localhost');
  336. unset($data['act']['prize_list']);
  337. unset($data['act']['prize_rule']);
  338. //获取当前用户中奖信息
  339. $map['act_id'] = $this->act_id;
  340. $map['uid'] = $this->uid;
  341. $map['rule_id'] = array("neq", 0);
  342. //$data['my_prizes'] = M('prize_log_v2')->field('prize_id,prize_name')->where($map)->select();
  343. $my_prizes = M('prize_log_v2')->field('created_at,prize_id')->where($map)->select() ? : array();
  344. if($my_prizes){
  345. foreach ($my_prizes as $k=>$value) {
  346. $my_prizes[$k]['prize_name'] = $prizes[$value['prize_id']]['prize_name'];
  347. $data['my_prizes'][] = ['prize_id'=>$value['prize_id'],'prize_name'=>$prizes[$value['prize_id']]['prize_name']];
  348. }
  349. }
  350. //获取用户剩余中奖次数
  351. $data['remain_chance'] = $this->_getUserChance();
  352. $this->responseSuccess($data);
  353. }
  354. /**
  355. * 获取用户当日抽奖次数
  356. * @param [type] $uid [description]
  357. * @return [type] [description]
  358. */
  359. public function _getUserChance()
  360. {
  361. $map['uid'] = $this->uid;
  362. $map['act_id'] = $this->act_id;
  363. $map['date'] = date('Ymd');
  364. $chance = M('prize_log_v2')->where($map)->count();
  365. if(($this->activityinfo['is_test'] == 1) && (I('is_test') == 1)){
  366. return 1;
  367. }
  368. if($chance){
  369. return 0;
  370. }
  371. //是否参与了投票
  372. $bool = M('activity_vote')->where([$map])->find();
  373. if($bool){
  374. return 1;
  375. }else{
  376. return -1;
  377. }
  378. }
  379. /**
  380. * 获取用户剩余抽奖次数
  381. * @return [type] [description]
  382. */
  383. public function getPrizeNum()
  384. {
  385. $remain_chance = $this->_getUserChance();
  386. if($remain_chance == 0){
  387. $this->responseSuccess(['num'=>0],'您今天的抽奖次数用完了');
  388. }
  389. if($remain_chance == 1){
  390. $this->responseSuccess(['num'=>1],'您今天的剩余次数');
  391. }
  392. if($remain_chance == -1){
  393. $this->responseSuccess(['num'=>-1],'参与投票,即可抽奖');
  394. }
  395. }
  396. /**
  397. * 所有中奖记录查询
  398. */
  399. public function prizelog($model = ''){
  400. $error[] = ['error' => 'error'];
  401. $where['pl.act_id'] = $this->act_id;
  402. $where['pl.prize_status'] = 1;
  403. $data = M('prize_log_v2')->alias('pl')
  404. ->join(array('left join '.C("DB_PREFIX").'activity_user_v2 as iu on iu.uid = pl.uid and iu.act_id = '.$this->act_id))
  405. ->where($where)
  406. ->order('pl.id desc')
  407. ->field('pl.prize_id,pl.created_at,pl.uid,iu.user_phone')
  408. ->select();
  409. $prizes = json_decode($this->activityinfo['prize_list'],TRUE);
  410. foreach ($prizes as $value) {
  411. $prize_list[$value['prize_id']] = $value;
  412. }
  413. if($data){
  414. foreach ($data as $key=>$value) {
  415. $data[$key]['prize_name'] = $prize_list[$value['prize_id']]['prize_name'];
  416. $data[$key]['prize_img'] = __MY_URL__.$prize_list[$value['prize_id']]['prize_img'];
  417. }
  418. }
  419. //增加虚拟中奖信息
  420. $default_data = $this->getPrizeDefault();
  421. if($data || $default_data){
  422. if($data){
  423. foreach ($data as $k=>$v){
  424. if($v['user_phone']){
  425. $data[$k]['user_phone'] = substr_replace($v['user_phone'],'****',3,4);
  426. }else {
  427. $data[$k]['user_phone'] = substr_replace($v['uid'],'****',6);
  428. }
  429. unset($data[$k]['uid']);
  430. }
  431. }
  432. if($data && $default_data){
  433. $data = array_merge($default_data,$data);
  434. }else{
  435. $data = $data ? : $default_data;
  436. }
  437. array_multisort(array_column($data,'created_at'),SORT_DESC,$data);
  438. if($model == 'localhost'){
  439. return $data;
  440. }
  441. $this->responseSuccess($data, '查询成功!');
  442. }else{
  443. if($model == 'localhost'){
  444. return array();
  445. }
  446. $this->responseError('没有抽奖记录!', -3, $error);
  447. }
  448. }
  449. /**
  450. * 我的中奖记录查询
  451. */
  452. public function userPrizelog(){
  453. $error[] = ['error' => 'error'];
  454. //获取用户
  455. if($user_phone = M('activity_user_v2')->where(['uid'=>$this->uid,'act_id'=>$this->act_id])->getField('user_phone')){
  456. $phone_staus = 1;//填过
  457. }else{
  458. $phone_staus = 2;//填过
  459. }
  460. $where = array();
  461. $where['pl.prize_status'] = 1;
  462. $where['pl.uid'] = $this->uid;
  463. $where['pl.act_id'] = $this->act_id;
  464. $data = M('prize_log_v2')->alias('pl')
  465. ->where($where)
  466. ->order('pl.id desc')
  467. ->field('pl.prize_id,pl.created_at')
  468. ->select();
  469. // echo M()->getLastSql();die;
  470. $prizes = json_decode($this->activityinfo['prize_list'],TRUE);
  471. foreach ($prizes as $value) {
  472. $prize_list[$value['prize_id']] = $value;
  473. }
  474. $flag = $data ? 1 : 0;
  475. if($flag){
  476. if($data){
  477. foreach ($data as $k => $v){
  478. $data[$k]['prize_name'] = $prize_list[$v['prize_id']]['prize_name'];
  479. $data[$k]['prize_object'] = 'real';
  480. $data[$k]['show_image'] = $prize_list[$v['prize_id']]['prize_img'] ? __MY_URL__.$prize_list[$v['prize_id']]['prize_img']:null;
  481. unset($data[$k]['prize_id']);
  482. }
  483. }
  484. $return_data = array(
  485. 'my_prizes'=>$data,
  486. 'user_phone'=>['phone_status'=>$phone_staus,'user_phone'=>$user_phone ? :''],
  487. 'prize_status'=>$data ? 'real' : 'vitual'
  488. );
  489. $this->responseSuccess($return_data, '查询成功!');
  490. }else{
  491. $this->responseError('没有抽奖记录!', -3, $error);
  492. }
  493. }
  494. /**
  495. * 抽奖
  496. */
  497. public function prize(){
  498. $error[] = ['error' => 'error'];
  499. $chance = $this->_getUserChance();
  500. if($chance == 0){
  501. $this->responseError('您今天的抽奖次数用完了');
  502. }
  503. if($chance == -1){
  504. $this->responseSuccess('参与投票,即可抽奖');
  505. }
  506. //开始抽奖
  507. $prize_data = $this->_get_prize();
  508. unset($prize_data['yes']['rule_id']);
  509. $this->responseSuccess($prize_data, '抽奖成功!');
  510. }
  511. private function _get_prize() {
  512. $result = $this->getPrizeSetDeatil();
  513. // var_dump($result);die;
  514. $this->_writePrizeLog($result);
  515. $res = array();
  516. $res['yes']['prize_name'] = $result['prize_name']; //中奖项
  517. $res['yes']['prize_status'] = $result['prize_object'] ? : 'none';
  518. $res['yes']['prize_img'] = $result['prize_img'] ? __MY_URL__.$result['prize_img'] : '';
  519. $res['yes']['rule_id'] = $result['rule_id'];
  520. return $res;
  521. }
  522. /**
  523. * @param desc 写入抽奖日志
  524. * @param type $result
  525. */
  526. private function _writePrizeLog(&$result) {
  527. $log_data['uid'] = $this->uid;
  528. $log_data['prize_id'] = $result['prize_id'];
  529. $log_data['is_vip'] = $this->vip_level;
  530. $log_data['act_id'] = $this->act_id;
  531. $log_data['created_at'] = date('Y-m-d H:i:s');
  532. $log_data['date'] = date('Ymd');
  533. $log_data['rule_id'] = $result['rule_id'];
  534. $log_data['prize_status'] = $result['prize_status'];
  535. if(!M('prize_log_v2')->add($log_data)){
  536. Writelog(M()->getLastSql(),'sql','activity');
  537. $this->responseError('系统繁忙抽奖失败', -1);
  538. }
  539. }
  540. /**
  541. * 返回中奖的信息
  542. */
  543. private function getPrizeSetDeatil()
  544. {
  545. $prize_list = json_decode($this->activityinfo['prize_list'],TRUE);//获取奖项设置
  546. if($prize_list){
  547. foreach ($prize_list as $row) {
  548. $limit_sales[$row['prize_id']] = $row;
  549. if($row['prize_level']== 0){
  550. $impossible_prize = ['prize_id'=>$row['prize_id'],'prize_name'=>$row['prize_name'],'rule_id'=>0,'prize_img'=>'','prize_status'=>0];
  551. }
  552. }
  553. }
  554. //获取所有规则
  555. $rules = json_decode($this->activityinfo['prize_rule'],TRUE);
  556. if(!$rules){
  557. return $impossible_prize;
  558. }
  559. if(I('run')!='complate'){
  560. return $impossible_prize;
  561. }
  562. //如果中了一次实物奖就不允许中第二次
  563. $map = array(
  564. 'act_id'=> $this->act_id,
  565. 'uid'=> $this->uid,
  566. 'prize_status'=>1,
  567. );
  568. if(M('Prize_log_v2')->where($map)->find()){
  569. return $impossible_prize;
  570. }
  571. $swithc_no = '';
  572. foreach ($rules as $value) {
  573. if($value['rule_status'] == 0){
  574. $swithc_no = $swithc_no. 0;
  575. continue;
  576. }
  577. //1.检查是否符合身份
  578. if(FALSE === $this->_checkRuleRole($value['rule_role'])){
  579. $swithc_no = $swithc_no. 1;
  580. continue;
  581. }
  582. //2.检查中奖日期设置
  583. if(FALSE === $this->_checkRuleDate($value['rule_date'])){
  584. $swithc_no = $swithc_no. 2;
  585. continue;
  586. }
  587. //3.检查时段设置
  588. if(FALSE === $this->_checkRuleHour($value['rule_hour'])){
  589. $swithc_no = $swithc_no. 3;
  590. continue;
  591. }
  592. //4.概率摇奖
  593. if(FALSE === $this->_getRandNum($value['rule_probability'])){
  594. $swithc_no = $swithc_no. 4;
  595. continue;
  596. }
  597. //5.查询是否超出当前规则下的出奖数
  598. if(FALSE === $this->_checkCurrentRuleSale($value)){
  599. $swithc_no = $swithc_no. 5;
  600. continue;
  601. }
  602. //6.查询是否超出奖品总数
  603. if(FALSE === $this->_checkGolabRule($value,$limit_sales)){
  604. $swithc_no = $swithc_no. 6;
  605. continue;
  606. }
  607. //8.如果中了一次实物奖就不允许中第二次
  608. if('real'== $limit_sales[$value['rule_prize_id']]['prize_object']){
  609. if(M('Prize_log_v2')->where(['act_id'=>$this->act_id,'uid'=>$this->uid,'prize_status'=>1])->find()){
  610. $swithc_no = $swithc_no. 8;
  611. continue;
  612. }
  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. }