多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 商品规格 商品规格 文件路径:shop/controllers/Seller/Goods/SpecCtl.php; 商品类别: ``` <pre class="calibre14">``` <span class="token5">//获取路径:</span> url<span class="token1">:</span> SITE_URL <span class="token">+</span> <span class="token4">'?ctl=Goods_Cat&met=cat&typ=json&type_number=goods_cat&is_delete=2&filter=true'</span><span class="token1">,</span> <span class="token5">//如果存在父类</span> $data_rows <span class="token">=</span> $Goods_CatModel<span class="token">-</span><span class="token">></span><span class="token3">getCatTreeData</span><span class="token1">(</span>$cat_parent_id<span class="token1">,</span> <span class="token6">false</span><span class="token1">,</span> <span class="token6">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">is_array</span><span class="token1">(</span>$cat_parent_id<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> $cond_row <span class="token">=</span> <span class="token3">array</span><span class="token1">(</span><span class="token4">'cat_parent_id:in'</span> <span class="token">=</span><span class="token">></span> $cat_parent_id<span class="token1">)</span><span class="token1">;</span> $cache_key <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>_cacheKeyPrefix <span class="token1">.</span> <span class="token4">'cat_parent_id|'</span> <span class="token1">.</span> <span class="token3">implode</span><span class="token1">(</span><span class="token4">':'</span><span class="token1">,</span> $cat_parent_id<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> $cond_row <span class="token">=</span> <span class="token3">array</span><span class="token1">(</span><span class="token4">'cat_parent_id'</span> <span class="token">=</span><span class="token">></span> $cat_parent_id<span class="token1">)</span><span class="token1">;</span> $cache_key <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>_cacheKeyPrefix <span class="token1">.</span> <span class="token4">'cat_parent_id|'</span> <span class="token1">.</span> $cat_parent_id<span class="token1">;</span> <span class="token1">}</span> <span class="token5">//设置cache</span> $Cache <span class="token">=</span> Yf_Cache<span class="token1">:</span><span class="token1">:</span><span class="token3">create</span><span class="token1">(</span><span class="token4">'base'</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">//如果有缓存</span> <span class="token2">if</span> <span class="token1">(</span>$cat_rows <span class="token">=</span> $Cache<span class="token">-</span><span class="token">></span><span class="token3">get</span><span class="token1">(</span>$cache_key<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> $cat_rows <span class="token">=</span> $this<span class="token">-</span><span class="token">></span><span class="token3">getByWhere</span><span class="token1">(</span>$cond_row<span class="token1">,</span> <span class="token3">array</span><span class="token1">(</span><span class="token4">'cat_displayorder'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'ASC'</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> $Cache<span class="token">-</span><span class="token">></span><span class="token3">save</span><span class="token1">(</span>$cat_rows<span class="token1">,</span> $cache_key<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token5">//不存在父类</span> $data <span class="token">=</span> $Goods_CatModel<span class="token">-</span><span class="token">></span><span class="token3">getCatTree</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> $Cache <span class="token">=</span> Yf_Cache<span class="token1">:</span><span class="token1">:</span><span class="token3">create</span><span class="token1">(</span><span class="token4">'base'</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span>$data_rows <span class="token">=</span> $Cache<span class="token">-</span><span class="token">></span><span class="token3">get</span><span class="token1">(</span>$this<span class="token">-</span><span class="token">></span>treeAllKey<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> $data_rows <span class="token">=</span> $this<span class="token">-</span><span class="token">></span><span class="token3">getCatTreeData</span><span class="token1">(</span>$cat_parent_id<span class="token1">,</span> $recursive<span class="token1">,</span> $level<span class="token1">)</span><span class="token1">;</span> $Cache<span class="token">-</span><span class="token">></span><span class="token3">save</span><span class="token1">(</span>$data_rows<span class="token1">,</span> $this<span class="token">-</span><span class="token">></span>treeAllKey<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` 添加/修改规格值(spec()) 表单提交: ``` <pre class="calibre14">``` $<span class="token1">(</span><span class="token4">'#form'</span><span class="token1">)</span><span class="token1">.</span><span class="token3">prop</span><span class="token1">(</span><span class="token4">'action'</span><span class="token1">,</span> SITE_URL <span class="token">+</span> <span class="token4">"?ctl=Seller_Goods_Spec&met=saveSpecValue&typ=json&spec_id="</span> <span class="token">+</span> param_data<span class="token1">.</span>spec_id<span class="token1">)</span><span class="token1">;</span> <span class="token5">//如果存在旧数据</span> foreach <span class="token1">(</span>$old_data as $key <span class="token">=</span><span class="token">></span> $val<span class="token1">)</span> <span class="token1">{</span> $update_data<span class="token1">[</span><span class="token4">'spec_value_displayorder'</span><span class="token1">]</span> <span class="token">=</span> $val<span class="token1">[</span><span class="token4">'displayorder'</span><span class="token1">]</span><span class="token1">;</span> $update_data<span class="token1">[</span><span class="token4">'spec_value_name'</span><span class="token1">]</span> <span class="token">=</span> $val<span class="token1">[</span><span class="token4">'spec_value_name'</span><span class="token1">]</span><span class="token1">;</span> $flag <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>goodsSpecValueModel<span class="token">-</span><span class="token">></span><span class="token3">editSpecValue</span><span class="token1">(</span>$key<span class="token1">,</span> $update_data<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token5">//如果存在新数据</span> $update_data<span class="token1">[</span><span class="token4">'spec_id'</span><span class="token1">]</span> <span class="token">=</span> $spec_id<span class="token1">;</span> foreach <span class="token1">(</span>$new_data as $key <span class="token">=</span><span class="token">></span> $val<span class="token1">)</span> <span class="token1">{</span> $update_data<span class="token1">[</span><span class="token4">'spec_value_displayorder'</span><span class="token1">]</span> <span class="token">=</span> $val<span class="token1">[</span><span class="token4">'displayorder'</span><span class="token1">]</span><span class="token1">;</span> $update_data<span class="token1">[</span><span class="token4">'spec_value_name'</span><span class="token1">]</span> <span class="token">=</span> $val<span class="token1">[</span><span class="token4">'spec_value_name'</span><span class="token1">]</span><span class="token1">;</span> $this<span class="token">-</span><span class="token">></span>goodsSpecValueModel<span class="token">-</span><span class="token">></span><span class="token3">addSpecValue</span><span class="token1">(</span>$update_data<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token5">//商城添加规格值</span> $spec_value_id <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>goodsSpecValueModel<span class="token">-</span><span class="token">></span><span class="token3">addSpecValue</span><span class="token1">(</span>$update_data<span class="token1">,</span> <span class="token6">true</span><span class="token1">)</span><span class="token1">;</span> ``` ``` 删除规格值: ``` <pre class="calibre15">``` $<span class="token1">(</span><span class="token4">'.delete'</span><span class="token1">)</span><span class="token1">.</span><span class="token3">click</span><span class="token1">(</span><span class="token2">function</span><span class="token1">(</span><span class="token1">)</span><span class="token1">{</span> var _this <span class="token">=</span> $<span class="token1">(</span>this<span class="token1">)</span><span class="token1">;</span> $<span class="token1">.</span>dialog<span class="token1">.</span><span class="token3">confirm</span><span class="token1">(</span><span class="token4">'<?=__("删除的数据将不能恢复,请确认是否删除")?>?'</span><span class="token1">,</span><span class="token2">function</span><span class="token1">(</span><span class="token1">)</span><span class="token1">{</span> spec_value_id <span class="token">=</span> _this<span class="token1">.</span><span class="token3">children</span><span class="token1">(</span><span class="token4">'a'</span><span class="token1">)</span><span class="token1">.</span><span class="token3">data</span><span class="token1">(</span><span class="token4">'id'</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span>typeof spec_value_id <span class="token">!=</span> <span class="token4">'undefined'</span><span class="token1">)</span> <span class="token1">{</span> $<span class="token1">.</span><span class="token3">post</span><span class="token1">(</span> SITE_URL <span class="token">+</span> <span class="token4">"?ctl=Seller_Goods_Spec&met=removeSpecValue&typ=json"</span><span class="token1">,</span> <span class="token1">{</span> spec_value_id<span class="token1">:</span> spec_value_id<span class="token1">}</span><span class="token1">,</span> <span class="token2">function</span><span class="token1">(</span>data<span class="token1">)</span> <span class="token1">{</span> <span class="token1">}</span><span class="token1">)</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> _this<span class="token1">.</span><span class="token3">parent</span><span class="token1">(</span><span class="token1">)</span><span class="token1">.</span><span class="token3">parent</span><span class="token1">(</span><span class="token1">)</span><span class="token1">.</span><span class="token3">remove</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> $spec_value_id <span class="token">=</span> <span class="token3">request_int</span><span class="token1">(</span><span class="token4">'spec_value_id'</span><span class="token1">)</span><span class="token1">;</span> $flag <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>goodsSpecValueModel<span class="token">-</span><span class="token">></span><span class="token3">removeSpecValue</span><span class="token1">(</span>$spec_value_id<span class="token1">)</span><span class="token1">;</span> ``` ```