链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.4 KiB

  1. <?php
  2. namespace App\Admin\Forms\v3;
  3. use Dcat\Admin\Widgets\Form;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use App\Models\v3\Goods as GoodsModel;
  6. use Dcat\Admin\Form\NestedForm;
  7. class GoodsSpecForm extends Form
  8. {
  9. /**
  10. * Handle the form request.
  11. *
  12. * @param array $input
  13. *
  14. * @return Response
  15. */
  16. public function handle(array $input)
  17. {
  18. // 获取外部传递参数
  19. $goodsId = $input['goods_id'];
  20. // $tags= isset($input['tags'])?$input['tags']:[];
  21. $spec= isset($input['spec'])?$input['spec']:[];
  22. $edit = false;
  23. $goods = GoodsModel::find($goodsId);
  24. if(empty($goods)){
  25. $this->error('商品不存在!');
  26. }
  27. // if(!empty($tags)){
  28. // $tagsArr = [];
  29. // if(!empty($tags)){
  30. // foreach($tags as $kt => $tag){
  31. // if(empty($tag)){
  32. // unset($tags[$kt]);
  33. // continue;
  34. // }
  35. // $tagsArr[] = $tag;
  36. // }
  37. // };
  38. // $goods->tags = json_encode($tagsArr);
  39. // }
  40. if(!empty($spec)){
  41. $goods->spec = $spec;
  42. $edit = true;
  43. }
  44. if($edit && $goods->save()){
  45. return $this->error('修改成功!');
  46. }
  47. return $this->error('未做任何修改或者修改失败!');
  48. }
  49. /**
  50. * Build a form here.
  51. */
  52. public function form()
  53. {
  54. $id = $this->getKey();
  55. $goods = GoodsModel::select('name','tags','spec')->find($id);
  56. $goodName = empty($goods->name)?'':$goods->name;
  57. // $tags = empty($goods->tags)?[]:$goods->tags;
  58. $spec = empty($goods->spec)?[]:$goods->spec;
  59. $this->hidden('goods_id')->value($id);
  60. $this->display('name','商品名称')->value($goodName);
  61. // $this->tags('tags','标签')->options(['新品','热销','新鲜'])->value($tags);
  62. $this->table('spec', function (NestedForm $table) {
  63. $table->text('spec_key')->required()->help('如:净含量:500克,保质期:120天,包装:12个/盒等');
  64. $table->text('spec_value')->required();
  65. })->value($spec);
  66. }
  67. /**
  68. * The data of the form.
  69. *
  70. * @return array
  71. */
  72. public function default()
  73. {
  74. return [];
  75. }
  76. }