0) {
$key = 'cate_child_id';
}
} else {
$key = 'channel_id';
}
$where = [$key => $cate['id'], 'status' => 1];
$search = Dever::input('search');
if ($search) {
$where['name'] = ['like', $search];
}
$set = ['col' => 'id,sku_id,cate_child_id,name,info,price,pic,type,num_sell+num_sell_add as num_sell,show_page'];
if ($limit) {
$set['limit'] = $limit;
}
if ($page) {
$set['num'] = $page;
}
$order = Dever::input('order');
if ($order == 1) {
# 销量排序
$set['order'] = 'num_sell desc';
} elseif ($order == 3) {
# 价格正序
$set['order'] = 'price asc';
} elseif ($order == 4) {
# 价格倒序
$set['order'] = 'price desc';
}
$data = Dever::db('psource/info')->select($where, $set);
if ($data) {
foreach ($data as &$v) {
$v = $this->get($v, $cate);
}
}
return $data;
}
# 获取资源信息
public function get($info, $cate = [])
{
if (is_numeric($info)) {
$info = Dever::db('psource/info')->find(['id' => $info, 'status' => 1], ['col' => 'id,name,pic,price,cate_id']);
}
if ($info) {
if (!$cate) {
$cate = Dever::load(Cate::class)->getInfo($info['cate_id']);
}
# 获取封面
$info['cover'] = '';
$pic = explode(',', $info['pic']);
unset($info['pic']);
if (isset($pic[0])) {
$info['cover'] = $pic[0];
}
$info = Dever::load(Price::class)->get($info, $cate, -1);
}
return $info;
}
# 获取资源详细信息
public function getInfo($uid, $id)
{
$info = Dever::db('psource/info')->find(['id' => $id, 'status' => 1], ['col' => 'id,name,info,price,sku_id,pic,content,channel_id,cate_parent_id,cate_id,have_vip,num_sell+num_sell_add as num_sell,show_page,show_submit,show_sell']);
if (!$info) {
Dever::error('资源不存在');
}
# 分类
$cate = Dever::load(Cate::class)->getInfo($info['cate_id']);
# 价格
$info = Dever::load(Price::class)->get($info, $cate, -1);
$info['pic'] = explode(',', $info['pic']);
$info['content'] = htmlspecialchars_decode($info['content']);
if ($info['content'] == '
') {
$info['content'] = '';
}
# 获取sku基本信息
$sku = Dever::db('psource/sku')->find($info['sku_id']);
if ($sku) {
$info['sku_name'] = Dever::load(\Api\Lib\Sku::class)->getName($sku['key'], 'psource');
}
# 查看是否专享
$info['have_vip'] = Dever::load(Price::class)->getHave($info);
# 展示说明
$info['help'] = Dever::load(Help::class)->getList($cate['help_cate_id']);
# 是否收藏
$info['collect'] = false;
if ($uid) {
$act = Act::load('collect', $uid, 1, $info['id']);
if ($act->getInfo()) {
$info['collect'] = true;
}
}
return $info;
}
# 获取资源规格信息
public function getSpec($id)
{
$info = Dever::db('psource/info')->find(['id' => $id, 'status' => 1], ['col' => 'id,sku_id,cate_id']);
if (!$info) {
Dever::error('资源不存在');
}
return Dever::load(\Api\Lib\Spec::class)->getList(['info_id' => $info['id']], $info['sku_id'], 'psource');
}
# 根据资源规格获取价格
public function getSku($id, $spec = '', $default_sku_id = 0)
{
$info = Dever::db('psource/info')->find(['id' => $id, 'status' => 1], ['col' => 'id,sku_id,cate_id,pic']);
if (!$info) {
Dever::error('资源不存在');
}
if (!$spec) {
$where = ['id' => $info['sku_id'], 'info_id' => $info['id']];
} else {
$where = ['info_id' => $info['id'], 'key' => $spec];
}
$sku = $this->getSkuPrice($where, $info);
if ((!$sku && $default_sku_id) || ($sku['price'] == null && $sku['code'] == null)) {
# 如果没有,获取默认值
$where = ['id' => $default_sku_id, 'info_id' => $info['id']];
$sku = $this->getSkuPrice($where, $info);
if ($sku) {
$sku['stock']['num'] = 0;
}
}
return $sku;
}
# 获取价格
public function getSkuPrice($where, $info)
{
$sku = Dever::load(\Api\Lib\Sku::class)->getInfo($where, 'psource');
if ($sku) {
if ($sku['price'] === '') {
return false;
}
if (!$sku['pic']) {
$pic = explode(',', $info['pic']);
$sku['pic'] = $pic[0];
}
# 分类
$cate = Dever::load(Cate::class)->getInfo($info['cate_id']);
$sku = Dever::load(Price::class)->get($sku, $cate, -1);
}
return $sku;
}
# 获取资源基本信息
public function getBaseInfo($source_id)
{
$info = Dever::db('psource/info')->find($source_id, ['col' => 'id,name,info,channel_id,cate_parent_id']);
if (!$info) {
Dever::error('资源不存在');
}
return $info;
}
# 获取资源编号或者ID
public function getCode($id, $encode = true)
{
if (Dever::getData('muser')) {
$salt = Dever::getData('muser')['select']['data_id'];//品牌id
$xorkey = Dever::getData('muser')['id'];//当前登录的管理员id
} else {
$place = Dever::load(\Place::class);
$salt = $place['info']['id'];//品牌id
$xorkey = $place['uid'];//当前登录的管理员id
}
return \Dever\Helper\Str::uid($id, $encode, $salt, $xorkey);
}
# 根据$cate获取where条件
public function getWhereByCate($cate, $prefix = '')
{
if ($cate) {
$cate = explode(',', $cate);
if (isset($cate[2]) && $cate[2]) {
$where = [$prefix . 'cate_child_id' => $cate[2]];
} elseif (isset($cate[1]) && $cate[1]) {
$where = [$prefix . 'cate_parent_id' => $cate[1]];
} else {
$where = [$prefix . 'channel_id' => $cate[0]];
}
}
$where[$prefix . 'status'] = 1;
return $where;
}
# 根据$cate获取资源列表
public function getListByCate($cate)
{
$where = $this->getWhereByCate($cate);
return Dever::db('psource/info')->select($where);
}
# 根据$cate获取资源sku列表
public function getSkuByCate($cate)
{
$set['join'] = array
(
array
(
'table' => 'psource_info',
'type' => 'left join',
'on' => 'main.info_id=t0.id',
),
);
$set['col'] = 'main.id,main.info_id,main.key,main.pic,main.price,main.code';
$set['order'] = 'main.id asc';
$where = $this->getWhereByCate($cate, 't0.');
$where['main.state'] = 1;
return Dever::db('psource/sku')->select($where, $set);
}
# 建立规格
public function createSku($i, $id, $name, $code, $price, $pic, $type = 1)
{
if ($name) {
$createCode = false;
if (!$code) {
$createCode = true;
}
if (!$type) {
$type = 1;
}
foreach ($name as $v) {
if ($createCode) {
$code = $this->createCode($i);
}
$temp = explode('&', $v);
$spec_value_id = [];
foreach ($temp as $k => $t) {
# 颜色:白色 & 尺寸:35
$t = trim($t);
list($spec_name, $spec_value) = explode(':', $t);
$spec_data = ['info_id' => $id, 'name' => $spec_name];
$spec = Dever::db('psource/spec')->find($spec_data);
if ($spec) {
$spec_id = $spec['id'];
} else {
$spec_data['sort'] = $i;
$spec_id = Dever::db('psource/spec')->insert($spec_data);
}
if ($spec_id) {
$spec_data = ['info_id' => $id, 'spec_id' => $spec_id, 'value' => $spec_value];
$spec_value = Dever::db('psource/spec_value')->find($spec_data);
if ($spec_value) {
$spec_value_id[] = $spec_value['id'];
} else {
$spec_data['sort'] = $i;
if ($type == 1) {
$spec_data['pic'] = $pic;
}
$spec_value_id[] = Dever::db('psource/spec_value')->insert($spec_data);
}
}
}
$update = [];
if ($spec_value_id) {
$spec_value_id = implode(',', $spec_value_id);
$update['spec_type'] = 3;
} else {
$spec_value_id = -1;
$update['spec_type'] = 2;
}
$sku_data = ['info_id' => $id, 'key' => $spec_value_id];
$sku = Dever::db('psource/sku')->find($sku_data);
$sku_data['price'] = $price;
$sku_data['code'] = $code;
if ($type == 2) {
$sku_data['pic'] = $pic;
}
if ($sku) {
Dever::db('psource/sku')->update($sku['id'], $sku_data);
} else {
Dever::db('psource/sku')->insert($sku_data);
}
$sku = Dever::load(\Api\Lib\Sku::class)->getPrice(['info_id' => $id], $update['spec_type'], 'psource');
$update['price'] = $sku['price'];
$update['sku_id'] = $sku['id'];
Dever::db('psource/info')->update($id, $update);
$i++;
}
}
return $i;
}
public function createPic($pic, $info_pic = '')
{
if ($pic) {
$temp = explode('?', $pic);
if (!strstr($info_pic, $temp[0])) {
$info_pic = explode(',', $info_pic);
$info_pic[] = $pic;
return implode(',', $info_pic);
}
}
return '';
}
private function createCode($i)
{
// 流水号,补足4位,不足左边补0
$line = str_pad((string)($i + 1), 4, '0', STR_PAD_LEFT);
// 当前时间 (年月日时分)
$dateStr = date("YmdHi"); // Y=年, m=月, d=日, H=时, i=分
return $dateStr . $line;
}
}