Cart.php 17.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
<?php

namespace Index;

use LibModels\Wap\Home\CartData;
use Plugin\Helpers;
use Plugin\Images;

/**
 *
 * @name CartModel
 * @package models/Index
 * @copyright yoho.inc
 * @version 1.0 (2015-11-09 14:05:09)
 * @author Gtskk (tttt6399998@126.com)
 */
class CartModel
{

	/**
	 * 加入购物车
	 *
	 * @param int $productSku 商品SKU
	 * @param int $buyNumber 购买数量
	 * @param int $goodsType 商品类型,0表示普通商品,1表示加价购商品
	 * @param int int $isEdit 是否是编辑商品SKU,0表示不是编辑
	 * @param null|int $promotionId 促销id,默认null(加价购有关)
	 * @param null|int $uid 用户UID,可以不传
	 * @return array 加入购物车接口返回的数据
	 */
	public static function addToCart($productSku, $buyNumber, $goodsType, $isEdit, $promotionId, $uid)
	{
		$result = array('code' => 400, 'message' => '出错啦~~');

		$addCart = CartData::addToCart($productSku, $buyNumber, $goodsType, $isEdit, $promotionId, $uid);
		if ($addCart && isset($addCart['code'])) {
			$result = $addCart;
		}

		return $result;
	}

	/**
	 * @param integer $uid 用户ID
	 * @param string $shoppingKey 未登录用户唯一识别码
	 * @return array|mixed 处理之后的购物车数据
	 */
	public static function getCartData($uid, $shoppingKey)
	{
		$result = array('cartNav' => true);

		// 用户是否登录
		if (empty($uid)) {
			$result['showLoginInfo'] = true;
		}

		// 调用接口获取购物车的数据
		$cartData = CartData::cartData($uid, $shoppingKey);

		// 处理普通购物车和预售购物车的数据
		if (isset($cartData['data']) && !empty($cartData['data'])) {
			$cart = $cartData['data'];

			/* 普通购物车 */
			if(isset($cart['ordinary_cart_data'])) {
				$result['commonGoodsCount'] = count($cart['ordinary_cart_data']['goods_list']);
				$result['commonCart'] = self::procCartData($cart['ordinary_cart_data']);
			}

			/* 预售购物车 */
			if(isset($cart['advance_cart_data'])) {
				$result['presellGoodsCount'] = count($cart['advance_cart_data']['goods_list']);
				$result['preSellCart'] = self::procCartData($cart['advance_cart_data']);
			}

		}

		return $result;
	}

	/**
	 * 移出购物车
	 *
	 * @param int $uid 用户ID
	 * @param string $sku 商品sku列表
	 * @param string $shoppingKey 未登录用户唯一识别码
	 * @return array 接口返回的数据
	 */
	public static function removeFromCart($uid, $sku, $shoppingKey)
	{
		$result = array('code' => 400, 'message' => '出错啦~');

		// 处理sku
		$sku_list = json_encode(array($sku => 1));

		$remove = CartData::removeFromCart($uid, $sku_list, $shoppingKey);
		if ($remove && isset($remove['code'])) {
			$result['code'] = $remove['code'];
			$result['message'] = $remove['message'];
		}

		return $result;
	}

	/**
	 * 移入收藏夹
	 *
	 * @param int $uid 用户ID
	 * @param string $sku 商品sku列表
	 * @return array 接口返回的数据
	 */
	public static function addToFav($uid, $sku)
	{
		$result = array('code' => 400, 'message' => '出错啦~');

		if (empty($uid)) {
			$result['code'] = 300;
			$reult['message'] = '请先登录';
			$result['data'] = '/signin.html';

			return $result;
		}

		// 处理sku
		$sku_list = json_encode(array($sku => 1));

		$add = CartData::addToFav($uid, $sku_list);
		if ($add && isset($add['code'])) {
			$result['code'] = $add['code'];
			$result['message'] = $add['message'];
		}

		return $result;
	}

