| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace addons\Community\common\helper;
- use addons\Community\common\enums\CommunityEnum;
- use addons\Community\common\enums\ConfigEnum;
- use Yii;
- use yii\helpers\Json;
- class CommunityHelper
- {
- /**
- * 获取预计提货时间
- * @param int $mall_id
- * @param int $order_pay_at
- * @return false|string
- */
- public static function getPickUpAt(int $mall_id, int $order_pay_at)
- {
- $config = Yii::$app->services->setting->addons->get($mall_id, CommunityEnum::ADDONS_NAME, 'basic');
- $day = intval($config['can_receive_day']??0);
- $order_pay_at += 86400 * $day; // + N天
- $pick_up_type = $config['pick_up_time_range_type']; // 取货类型
- $receive_at = "";
- switch ($pick_up_type) {
- case ConfigEnum::PICK_UP_TIME_RANGE_TYPE_CUSTOM:
- $weeks = Json::decode($config['pick_up_time_range_custom']); // 取货类型
- // 格式化星期几
- if (count($weeks) < 7) {
- $weeks = array_combine($weeks, $weeks);
- for ($i = 0; $i < 7; $i++) {
- if(!isset($weeks[$i])) {
- $weeks[$i] = -1;
- }
- }
- }
- $at_times = Json::decode($config['pick_up_time_range_custom_at']); // 取货类型
- ksort($weeks);
- // 如果有当前未指定,则需要顺延
- $week = date('w', $order_pay_at);
- if (in_array($week, $weeks)) { // 当天可以去自提
- $receive_at = date('Y-m-d', $order_pay_at);
- } else { // 当天不能去自提,那么就需要顺延
- // 判断今天能不能提货,能的话就不需要顺延
- $t_day = strtotime(date('Y-m-d'));
- $t_week = date('w', $t_day);
- if ($t_day > $order_pay_at && in_array($t_week, $weeks)) {
- $receive_at = date('Y-m-d');
- } else {
- // 顺延
- $tmp_week = -1;
- foreach ($weeks as $weekItem) {
- if ($weekItem > $week) {
- // 顺延成功
- $tmp_week = $weekItem;
- break;
- }
- }
- if ($tmp_week > -1) { // 顺延成功
- $order_pay_at += ($tmp_week - $week) * 86400; // 加天数
- } else {
- // 有可能是6 有可能是 7?
- // 什么情况下是6/什么情况下是7 7 - 6 + 3
- $order_pay_at += ((7 - $week) + $tmp_week) * 86400;
- }
- $receive_at = date('Y-m-d', $order_pay_at);
- }
- }
- // 时间戳
- $receive_ts = strtotime($receive_at);
- // 相差天数
- $diff_day = ceil($receive_ts - strtotime(date('Y-m-d'))) / 86400;
- if ($diff_day < 3) {
- switch ($diff_day) {
- case 0: // 今天
- $receive_at = sprintf("今天 %s - %s", $at_times[0], $at_times[1]);
- break;
- case 1: // 明天
- $receive_at = sprintf("明天 %s - %s", $at_times[0], $at_times[1]);
- break;
- case 2: // 后天
- $receive_at = sprintf("后天 %s - %s", $at_times[0], $at_times[1]);
- break;
- }
- } else {
- // 如果小于当前时间,那么就需要顺延?
- $receive_at = sprintf("%s %s - %s", date('m-d', $receive_ts), $at_times[0], $at_times[1]);
- }
- break;
- case ConfigEnum::PICK_UP_TIME_RANGE_TYPE_ALL_DAY:
- // 全天
- switch ($day) {
- case 0:
- $day_txt = "今天";
- break;
- case 1:
- $day_txt = "明天";
- break;
- case 2:
- $day_txt = "后天";
- break;
- default:
- $day_txt = date("n月d号", strtotime("+$day days"));
- break;
- }
- $receive_at = "{$day_txt}[全天]";
- break;
- }
- return $receive_at;
- }
- }
|