/** * 展示预约页面 * 选择美容师与预约时间 * 此处要验证授权 * * @param $shopId 店铺ID */ public function index($shopId) { $weixinUtil = new WeixinUtil(); // 验证是否已授权 $weixinUtil->authorize("appointment/index/{$shopId}"); // 获得预约项目 $projectId = (new CartUtil())->cart(); $projectId += 0; if (!$projectId) { $this->message('预约项目不存在!'); } $openId = $weixinUtil->getOpenId(); if ((new ProjectPropertyModel())->projectOnlyForNewUser($projectId, $openId)) { $this->message('此美容项目只针对新用户!'); } //是否已经选择了店铺,并且店铺是有效的 $shops = (new ShopModel())->getAllShops(); if (is_numeric($shopId) && array_key_exists($shopId, $shops)) { // 获得项目信息 $project = (new ProjectModel())->readOne($projectId); if (!$project) { $this->message('预约项目不存在!'); } // 跳转到 选择 美容师 $beauticians = (new BeauticianModel())->getAllBeauticians(); $lastOrder = (new OrderModel())->getLastOrder($openId); $days = DateUtil::buildDays(); $coupons = array(); if ($project['can_use_coupon']) { // 查询优惠券 $coupons = (new CustomerCouponModel())->getCustomerNotUseCouponList($openId); } $this->load->view('frontend/appointment/beautician', array('beauticians' => $beauticians, 'project' => $project, 'shopId' => $shopId, 'days' => $days, 'lastOrder' => $lastOrder, 'coupons' => $coupons)); } else { // 跳转到选择店铺 $returnUrl = urlencode(UrlUtil::createUrl('appointment/index')); ResponseUtil::redirect(UrlUtil::createUrl("shop/index?returnUrl={$returnUrl}")); } }
public function __construct() { parent::__construct(); if (!UserUtil::getUserId()) { ResponseUtil::redirect(UrlUtil::createBackendUrl('login')); } $controller = strtolower($this->router->class); $method = strtolower($this->router->method); if (UserUtil::isShopKeeper()) { if (!array_key_exists($controller, $this->shopKeeperPermissions)) { $this->message('你没有权限执行本步骤!'); } $methods = $this->shopKeeperPermissions[$controller]; if ($methods == '*') { return true; } else { if (!in_array($method, $methods)) { $this->message('你没有权限执行本步骤!'); } else { return true; } } } }
/** * 授权 * @param $returnUrl * @return bool */ public function authorize($returnUrl) { if (!$returnUrl) { get_instance()->message('授权回调地址为空!'); } // 如果是微信授权后返回 if (isset($_GET['code'])) { // 获得accessToken $callback = $this->loginCallback($_GET['code']); if (!$callback) { get_instance()->message('获得微信授权失败,请重试!'); } } // 检测是否已经授权 $openId = $this->getOpenId(); if ($openId) { // 刷新token过期 if ($this->isNeedRefreshAccessToken()) { if (!$this->refreshAccessToken()) { ResponseUtil::redirect($this->toAuthorize(UrlUtil::createUrl($returnUrl))); } } } else { // 去微信授权 ResponseUtil::redirect($this->toAuthorize(UrlUtil::createUrl($returnUrl))); } return true; }
/** * 退出 */ public function logout() { session_destroy(); ResponseUtil::redirect(UrlUtil::createBackendUrl('login')); }
/** * 下单 * @param $shopId * @param $beauticianId * @param $appointmentDay * @param $appointmentTime * @param $userName * @param $phoneNumber */ public function order($shopId, $beauticianId, $appointmentDay, $appointmentTime, $userName, $phoneNumber) { $openId = (new WeixinUtil())->getOpenId(); if (!$openId) { $this->message('错误的授权'); } if (!(new ShopModel())->isValidShopId($shopId)) { $this->message('门店信息错误,请检查!'); } // 检查美容师 if (!(new BeauticianModel())->isValidBeautician($beauticianId)) { $this->message('美容师信息错误,请检查!'); } $userName = urldecode($userName); // 检查用户 $userName = trim(strip_tags($userName)); if (empty($userName)) { $this->message('联系人不能为空,请检查!'); } if (!preg_match('~^1\\d{10}$~', $phoneNumber)) { $this->message('手机号错误,请检查!'); } // 检查日期,日期为今天或者以后 $today = date('Y-m-d'); if ($appointmentDay < $today) { $this->message('错误的预约日期!'); } // 检查时间 $appointmentTime = explode(',', urldecode($appointmentTime)); if (!$appointmentTime || count($appointmentTime) < 1) { $this->message('错误的预约时间!'); } // 只有30分钟的项目 if (count($appointmentTime) == 1) { array_push($appointmentTime, $appointmentTime[0]); } // 只保留头和尾的两个数据 $startTime = array_shift($appointmentTime); $endTime = array_pop($appointmentTime); if ($endTime < $startTime) { $this->message('错误的预约时间!'); } // 预约时间是否小于当前时间 $now = date('Y-m-d H:i'); if (DateUtil::buildDateTime($appointmentDay, $startTime) < $now) { $this->message('错误的预约开始时间!'); } if (DateUtil::buildDateTime($appointmentDay, $endTime) < $now) { $this->message('错误的预约结束时间!'); } // 结束时间 + 30分钟为真正的结束时间 //$timeStamp = DateUtil::buildDateTime($appointmentDay, $endTime); //$timeStamp += 1800; //$endTime = date('H:i', $timeStamp); // 处理优惠 $couponId = $this->input->get('coupon_id', true) + 0; $couponCode = $this->input->get('coupon_code', true) + 0; $customerCouponModel = new CustomerCouponModel(); $couponCodeModel = new CouponCodeModel(); $today = date('Y-m-d'); if ($couponId) { $couponCode = ''; $coupon = $customerCouponModel->readOneById($couponId); // 判断是否能使用 if ($coupon['is_use'] == 1) { $this->message('选择的优惠券已被使用!'); } // 是否到领取时间 if ($today < $coupon['start_time']) { $this->message('优惠券未到使用时间!'); } if ($today > $coupon['expire_time']) { $this->message('优惠券已到期!'); } } else { if ($couponCode) { $couponId = ''; $queryCouponCode = $couponCodeModel->readOneByCode($couponCode); if (!$queryCouponCode) { $this->message('优惠码不存在!'); } // 是否到使用时间 if ($today < $queryCouponCode['start_time']) { $this->message('优惠码未到使用时间!'); } // 是有已过期 if ($today > $queryCouponCode['expire_time']) { $this->message('优惠码已到期!'); } } else { } } //**********处理下单************// $projectId = (new CartUtil())->cart(); if (empty($projectId) || $projectId <= 0) { $this->message('预约项目为空!'); } if ((new ProjectPropertyModel())->projectOnlyForNewUser($projectId, $openId)) { $this->message('此美容项目只针对新用户!'); } $orderProjectModel = new OrderProjectModel(); // 获得购物车的项目 $project = (new ProjectModel())->readOne($projectId); // 判断订单金额是否可以使用优惠券 $totalFee = $originalTotalFee = $project['price']; if ($couponId && $project['can_use_coupon']) { if ($totalFee < $coupon['use_rule']) { $this->message('当前订单金额不足' . $coupon['use_rule'] . '元,不能使用此优惠券'); } } else { if ($couponCode && $project['can_use_coupon_code']) { if ($totalFee < $queryCouponCode['use_rule']) { $this->message('当前订单金额不足' . $queryCouponCode['use_rule'] . '元,不能使用此优惠码'); } } else { } } // 生成订单号, 有重复订单号则重新生成,直到不重复为止 $orderNo = StringUtil::generateOrderNo(); $orderModel = new OrderModel(); while ((new CurdUtil($orderModel))->readOne(array('order_no' => $orderNo))) { $orderNo = StringUtil::generateOrderNo(); } // 优惠 if ($couponId && $project['can_use_coupon']) { // 使用优惠码, 抵消金额 $totalFee -= $coupon['counteract_amount']; } else { if ($couponCode && $project['can_use_coupon_code']) { $totalFee *= $queryCouponCode['discount']; } else { } } // 订单数据 $orderData = array('order_no' => $orderNo, 'shop_id' => $shopId, 'create_time' => DateUtil::now(), 'original_total_fee' => $originalTotalFee, 'total_fee' => $totalFee, 'open_id' => $openId, 'order_status' => OrderModel::ORDER_NOT_PAY, 'beautician_id' => $beauticianId, 'appointment_day' => $appointmentDay, 'appointment_start_time' => $startTime, 'appointment_end_time' => $endTime, 'user_name' => $userName, 'phone_number' => $phoneNumber, 'use_coupon_id' => $couponId, 'use_coupon_code' => $couponCode); // 事务开始 $this->db->trans_start(); // 设置优惠券已使用 if ($couponId && $project['can_use_coupon']) { $customerCouponModel->useCoupon($couponId, $openId); } else { if ($couponCode && $project['can_use_coupon_code']) { $couponCodeModel->addUseTimes($couponCode); } else { } } $insertOrderNo = (new CurdUtil($orderModel))->create($orderData); if ($insertOrderNo) { $orderProjectData = array('order_id' => $insertOrderNo, 'project_id' => $project['project_id'], 'project_use_time' => $project['use_time'], 'project_price' => $project['price'], 'create_time' => DateUtil::now(), 'project_name' => $project['project_name'], 'project_cover' => $project['project_cover']); } else { $this->message('提交订单失败,请重试!'); } (new CurdUtil($orderProjectModel))->create($orderProjectData); $this->db->trans_complete(); if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); $this->message('提交订单失败,请重试!'); } else { $this->db->trans_commit(); // 清空购物车 (new CartUtil())->emptyCart(); // 跳到 订单显示 ResponseUtil::redirect(UrlUtil::createUrl('order/pay/' . $orderNo)); } }