优化商城商品管理

This commit is contained in:
Anyon 2020-09-10 15:02:49 +08:00
parent fefbe6391b
commit 62b1134d61
5 changed files with 70 additions and 18 deletions

View File

@ -39,6 +39,11 @@ class Goods extends Controller
*/
public function getGoods()
{
if ($code = input('code', null)) {
$this->app->db->name('ShopGoods')->where(['code' => $code])->update([
'num_read' => $this->app->db->raw('num_read+1'),
]);
}
$map = ['deleted' => 0, 'status' => 1];
$query = $this->_query('ShopGoods')->like('name,mark')->equal('code,cate');
$result = $query->where($map)->order('sort desc,id desc')->page(true, false, false, 10);

View File

@ -81,9 +81,25 @@ class GoodsService extends Service
return $query->where($map)->order('sort desc,id desc')->column('name');
}
/**
* 一维数组生成数据树
* @param array $list 待处理数据
* @param string $cid 自己的主键
* @param string $pid 上级的主键
* @param string $sub 子数组名称
* @return array
*/
public function arr2tree(array $list, string $cid = 'id', string $pid = 'pid', string $sub = 'sub'): array
{
[$tree, $tmp] = [[], array_combine(array_column($list, $cid), array_values($list))];
foreach ($list as $vo) isset($vo[$pid]) && isset($tmp[$vo[$pid]]) ? $tmp[$vo[$pid]][$sub][] = &$tmp[$vo[$cid]] : $tree[] = &$tmp[$vo[$cid]];
unset($tmp, $list);
return $tree;
}
/**
* 商品数据绑定
* @param array $list
* @param array $list 商品主数据
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
@ -91,20 +107,28 @@ class GoodsService extends Service
*/
public function buildItemData(array &$list = []): array
{
$cates = $this->app->db->name('ShopGoodsCate')->column('id,pid,name', 'id');
foreach ($cates as $cate) if (isset($cates[$cate['pid']])) {
$cates[$cate['id']]['parent'] =& $cates[$cate['pid']];
}
$codes = array_unique(array_column($list, 'code'));
$map = [['goods_code', 'in', $codes], ['status', '=', 1]];
$items = $this->app->db->name('ShopGoodsItem')->where($map)->select()->toArray();
$where = [['goods_code', 'in', $codes], ['status', '=', 1]];
$items = $this->app->db->name('ShopGoodsItem')->withoutField('id,status,create_at')->where($where)->select()->toArray();
foreach ($list as &$vo) {
$vo['marks'] = think_string_to_array($vo['mark']);
$vo['cates'] = $cates[$vo['cate']] ?? [];
$vo['slider'] = explode('|', $vo['slider']);
$vo['specs'] = json_decode($vo['data_specs'], true);
$vo['items'] = [];
foreach ($items as $item) {
if ($item['goods_code'] === $vo['code']) {
$vo['items'][] = $item;
}
}
foreach ($items as $item) if ($item['goods_code'] === $vo['code']) $vo['items'][] = $item;
unset($vo['mark'], $vo['sort'], $vo['status'], $vo['deleted'], $vo['data_items'], $vo['data_specs']);
}
dump($list);
exit;
return $list;
}
/**
* 最大分类级别
* @return integer

View File

@ -1,11 +1,26 @@
<?php
if (!function_exists('show_goods_spec')) {
if (!function_exists('think_string_to_array')) {
/**
* 字符串转数组
* @param string $text
* @param string $separ
* @return array|false|string[]
*/
function think_string_to_array(string $text, string $separ = ','): array
{
$text = trim($text, $separ);
return $text ? explode($separ, $text) : [];
}
}
if (!function_exists('think_show_goods_spec')) {
/**
* 商品规格过滤显示
* @param string $spec
* @param string $spec 原规格内容
* @return string
*/
function show_goods_spec($spec)
function think_show_goods_spec(string $spec): string
{
$specs = [];
foreach (explode(';;', $spec) as $sp) {

View File

@ -82,12 +82,12 @@
<thead>
<tr>
<th ng-repeat="x in navas track by $index" class="nowrap" ng-bind="x"></th>
<th width="10%" class="text-center nowrap">商品SKU <a ng-click="batchSet('sku',null)" data-tips-text="批量设置" class="layui-icon">&#xe63c;</a></th>
<th width="12%" class="text-center nowrap">商品SKU <a ng-click="batchSet('sku',null)" data-tips-text="批量设置" class="layui-icon">&#xe63c;</a></th>
<th width="10%" class="text-center nowrap">市场价格 <a ng-click="batchSet('market',2)" data-tips-text="批量设置" class="layui-icon">&#xe63c;</a></th>
<th width="10%" class="text-center nowrap">销售价格 <a ng-click="batchSet('selling',2)" data-tips-text="批量设置" class="layui-icon">&#xe63c;</a></th>
<th width="10%" class="text-center nowrap">虚拟销量 <a ng-click="batchSet('virtual',0)" data-tips-text="批量设置" class="layui-icon">&#xe63c;</a></th>
<th width="10%" class="text-center nowrap">快递计件 <a ng-click="batchSet('express',0)" data-tips-text="批量设置" class="layui-icon">&#xe63c;</a></th>
<th width="10%" class="text-center nowrap">销售状态</th>
<th width="8%" class="text-center nowrap">销售状态</th>
</tr>
</thead>
<tbody>
@ -161,6 +161,14 @@
var app = angular.module("GoodsForm", []).run(callback);
angular.bootstrap(document.getElementById(app.name), [app.name]);
function getRand(length, prefix) {
return (function (time, code) {
code += parseInt(time.substr(0, 1)) + parseInt(time.substr(1, 1)) + time.substr(2, 8);
while (code.length < length) code += Math.round(Math.random() * 10);
return code;
})(Date.now().toString(), prefix || '' + '')
}
function callback($rootScope) {
$rootScope.mode = '{$mode|default="add"}', $rootScope.navas = [];
$rootScope.items = angular.fromJson(angular.element('#GoodsItems').val() || '[]') || {};
@ -236,7 +244,7 @@
keys.push(item.group + '::' + item.name);
}), rows.every(function (item) {
item.key = keys.join(';;');
item.sku = $rootScope.getValue(item.key, 'sku', 'S' + Date.now() + (Math.random() * 100).toFixed());
item.sku = $rootScope.getValue(item.key, 'sku', getRand(12, 'S'));
item.status = !!$rootScope.getValue(item.key, 'status', 1);
item.market = $rootScope.getValue(item.key, 'market', '0.00');
item.selling = $rootScope.getValue(item.key, 'selling', '0.00');

View File

@ -25,7 +25,7 @@
{foreach $vo.list as $goods}
<tr>
<td width="28%" class="layui-bg-gray">
{$goods.goods_spec|show_goods_spec}
{$goods.goods_spec|think_show_goods_spec}
</td>
<td class="padding-0" width="12%">
<input class="layui-input text-center layui-bg-gray border-0 padding-left-0" disabled value="{$goods.price_market}">