AddForm.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace hd\models\goods;
  3. use hd\models\BaseForm;
  4. /**
  5. * 商品添加表单模型
  6. */
  7. class AddForm extends BaseForm
  8. {
  9. /**
  10. * 商品名称
  11. * @var string
  12. */
  13. public $name;
  14. /**
  15. * 商品单价
  16. * @var float
  17. */
  18. public $price;
  19. /**
  20. * 价格类型
  21. * @var int
  22. */
  23. public $priceType;
  24. /**
  25. * 历史销量
  26. * @var int
  27. */
  28. public $sold;
  29. /**
  30. * 花朵数量
  31. * @var int
  32. */
  33. public $flowerNum;
  34. /**
  35. * 重量
  36. * @var float
  37. */
  38. public $weight;
  39. /**
  40. * 商品图片
  41. * @var array
  42. */
  43. public $shopImg;
  44. /**
  45. * 商品封面图
  46. * @var string
  47. */
  48. public $cover;
  49. /**
  50. * 商品库存
  51. * @var int
  52. */
  53. public $stock;
  54. /**
  55. * 商品分类ID
  56. * @var array
  57. */
  58. public $catIds;
  59. /**
  60. * 库存状态
  61. * @var int
  62. */
  63. public $stockSet;
  64. /**
  65. * 商品状态
  66. * @var int
  67. */
  68. public $status;
  69. /**
  70. * 是否需要发货
  71. * @var int
  72. */
  73. public $needSend;
  74. /**
  75. * 运费类型
  76. * @var int
  77. */
  78. public $freightType;
  79. /**
  80. * 自动涨价
  81. * @var int
  82. */
  83. public $autoRise;
  84. public $useCaseIdList;
  85. public $specEnabled;
  86. public $specList;
  87. /**
  88. * 规格名称:多规格时把首规格名称回写到主记录,便于主记录可售
  89. * @var string
  90. */
  91. public $specName;
  92. /**
  93. * 商品规格形态:0 单规格,1 多规格(主记录承载第一个规格)
  94. * @var int
  95. */
  96. public $goodsStyle;
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function rules()
  101. {
  102. return [
  103. // 必填字段验证
  104. [['name'], 'required', 'message' => '请填写商品名称'],
  105. [['priceType'], 'required', 'message' => '请选择价格类型', 'when' => function($model) {
  106. return (int)$model->specEnabled !== 1;
  107. }],
  108. [['catIds'], 'required', 'message' => '请选择商品分类'],
  109. ['weight', 'required', 'message' => '请填写重量', 'when' => function($model) {
  110. return (int)$model->specEnabled !== 1;
  111. }],
  112. // 默认值设置
  113. ['price', 'default', 'value' => 0],
  114. ['flowerNum', 'default', 'value' => 0],
  115. ['sold', 'default', 'value' => 0],
  116. ['catIds', 'default', 'value' => []],
  117. ['useCaseIdList', 'default', 'value' => []],
  118. ['specEnabled', 'default', 'value' => 0],
  119. ['specName', 'default', 'value' => ''],
  120. ['goodsStyle', 'default', 'value' => 0],
  121. // 数据类型验证
  122. [['name', 'cover'], 'string'],
  123. [['flowerNum', 'stock'], 'integer'],
  124. ['priceType', 'in', 'range' => [0, 1]],
  125. ['price', 'number', 'min' => 0, 'message' => '商品价格必须大于或等于0'],
  126. ['flowerNum', 'number', 'min' => 0, 'message' => '花朵数量必须大于或等于0'],
  127. ['weight', 'number', 'min' => 0, 'message' => '重量必须大于或等于0'],
  128. ['sold', 'integer', 'min' => 0, 'message' => '已售数量必须大于或等于0'],
  129. [['stockSet'], 'in', 'range' => [0, 1]],
  130. [['needSend'], 'in', 'range' => [0, 1]],
  131. [['freightType'], 'in', 'range' => [0, 1]],
  132. [['status'], 'in', 'range' => [0, 1]],
  133. [['autoRise'], 'in', 'range' => [0, 1]],
  134. [['specEnabled'], 'in', 'range' => [0, 1]],
  135. [['goodsStyle'], 'in', 'range' => [0, 1]],
  136. [['specName'], 'string'],
  137. [['useCaseIdList'], 'validateIdList'],
  138. [['specList'], 'validateSpecList'],
  139. // 添加条件验证
  140. // ['price', 'required', 'message' => '请填写商品价格', 'when' => function($model) {
  141. // return isset($_POST['priceType']) && $_POST['priceType'] == '1';
  142. // }],
  143. // 数组类型验证
  144. [['shopImg'], 'validateShopImg'],
  145. // 其他业务规则验证
  146. [['flowerNum'], 'number', 'min' => 0, 'message' => '花朵数量必须大于或等于0'],
  147. ];
  148. }
  149. /**
  150. * 验证商品图片
  151. * @param string $attribute 属性名
  152. * @param array $params 参数
  153. */
  154. public function validateShopImg($attribute, $params)
  155. {
  156. if (!empty($this->$attribute) && !is_array($this->$attribute)) {
  157. $this->addError($attribute, '商品图片格式不正确');
  158. }
  159. }
  160. public function validateIdList($attribute) {
  161. if (!is_array($this->$attribute)) $this->addError($attribute, '使用场景格式不正确');
  162. }
  163. /**
  164. * 校验多规格列表:关闭时清空规格并标记单规格;开启时把首规格价库/名称回写主记录字段
  165. * @param string $attribute 属性名
  166. */
  167. public function validateSpecList($attribute)
  168. {
  169. if ((int)$this->specEnabled !== 1) {
  170. $this->$attribute = [];
  171. // 单规格:主记录不挂规格名,goodsStyle 标记为单规格
  172. $this->specName = '';
  173. $this->goodsStyle = 0;
  174. return;
  175. }
  176. $list = $this->$attribute;
  177. if (is_string($list)) {
  178. $list = json_decode($list, true);
  179. }
  180. if (!is_array($list) || empty($list)) {
  181. $this->addError($attribute, '请至少填写一个规格');
  182. return;
  183. }
  184. $formatList = [];
  185. foreach ($list as $index => $spec) {
  186. $name = '规格' . ($index + 1);
  187. if (!is_array($spec)) {
  188. $this->addError($attribute, $name . '格式错误');
  189. return;
  190. }
  191. $specName = trim($spec['specName'] ?? '');
  192. if ($specName === '') {
  193. $this->addError($attribute, '请填写' . $name . '名称');
  194. return;
  195. }
  196. $priceType = $spec['priceType'] ?? null;
  197. if ($priceType === null || !in_array((int)$priceType, [0, 1], true)) {
  198. $this->addError($attribute, $name . '价格类型错误');
  199. return;
  200. }
  201. $price = $spec['price'] ?? 0;
  202. if ((int)$priceType === 1 && (!is_numeric($price) || $price <= 0)) {
  203. $this->addError($attribute, '请填写' . $name . '价格');
  204. return;
  205. }
  206. $stockSet = $spec['stockSet'] ?? 0;
  207. if (!in_array((int)$stockSet, [0, 1], true)) {
  208. $this->addError($attribute, $name . '库存类型错误');
  209. return;
  210. }
  211. $stock = $spec['stock'] ?? 0;
  212. if ((int)$stockSet === 1 && (!is_numeric($stock) || (int)$stock < 0 || (int)$stock != $stock)) {
  213. $this->addError($attribute, $name . '库存数量错误');
  214. return;
  215. }
  216. $flowerNum = $spec['flowerNum'] ?? 0;
  217. if (!is_numeric($flowerNum) || (int)$flowerNum < 0 || (int)$flowerNum != $flowerNum) {
  218. $this->addError($attribute, $name . '花支数错误');
  219. return;
  220. }
  221. $weight = $spec['weight'] ?? 1;
  222. if (!is_numeric($weight) || $weight < 0) {
  223. $this->addError($attribute, $name . '重量错误');
  224. return;
  225. }
  226. $sold = $spec['sold'] ?? 0;
  227. if (!is_numeric($sold) || (int)$sold < 0 || (int)$sold != $sold) {
  228. $this->addError($attribute, $name . '已售数量错误');
  229. return;
  230. }
  231. $formatList[] = [
  232. 'id' => (int)($spec['id'] ?? 0),
  233. 'specName' => $specName,
  234. 'priceType' => (int)$priceType,
  235. 'price' => (int)$priceType === 1 ? (float)$price : 0,
  236. 'stockSet' => (int)$stockSet,
  237. 'stock' => (int)$stockSet === 1 ? (int)$stock : 9999,
  238. 'flowerNum' => (int)$flowerNum,
  239. 'weight' => (float)$weight,
  240. 'sold' => (int)$sold,
  241. 'shopImg' => is_array($spec['shopImg'] ?? null) ? $spec['shopImg'] : [],
  242. 'cover' => trim($spec['cover'] ?? ''),
  243. ];
  244. }
  245. $this->$attribute = $formatList;
  246. // 多规格:主记录承载首规格的价库与规格名,goodsStyle=1 供列表/下单判断
  247. $firstSpec = $formatList[0];
  248. $this->specName = $firstSpec['specName'];
  249. $this->goodsStyle = 1;
  250. $this->priceType = $firstSpec['priceType'];
  251. $this->price = $firstSpec['price'];
  252. $this->stockSet = $firstSpec['stockSet'];
  253. $this->stock = $firstSpec['stock'];
  254. $this->flowerNum = $firstSpec['flowerNum'];
  255. $this->weight = $firstSpec['weight'];
  256. $this->sold = $firstSpec['sold'];
  257. }
  258. /**
  259. * 获取属性标签
  260. * @return array
  261. */
  262. public function attributeLabels()
  263. {
  264. return [
  265. 'name' => '商品名称',
  266. 'price' => '商品价格',
  267. 'kindId' => '商品种类ID',
  268. 'kindName' => '商品种类名称',
  269. 'flowerNum' => '花朵数量',
  270. 'shopImg' => '商品图片',
  271. 'cover' => '商品封面图',
  272. 'stock' => '商品库存',
  273. 'spu' => '商品编码',
  274. 'autoRise' => '自动涨价',
  275. ];
  276. }
  277. }