BookController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\food\controllers\client;
  8. use app\models\Option;
  9. use app\models\Store;
  10. use app\modules\client\models\v1\LoginForm;
  11. use app\plugins\food\controllers\BaseController;
  12. use app\plugins\food\models\client\BookForm;
  13. use app\plugins\food\models\FoodBook;
  14. use yii\base\BaseObject;
  15. use yii\base\Controller;
  16. /**
  17. * 点餐预约
  18. * Class BookController
  19. * @package app\plugins\food\controllers\client
  20. */
  21. class BookController extends BaseController
  22. {
  23. public function actionSubmit() {
  24. $form = new BookForm();
  25. $form->attributes = post_params();
  26. $form->store_id = get_store_id();
  27. $form->user_id = get_user_id();
  28. return $form->submit();
  29. }
  30. /**
  31. * 发送验证码
  32. */
  33. public function actionSendCode()
  34. {
  35. $form = new BookForm();
  36. $form->mobile = post_params('mobile');
  37. $form->store_id = get_store_id();
  38. return $form->sendCode();
  39. }
  40. /**
  41. * 预约配置
  42. * @return array
  43. */
  44. public function actionConfig() {
  45. $store_id = get_store_id();
  46. $user_id = get_user_id();
  47. // 商户信息
  48. $store_info = Store::findOne(['id' => $store_id, 'is_delete' => 0]);
  49. $food_config = Option::getFoodBookConfig();
  50. $store = [
  51. 'name' => $food_config['name'],
  52. 'address' => $food_config['address'],
  53. 'phone' => $food_config['phone'],
  54. 'logo' => $food_config['logo'],
  55. 'open_time' => $food_config['open_time'],
  56. 'latitude' => explode(',', $store_info['coordinate'])[0] ?: '',
  57. 'longitude' => explode(',', $store_info['coordinate'])[1] ?: ''
  58. ];
  59. $book = FoodBook::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'status' => FoodBook::BOOK_WAIT])
  60. ->orderBy('id desc')->limit(1)->one();
  61. if (!empty($book)) {
  62. return [
  63. 'code' => 0,
  64. 'msg' => 'success',
  65. 'data' => [
  66. 'switch' => $food_config['switch'],
  67. 'description' => $food_config['description'],
  68. 'result' => 1,
  69. 'id' => $book->id,
  70. 'store' => $store,
  71. 'order_code' => BookForm::bookNumber($book->start_time),
  72. 'order_time' => date('Y-m-d H:i', $book->start_time) . '-' . date('H:i', $book->end_time)
  73. ]
  74. ];
  75. }
  76. // 预约配置
  77. if (empty($food_config)) {
  78. return [
  79. 'code' => 1,
  80. 'msg' => '配置信息有误'
  81. ];
  82. }
  83. if ($food_config['switch'] == 0) {
  84. return [
  85. 'code' => 1,
  86. 'msg' => '店铺未开启预约'
  87. ];
  88. }
  89. return [
  90. 'code' => 0,
  91. 'msg' => 'success',
  92. 'data' => [
  93. 'description' => $food_config['description'],
  94. 'result' => 0,
  95. 'time' => $food_config['time'],
  96. 'person' => $food_config['person']
  97. ]
  98. ];
  99. }
  100. /**
  101. * 预约取消
  102. */
  103. public function actionCancel()
  104. {
  105. $book = FoodBook::findOne(['id' => post_params('id'), ['in', 'status', [FoodBook::BOOK_WAIT, FoodBook::BOOK_CONFIRM]]]);
  106. if (!$book) {
  107. return [
  108. 'code' => 1,
  109. 'msg' => '未找到该预约记录'
  110. ];
  111. }
  112. $book->status = FoodBook::BOOK_NOT_EXIST;
  113. if ($book->save()) {
  114. return [
  115. 'code' => 0,
  116. 'msg' => '取消成功'
  117. ];
  118. } else{
  119. return [
  120. 'code' => 1,
  121. 'msg' => $book->errors[0]
  122. ];
  123. }
  124. }
  125. }