| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\food\models\client;
- use app\models\Option;
- use app\models\Store;
- use app\plugins\food\models\FoodBook;
- use app\utils\Notice\NoticeSend;
- use yii\base\Model;
- class BookForm extends Model
- {
- public $store_id;
- public $user_id;
- public $start_time;
- public $end_time;
- public $person;
- public $name;
- public $mobile;
- public $remark;
- public $code;
- public static $key = '_food_book_';
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['start_time', 'end_time', 'person', 'name', 'mobile', 'code'], 'required'],
- [['store_id', 'user_id', 'person', 'code'], 'integer'],
- [['name', 'mobile', 'remark', 'start_time', 'end_time'], 'string'],
- [['remark'], 'string', 'max' => 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;
- }
- }
|