	/**
	 * 处理购物车商品数据
	 *
	 * @param int $uid 用户ID
	 * @param int $skn 商品skn
	 * @param int $num 购买数目
	 * @return array 接口返回的数据
	 */
	public static function cartProductData($uid, $skn, $num)
	{
		$result = array('code' => 400, 'message' => '出错啦~');

		$product = CartData::cartProductData($uid, $skn);
		if (isset($product['code']) && $product['code'] === 200) {
			$result['code'] = 200;
			$data = array();
			$productData = $product['data'];

			// 品牌信息
			if (isset($productData['brand_info']) && !empty($productData['brand_info'])) {
				$data['thumb'] = Helpers::getImageUrl($productData['brand_info']['brand_ico'], 120, 120);
			}

			$data['name'] = $productData['product_name'];
			$data['price'] = $productData['market_price'];
			$data['salePrice'] = $productData['sales_price'];
			$data['storage'] = $productData['storage_sum'];
			$data['num'] = $num;

			// 商品选择
			if (isset($productData['goods_list'])) {
				$goodsList = $productData['goods_list'];

				$colors = array();
				$oneColor = array();
				$sizes = array();
				$oneSize = array();
				foreach ($goodsList as $val) {
					// 颜色
					$oneColor = array();
					$oneColor['id'] = $val['color_id'];
					$oneColor['name'] = $val['color_name'];

					// 尺码
					foreach ($val['size_list'] as $one) {
						$oneSize = array();
						$oneSize['id'] = $one['size_id'];
						$oneSize['name'] = $one['size_name'];
					}
					$sizes[] = $oneSize;

					$oneColor['sizes'] = $sizes;
					$colors[] = $oneColor;
				}

				$data['colors'] = $colors;
			}

			$result['data'] = $data;
		}

		return $result;
	}

	/**
	 * 处理加价购商品数据
	 *
	 * @param int $skn 商品skn
	 * @param int $promotionId 加价购商品促销ID
	 * @return array
	 */
	public static function giftProductData($skn, $promotionId)
	{
		$result = array();

		$product = CartData::giftProductData($skn, $promotionId);
		if (isset($product['code']) && $product['code'] === 200) {
			$result['code'] = 200;
			$data = array();
			$productData = $product['data'];

			// 品牌信息
			if (isset($productData['brand_info']) && !empty($productData['brand_info'])) {
				$data['thumb'] = Helpers::getImageUrl($productData['brand_info']['brand_ico'], 120, 120);
			}

			$data['name'] = $productData['product_name'];
			$data['price'] = $productData['market_price'];
			$data['salePrice'] = $productData['sales_price'];
			$data['storage'] = $productData['storage_sum'];

			// 商品选择
			if (isset($productData['goods_list'])) {
				$goodsList = $productData['goods_list'];

				$colors = array();
				$oneColor = array();
				$sizes = array();
				$oneSize = array();
				foreach ($goodsList as $val) {
					// 颜色
					$oneColor = array();
					$oneColor['id'] = $val['color_id'];
					$oneColor['name'] = $val['color_name'];

					// 尺码
					foreach ($val['size_list'] as $one) {
						$oneSize = array();
						$oneSize['id'] = $one['size_id'];
						$oneSize['name'] = $one['size_name'];
					}
					$sizes[] = $oneSize;

					$oneColor['sizes'] = $sizes;
					$colors[] = $oneColor;
				}

				$data['colors'] = $colors;
			}

			$result['data'] = $data;
		}

		return $result;
	}

	/**
	 * 修改购物车商品数据
	 *
	 * @param int $uid 用户ID
	 * @param string $param 要更改的数据
	 * @param string $shoppingKey 未登录用户唯一识别码
	 * @return array 接口返回的数据
	 */
	public static function modifyCartProduct($uid, $param, $shoppingKey)
	{
		$result = array('code' => 400, 'message' => '出错啦~');

		// 处理要更改的数据
		$swapData = json_encode(array($param));

		$modify = CartData::modifyCartProduct($uid, $swapData, $shoppingKey);
		if ($modify && isset($modify['code'])) {
			$result['code'] = $modify['code'];
			$result['message'] = $modify['message'];
		}

		return $result;
	}

