CommunityHelper.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace addons\Community\common\helper;
  3. use addons\Community\common\enums\CommunityEnum;
  4. use addons\Community\common\enums\ConfigEnum;
  5. use Yii;
  6. use yii\helpers\Json;
  7. class CommunityHelper
  8. {
  9. /**
  10. * 获取预计提货时间
  11. * @param int $mall_id
  12. * @param int $order_pay_at
  13. * @return false|string
  14. */
  15. public static function getPickUpAt(int $mall_id, int $order_pay_at)
  16. {
  17. $config = Yii::$app->services->setting->addons->get($mall_id, CommunityEnum::ADDONS_NAME, 'basic');
  18. $day = intval($config['can_receive_day']??0);
  19. $order_pay_at += 86400 * $day; // + N天
  20. $pick_up_type = $config['pick_up_time_range_type']; // 取货类型
  21. $receive_at = "";
  22. switch ($pick_up_type) {
  23. case ConfigEnum::PICK_UP_TIME_RANGE_TYPE_CUSTOM:
  24. $weeks = Json::decode($config['pick_up_time_range_custom']); // 取货类型
  25. // 格式化星期几
  26. if (count($weeks) < 7) {
  27. $weeks = array_combine($weeks, $weeks);
  28. for ($i = 0; $i < 7; $i++) {
  29. if(!isset($weeks[$i])) {
  30. $weeks[$i] = -1;
  31. }
  32. }
  33. }
  34. $at_times = Json::decode($config['pick_up_time_range_custom_at']); // 取货类型
  35. ksort($weeks);
  36. // 如果有当前未指定,则需要顺延
  37. $week = date('w', $order_pay_at);
  38. if (in_array($week, $weeks)) { // 当天可以去自提
  39. $receive_at = date('Y-m-d', $order_pay_at);
  40. } else { // 当天不能去自提,那么就需要顺延
  41. // 判断今天能不能提货,能的话就不需要顺延
  42. $t_day = strtotime(date('Y-m-d'));
  43. $t_week = date('w', $t_day);
  44. if ($t_day > $order_pay_at && in_array($t_week, $weeks)) {
  45. $receive_at = date('Y-m-d');
  46. } else {
  47. // 顺延
  48. $tmp_week = -1;
  49. foreach ($weeks as $weekItem) {
  50. if ($weekItem > $week) {
  51. // 顺延成功
  52. $tmp_week = $weekItem;
  53. break;
  54. }
  55. }
  56. if ($tmp_week > -1) { // 顺延成功
  57. $order_pay_at += ($tmp_week - $week) * 86400; // 加天数
  58. } else {
  59. // 有可能是6 有可能是 7?
  60. // 什么情况下是6/什么情况下是7 7 - 6 + 3
  61. $order_pay_at += ((7 - $week) + $tmp_week) * 86400;
  62. }
  63. $receive_at = date('Y-m-d', $order_pay_at);
  64. }
  65. }
  66. // 时间戳
  67. $receive_ts = strtotime($receive_at);
  68. // 相差天数
  69. $diff_day = ceil($receive_ts - strtotime(date('Y-m-d'))) / 86400;
  70. if ($diff_day < 3) {
  71. switch ($diff_day) {
  72. case 0: // 今天
  73. $receive_at = sprintf("今天 %s - %s", $at_times[0], $at_times[1]);
  74. break;
  75. case 1: // 明天
  76. $receive_at = sprintf("明天 %s - %s", $at_times[0], $at_times[1]);
  77. break;
  78. case 2: // 后天
  79. $receive_at = sprintf("后天 %s - %s", $at_times[0], $at_times[1]);
  80. break;
  81. }
  82. } else {
  83. // 如果小于当前时间,那么就需要顺延?
  84. $receive_at = sprintf("%s %s - %s", date('m-d', $receive_ts), $at_times[0], $at_times[1]);
  85. }
  86. break;
  87. case ConfigEnum::PICK_UP_TIME_RANGE_TYPE_ALL_DAY:
  88. // 全天
  89. switch ($day) {
  90. case 0:
  91. $day_txt = "今天";
  92. break;
  93. case 1:
  94. $day_txt = "明天";
  95. break;
  96. case 2:
  97. $day_txt = "后天";
  98. break;
  99. default:
  100. $day_txt = date("n月d号", strtotime("+$day days"));
  101. break;
  102. }
  103. $receive_at = "{$day_txt}[全天]";
  104. break;
  105. }
  106. return $receive_at;
  107. }
  108. }