Commit 62e60529 by doszhang

Update IndexController.class.php

parent b2d2a156
<?php <?php
namespace Home\Controller; namespace Home\Controller;
use Think\Controller; use Think\Controller;
use SignedCookie; use SignedCookie;
use Zhida\OAuthHelperV3; use Zhida\OAuthHelperV3;
use Think\Upload\Driver\Qiniu\QiniuStorage; use Think\Upload\Driver\Qiniu\QiniuStorage;
use Think\Log; use Think\Log;
class IndexController extends Controller class IndexController extends Controller
{ {
private $auth_type = OAuthHelperV3::SCOPE_USERINFO; private $auth_type = OAuthHelperV3::SCOPE_USERINFO;
private $cookie_uid_key = 'skp-rotor-v1'; private $cookie_uid_key = 'skp-rotor-v1';
private $debug = 1; private $debug = 1;
private $today; private $today;
use ControllerTrait; use ControllerTrait;
public function index() public function index()
{ {
session_start(); session_start();
$_SESSION['joinTime'] = time(); $_SESSION['joinTime'] = time();
$user = $this->get_user_info_from_cookie(); $user = $this->get_user_info_from_cookie();
// 如果没有获取到用户信息,引导用户授权 // 如果没有获取到用户信息,引导用户授权
if (!$user) { if (!$user) {
$this->request_auth($this->get_url()); $this->request_auth($this->get_url());
return; return;
} }
M('join_num')->where('id=1')->setInc('num'); M('join_num')->where('id=1')->setInc('num');
$joinNum = M('join_num')->where('id=1')->find(); $joinNum = M('join_num')->where('id=1')->find();
$this->assign('pt',$user['prize_type']); $this->assign('pt',$user['prize_type']);
$this->assign('join_num',$joinNum['num']); $this->assign('join_num',$joinNum['num']);
$this->display(); $this->display();
} }
public function get_random_prize($user) public function get_random_prize($user)
{ {
$v = rand(0, 1000000) / 1000000; $v = rand(0, 1000000) / 1000000;
$today_ratio = 0.01; $today_ratio = 0.05;
if ($v > $today_ratio) { if ($v > $today_ratio) {
return false; return false;
} }
$now = time(); $now = time();
$type = 1; $type = 1;
$c = M('prize')->where(array( $c = M('prize')->where(array(
'prize_type' => $type, 'prize_type' => $type,
'is_get' => 1 'is_get' => 1
))->count(); ))->count();
if ($c >= 8) { if ($c >= 8) {
return false; return false;
} }
$prize = M('Prize')->where(array( $prize = M('Prize')->where(array(
'is_get' => 0, 'is_get' => 0,
'prize_type' => $type, 'prize_type' => $type,
'uid' => 0 'uid' => 0
))->order('rand()')->limit(1)->find(); ))->order('rand()')->limit(1)->find();
if (!$prize) { if (!$prize) {
return false; return false;
} }
$result = M('Prize')->where(array( $result = M('Prize')->where(array(
'id' => $prize['id'], 'id' => $prize['id'],
'is_get' => 0, 'is_get' => 0,
'uid' => 0, 'uid' => 0,
))->save(array( ))->save(array(
'is_get' => 1, 'is_get' => 1,
'uid' => $user['id'], 'uid' => $user['id'],
'add_time' => $now 'add_time' => $now
)); ));
if ($result > 0) { if ($result > 0) {
return $prize; return $prize;
} }
return false; return false;
} }
public function tryPrize() public function tryPrize()
{ {
$referer = $_SERVER['HTTP_REFERER']; $referer = $_SERVER['HTTP_REFERER'];
$refererInfo = parse_url( $referer ); $refererInfo = parse_url( $referer );
if( $refererInfo['host'] != $_SERVER['HTTP_HOST'] ) if( $refererInfo['host'] != $_SERVER['HTTP_HOST'] )
{ {
header("HTTP/1.0 404 Not Found"); header("HTTP/1.0 404 Not Found");
exit; exit;
} }
session_start(); session_start();
if(!$_SESSION['joinTime'] || time() - $_SESSION['joinTime'] < 2) if(!$_SESSION['joinTime'] || time() - $_SESSION['joinTime'] < 2)
{ {
header("HTTP/1.0 404 Not Found"); header("HTTP/1.0 404 Not Found");
exit; exit;
} }
$user = $this->get_user_info_from_cookie(); $user = $this->get_user_info_from_cookie();
if (!$user) { if (!$user) {
$this->ajaxReturn(array( $this->ajaxReturn(array(
'status' => 404, 'status' => 404,
'msg' => '用户信息错误', 'msg' => '用户信息错误',
'step' => 0 'step' => 0
)); ));
return; return;
} }
if($user['prize_type'] !== '0') { if($user['prize_type'] !== '0') {
$this->ajaxReturn(array( $this->ajaxReturn(array(
'status' => -1, 'status' => -1,
'msg' => '很遗憾,没有中奖', 'msg' => '很遗憾,没有中奖',
'step' => 3, 'step' => 3,
'times' => $playTimes, 'times' => $playTimes,
)); ));
return; return;
} }
$prize = $this->get_random_prize($user); $prize = $this->get_random_prize($user);
if ($prize) { if ($prize) {
M('user')->where(array( M('user')->where(array(
'id' => $user['id'] 'id' => $user['id']
))->save(array( ))->save(array(
'prize_type' => 1 'prize_type' => 1
)); ));
$this->ajaxReturn(array( $this->ajaxReturn(array(
'status' => 1, 'status' => 1,
'msg' => '恭喜您中奖了!', 'msg' => '恭喜您中奖了!',
'step' => 4 'step' => 4
)); ));
} else { } else {
M('user')->where(array( M('user')->where(array(
'id' => $user['id'] 'id' => $user['id']
))->save(array( ))->save(array(
'prize_type' => 2 'prize_type' => 2
)); ));
$this->ajaxReturn(array( $this->ajaxReturn(array(
'status' => 2, 'status' => 2,
'msg' => '恭喜您中奖了!', 'msg' => '恭喜您中奖了!',
'step' => 5, 'step' => 5,
)); ));
} }
} }
public function submitInfo(){ public function submitInfo(){
$user = $this->get_user_info_from_cookie(); $user = $this->get_user_info_from_cookie();
if (!$user) { if (!$user) {
$this->ajaxReturn(array( $this->ajaxReturn(array(
'status' => 404, 'status' => 404,
'msg' => '用户信息错误', 'msg' => '用户信息错误',
'step' => 0 'step' => 0
)); ));
return; return;
} }
$name = I('post.name'); $name = I('post.name');
$tel = I('post.tel'); $tel = I('post.tel');
$address = I('post.address'); $address = I('post.address');
$result = M('user')->where(array( $result = M('user')->where(array(
'id' => $user['id'] 'id' => $user['id']
))->save(array( ))->save(array(
'name' => $name, 'name' => $name,
'tel' => $tel, 'tel' => $tel,
'address' => $address 'address' => $address
)); ));
$this->ajaxReturn(array( $this->ajaxReturn(array(
'status' => 1, 'status' => 1,
'msg' => '信息提交成功', 'msg' => '信息提交成功',
'step' => 0 'step' => 0
)); ));
return; return;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment