SiteController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. namespace frontend\controllers;
  3. if(!session_id()) session_start();
  4. use Codeception\Command\Build;
  5. use common\library\Sms;
  6. use common\library\ZM_Geohash;
  7. use common\models\ActivityReceive;
  8. use common\models\Area;
  9. use common\models\Building;
  10. use common\models\Information;
  11. use common\models\Notice;
  12. use common\models\RedActivity;
  13. use common\models\RedRule;
  14. use common\models\Shortmsg;
  15. use common\models\SortMessage;
  16. use common\models\User;
  17. use common\models\UserInfo;
  18. use Yii;
  19. use yii\base\InvalidParamException;
  20. use yii\web\BadRequestHttpException;
  21. use yii\web\Controller;
  22. use yii\filters\VerbFilter;
  23. use yii\filters\AccessControl;
  24. use common\models\LoginForm;
  25. use frontend\models\PasswordResetRequestForm;
  26. use frontend\models\ResetPasswordForm;
  27. use frontend\models\SignupForm;
  28. use frontend\models\ContactForm;
  29. use yii\web\Cookie;
  30. /**
  31. * Site controller
  32. */
  33. class SiteController extends BasewechatController
  34. {
  35. public $enableCsrfValidation = false;
  36. public static $send_code_time = 120;
  37. /**
  38. * @inheritdoc
  39. */
  40. public function behaviors()
  41. {
  42. return [
  43. 'access' => [
  44. 'class' => AccessControl::className(),
  45. 'only' => ['logout', 'signup'],
  46. 'rules' => [
  47. [
  48. 'actions' => ['signup'],
  49. 'allow' => true,
  50. 'roles' => ['?'],
  51. ],
  52. [
  53. 'actions' => ['logout'],
  54. 'allow' => true,
  55. 'roles' => ['@'],
  56. ],
  57. ],
  58. ],
  59. 'verbs' => [
  60. 'class' => VerbFilter::className(),
  61. 'actions' => [
  62. 'logout' => ['post'],
  63. ],
  64. ],
  65. ];
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function actions()
  71. {
  72. return [
  73. 'error' => [
  74. 'class' => 'yii\web\ErrorAction',
  75. ],
  76. 'captcha' => [
  77. 'class' => 'yii\captcha\CaptchaAction',
  78. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  79. ],
  80. ];
  81. }
  82. static $pagenum = 10;
  83. /**
  84. * Displays homepage.
  85. *
  86. * @return mixed
  87. */
  88. public function actionIndex()
  89. {
  90. $wechat = new \common\library\WeChat();
  91. $ticket = $wechat->getTicket();
  92. $jssdk = new \common\library\Jssdk(Yii::$app->params['wechat']['appID'],Yii::$app->params['wechat']['appsecret'],$ticket);
  93. $signPackage = $jssdk->getSignPackage();
  94. $userid = YII::$app->user->id;
  95. $user = User::find()->select('tel')->where('id=:id and status=:status',[':id'=>$userid,':status'=>User::STATUS_ACTIVE])->one();
  96. $usertel = "";
  97. if(!empty($user))
  98. {
  99. $usertel = $user->tel;
  100. }
  101. $datas = Notice::find()->joinWith('information')->where(['bd_notice.status'=>Notice::STATUS_ACTIVE,'bd_information.type'=>Information::INFORMATION ]);
  102. $time = time();
  103. $datas = $datas->andwhere(['<' , 'bd_notice.release_time' , $time]);
  104. $datas = $datas->andwhere(['>' , 'bd_notice.over_time' , $time]);
  105. $datas = $datas->orderBy('bd_notice.updated_at DESC')->one();
  106. $city = Yii::$app->request->get('city');
  107. return $this->render('index',['city'=>$city,'datas'=>$datas,'usertel'=>$usertel,'signPackage'=>$signPackage]);
  108. }
  109. public function actionIndex2()
  110. {
  111. phpinfo();exit;
  112. // $wechat = new \common\library\WeChat();
  113. // $ticket = $wechat->getTicket();
  114. // $jssdk = new \common\library\Jssdk(Yii::$app->params['wechat']['appID'],Yii::$app->params['wechat']['appsecret'],$ticket);
  115. // $signPackage = $jssdk->getSignPackage();
  116. $signPackage = "";
  117. $userid = YII::$app->user->id;
  118. $user = User::find()->select('tel')->where('id=:id and status=:status',[':id'=>$userid,':status'=>User::STATUS_ACTIVE])->one();
  119. $usertel = "";
  120. if(!empty($user))
  121. {
  122. $usertel = $user->tel;
  123. }
  124. $datas = Notice::find()->joinWith('information')->where(['bd_notice.status'=>Notice::STATUS_ACTIVE,'bd_information.type'=>Information::INFORMATION ]);
  125. $time = time();
  126. $datas = $datas->andwhere(['<' , 'bd_notice.release_time' , $time]);
  127. $datas = $datas->andwhere(['>' , 'bd_notice.over_time' , $time]);
  128. $datas = $datas->orderBy('bd_notice.updated_at DESC')->one();
  129. $city = Yii::$app->request->get('city');
  130. return $this->render('index2',['city'=>$city,'datas'=>$datas,'usertel'=>$usertel,'signPackage'=>$signPackage]);
  131. }
  132. /**
  133. * Logs in a user.
  134. *
  135. * @return mixed
  136. */
  137. public function actionLogin()
  138. {
  139. if (!Yii::$app->user->isGuest) {
  140. return $this->goHome();
  141. }
  142. $model = new LoginForm();
  143. $cookies=Yii::$app->response->cookies;
  144. if(Yii::$app->request->isPost){
  145. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  146. $rememberMe=(Yii::$app->request->post('rememberMe'))?1:0;
  147. if($rememberMe==1){
  148. $cookietime = time()+60*60*24*30*3;
  149. $cookies->add(new\yii\web\Cookie([
  150. 'name'=>'username',
  151. 'value'=>Yii::$app->request->post('LoginForm')['username'],
  152. 'expire'=>$cookietime
  153. ]));
  154. $cookies->add(new\yii\web\Cookie([
  155. 'name'=>'password',
  156. 'value'=>Yii::$app->request->post('LoginForm')['password'],
  157. 'expire'=>$cookietime
  158. ]));
  159. $cookies->add(new\yii\web\Cookie([
  160. 'name'=>'rememberme',
  161. 'value'=>$rememberMe,
  162. 'expire'=>$cookietime
  163. ]));
  164. }
  165. return $this->redirect(['site/index']);
  166. } else {
  167. $this->admin_alert("账号密码错误,请重新输入!","");
  168. // Yii::$app->getSession()->setFlash('error', '账号密码错误,请重新输入!');
  169. }
  170. }else{
  171. if(!empty($cookies['username'])&&!empty($cookies['password'])){
  172. $model->username = $cookies->getValue('username');
  173. $model->password = $cookies->getValue('password');
  174. if( $model->login()){
  175. return $this->redirect(['site/index']);
  176. }else{
  177. $cookies = Yii::$app->response->cookies;
  178. $cookietime = time();
  179. $cookies->add(new\yii\web\Cookie([
  180. 'name'=>'username',
  181. 'value'=>$cookies->getValue('username'),
  182. 'expire'=>$cookietime
  183. ]));
  184. $cookies->add(new\yii\web\Cookie([
  185. 'name'=>'password',
  186. 'value'=>$cookies->getValue('password'),
  187. 'expire'=>$cookietime
  188. ]));
  189. $cookies->remove('username');
  190. $cookies->remove('password');
  191. $this->admin_alert("密码错误,请重新输入!","");
  192. // Yii::$app->getSession()->setFlash('error', '密码错误,请重新输入!');
  193. // return $this->redirect(['site/login']);
  194. }
  195. }
  196. }
  197. return $this->render('login', [
  198. 'model' => $model,
  199. ]);
  200. }
  201. /**
  202. * Logs out the current user.
  203. *
  204. * @return mixed
  205. */
  206. public function actionLogout()
  207. {
  208. Yii::$app->user->logout();
  209. return $this->goHome();
  210. }
  211. /**
  212. * 找回密码页面
  213. * @return string
  214. */
  215. public function actionForgetpwd(){
  216. return $this->render('forgetpwd');
  217. }
  218. //找回密码
  219. public function actionUpdatepwd()
  220. {
  221. $username = Yii::$app->request->post('username');
  222. $code = Yii::$app->request->post('code');
  223. $password = Yii::$app->request->post('password');
  224. $passwordAgain = Yii::$app->request->post('password-again');
  225. $model = User::find()->where('username=:username',[':username'=>$username])->one();
  226. if(!empty($model)){
  227. $mod = SortMessage::find()->where('tel=:tell and created_at >= :time',[':tell'=>$model->username,':time'=>(time()-1800)])->orderBy('created_at DESC')->one();
  228. if($code==$mod->code){
  229. if($password==$passwordAgain){
  230. $hash_password = Yii::$app->security->generatePasswordHash($password);
  231. $model->password_hash = $hash_password;
  232. $model->updated_at = time();
  233. if($model->validate() && $model->save()){
  234. $this->admin_alert("修改成功","login");
  235. }else{
  236. $this->admin_alert("修改失败","");
  237. }
  238. }else{
  239. $this->admin_alert("确认密码不同","");
  240. }
  241. }else{
  242. $this->admin_alert("确认密码不同","");
  243. // $this->redirect(['site/forgetpwd']);
  244. }
  245. }else{
  246. $this->admin_alert("该账号未注册","");
  247. }
  248. }
  249. /**
  250. * Displays contact page.
  251. *
  252. * @return mixed
  253. */
  254. public function actionContact()
  255. {
  256. $model = new ContactForm();
  257. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  258. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  259. Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
  260. } else {
  261. Yii::$app->session->setFlash('error', 'There was an error sending your message.');
  262. }
  263. return $this->refresh();
  264. } else {
  265. return $this->render('contact', [
  266. 'model' => $model,
  267. ]);
  268. }
  269. }
  270. /**
  271. * Displays about page.
  272. *
  273. * @return mixed
  274. */
  275. public function actionAbout()
  276. {
  277. return $this->render('about');
  278. }
  279. /**
  280. * Signs user up.
  281. *
  282. * @return mixed
  283. */
  284. public function actionSignup()
  285. {
  286. $tel = Yii::$app->request->post('tel');
  287. $pwd = Yii::$app->request->post('pwd');
  288. $pwd_again = Yii::$app->request->post('pwd_again');
  289. $code = Yii::$app->request->post('code');
  290. if (Yii::$app->request->post()) {
  291. $model = new User();
  292. $redata = User::find()->where(['username' => $tel])->one();
  293. if (!empty($redata)) {
  294. Yii::$app->getSession()->setFlash('error', '该号码已经注册!');
  295. return $this->redirect(['site/signup']);
  296. }
  297. if($pwd!=$pwd_again){
  298. Yii::$app->getSession()->setFlash('error', '确认密码不正确!');
  299. return $this->redirect(['site/signup']);
  300. }
  301. $mod = SortMessage::find()->where('tel=:tell and created_at >= :time',[':tell'=>$tel,':time'=>(time()-1800)])->orderBy('created_at DESC')->one();
  302. if(!empty($mod)){
  303. $model->username = $tel;
  304. $model->setPassword($pwd);
  305. $model->generateAuthKey();
  306. $model->status = User::STATUS_ACTIVE;
  307. $model->state = User::USER_STATE_THROUGH;
  308. $model->role = User::USER_ROLE_COMMON;
  309. $model->created_at = time();
  310. $model->updated_at = time();
  311. if ($model->validate()) {
  312. if($code==$mod->code){
  313. $userinfo = new UserInfo();
  314. $transaction = Yii::$app->db->beginTransaction();
  315. if($model->save()){
  316. $userinfo->tel = $tel;
  317. $userinfo->uid = $model->id;
  318. $userinfo->nickname = $tel;
  319. $userinfo->updated_at = time();
  320. if($userinfo->validate() && $userinfo->save()){
  321. $transaction->commit();
  322. $cookies=Yii::$app->response->cookies;
  323. $cookies->add(new\yii\web\Cookie([
  324. 'name'=>'username',
  325. 'value'=>$model->username,
  326. ]));
  327. $cookies->add(new\yii\web\Cookie([
  328. 'name'=>'password',
  329. 'value'=>Yii::$app->request->post('password_hash'),
  330. ]));
  331. $loginForm = new LoginForm;
  332. $loginForm->username=$cookies->getValue('username');
  333. $loginForm->password=$cookies->getValue('password');
  334. if($loginForm->login()) {
  335. return $this->redirect(['site/index']);
  336. }else{
  337. $this->admin_alert("注册成功,请先去登录","login");
  338. Yii::$app->getSession()->setFlash('error', '注册成功,请先去登录');
  339. return $this->redirect(['site/login']);
  340. }
  341. }else{
  342. $transaction->rollBack();
  343. $this->admin_alert("注册失败","");
  344. var_dump($userinfo->getErrors());exit;
  345. Yii::$app->getSession()->setFlash('error', '注册失败');
  346. }
  347. }else{
  348. $transaction->rollBack();
  349. $this->admin_alert("注册失败","");
  350. var_dump($userinfo->getErrors());exit;
  351. Yii::$app->getSession()->setFlash('error', '注册失败');
  352. }
  353. }else{
  354. $this->admin_alert("验证码不正确","");
  355. var_dump('验证码不正确');exit;
  356. Yii::$app->getSession()->setFlash('error', '验证码不正确');
  357. }
  358. } else {
  359. $this->admin_alert("注册失败","");
  360. var_dump($model->getErrors());
  361. exit;
  362. Yii::$app->getSession()->setFlash('error', '注册失败');
  363. }
  364. }else{
  365. $this->admin_alert("请先获取手机验证码","");
  366. // var_dump('请先获取手机验证码');exit;
  367. // Yii::$app->getSession()->setFlash('error', '请先获取手机验证码!');
  368. }
  369. }
  370. // var_dump(Yii::$app->request->post());exit;
  371. $model = new SignupForm();
  372. if ($model->load(Yii::$app->request->post())) {
  373. if ($user = $model->signup()) {
  374. if (Yii::$app->getUser()->login($user)) {
  375. return $this->goHome();
  376. }
  377. }
  378. }
  379. return $this->render(
  380. 'signup',['model' => $model,
  381. ]);
  382. }
  383. /**
  384. * Requests password reset.
  385. *
  386. * @return mixed
  387. */
  388. public function actionRequestPasswordReset()
  389. {
  390. $model = new PasswordResetRequestForm();
  391. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  392. if ($model->sendEmail()) {
  393. Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
  394. return $this->goHome();
  395. } else {
  396. Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
  397. }
  398. }
  399. return $this->render('requestPasswordResetToken', [
  400. 'model' => $model,
  401. ]);
  402. }
  403. /**
  404. * Resets password.
  405. *
  406. * @param string $token
  407. * @return mixed
  408. * @throws BadRequestHttpException
  409. */
  410. public function actionResetPassword($token)
  411. {
  412. try {
  413. $model = new ResetPasswordForm($token);
  414. } catch (InvalidParamException $e) {
  415. throw new BadRequestHttpException($e->getMessage());
  416. }
  417. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  418. Yii::$app->session->setFlash('success', 'New password saved.');
  419. return $this->goHome();
  420. }
  421. return $this->render('resetPassword', [
  422. 'model' => $model,
  423. ]);
  424. }
  425. //验证注册图片验证码
  426. public function actionYzm(){
  427. $code = Yii::$app->request->post('code');
  428. if(!empty($code)){
  429. $session = Yii::$app->session;
  430. $code1 = $session['code'];
  431. if($code==$code1){
  432. $result=['sign'=>1,'msg'=>'ok'];
  433. }else{
  434. $result=['sign'=>0,'msg'=>'验证码不一致'];
  435. }
  436. }else{
  437. $result=['sign'=>0,'msg'=>'验证码不能为空'];
  438. }
  439. return json_encode($result);
  440. }
  441. /**
  442. * 注册发送验证码
  443. * @return string
  444. */
  445. public function actionSendcode(){
  446. $model = new SortMessage();
  447. $tel = Yii::$app->request->post('tel');
  448. $type = Yii::$app->request->post('type');
  449. $code = rand(100000,999999);
  450. $model->tel = $tel;
  451. $model->code = "".$code;
  452. $model->created_at = time();
  453. $telcheck = User::find()->where('username=:tel',[':tel'=>$tel])->one();
  454. if(empty($telcheck)) {
  455. $checkcode = SortMessage::find()->where('tel=:tel',[':tel'=>$tel])->orderBy('created_at')->one();
  456. if(empty($checkcode) || $checkcode->created_at+self::$send_code_time<time()){
  457. if ($model->validate() && $model->save()) {
  458. if(preg_match("/^1[34578]\d{9}$/", $tel)){
  459. if($this->SendShortmsg($model,$type)){
  460. return json_encode(['sign' => '1','msg'=>'验证码发送成功!']);
  461. }
  462. }else{
  463. return json_encode(['sign' => '0','msg'=>'手机号码格式不正确!']);
  464. }
  465. }
  466. return json_encode(['sign' => '0','msg'=>'验证码发送失败!']);
  467. }else{
  468. return json_encode(['sign' => '0','msg'=>'验证码已发送,请稍等!']);
  469. }
  470. }else{
  471. return json_encode(['sign'=>'0','msg'=>'该账号已注册!']);
  472. }
  473. }
  474. /**
  475. * 忘记密码发送验证码
  476. * @return string
  477. */
  478. public function actionSendcode1(){
  479. $model = new SortMessage();
  480. $tel = Yii::$app->request->post('tel');
  481. $type = Yii::$app->request->post('type');
  482. $code = rand(100000,999999);
  483. $model->tel = $tel;
  484. $model->code = "".$code;
  485. $model->created_at = time();
  486. $telcheck = User::find()->where('username=:tel',[':tel'=>$tel])->one();
  487. if(!empty($telcheck)) {
  488. $checkcode = SortMessage::find()->where('tel=:tel',[':tel'=>$tel])->orderBy('created_at')->one();
  489. if(empty($checkcode) || $checkcode->created_at+self::$send_code_time<time()){
  490. if ($model->validate() && $model->save()) {
  491. if(preg_match("/^1[34578]\d{9}$/", $tel)){
  492. if($this->SendShortmsg($model,$type)){
  493. return json_encode(['sign' => '1','msg'=>'验证码发送成功!']);
  494. }
  495. }else{
  496. return json_encode(['sign' => '0','msg'=>'手机号码格式不正确!']);
  497. }
  498. }
  499. return json_encode(['sign' => '0','msg'=>'验证码发送失败!']);
  500. }else{
  501. return json_encode(['sign' => '0','msg'=>'验证码已发送,请稍等!']);
  502. }
  503. }else{
  504. return json_encode(['sign'=>'0','msg'=>'该账号未注册!']);
  505. }
  506. }
  507. /**
  508. * 注册发送验证码
  509. * @return string
  510. */
  511. public function actionSendcode2(){
  512. $userid = YII::$app->user->id;
  513. $model = new SortMessage();
  514. $tel = Yii::$app->request->post('tel');
  515. $type = Yii::$app->request->post('type');
  516. $code = rand(100000,999999);
  517. $model->tel = $tel;
  518. $model->code = "".$code;
  519. $model->created_at = time();
  520. $telcheck = User::find()->where('tel=:tel and id=:id',[':tel'=>$tel,':id'=>$userid])->one();
  521. if(empty($telcheck)) {
  522. $checkcode = SortMessage::find()->where('tel=:tel',[':tel'=>$tel])->orderBy('created_at')->one();
  523. if(empty($checkcode) || $checkcode->created_at+self::$send_code_time<time()){
  524. if ($model->validate() && $model->save()) {
  525. if(preg_match("/^1[34578]\d{9}$/", $tel)){
  526. if($this->SendShortmsg($model,$type)){
  527. return json_encode(['sign' => '1','msg'=>'验证码发送成功!']);
  528. }else{
  529. return json_encode(['sign' => '0','msg'=>'验证码发送失败!']);
  530. }
  531. }else{
  532. return json_encode(['sign' => '0','msg'=>'手机号码格式不正确!']);
  533. }
  534. }else{
  535. return json_encode(['sign' => '0','msg'=>'验证码发送失败!存入数据有误']);
  536. }
  537. }else{
  538. return json_encode(['sign' => '0','msg'=>'验证码已发送,请稍等!']);
  539. }
  540. }else{
  541. return json_encode(['sign'=>'0','msg'=>'您已绑定手机号!']);
  542. }
  543. }
  544. /**
  545. * 发送短信
  546. * @param $model
  547. * @return bool
  548. */
  549. public static function SendShortmsg($model,$type){
  550. $message = '';
  551. if($type=='注册'){
  552. $message = "您的注册验证码为" . $model->code."。";
  553. }elseif($type == '找回密码'){
  554. $message = "您的找回密码验证码为" . $model->code."。";
  555. }elseif($type == '绑定手机号码'){
  556. $message = "您的绑定手机号码验证码为" . $model->code."。";
  557. }
  558. $sms = new Sms();
  559. $result =$sms->SendMessage($model->tel,$message);
  560. if($result) {
  561. return true;
  562. }else{
  563. return false;
  564. }
  565. }
  566. public function actionQuerybuilding()
  567. {
  568. if(Yii::$app->request->isAjax)
  569. {
  570. $page =Yii::$app->request->post('page');
  571. $page = empty($page) ? 1 :$page;
  572. $result=['error'=>0,'msg'=>'ok'];
  573. $post = Yii::$app->request->post();
  574. $lat = $post['latitude'];
  575. $long = $post['longitude'];
  576. $distance = 10000;
  577. $datas = [];
  578. $count = 0;
  579. $city = '';
  580. if(!empty($post['city'])){
  581. $city = $post['city'];
  582. }
  583. if (empty($lat) || empty($long)) {
  584. $result['error']=1;
  585. } else {
  586. $array_data = ['name'=>'localtion','expire'=>time()+3600,'value'=>['lat'=>$lat,'lng'=>$long]];
  587. Yii::$app->response->cookies->add(new Cookie($array_data));
  588. Yii::$app->response->send();//代码有exit,die,需要send
  589. $geohash = new ZM_Geohash();
  590. $geohash->init_codingMap();
  591. $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();
  592. $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();
  593. $result['count']=$count;
  594. //$code = $geohash->encode($lat, $long);
  595. //$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();
  596. $result['msg']=$datas;
  597. if(!empty($datas))
  598. {
  599. foreach ($datas as $key=>$val)
  600. {
  601. $redData = RedActivity::findBySql("SELECT * FROM {{%red_activity}} WHERE area = :area AND valid_at > :valid_at",[':area'=>$val['district'],':valid_at'=>time()])->asArray()->one();
  602. if(!empty($redData))
  603. {
  604. $results = RedActivity::isRed($redData['id'],$val['id'],Yii::$app->user->id);
  605. if($results['error']==0 || $results['error']==2)
  606. {
  607. $datas[$key]['type'] = 3;
  608. }
  609. } else{
  610. $datas[$key]['error']=$val['city'];
  611. }
  612. }
  613. }
  614. //$result['count']=$count;
  615. $result['msg']=$datas;
  616. }
  617. exit(json_encode($result));
  618. }
  619. }
  620. //获取红包
  621. public function actionGetred()
  622. {
  623. $result=array('error'=>1,'msg'=>'');
  624. if(Yii::$app->request->isAjax)
  625. {
  626. $id = Yii::$app->request->post('building_id');
  627. //1,获得工地信息
  628. $buildata = Building::find()->where(['id'=>$id])->select(['city'])->one();
  629. if($buildata){
  630. $redData = RedActivity::findBySql("SELECT id FROM {{%red_activity}} WHERE city = :city AND valid_at > :valid_at",[':city'=>$buildata->city,':valid_at'=>time()])->asArray()->one();
  631. if($redData)
  632. {
  633. $results = RedActivity::isRed($redData['id'],$id,Yii::$app->user->id);
  634. if($results['error']==0)//红包记录
  635. {
  636. $resultlog = $this->insertRedLog($id,$redData['id']);
  637. if($resultlog['error']==0)
  638. {
  639. $result['error']=0;
  640. $result['amount'] =$resultlog['msg'];
  641. }else{
  642. $result['error']=1;
  643. }
  644. }elseif ($results['error']==2)
  645. {
  646. $result['error']=0;
  647. $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();
  648. if($actData['status']==ActivityReceive::STATUS_NOT)
  649. {
  650. $result['error']=0;
  651. $result['amount'] =$actData['amount'];
  652. }
  653. }
  654. }
  655. }
  656. }
  657. exit(json_encode($result));
  658. }
  659. //记录红包
  660. private function insertRedLog($building,$action_id)
  661. {
  662. $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();
  663. if($redData)
  664. {
  665. $model = new ActivityReceive();
  666. $model->uid = Yii::$app->user->id;
  667. $model->activity_id = $action_id;
  668. $model->source_id = $building;
  669. $model->receive_at =time();
  670. $model->amount =$this->randomFloat($redData['start_price'],$redData['end_price']);
  671. $model->valid_at = $redData['valid_at'];
  672. if($model->save())
  673. {
  674. $ruleModel = RedRule::findOne($redData['rule_id']);
  675. $ruleModel->usecount=$ruleModel->usecount+1;
  676. $ruleModel->save();
  677. return ['error'=>0,'msg'=>$model->amount];
  678. }else{
  679. return ['error'=>1];
  680. }
  681. }
  682. return ['error'=>1];
  683. }
  684. private function randomFloat($min = 0, $max = 10)
  685. {
  686. $num = $min + mt_rand() / mt_getrandmax() * ($max - $min);
  687. return sprintf("%.2f", $num);
  688. }
  689. //弹窗提示
  690. function admin_alert($alert,$href=''){
  691. if(empty($href)){
  692. exit("<script>alert('{$alert}');history.back();</script>");
  693. }else{
  694. exit("<script>alert('{$alert}');window.location.href='{$href}';</script>");
  695. }
  696. }
  697. }