uid = intval(Yii::$app->user->id); $this->mobile = $mobile; $this->verifycode = Utils::random(4, true); $this->ip = Utils::getIp(); $this->status = 0; $this->scenario = $scenario; if (!$this->validate()) { return current($this->getFirstErrors()); } $result = $this->save(); if ($result !== true) { return '短信发送失败: 生成记录失败,请重试!'; } // $content = '【'.Config::getConfig('MESSAGE_API_USERNAME').'】您的验证码是' . $this->verifycode . ',5分钟内有效,若非本人操作请忽略此消息'; $content = $this->verifycode ; return Sms::send($this->mobile, $content); } /** * @param $mobile * @param string $verifycode * @return bool|string */ public function check($mobile, $verifycode = '') { $result = self::find()->where(['mobile' => $mobile, 'status' => 0])->orderBy('id DESC')->one(); if (empty($result)) { return '短信验证码错误,请重新输入或获取[1]!'; } if (empty($result->ip) || $result->ip !== Utils::getIp()) { return '短信验证码错误,请重新输入或获取[2]!'; } if (empty($result->verifycode) || $result->verifycode !== $verifycode) { return '短信验证码错误,请重新输入或获取!'; } if (empty($result->created_at) || $result->created_at < time() - 300) { return '短信验证码过期,请重新获取!'; } $result->status = 1; $result->save(false); return true; } /** * @inheritdoc */ public function rules() { return [ [['mobile', 'verifycode'], 'required'], [['mobile'], 'match', 'pattern' => '/^1[0-9]{10}$/', 'message' => '手机号必须为1开头的11位纯数字!'], // ['mobile', 'unique', 'targetClass' => '\common\models\User', 'message' => '手机号已注册,请登录!', 'on' => 'register'], // ['mobile', 'required', 'on' => 'register'], ['mobile', 'exist', 'targetClass' => '\common\models\User', 'message' => '手机号未注册,请先注册!', 'on' => 'reset'], ['mobile', 'exist', 'targetClass' => '\common\models\User', 'message' => '手机号未注册,请先注册!', 'on' => ''], ]; } /** * @inheritdoc */ public function scenarios() { $scenarios = parent::scenarios(); $scenarios['register'] = ['mobile', 'verifycode']; $scenarios['reset'] = ['mobile', 'verifycode']; return $scenarios; } /** * @inheritdoc */ public function attributeLabels() { return [ 'uid' => '会员编号', 'mobile' => '手机号', 'verifycode' => '验证码', 'status' => '状态', 'created_at' => '创建时间', 'update_at' => '更新时间', ]; } /** * @inheritdoc */ public function beforeSave($insert) { if (!parent::beforeSave($insert)) { return false; } $this->uid = intval(Yii::$app->user->id); return true; } /** * @inheritdoc */ public function behaviors() { return [ [ 'class' => TimestampBehavior::className(), 'createdAtAttribute' => 'created_at', 'updatedAtAttribute' => 'updated_at', ], ]; } }