	/**
	 * 调用购物车结算接口返回的数据处理
	 *
	 * @param int $uid 用户ID
	 * @param string $cartType 购物车类型,ordinary表示普通购物车
	 * @param null|string $cookieData cookie中记录的一些订单有关数据
	 * @return array 接口返回的数据
	 */
	public static function cartPay($uid, $cartType, $cookieData)
	{
		$result = array();

		$pay = CartData::cartPay($uid, $cartType);

		if ($pay && isset($pay['code']) && $pay['code'] === 200) {
			$payReturn = $pay['data'];
			$orderInfo = array();
			$address = array();
			$orderCompute = array();

			// cookie保存的数据
			if (!empty($cookieData)) {
				$orderInfo = json_decode($cookieData, true);
				$orderCompute = self::orderCompute($uid, $cartType, $orderInfo['deliveryId'], $orderInfo['paymentTypeId'], $orderInfo['couponCode'], $orderInfo['yohoCoin']);
			}

			// 根据地址id查询地址信息
			if (isset($orderInfo['addressId'])) {
				$address = UserModel::getAddressDataById($uid, $orderInfo['addressId']);
			}

			// 收货人有关信息
			if (isset($payReturn['delivery_address']) && !empty($payReturn['delivery_address'])) {
				$result['addressId'] = isset($address['address_id']) ? $address['address_id'] : $payReturn['delivery_address']['address_id'];
				$result['name'] = isset($address['consignee']) ? $address['consignee'] : $payReturn['delivery_address']['consignee'];
				$result['phoneNum'] = isset($address['mobile']) ? $address['mobile'] : $payReturn['delivery_address']['mobile'];
                $result['area'] = isset($address['area']) ? $address['area'] : $payReturn['delivery_address']['area'];
                $result['address'] = isset($address['address']) ? $address['address'] : $payReturn['delivery_address']['address'];
				$result['isSupport'] = ($payReturn['delivery_address']['is_support'] === 'Y');
			}

			// 配送方式
			if (isset($payReturn['delivery_way'])) {
				$oneDeliv = array();
				foreach ($payReturn['delivery_way'] as $val) {
					$oneDeliv = array();
					$oneDeliv['id'] = $val['delivery_way_id'];
					$oneDeliv['name'] = $val['delivery_way_name'];

					isset($orderInfo['deliveryId']) && $orderInfo['deliveryId'] === $oneDeliv['id'] && $oneDeliv['isSelected'] = true;

                    !isset($oneDeliv['isSelected']) && $oneDeliv['isSelected'] = ($val['default'] === 'Y');

					$result['dispatchMode'][] = $oneDeliv;
				}
			}

			// 配送时间
			if (isset($payReturn['delivery_time'])) {
				$oneDelivTime = array();
				foreach ($payReturn['delivery_time'] as $one) {
					$oneDelivTime = array();
					$oneDelivTime['id'] = $one['delivery_time_id'];
					$oneDelivTime['name'] = $one['delivery_time_string'];
					$oneDelivTime['default'] = ($one['default'] === 'Y');
					isset($orderInfo['deliveryTimeId']) && $orderInfo['deliveryTimeId'] === $oneDelivTime['id'] && $oneDeliv['isSelected'] = true;

					$result['dispatchTime'][] = $oneDelivTime;
				}
			}

			// 订单商品
			if (isset($payReturn['goods_list'])) {
				$oneGoods = array();
				foreach ($payReturn['goods_list'] as $single) {
					$oneGoods = array();
					$oneGoods['id'] = $single['product_sku']; // TODO 未确定用哪个
					$oneGoods['thumb'] = Images::getImageUrl($single['goods_images'], 120, 120);
					$oneGoods['name'] = $single['product_name'];
					$oneGoods['color'] = $single['color_name'];
					$oneGoods['size'] = $single['size_name'];
					$oneGoods['price'] = Helpers::transPrice($single['real_price']); // last_price有些带.00,有些不带,real_price都不带.00
					$oneGoods['count'] = $single['buy_number'];

					$result['goods'][] = $oneGoods;
				}
			}

			// 支付方式
			if (isset($payReturn['payment_way'])) {
				$onePay = array();
				foreach ($payReturn['payment_way'] as $pay) {
					$onePay = array();
					$onePay['id'] = $pay['payment_id'];
					$onePay['paymentType'] = $pay['payment_type'];
					$onePay['name'] = $pay['payment_type_name'];
					$onePay['default'] = ($pay['default'] === 'Y');

					$result['paymentWay'][] = $onePay;
				}
			}

			// 有货币
			$result['yohoCoin'] = isset($orderCompute['use_yoho_coin']) ? isset($orderCompute['use_yoho_coin']) : $payReturn['yoho_coin'];

			// 订单数据
			if (isset($payReturn['shopping_cart_data']) && !empty($payReturn['shopping_cart_data'])) {
				$sumPrice = isset($orderCompute['order_amount']) ? $orderCompute['order_amount'] : $payReturn['shopping_cart_data']['order_amount'];
				$salePrice = isset($orderCompute['discount_amount']) ? $orderCompute['discount_amount'] : $payReturn['shopping_cart_data']['discount_amount'];
				$price = isset($orderCompute['last_order_amount']) ? $orderCompute['last_order_amount'] : $payReturn['shopping_cart_data']['last_order_amount'];
				$freight = isset($orderCompute['promotion_formula_list']['promotion_amount']) ? $orderCompute['promotion_formula_list']['promotion_amount'] : $payReturn['shopping_cart_data']['promotion_formula_list'][1]['promotion_amount'];
				$result['sumPrice'] = Helpers::transPrice($sumPrice);
				$result['salePrice'] = Helpers::transPrice($salePrice);
				$result['price'] = Helpers::transPrice($price);
				$result['freight'] = strtr($freight, '¥', '') . '.00';
			}

			// 发票有关数据
			if (isset($payReturn['invoices']) && !empty($payReturn['invoices'])) {
				$one = array();
				foreach ($payReturn['invoices']['invoices_type_list'] as $inv) {
					$one = array();
					$one['id'] = $inv['invoices_type_id'];
					$one['name'] = $inv['invoices_type_name'];

					$result['invoice'][] = $one;
				}
			}

			// 优惠券数据
			$coupons = array('notUsed' => true);
			!empty($orderCompute['coupon_amount']) && $coupons['value'] = $orderInfo['couponValue'];
			$coupons += self::getCouponList($uid, 0, 1, true);
			$result['coupon'] = $coupons;
		}

		return $result;
	}

