PHP code example of lty5240 / dcat-easy-sku

1. Go to this page and download the library: Download lty5240/dcat-easy-sku library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lty5240 / dcat-easy-sku example snippets


Form::make($builder, function(Form $form){
    $sku_params = [
        [
            'name'    => '拓展列第一列', // table 第一行 title 
            'field'   => 'column1', // input 的 field_name 名称
            'default' => '5', // 默认值
        ],
        [
            'name'    => '拓展列第二列',
            'field'   => 'column2',
            'default' => '',
        ],
    ];

    $form->sku('sku', json_encode($sku_params))->display(true)->customFormat(function ($value) use ($form){
        if($value === null){
            // 这里是给sku喂数据, 数据格式为
            $data = new stdClass();
            
            $data->attrs = [
                '颜色' => [
                    '红色',
                    '蓝色',
                ],
                '大小' => [
                    '20',
                ],               
            ];
            $data->sku = [
                [
                    "颜色" => "红色",
                    "大小" => '20',
                    "pic" => '图片',
                    "stock" => '库存',
                    "price" => '价格',
                    ...
                    // 如果存在其他属性,则同样在这里塞进去
                ],
                ...
                // 根据attrs做笛卡尔积,会有两种情况, 红色20 蓝色20,所以这两中都应该有,自行编辑数据
            ];
            return json_encode($data);
        }       
        return null;
    });
});


    $form->submitted(function (Form $form) {
        $this->sku = $form->input('sku');
        $form->deleteInput('sku');
    });

public function setSkuAttribute()
{
}


// 可以用saved回调,等该模型保存完后,再进行处理
$form->saved(function (Form $form, int $result) {
    if ($result) {
        $sku = json_decode($form->input('sku'));
        $attrs = $sku->attrs;
        // 这边拿到sku的数据就自己处理吧
    }
}

    /**
     * 上传商品规格图片
     *
     * @param Request $request
     *
     * @return string[]
     */
    public function skuImage(Request $request)
    {
        if ($request->hasFile('file')) {
            $file = $request->file('file');
            $disk = Storage::disk('cosv5');
            $url = 'sku';
            $res = $disk->put($url, $file);
            // 返回格式
            return ['url' => config('app.cos.cdn') . $res];
        }
        return [];
    }