BookForm.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\food\models\client;
  8. use app\models\Option;
  9. use app\models\Store;
  10. use app\plugins\food\models\FoodBook;
  11. use app\utils\Notice\NoticeSend;
  12. use yii\base\Model;
  13. class BookForm extends Model
  14. {
  15. public $store_id;
  16. public $user_id;
  17. public $start_time;
  18. public $end_time;
  19. public $person;
  20. public $name;
  21. public $mobile;
  22. public $remark;
  23. public $code;
  24. public static $key = '_food_book_';
  25. /**
  26. * @inheritdoc
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['start_time', 'end_time', 'person', 'name', 'mobile', 'code'], 'required'],
  32. [['store_id', 'user_id', 'person', 'code'], 'integer'],
  33. [['name', 'mobile', 'remark', 'start_time', 'end_time'], 'string'],
  34. [['remark'], 'string', 'max' => 255],
  35. ];
  36. }
  37. public function submit() {
  38. if (!$this->validate()) {
  39. return [
  40. 'code' => 1,
  41. 'msg' => $this->getErrorSummary(false)[0]
  42. ];
  43. }
  44. if (!$this->verifySmsCode()) {
  45. return [
  46. 'code' => 1,
  47. 'msg' => '验证码错误'
  48. ];
  49. }
  50. $book = FoodBook::findOne(['store_id' => $this->store_id, 'user_id' => $this->user_id,
  51. 'start_time' => strtotime($this->start_time), 'end_time' => strtotime($this->end_time), 'status' => FoodBook::BOOK_WAIT]);
  52. if ($book) {
  53. return [
  54. 'code' => 1,
  55. 'msg' => '已存在预约信息'
  56. ];
  57. }
  58. // 商户信息
  59. $store_id = $this->store_id;
  60. $store_info = Store::findOne(['id' => $store_id, 'is_delete' => 0]);
  61. $food_config = Option::getFoodBookConfig();
  62. $store = [
  63. 'name' => $food_config['name'],
  64. 'address' => $food_config['address'],
  65. 'phone' => $food_config['phone'],
  66. 'logo' => $food_config['logo'],
  67. 'open_time' => $food_config['open_time'],
  68. 'latitude' => explode(',', $store_info['coordinate'])[0] ?: '',
  69. 'longitude' => explode(',', $store_info['coordinate'])[1] ?: ''
  70. ];
  71. $book = new FoodBook();
  72. $book->store_id = $this->store_id;
  73. $book->user_id = $this->user_id;
  74. $book->number = self::createNumber();
  75. $book->person = intval($this->person);
  76. $book->start_time = strtotime($this->start_time);
  77. $book->end_time = strtotime($this->end_time);
  78. $book->name = trim($this->name);
  79. $book->mobile = trim($this->mobile);
  80. $book->remark = !empty(trim($this->remark)) ? trim($this->remark) : '';
  81. $book->status = FoodBook::BOOK_WAIT;
  82. $book->created_at = $book->updated_at = time();
  83. if ($book->save()) {
  84. return [
  85. 'code' => 0,
  86. 'msg' => 'success',
  87. 'data' => [
  88. 'id' => $book->id,
  89. 'store' => $store,
  90. 'order_code' => self::bookNumber($book->start_time, true),
  91. 'order_time' => date('Y-m-d H:i', $book->start_time) . '-' . date('H:i', $book->end_time)
  92. ]
  93. ];
  94. } else {
  95. return [
  96. 'code' => 1,
  97. 'msg' => $book->errors[0]
  98. ];
  99. }
  100. }
  101. /**
  102. * 生成或获取预约号
  103. * @param integer $time
  104. * @param boolean $is_create
  105. * @param integer $book_id
  106. * @return string
  107. */
  108. public static function bookNumber($time, $is_create = false, $book_id = null) {
  109. $store_id = get_store_id();
  110. $user_id = get_user_id();
  111. $where = [
  112. 'store_id' => $store_id
  113. ];
  114. if (!$is_create && $user_id > 0) {
  115. $where['user_id'] = $user_id;
  116. } else {
  117. $where['id'] = $book_id;
  118. }
  119. $str = date('m', $time) . '-' . date('d', $time);
  120. $book = FoodBook::find()->where($where)->orderBy('id desc')->limit(1)->one();
  121. return $str . '-' .str_pad($book->number, 4, "0", STR_PAD_LEFT);
  122. }
  123. /**
  124. * 验证验证码
  125. * @return boolean
  126. */
  127. private function verifySmsCode()
  128. {
  129. $smsCode = cache()->get($this->mobile . self::$key . $this->store_id);
  130. if (!$smsCode) {
  131. return false;
  132. }
  133. if (strval($this->code) !== strval($smsCode)) {
  134. return false;
  135. }
  136. cache()->delete($this->mobile . self::$key . $this->store_id);
  137. return true;
  138. }
  139. /**
  140. * 发送验证码
  141. * @return array
  142. */
  143. public function sendCode()
  144. {
  145. if (!$this->mobile || !preg_match("/^1[3456789]\d{9}$/", $this->mobile)) {
  146. return [
  147. 'code' => 1,
  148. 'msg' => '参数不正确',
  149. ];
  150. }
  151. $sms_code = mt_rand(100000, 999999);
  152. $sendResult = NoticeSend::VerifyCode($this->mobile, $sms_code);
  153. if ($sendResult['code'] == 1) {
  154. return $sendResult;
  155. }
  156. \Yii::error([$this->mobile . self::$key . $this->store_id, $sms_code]);
  157. // 验证码有效期5分钟
  158. cache()->set($this->mobile . self::$key . $this->store_id, $sms_code, 600);
  159. return [
  160. 'code' => 0,
  161. 'msg' => '发送成功',
  162. ];
  163. }
  164. /**
  165. * 生成number
  166. * @return integer
  167. */
  168. public static function createNumber() {
  169. $store_id = get_store_id();
  170. $book = FoodBook::find()->where(['store_id' => $store_id])->orderBy('id desc')->limit(1)->asArray()->one();
  171. if (!$book) {
  172. return 1;
  173. }
  174. return $book['number'] + 1;
  175. }
  176. }