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, [], '请求成功'); } }