| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace frontend\controllers;
- use Yii;
- use frontend\models\LottoSms;
- use common\helpers\Utils;
- use backend\helpers\DateTimeHelper;
- use backend\helpers\RandomHelper;
- class LottoSmsController extends BaseController
- {
- /**
- * 抽奖短信记录
- */
- public function actionList()
- {
- $post = Yii::$app->request->post();
- $order = isset($post['order']) ? strtolower($post['order']) : '';
- $orderBy = isset($post['orderBy']) ? strtolower($post['orderBy']) : 'desc';
- $search = isset($post['search']) ? $post['search'] : '';
- $start = isset($post['start']) ? (int) $post['start'] : 0;
- $length = isset($post['length']) ? (int) $post['length'] : 20;
- $draw = isset($post['draw']) ? $post['draw'] : 1;
- // 搜索
- $where = ['and'];
- if ($search) {
- $where[] = ['like', 'mobile', $search];
- }
- // 排序
- $allowOrderColumn = ['id', 'mobile', 'in_time', 'code', 'code_time', 'lottery'];
- if (in_array($order, $allowOrderColumn) && in_array($orderBy, ['asc', 'desc'])) {
- if ($orderBy == 'asc') {
- $orderCondition = [$order => SORT_ASC];
- } else {
- $orderCondition = [$order => SORT_DESC];
- }
- } else {
- $orderCondition = ['id' => SORT_DESC];
- }
- $query = LottoSms::find();
- $query->where($where);
- $count = $query->count();
-
- $list = [];
- if ($count) {
- $list = $query->orderBy($orderCondition)->offset($start)->limit($length)->asArray()->all();
- }
- $data['data'] = $list;
- $data['draw'] = $draw;
- $data['recordsFiltered'] = $count;
- $data['recordsTotal'] = $count;
- return $this->outJson(1, $data);
- }
-
- /**
- * 删除抽奖短信记录
- */
- public function actionDelete()
- {
- $id = (int) Yii::$app->request->post('id');
- $model = LottoSms::find()->where(['id' => $id])->limit(1)->one();
- if (!$model) {
- return $this->outJson(0, [], '参数错误');
- }
- $result = $model->delete();
- if ($result) {
- return $this->outJson(1);
- } else {
- return $this->outJson(0, [], '删除失败');
- }
- }
-
- /**
- * 发送短信
- */
- public function actionSendSmsCode()
- {
- $mobile = Yii::$app->request->post('mobile');
- if (!Utils::checkPhoneNumber($mobile)) {
- return $this->outJson(0, [], '手机号码格式错误');
- }
-
- /** @var LottoSms $model */
- $model = LottoSms::find()->where(['mobile' => $mobile])->limit(1)->one();
- if ($model && $model->code) {
- return $this->outJson(0, [], '您已抽奖,请勿重复提交');
- }
- $code = RandomHelper::getRandomNo(4);
- $sendResult = LottoSms::sendSmsCode($mobile, $code);
- if ($sendResult['code'] != 1) {
- return $this->outJson(0, [], '发送短信失败: ' . $sendResult['message']);
- }
-
- if (!$model) {
- $model = new LottoSms();
- $model->mobile = $mobile;
- $model->in_time = DateTimeHelper::microtime_float();
- $model->code = $code;
- $model->code_time = DateTimeHelper::microtime_float();
- $model->save();
- } else {
- $model->code = $code;
- $model->code_time = DateTimeHelper::microtime_float();
- $model->save();
- }
-
- return $this->outJson(1, [], '短信发送成功');
- }
-
- /**
- * 验证短信验证码是否正确
- */
- public function actionVerifySmsCode()
- {
- $mobile = Yii::$app->request->post('mobile');
- $code = Yii::$app->request->post('code');
- /** @var LottoSms $model */
- $model = LottoSms::find()->where(['mobile' => $mobile])->limit(1)->one();
- if (!$model || !$model->code) {
- return $this->outJson(0, [], '请先获取短信验证码');
- }
-
- if ($code != $model->code) {
- return $this->outJson(0, [], '短信验证码错误');
- }
-
- if ($model->lottery) {
- return $this->outJson(0, [], '您已抽奖,请勿重复提交');
- }
- return $this->outJson(1, [], '验证通过');
- }
- /**
- * 保存中间记录
- */
- public function actionSaveLottery()
- {
- $mobile = Yii::$app->request->post('mobile');
- $lottery = Yii::$app->request->post('lottery');
- /** @var LottoSms $model */
- $model = LottoSms::find()->where(['mobile' => $mobile])->limit(1)->one();
- if (!$model) {
- return $this->outJson(0, [], '请先获取验证码抽奖');
- }
-
- $model->lottery = $lottery;
- $model->save();
- return $this->outJson(1, [], '请求成功');
- }
- }
|