123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743 |
- <?php
- namespace frontend\controllers;
- if(!session_id()) session_start();
- use Codeception\Command\Build;
- use common\library\Sms;
- use common\library\ZM_Geohash;
- use common\models\ActivityReceive;
- use common\models\Area;
- use common\models\Building;
- use common\models\Information;
- use common\models\Notice;
- use common\models\RedActivity;
- use common\models\RedRule;
- use common\models\Shortmsg;
- use common\models\SortMessage;
- use common\models\User;
- use common\models\UserInfo;
- use Yii;
- use yii\base\InvalidParamException;
- use yii\web\BadRequestHttpException;
- use yii\web\Controller;
- use yii\filters\VerbFilter;
- use yii\filters\AccessControl;
- use common\models\LoginForm;
- use frontend\models\PasswordResetRequestForm;
- use frontend\models\ResetPasswordForm;
- use frontend\models\SignupForm;
- use frontend\models\ContactForm;
- use yii\web\Cookie;
- /**
- * Site controller
- */
- class SiteController extends BasewechatController
- {
- public $enableCsrfValidation = false;
- public static $send_code_time = 120;
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'only' => ['logout', 'signup'],
- 'rules' => [
- [
- 'actions' => ['signup'],
- 'allow' => true,
- 'roles' => ['?'],
- ],
- [
- 'actions' => ['logout'],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'logout' => ['post'],
- ],
- ],
- ];
- }
- /**
- * @inheritdoc
- */
- public function actions()
- {
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- 'captcha' => [
- 'class' => 'yii\captcha\CaptchaAction',
- 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
- ],
- ];
- }
- static $pagenum = 10;
- /**
- * Displays homepage.
- *
- * @return mixed
- */
- public function actionIndex()
- {
- $wechat = new \common\library\WeChat();
- $ticket = $wechat->getTicket();
- $jssdk = new \common\library\Jssdk(Yii::$app->params['wechat']['appID'],Yii::$app->params['wechat']['appsecret'],$ticket);
- $signPackage = $jssdk->getSignPackage();
- $userid = YII::$app->user->id;
- $user = User::find()->select('tel')->where('id=:id and status=:status',[':id'=>$userid,':status'=>User::STATUS_ACTIVE])->one();
- $usertel = "";
- if(!empty($user))
- {
- $usertel = $user->tel;
- }
- $datas = Notice::find()->joinWith('information')->where(['bd_notice.status'=>Notice::STATUS_ACTIVE,'bd_information.type'=>Information::INFORMATION ]);
- $time = time();
- $datas = $datas->andwhere(['<' , 'bd_notice.release_time' , $time]);
- $datas = $datas->andwhere(['>' , 'bd_notice.over_time' , $time]);
- $datas = $datas->orderBy('bd_notice.updated_at DESC')->one();
- $city = Yii::$app->request->get('city');
- return $this->render('index',['city'=>$city,'datas'=>$datas,'usertel'=>$usertel,'signPackage'=>$signPackage]);
- }
- public function actionIndex2()
- {
- phpinfo();exit;
- // $wechat = new \common\library\WeChat();
- // $ticket = $wechat->getTicket();
- // $jssdk = new \common\library\Jssdk(Yii::$app->params['wechat']['appID'],Yii::$app->params['wechat']['appsecret'],$ticket);
- // $signPackage = $jssdk->getSignPackage();
- $signPackage = "";
- $userid = YII::$app->user->id;
- $user = User::find()->select('tel')->where('id=:id and status=:status',[':id'=>$userid,':status'=>User::STATUS_ACTIVE])->one();
- $usertel = "";
- if(!empty($user))
- {
- $usertel = $user->tel;
- }
- $datas = Notice::find()->joinWith('information')->where(['bd_notice.status'=>Notice::STATUS_ACTIVE,'bd_information.type'=>Information::INFORMATION ]);
- $time = time();
- $datas = $datas->andwhere(['<' , 'bd_notice.release_time' , $time]);
- $datas = $datas->andwhere(['>' , 'bd_notice.over_time' , $time]);
- $datas = $datas->orderBy('bd_notice.updated_at DESC')->one();
- $city = Yii::$app->request->get('city');
- return $this->render('index2',['city'=>$city,'datas'=>$datas,'usertel'=>$usertel,'signPackage'=>$signPackage]);
- }
- /**
- * Logs in a user.
- *
- * @return mixed
- */
- public function actionLogin()
- {
- if (!Yii::$app->user->isGuest) {
- return $this->goHome();
- }
- $model = new LoginForm();
- $cookies=Yii::$app->response->cookies;
- if(Yii::$app->request->isPost){
- if ($model->load(Yii::$app->request->post()) && $model->login()) {
- $rememberMe=(Yii::$app->request->post('rememberMe'))?1:0;
- if($rememberMe==1){
- $cookietime = time()+60*60*24*30*3;
- $cookies->add(new\yii\web\Cookie([
- 'name'=>'username',
- 'value'=>Yii::$app->request->post('LoginForm')['username'],
- 'expire'=>$cookietime
- ]));
- $cookies->add(new\yii\web\Cookie([
- 'name'=>'password',
- 'value'=>Yii::$app->request->post('LoginForm')['password'],
- 'expire'=>$cookietime
- ]));
- $cookies->add(new\yii\web\Cookie([
- 'name'=>'rememberme',
- 'value'=>$rememberMe,
- 'expire'=>$cookietime
- ]));
- }
- return $this->redirect(['site/index']);
- } else {
- $this->admin_alert("账号密码错误,请重新输入!","");
- // Yii::$app->getSession()->setFlash('error', '账号密码错误,请重新输入!');
- }
- }else{
- if(!empty($cookies['username'])&&!empty($cookies['password'])){
- $model->username = $cookies->getValue('username');
- $model->password = $cookies->getValue('password');
- if( $model->login()){
- return $this->redirect(['site/index']);
- }else{
- $cookies = Yii::$app->response->cookies;
- $cookietime = time();
- $cookies->add(new\yii\web\Cookie([
- 'name'=>'username',
- 'value'=>$cookies->getValue('username'),
- 'expire'=>$cookietime
- ]));
- $cookies->add(new\yii\web\Cookie([
- 'name'=>'password',
- 'value'=>$cookies->getValue('password'),
- 'expire'=>$cookietime
- ]));
- $cookies->remove('username');
- $cookies->remove('password');
- $this->admin_alert("密码错误,请重新输入!","");
- // Yii::$app->getSession()->setFlash('error', '密码错误,请重新输入!');
- // return $this->redirect(['site/login']);
- }
- }
- }
- return $this->render('login', [
- 'model' => $model,
- ]);
- }
- /**
- * Logs out the current user.
- *
- * @return mixed
- */
- public function actionLogout()
- {
- Yii::$app->user->logout();
- return $this->goHome();
- }
- /**
- * 找回密码页面
- * @return string
- */
- public function actionForgetpwd(){
- return $this->render('forgetpwd');
- }
- //找回密码
- public function actionUpdatepwd()
- {
- $username = Yii::$app->request->post('username');
- $code = Yii::$app->request->post('code');
- $password = Yii::$app->request->post('password');
- $passwordAgain = Yii::$app->request->post('password-again');
- $model = User::find()->where('username=:username',[':username'=>$username])->one();
- if(!empty($model)){
- $mod = SortMessage::find()->where('tel=:tell and created_at >= :time',[':tell'=>$model->username,':time'=>(time()-1800)])->orderBy('created_at DESC')->one();
- if($code==$mod->code){
- if($password==$passwordAgain){
- $hash_password = Yii::$app->security->generatePasswordHash($password);
- $model->password_hash = $hash_password;
- $model->updated_at = time();
- if($model->validate() && $model->save()){
- $this->admin_alert("修改成功","login");
- }else{
- $this->admin_alert("修改失败","");
- }
- }else{
- $this->admin_alert("确认密码不同","");
- }
- }else{
- $this->admin_alert("确认密码不同","");
- // $this->redirect(['site/forgetpwd']);
- }
- }else{
- $this->admin_alert("该账号未注册","");
- }
- }
- /**
- * Displays contact page.
- *
- * @return mixed
- */
- public function actionContact()
- {
- $model = new ContactForm();
- if ($model->load(Yii::$app->request->post()) && $model->validate()) {
- if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
- Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
- } else {
- Yii::$app->session->setFlash('error', 'There was an error sending your message.');
- }
- return $this->refresh();
- } else {
- return $this->render('contact', [
- 'model' => $model,
- ]);
- }
- }
- /**
- * Displays about page.
- *
- * @return mixed
- */
- public function actionAbout()
- {
- return $this->render('about');
- }
- /**
- * Signs user up.
- *
- * @return mixed
- */
- public function actionSignup()
- {
- $tel = Yii::$app->request->post('tel');
- $pwd = Yii::$app->request->post('pwd');
- $pwd_again = Yii::$app->request->post('pwd_again');
- $code = Yii::$app->request->post('code');
- if (Yii::$app->request->post()) {
- $model = new User();
- $redata = User::find()->where(['username' => $tel])->one();
- if (!empty($redata)) {
- Yii::$app->getSession()->setFlash('error', '该号码已经注册!');
- return $this->redirect(['site/signup']);
- }
- if($pwd!=$pwd_again){
- Yii::$app->getSession()->setFlash('error', '确认密码不正确!');
- return $this->redirect(['site/signup']);
- }
- $mod = SortMessage::find()->where('tel=:tell and created_at >= :time',[':tell'=>$tel,':time'=>(time()-1800)])->orderBy('created_at DESC')->one();
- if(!empty($mod)){
- $model->username = $tel;
- $model->setPassword($pwd);
- $model->generateAuthKey();
- $model->status = User::STATUS_ACTIVE;
- $model->state = User::USER_STATE_THROUGH;
- $model->role = User::USER_ROLE_COMMON;
- $model->created_at = time();
- $model->updated_at = time();
- if ($model->validate()) {
- if($code==$mod->code){
- $userinfo = new UserInfo();
- $transaction = Yii::$app->db->beginTransaction();
- if($model->save()){
- $userinfo->tel = $tel;
- $userinfo->uid = $model->id;
- $userinfo->nickname = $tel;
- $userinfo->updated_at = time();
- if($userinfo->validate() && $userinfo->save()){
- $transaction->commit();
- $cookies=Yii::$app->response->cookies;
- $cookies->add(new\yii\web\Cookie([
- 'name'=>'username',
- 'value'=>$model->username,
- ]));
- $cookies->add(new\yii\web\Cookie([
- 'name'=>'password',
- 'value'=>Yii::$app->request->post('password_hash'),
- ]));
- $loginForm = new LoginForm;
- $loginForm->username=$cookies->getValue('username');
- $loginForm->password=$cookies->getValue('password');
- if($loginForm->login()) {
- return $this->redirect(['site/index']);
- }else{
- $this->admin_alert("注册成功,请先去登录","login");
- Yii::$app->getSession()->setFlash('error', '注册成功,请先去登录');
- return $this->redirect(['site/login']);
- }
- }else{
- $transaction->rollBack();
- $this->admin_alert("注册失败","");
- var_dump($userinfo->getErrors());exit;
- Yii::$app->getSession()->setFlash('error', '注册失败');
- }
- }else{
- $transaction->rollBack();
- $this->admin_alert("注册失败","");
- var_dump($userinfo->getErrors());exit;
- Yii::$app->getSession()->setFlash('error', '注册失败');
- }
- }else{
- $this->admin_alert("验证码不正确","");
- var_dump('验证码不正确');exit;
- Yii::$app->getSession()->setFlash('error', '验证码不正确');
- }
- } else {
- $this->admin_alert("注册失败","");
- var_dump($model->getErrors());
- exit;
- Yii::$app->getSession()->setFlash('error', '注册失败');
- }
- }else{
- $this->admin_alert("请先获取手机验证码","");
- // var_dump('请先获取手机验证码');exit;
- // Yii::$app->getSession()->setFlash('error', '请先获取手机验证码!');
- }
- }
- // var_dump(Yii::$app->request->post());exit;
- $model = new SignupForm();
- if ($model->load(Yii::$app->request->post())) {
- if ($user = $model->signup()) {
- if (Yii::$app->getUser()->login($user)) {
- return $this->goHome();
- }
- }
- }
- return $this->render(
- 'signup',['model' => $model,
- ]);
- }
- /**
- * Requests password reset.
- *
- * @return mixed
- */
- public function actionRequestPasswordReset()
- {
- $model = new PasswordResetRequestForm();
- if ($model->load(Yii::$app->request->post()) && $model->validate()) {
- if ($model->sendEmail()) {
- Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
- return $this->goHome();
- } else {
- Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
- }
- }
- return $this->render('requestPasswordResetToken', [
- 'model' => $model,
- ]);
- }
- /**
- * Resets password.
- *
- * @param string $token
- * @return mixed
- * @throws BadRequestHttpException
- */
- public function actionResetPassword($token)
- {
- try {
- $model = new ResetPasswordForm($token);
- } catch (InvalidParamException $e) {
- throw new BadRequestHttpException($e->getMessage());
- }
- if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
- Yii::$app->session->setFlash('success', 'New password saved.');
- return $this->goHome();
- }
- return $this->render('resetPassword', [
- 'model' => $model,
- ]);
- }
- //验证注册图片验证码
- public function actionYzm(){
- $code = Yii::$app->request->post('code');
- if(!empty($code)){
- $session = Yii::$app->session;
- $code1 = $session['code'];
- if($code==$code1){
- $result=['sign'=>1,'msg'=>'ok'];
- }else{
- $result=['sign'=>0,'msg'=>'验证码不一致'];
- }
- }else{
- $result=['sign'=>0,'msg'=>'验证码不能为空'];
- }
- return json_encode($result);
- }
- /**
- * 注册发送验证码
- * @return string
- */
- public function actionSendcode(){
- $model = new SortMessage();
- $tel = Yii::$app->request->post('tel');
- $type = Yii::$app->request->post('type');
- $code = rand(100000,999999);
- $model->tel = $tel;
- $model->code = "".$code;
- $model->created_at = time();
- $telcheck = User::find()->where('username=:tel',[':tel'=>$tel])->one();
- if(empty($telcheck)) {
- $checkcode = SortMessage::find()->where('tel=:tel',[':tel'=>$tel])->orderBy('created_at')->one();
- if(empty($checkcode) || $checkcode->created_at+self::$send_code_time<time()){
- if ($model->validate() && $model->save()) {
- if(preg_match("/^1[34578]\d{9}$/", $tel)){
- if($this->SendShortmsg($model,$type)){
- return json_encode(['sign' => '1','msg'=>'验证码发送成功!']);
- }
- }else{
- return json_encode(['sign' => '0','msg'=>'手机号码格式不正确!']);
- }
- }
- return json_encode(['sign' => '0','msg'=>'验证码发送失败!']);
- }else{
- return json_encode(['sign' => '0','msg'=>'验证码已发送,请稍等!']);
- }
- }else{
- return json_encode(['sign'=>'0','msg'=>'该账号已注册!']);
- }
- }
- /**
- * 忘记密码发送验证码
- * @return string
- */
- public function actionSendcode1(){
- $model = new SortMessage();
- $tel = Yii::$app->request->post('tel');
- $type = Yii::$app->request->post('type');
- $code = rand(100000,999999);
- $model->tel = $tel;
- $model->code = "".$code;
- $model->created_at = time();
- $telcheck = User::find()->where('username=:tel',[':tel'=>$tel])->one();
- if(!empty($telcheck)) {
- $checkcode = SortMessage::find()->where('tel=:tel',[':tel'=>$tel])->orderBy('created_at')->one();
- if(empty($checkcode) || $checkcode->created_at+self::$send_code_time<time()){
- if ($model->validate() && $model->save()) {
- if(preg_match("/^1[34578]\d{9}$/", $tel)){
- if($this->SendShortmsg($model,$type)){
- return json_encode(['sign' => '1','msg'=>'验证码发送成功!']);
- }
- }else{
- return json_encode(['sign' => '0','msg'=>'手机号码格式不正确!']);
- }
- }
- return json_encode(['sign' => '0','msg'=>'验证码发送失败!']);
- }else{
- return json_encode(['sign' => '0','msg'=>'验证码已发送,请稍等!']);
- }
- }else{
- return json_encode(['sign'=>'0','msg'=>'该账号未注册!']);
- }
- }
- /**
- * 注册发送验证码
- * @return string
- */
- public function actionSendcode2(){
- $userid = YII::$app->user->id;
- $model = new SortMessage();
- $tel = Yii::$app->request->post('tel');
- $type = Yii::$app->request->post('type');
- $code = rand(100000,999999);
- $model->tel = $tel;
- $model->code = "".$code;
- $model->created_at = time();
- $telcheck = User::find()->where('tel=:tel and id=:id',[':tel'=>$tel,':id'=>$userid])->one();
- if(empty($telcheck)) {
- $checkcode = SortMessage::find()->where('tel=:tel',[':tel'=>$tel])->orderBy('created_at')->one();
- if(empty($checkcode) || $checkcode->created_at+self::$send_code_time<time()){
- if ($model->validate() && $model->save()) {
- if(preg_match("/^1[34578]\d{9}$/", $tel)){
- if($this->SendShortmsg($model,$type)){
- return json_encode(['sign' => '1','msg'=>'验证码发送成功!']);
- }else{
- return json_encode(['sign' => '0','msg'=>'验证码发送失败!']);
- }
- }else{
- return json_encode(['sign' => '0','msg'=>'手机号码格式不正确!']);
- }
- }else{
- return json_encode(['sign' => '0','msg'=>'验证码发送失败!存入数据有误']);
- }
- }else{
- return json_encode(['sign' => '0','msg'=>'验证码已发送,请稍等!']);
- }
- }else{
- return json_encode(['sign'=>'0','msg'=>'您已绑定手机号!']);
- }
- }
- /**
- * 发送短信
- * @param $model
- * @return bool
- */
- public static function SendShortmsg($model,$type){
- $message = '';
- if($type=='注册'){
- $message = "您的注册验证码为" . $model->code."。";
- }elseif($type == '找回密码'){
- $message = "您的找回密码验证码为" . $model->code."。";
- }elseif($type == '绑定手机号码'){
- $message = "您的绑定手机号码验证码为" . $model->code."。";
- }
- $sms = new Sms();
- $result =$sms->SendMessage($model->tel,$message);
- if($result) {
- return true;
- }else{
- return false;
- }
- }
- public function actionQuerybuilding()
- {
- if(Yii::$app->request->isAjax)
- {
- $page =Yii::$app->request->post('page');
- $page = empty($page) ? 1 :$page;
- $result=['error'=>0,'msg'=>'ok'];
- $post = Yii::$app->request->post();
- $lat = $post['latitude'];
- $long = $post['longitude'];
- $distance = 10000;
- $datas = [];
- $count = 0;
- $city = '';
- if(!empty($post['city'])){
- $city = $post['city'];
- }
- if (empty($lat) || empty($long)) {
- $result['error']=1;
- } else {
- $array_data = ['name'=>'localtion','expire'=>time()+3600,'value'=>['lat'=>$lat,'lng'=>$long]];
- Yii::$app->response->cookies->add(new Cookie($array_data));
- Yii::$app->response->send();//代码有exit,die,需要send
- $geohash = new ZM_Geohash();
- $geohash->init_codingMap();
- $datas = Building::findBySql("select *,get_Distance(latitude,longitude,:latitude,:longitude) as distance from bd_building WHERE posted=1 AND opening_begin < :time AND opening_end > :time HAVING distance < :distance ORDER BY distance ASC LIMIT ".($page-1)*self::$pagenum.",".self::$pagenum, [':latitude' => $lat, ':longitude' => $long, ':time' => time(), ':distance' => $distance])->asArray()->all();
- $count = Building::findBySql("select *,get_Distance(latitude,longitude,:latitude,:longitude) as distance from bd_building WHERE posted=1 AND opening_begin < :time AND opening_end > :time HAVING distance < :distance ", [':latitude' => $lat, ':longitude' => $long, ':time' => time(), ':distance' => $distance])->count();
- $result['count']=$count;
- //$code = $geohash->encode($lat, $long);
- //$datas = Building::findBySql("select *,get_Distance(latitude,longitude,:latitude,:longitude) as distance from bd_building where status=10 and posted=1 and hashcode like :like and opening_begin < :time and opening_end > :time HAVING distance < :distance ORDER BY distance ASC LIMIT ".($page-1)*self::$pagenum.",".self::$pagenum, [':latitude' => $lat, ':longitude' => $long, ':like' => substr($code, 0, 3) . '%', ':time' => time(), ':distance' => $distance])->asArray()->all();
- $result['msg']=$datas;
- if(!empty($datas))
- {
- foreach ($datas as $key=>$val)
- {
- $redData = RedActivity::findBySql("SELECT * FROM {{%red_activity}} WHERE area = :area AND valid_at > :valid_at",[':area'=>$val['district'],':valid_at'=>time()])->asArray()->one();
- if(!empty($redData))
- {
- $results = RedActivity::isRed($redData['id'],$val['id'],Yii::$app->user->id);
- if($results['error']==0 || $results['error']==2)
- {
- $datas[$key]['type'] = 3;
- }
- } else{
- $datas[$key]['error']=$val['city'];
- }
- }
- }
- //$result['count']=$count;
- $result['msg']=$datas;
- }
- exit(json_encode($result));
- }
- }
- //获取红包
- public function actionGetred()
- {
- $result=array('error'=>1,'msg'=>'');
- if(Yii::$app->request->isAjax)
- {
- $id = Yii::$app->request->post('building_id');
- //1,获得工地信息
- $buildata = Building::find()->where(['id'=>$id])->select(['city'])->one();
- if($buildata){
- $redData = RedActivity::findBySql("SELECT id FROM {{%red_activity}} WHERE city = :city AND valid_at > :valid_at",[':city'=>$buildata->city,':valid_at'=>time()])->asArray()->one();
- if($redData)
- {
- $results = RedActivity::isRed($redData['id'],$id,Yii::$app->user->id);
- if($results['error']==0)//红包记录
- {
- $resultlog = $this->insertRedLog($id,$redData['id']);
- if($resultlog['error']==0)
- {
- $result['error']=0;
- $result['amount'] =$resultlog['msg'];
- }else{
- $result['error']=1;
- }
- }elseif ($results['error']==2)
- {
- $result['error']=0;
- $actData = ActivityReceive::findBySql('SELECT amount,status FROM {{%activity_receive}} WHERE uid = :uid AND activity_id = :activity_id AND source_id = :source_id',[':uid'=>Yii::$app->user->id,':activity_id'=>$redData['id'],':source_id'=>$id])->one();
- if($actData['status']==ActivityReceive::STATUS_NOT)
- {
- $result['error']=0;
- $result['amount'] =$actData['amount'];
- }
- }
- }
- }
- }
- exit(json_encode($result));
- }
- //记录红包
- private function insertRedLog($building,$action_id)
- {
- $redData = RedActivity::findBySql('SELECT a.valid_at,b.start_price,b.end_price,b.id as rule_id FROM {{%red_activity}} AS a LEFT JOIN {{%red_rule}} AS b ON a.id = b.red_id WHERE a.id = :id',[':id'=>$action_id])->asArray()->one();
- if($redData)
- {
- $model = new ActivityReceive();
- $model->uid = Yii::$app->user->id;
- $model->activity_id = $action_id;
- $model->source_id = $building;
- $model->receive_at =time();
- $model->amount =$this->randomFloat($redData['start_price'],$redData['end_price']);
- $model->valid_at = $redData['valid_at'];
- if($model->save())
- {
- $ruleModel = RedRule::findOne($redData['rule_id']);
- $ruleModel->usecount=$ruleModel->usecount+1;
- $ruleModel->save();
- return ['error'=>0,'msg'=>$model->amount];
- }else{
- return ['error'=>1];
- }
- }
- return ['error'=>1];
- }
- private function randomFloat($min = 0, $max = 10)
- {
- $num = $min + mt_rand() / mt_getrandmax() * ($max - $min);
- return sprintf("%.2f", $num);
- }
- //弹窗提示
- function admin_alert($alert,$href=''){
- if(empty($href)){
- exit("<script>alert('{$alert}');history.back();</script>");
- }else{
- exit("<script>alert('{$alert}');window.location.href='{$href}';</script>");
- }
- }
- }
|