AddForm.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. * {@inheritdoc}
  89. */
  90. public function rules()
  91. {
  92. return [
  93. // 必填字段验证
  94. [['name'], 'required', 'message' => '请填写商品名称'],
  95. [['priceType'], 'required', 'message' => '请选择价格类型', 'when' => function($model) {
  96. return (int)$model->specEnabled !== 1;
  97. }],
  98. [['catIds'], 'required', 'message' => '请选择商品分类'],
  99. ['weight', 'required', 'message' => '请填写重量', 'when' => function($model) {
  100. return (int)$model->specEnabled !== 1;
  101. }],
  102. // 默认值设置
  103. ['price', 'default', 'value' => 0],
  104. ['flowerNum', 'default', 'value' => 0],
  105. ['sold', 'default', 'value' => 0],
  106. ['catIds', 'default', 'value' => []],
  107. ['useCaseIdList', 'default', 'value' => []],
  108. ['specEnabled', 'default', 'value' => 0],
  109. // 数据类型验证
  110. [['name', 'cover'], 'string'],
  111. [['flowerNum', 'stock'], 'integer'],
  112. ['priceType', 'in', 'range' => [0, 1]],
  113. ['price', 'number', 'min' => 0, 'message' => '商品价格必须大于或等于0'],
  114. ['flowerNum', 'number', 'min' => 0, 'message' => '花朵数量必须大于或等于0'],
  115. ['weight', 'number', 'min' => 0, 'message' => '重量必须大于或等于0'],
  116. ['sold', 'integer', 'min' => 0, 'message' => '已售数量必须大于或等于0'],
  117. [['stockSet'], 'in', 'range' => [0, 1]],
  118. [['needSend'], 'in', 'range' => [0, 1]],
  119. [['freightType'], 'in', 'range' => [0, 1]],
  120. [['status'], 'in', 'range' => [0, 1]],
  121. [['autoRise'], 'in', 'range' => [0, 1]],
  122. [['specEnabled'], 'in', 'range' => [0, 1]],
  123. [['useCaseIdList'], 'validateIdList'],
  124. [['specList'], 'validateSpecList'],
  125. // 添加条件验证
  126. // ['price', 'required', 'message' => '请填写商品价格', 'when' => function($model) {
  127. // return isset($_POST['priceType']) && $_POST['priceType'] == '1';
  128. // }],
  129. // 数组类型验证
  130. [['shopImg'], 'validateShopImg'],
  131. // 其他业务规则验证
  132. [['flowerNum'], 'number', 'min' => 0, 'message' => '花朵数量必须大于或等于0'],
  133. ];
  134. }
  135. /**
  136. * 验证商品图片
  137. * @param string $attribute 属性名
  138. * @param array $params 参数
  139. */
  140. public function validateShopImg($attribute, $params)
  141. {
  142. if (!empty($this->$attribute) && !is_array($this->$attribute)) {
  143. $this->addError($attribute, '商品图片格式不正确');
  144. }
  145. }
  146. public function validateIdList($attribute) {
  147. if (!is_array($this->$attribute)) $this->addError($attribute, '使用场景格式不正确');
  148. }
  149. public function validateSpecList($attribute)
  150. {
  151. if ((int)$this->specEnabled !== 1) {
  152. $this->$attribute = [];
  153. return;
  154. }
  155. $list = $this->$attribute;
  156. if (is_string($list)) {
  157. $list = json_decode($list, true);
  158. }
  159. if (!is_array($list) || empty($list)) {
  160. $this->addError($attribute, '请至少填写一个规格');
  161. return;
  162. }
  163. $formatList = [];
  164. foreach ($list as $index => $spec) {
  165. $name = '规格' . ($index + 1);
  166. if (!is_array($spec)) {
  167. $this->addError($attribute, $name . '格式错误');
  168. return;
  169. }
  170. $specName = trim($spec['specName'] ?? '');
  171. if ($specName === '') {
  172. $this->addError($attribute, '请填写' . $name . '名称');
  173. return;
  174. }
  175. $priceType = $spec['priceType'] ?? null;
  176. if ($priceType === null || !in_array((int)$priceType, [0, 1], true)) {
  177. $this->addError($attribute, $name . '价格类型错误');
  178. return;
  179. }
  180. $price = $spec['price'] ?? 0;
  181. if ((int)$priceType === 1 && (!is_numeric($price) || $price <= 0)) {
  182. $this->addError($attribute, '请填写' . $name . '价格');
  183. return;
  184. }
  185. $stockSet = $spec['stockSet'] ?? 0;
  186. if (!in_array((int)$stockSet, [0, 1], true)) {
  187. $this->addError($attribute, $name . '库存类型错误');
  188. return;
  189. }
  190. $stock = $spec['stock'] ?? 0;
  191. if ((int)$stockSet === 1 && (!is_numeric($stock) || (int)$stock < 0 || (int)$stock != $stock)) {
  192. $this->addError($attribute, $name . '库存数量错误');
  193. return;
  194. }
  195. $flowerNum = $spec['flowerNum'] ?? 0;
  196. if (!is_numeric($flowerNum) || (int)$flowerNum < 0 || (int)$flowerNum != $flowerNum) {
  197. $this->addError($attribute, $name . '花支数错误');
  198. return;
  199. }
  200. $weight = $spec['weight'] ?? 1;
  201. if (!is_numeric($weight) || $weight < 0) {
  202. $this->addError($attribute, $name . '重量错误');
  203. return;
  204. }
  205. $sold = $spec['sold'] ?? 0;
  206. if (!is_numeric($sold) || (int)$sold < 0 || (int)$sold != $sold) {
  207. $this->addError($attribute, $name . '已售数量错误');
  208. return;
  209. }
  210. $formatList[] = [
  211. 'id' => (int)($spec['id'] ?? 0),
  212. 'specName' => $specName,
  213. 'priceType' => (int)$priceType,
  214. 'price' => (int)$priceType === 1 ? (float)$price : 0,
  215. 'stockSet' => (int)$stockSet,
  216. 'stock' => (int)$stockSet === 1 ? (int)$stock : 9999,
  217. 'flowerNum' => (int)$flowerNum,
  218. 'weight' => (float)$weight,
  219. 'sold' => (int)$sold,
  220. 'shopImg' => is_array($spec['shopImg'] ?? null) ? $spec['shopImg'] : [],
  221. 'cover' => trim($spec['cover'] ?? ''),
  222. ];
  223. }
  224. $this->$attribute = $formatList;
  225. $firstSpec = $formatList[0];
  226. $this->priceType = $firstSpec['priceType'];
  227. $this->price = $firstSpec['price'];
  228. $this->stockSet = $firstSpec['stockSet'];
  229. $this->stock = $firstSpec['stock'];
  230. $this->flowerNum = $firstSpec['flowerNum'];
  231. $this->weight = $firstSpec['weight'];
  232. $this->sold = $firstSpec['sold'];
  233. }
  234. /**
  235. * 获取属性标签
  236. * @return array
  237. */
  238. public function attributeLabels()
  239. {
  240. return [
  241. 'name' => '商品名称',
  242. 'price' => '商品价格',
  243. 'kindId' => '商品种类ID',
  244. 'kindName' => '商品种类名称',
  245. 'flowerNum' => '花朵数量',
  246. 'shopImg' => '商品图片',
  247. 'cover' => '商品封面图',
  248. 'stock' => '商品库存',
  249. 'spu' => '商品编码',
  250. 'autoRise' => '自动涨价',
  251. ];
  252. }
  253. }