	/**
	 * 购物车结算--支付方式和配送方式选择以及是否使用YOHO币接口返回的数据处理
	 *
	 * @param int $uid 用户ID
	 * @param string $cartType 购物车类型,ordinary表示普通购物车
	 * @param int $deliveryWay 配送方式,1表示普通快递,2表示顺丰速运
	 * @param int $paymentType 支付方式,1表示在线支付,2表示货到付款
	 * @param string $couponCode 优惠券码
	 * @param mixed $yohoCoin 使用的YOHO币数量
	 * @return array 接口返回的数据
	 */
	public static function orderCompute($uid, $cartType, $deliveryWay, $paymentType, $couponCode, $yohoCoin)
	{
		$result = array();

		$compute = CartData::orderCompute($uid, $cartType, $deliveryWay, $paymentType, $couponCode, $yohoCoin);
		if ($compute && isset($compute['code']) && $compute['code'] === 200) {
			$result = $compute['data'];
		}

		return $result;
	}

	/**
	 * 购物车结算--输入优惠券代码返回的结果处理
	 *
	 * @param int $uid 用户ID
	 * @param string $couponCode 优惠券代码
	 * @return array 接口返回的数据
	 */
	public static function searchCoupon($uid, $couponCode)
	{
		$result = array('code' => 400, 'message' => '出错啦~');

		$coupon = CartData::searchCoupon($uid, $couponCode);
		if ($coupon && isset($coupon['code'])) {
			$result = $coupon;
		}

		return $result;
	}

