255], ]; } public function submit() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } if (!$this->verifySmsCode()) { return [ 'code' => 1, 'msg' => '验证码错误' ]; } $book = FoodBook::findOne(['store_id' => $this->store_id, 'user_id' => $this->user_id, 'start_time' => strtotime($this->start_time), 'end_time' => strtotime($this->end_time), 'status' => FoodBook::BOOK_WAIT]); if ($book) { return [ 'code' => 1, 'msg' => '已存在预约信息' ]; } // 商户信息 $store_id = $this->store_id; $store_info = Store::findOne(['id' => $store_id, 'is_delete' => 0]); $food_config = Option::getFoodBookConfig(); $store = [ 'name' => $food_config['name'], 'address' => $food_config['address'], 'phone' => $food_config['phone'], 'logo' => $food_config['logo'], 'open_time' => $food_config['open_time'], 'latitude' => explode(',', $store_info['coordinate'])[0] ?: '', 'longitude' => explode(',', $store_info['coordinate'])[1] ?: '' ]; $book = new FoodBook(); $book->store_id = $this->store_id; $book->user_id = $this->user_id; $book->number = self::createNumber(); $book->person = intval($this->person); $book->start_time = strtotime($this->start_time); $book->end_time = strtotime($this->end_time); $book->name = trim($this->name); $book->mobile = trim($this->mobile); $book->remark = !empty(trim($this->remark)) ? trim($this->remark) : ''; $book->status = FoodBook::BOOK_WAIT; $book->created_at = $book->updated_at = time(); if ($book->save()) { return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'id' => $book->id, 'store' => $store, 'order_code' => self::bookNumber($book->start_time, true), 'order_time' => date('Y-m-d H:i', $book->start_time) . '-' . date('H:i', $book->end_time) ] ]; } else { return [ 'code' => 1, 'msg' => $book->errors[0] ]; } } /** * 生成或获取预约号 * @param integer $time * @param boolean $is_create * @param integer $book_id * @return string */ public static function bookNumber($time, $is_create = false, $book_id = null) { $store_id = get_store_id(); $user_id = get_user_id(); $where = [ 'store_id' => $store_id ]; if (!$is_create && $user_id > 0) { $where['user_id'] = $user_id; } else { $where['id'] = $book_id; } $str = date('m', $time) . '-' . date('d', $time); $book = FoodBook::find()->where($where)->orderBy('id desc')->limit(1)->one(); return $str . '-' .str_pad($book->number, 4, "0", STR_PAD_LEFT); } /** * 验证验证码 * @return boolean */ private function verifySmsCode() { $smsCode = cache()->get($this->mobile . self::$key . $this->store_id); if (!$smsCode) { return false; } if (strval($this->code) !== strval($smsCode)) { return false; } cache()->delete($this->mobile . self::$key . $this->store_id); return true; } /** * 发送验证码 * @return array */ public function sendCode() { if (!$this->mobile || !preg_match("/^1[3456789]\d{9}$/", $this->mobile)) { return [ 'code' => 1, 'msg' => '参数不正确', ]; } $sms_code = mt_rand(100000, 999999); $sendResult = NoticeSend::VerifyCode($this->mobile, $sms_code); if ($sendResult['code'] == 1) { return $sendResult; } \Yii::error([$this->mobile . self::$key . $this->store_id, $sms_code]); // 验证码有效期5分钟 cache()->set($this->mobile . self::$key . $this->store_id, $sms_code, 600); return [ 'code' => 0, 'msg' => '发送成功', ]; } /** * 生成number * @return integer */ public static function createNumber() { $store_id = get_store_id(); $book = FoodBook::find()->where(['store_id' => $store_id])->orderBy('id desc')->limit(1)->asArray()->one(); if (!$book) { return 1; } return $book['number'] + 1; } }