<?php
namespace app\common\service\wechat\wow;
use app\common\model\Wxapp as WxappModel;
use app\common\model\wow\Shoping as ShopingModel;
use app\common\model\wow\Setting as SettingModel;
use app\common\library\wechat\wow\Shoping as WowShoping;
use app\common\library\helper;
class Shoping
{
private $wxappId;
private $ApiDriver;
protected $error;
public function __construct($wxappId)
{
$this->wxappId = $wxappId;
$this->initApiDriver();
}
public function add($user, $goodsList)
{
$setting = SettingModel::getItem('basic', $this->wxappId);
if ($setting['is_shopping'] == false) {
return false;
}
$productList = $this->getProductListToAdd($goodsList);
$status = $this->ApiDriver->addList($user['open_id'], $productList);
if ($status == false) {
$this->error = $this->ApiDriver->getError();
return $status;
}
$goodsIds = helper::getArrayColumn($goodsList, 'goods_id');
$this->model()->add($user['user_id'], $goodsIds);
return $status;
}
public function delete($id)
{
$model = $this->model($id, ['user']);
$status = $this->ApiDriver->delete($model['user']['open_id'], [[
'item_code' => $model['goods_id'],
'sku_id' => $model['goods_id'],
]]);
if ($status == false) {
$this->error = $this->ApiDriver->getError();
}
$model->setDelete();
return true;
}
public function getError()
{
return $this->error;
}
private function initApiDriver()
{
$config = WxappModel::getWxappCache($this->wxappId);
$this->ApiDriver = new WowShoping($config['app_id'], $config['app_secret']);
}
private function model($id = null, $with = ['user'])
{
static $model;
if (!$model instanceof ShopingModel) {
$model = $id > 0 ? ShopingModel::detail($id, $with) : (new ShopingModel);
}
return $model;
}
private function getProductListToAdd(&$goodsList)
{
$productList = [];
foreach ($goodsList as $goods) {
$imageList = []; foreach ($goods['image'] as $image) {
$imageList[] = $image['file_path'];
}
$skuInfo = &$goods['sku'][0];
$productList[] = [
'item_code' => $goods['goods_id'],
'title' => $goods['goods_name'],
'category_list' => [$goods['category']['name']],
'image_list' => $imageList,
'src_wxapp_path' => "/pages/goods/index?goods_id={$goods['goods_id']}", 'sku_info' => [ 'sku_id' => $goods['goods_id'],
'price' => $skuInfo['goods_price'] * 100,
'original_price' => $skuInfo['line_price'] * 100, 'status' => 1,
],
];
}
return $productList;
}
}