	/**
	 * 处理优惠券列表数据
	 *
	 * @param int $uid 用户ID
	 * @param boolean $onlyTotal 只返回总数
	 * @return array|mixed 处理之后的优惠券数据
	 */
	public static function getCouponList($uid, $onlyTotal = false)
	{
		$result = array();

		// 调用接口获取优惠券数据
		$coupons = CartData::getCouponList($uid);

		// 处理优惠券数据
		if (isset($coupons['data'])) {

			if ($onlyTotal) {
				$result['count'] = $coupons['data']['total'];
			} else {
				$couponArr = array();
				isset($coupons['data']['couponList']) && $couponArr = $coupons['data']['couponList'];

				foreach ($couponArr as &$val) {
					$notAvailableRes = self::searchCoupon($uid, $val['couponCode']);
					// 处理可用的优惠券
					if (isset($notAvailableRes['code']) && $notAvailableRes['code'] !== 200) {
						$val['notAvailable'] = true;
					}
				}
				!empty($couponArr) && $result = $couponArr;
			}
		}

		return $result;
	}

	/**
	 * 购物车结算--提交结算信息
	 *
	 * @param int $uid 用户ID
	 * @param int $addressId 地址ID
	 * @param int $cartType 购物车类型ID
	 * @param int $deliveryTime 寄送时间ID
	 * @param int $deliveryWay 寄送方式ID
	 * @param string $invoiceTitle 发票说明
	 * @param int $invoiceId 发票类型ID
	 * @param int $paymentId 支付方式ID
	 * @param int $paymentType 支付类型ID
	 * @param string $remark 留言
	 * @param mixed $yohoCoin 使用的YOHO币数量或为空
	 * @return array 接口返回的数据
	 */
	public static function orderSub($uid, $addressId, $cartType, $deliveryTime, $deliveryWay, $invoiceTitle, $invoiceId, $paymentId, $paymentType, $remark, $yohoCoin)
	{
		$result = array();

		$orderSubRes = CartData::orderSub($uid, $addressId, $cartType, $deliveryTime, $deliveryWay, $invoiceTitle, $invoiceId, $paymentId, $paymentType, $remark, $yohoCoin);
		if ($orderSubRes && isset($orderSubRes['code']) && $orderSubRes['code'] === 200) {
			$result = $orderSubRes;
		}

		return $result;
	}


	/**
	 * 处理不同类型的购物车数据
	 *
	 * @param array $data 不同类型购物车数据
	 * @return array $result 处理之后的不同类型购物车数据
	 */
	private static function procCartData($data)
	{
		$result = array();

		$oneGoods = array();
		// 购买的商品列表
		foreach ($data['goods_list'] as $value) {
			$oneGoods['id'] = $value['product_sku'];
			$oneGoods['skn'] = $value['product_skn'];
			$oneGoods['name'] = $value['product_name'];
			$oneGoods['thumb'] = Images::getImageUrl($value['goods_images'], 120, 120);
			$oneGoods['color'] = $value['color_name'];
			$oneGoods['size'] = $value['size_name'];
			$oneGoods['appearDate'] = '12月'; // 目前app接口没有返回该数据
			$oneGoods['price'] = $value['real_price'];
			$oneGoods['count'] = $value['buy_number'];
			$oneGoods['lowStocks'] = true;

			$result['goods'][] = $oneGoods;
		}
		// 赠品
		count($data['gift_list']) && $result['freebieOrAdvanceBuy'] = true;
		$result['freebie'] = $data['gift_list'];
		// 加价购
		$result['advanceBuy'] = $data['price_gift'];
		// 结算数据
		$result['price'] = $data['shopping_cart_data']['order_amount'];
		$result['activityPrice'] = $data['shopping_cart_data']['discount_amount'];
		$result['count'] = $data['shopping_cart_data']['goods_count'];
		$result['sumPrice'] = $data['shopping_cart_data']['order_amount'];

		return $result;
	}

}