releaseProduct.vue 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. <template>
  2. <view class="container">
  3. <view class="form-container">
  4. <up-form labelPosition="left" :model="formData" :rules="rules" ref="formRef" :labelWidth="100">
  5. <!-- 商品分类 -->
  6. <view class="card-title" v-if="!isProductCenter">商品分类</view>
  7. <up-form-item class="form-item" label="商品分类" prop="categoryIds" :borderBottom="false"
  8. @click="showCategory = true" :required="true" v-if="!isProductCenter">
  9. <up-input v-model="formData.categoryDisplayName" disabled disabledColor="#ffffff"
  10. placeholder="请选择商品分类" inputAlign="right" border="none"></up-input>
  11. <template #right>
  12. <up-icon name="arrow-right"></up-icon>
  13. </template>
  14. </up-form-item>
  15. <!-- 商品信息 -->
  16. <view class="card-title">商品信息</view>
  17. <up-form-item label="商品名称" prop="storeName" :borderBottom="false" :required="true">
  18. <up-input v-model="formData.storeName" placeholder="请输入商品名称" inputAlign="right"
  19. border="none"></up-input>
  20. </up-form-item>
  21. <up-form-item label="商品关键字" prop="keyword" :borderBottom="false" :required="true"
  22. v-if="!isProductCenter">
  23. <up-input v-model="formData.keyword" placeholder="请输入商品关键字" inputAlign="right"
  24. border="none"></up-input>
  25. </up-form-item>
  26. <up-form-item label="商品简介" prop="storeInfo" :borderBottom="false" :required="true"
  27. v-if="!isProductCenter">
  28. <up-textarea v-model="formData.storeInfo" placeholder="请输入商品简介" inputAlign="right"
  29. border="none"></up-textarea>
  30. </up-form-item>
  31. <up-form-item label="单位" prop="unitName" :borderBottom="false" :required="true" v-if="!isProductCenter">
  32. <up-input v-model="formData.unitName" placeholder="请输入单位" inputAlign="right"
  33. border="none"></up-input>
  34. </up-form-item>
  35. <!-- 图片上传 -->
  36. <view class="upload-section" v-if="!isProductCenter">
  37. <view class="upload-item">
  38. <view class="upload-label"><text class="required">*</text>商品封面图</view>
  39. <up-upload @afterRead="async (e) => {
  40. await afterRead(e);
  41. getImage();
  42. }" @delete="onPreviewImageDelete" name="preview" max-count="1" :fileList="previewImages">
  43. <view class="upload-btn">
  44. <up-icon name="plus" size="20" color="#ccc"></up-icon>
  45. <text class="upload-tip">点击上传</text>
  46. </view>
  47. </up-upload>
  48. <text class="format-tip">支持上传PNG、JPG格式的图片,每张不超过5MB。</text>
  49. </view>
  50. </view>
  51. <view class="upload-section" v-if="!isProductCenter">
  52. <view class="upload-item">
  53. <view class="upload-label"><text class="required">*</text>商品图片</view>
  54. <up-upload @afterRead="async (e) => {
  55. await afterRead(e);
  56. getImageProduct();
  57. }" @delete="onProductImageDelete" name="product" multiple :maxCount="10" :fileList="productImages">
  58. <view class="upload-btn">
  59. <up-icon name="plus" size="20" color="#ccc"></up-icon>
  60. <text class="upload-tip">点击上传</text>
  61. </view>
  62. </up-upload>
  63. <text class="format-tip">支持上传PNG、JPG格式图片,每张不超过5MB,最多可上传10张。</text>
  64. </view>
  65. </view>
  66. <!-- 商品属性 -->
  67. <view class="card-title">商品属性</view>
  68. <up-form-item label="材质" prop="metalType" :borderBottom="false" :required="true"
  69. v-if="!isProductCenter">
  70. <up-radio-group v-model="formData.metalType" placement="row">
  71. <up-radio :customStyle="{marginRight: '20rpx'}" v-for="(item, index) in materialList"
  72. :key="index" :label="item.name" :name="item.code" activeColor="#F8C008">
  73. </up-radio>
  74. </up-radio-group>
  75. </up-form-item>
  76. <up-form-item class="form-item" label="运费模板" prop="tempIds" :borderBottom="false"
  77. @click="showTemp = true" :required="true">
  78. <up-input v-model="formData.tempName" disabled disabledColor="#ffffff" placeholder="请选择运费模板"
  79. inputAlign="right" border="none"></up-input>
  80. <template #right>
  81. <up-icon name="arrow-right"></up-icon>
  82. </template>
  83. </up-form-item>
  84. <!-- 商品描述 -->
  85. <view class="editor-section" v-if="!isProductCenter">
  86. <view class="editor-header">
  87. <text class="editor-label">商品描述</text>
  88. <text class="word-count">{{ descriptionText.length }}/500</text>
  89. </view>
  90. <sp-editor editorId="editor" :toolbar-config="toolbarConfig" :readOnly="readOnly"
  91. @input="onEditorInput" @upinImage="onUpinImage" @init="onEditorInit"
  92. @overMax="onOverMax"></sp-editor>
  93. </view>
  94. <!-- 商品规格 -->
  95. <view class="pro-cate-container">
  96. <view class="cate-title" v-if="!isProductCenter">商品规格批量设置</view>
  97. <view class="cate-list">
  98. <view class="cate-item">
  99. <view class="cate-pic-line">
  100. <!-- <image src="@/static/avator.png" class="cate-pic" mode="widthFix"></image> -->
  101. <up-upload :fileList="oneTemplate.imgs" @afterRead="async (e) => {
  102. await afterRead(e);
  103. getImageProductGg('oneTemplate');
  104. }" @delete="onProductImageGgDelete('oneTemplate')" name="productGg" :maxCount="1">
  105. <view class="upload-btn">
  106. <up-icon name="plus" size="20" color="#666"></up-icon>
  107. <text class="upload-tip">上传图片</text>
  108. </view>
  109. </up-upload>
  110. <!-- <view class="cate-name">
  111. {{oneTemplate.attrName}}
  112. </view> -->
  113. </view>
  114. <view class="cate-ipt-line">
  115. <view class="cate-ipt" style="margin-right: 16rpx;">
  116. <up-input class="" v-model="oneTemplate.price" placeholder="请输入工费" inputAlign="left"
  117. @blur="keyupEvent('price', oneTemplate.price, 0, 1)" border="none" type="digit"
  118. labelWidth="">
  119. <template #suffix>
  120. <text class="unit">元/g</text>
  121. </template>
  122. </up-input>
  123. </view>
  124. <view class="cate-ipt">
  125. <up-input class="" v-model="oneTemplate.stock" placeholder="请输入库存" inputAlign="left"
  126. @blur="keyupEvent('stock', oneTemplate.stock, 0, 1)" border="none" type="number"
  127. labelWidth="">
  128. <template #suffix>
  129. <text class="unit">件</text>
  130. </template>
  131. </up-input>
  132. </view>
  133. </view>
  134. <view class="cate-ipt-line">
  135. <view class="cate-ipt" style="margin-right: 16rpx;">
  136. <up-input class="" v-model="oneTemplate.weight" placeholder="请输入重量"
  137. inputAlign="left" @blur="keyupEvent('weight', oneTemplate.weight, 0, 1)"
  138. border="none" type="digit" labelWidth="">
  139. <template #suffix>
  140. <text class="unit">g</text>
  141. </template>
  142. </up-input>
  143. </view>
  144. <view class="cate-ipt">
  145. <up-input class="" v-model="oneTemplate.additionalAmount" placeholder="请输入附加金额"
  146. inputAlign="left"
  147. @blur="keyupEvent('additionalAmount', oneTemplate.additionalAmount, 0, 1)"
  148. border="none" type="number" labelWidth="">
  149. <template #suffix>
  150. <text class="unit">元</text>
  151. </template>
  152. </up-input>
  153. </view>
  154. </view>
  155. <view class="cate-ipt-line" style="padding: 0;">
  156. <view class="cate-ipt">
  157. <up-input class="" v-model="oneTemplate.barCode" placeholder="请输入商品编号"
  158. inputAlign="left" @blur="keyupEvent('barCode', oneTemplate.barCode, 0, 1)"
  159. border="none" type="digit" labelWidth="">
  160. </up-input>
  161. </view>
  162. </view>
  163. </view>
  164. </view>
  165. <view class="cate-add" @click="setAllAttr">
  166. 批量设置规格
  167. </view>
  168. </view>
  169. <view class="pro-cate-container">
  170. <view class="cate-title" v-if="!isProductCenter"><text style="color:red;">*</text>商品规格</view>
  171. <view class="cate-list">
  172. <view class="cate-item" v-for="(item,index) of attrTable" :key="'attr'+index">
  173. <view class="cate-pic-line">
  174. <!-- <image src="@/static/avator.png" class="cate-pic" mode="widthFix"></image> -->
  175. <up-upload :fileList="item.imgs" @afterRead="async (e) => {
  176. await afterRead(e);
  177. getImageProductGg(index);
  178. }" @delete="onProductImageGgDelete(index)" name="productGg" :maxCount="1">
  179. <view class="upload-btn">
  180. <up-icon name="plus" size="20" color="#666"></up-icon>
  181. <text class="upload-tip">上传图片</text>
  182. </view>
  183. </up-upload>
  184. <view class="cate-name">
  185. {{item.attrName}}
  186. </view>
  187. </view>
  188. <view class="cate-ipt-line">
  189. <view class="cate-ipt" style="margin-right: 16rpx;">
  190. <up-input class="" v-model="item.price" placeholder="请输入工费" inputAlign="left"
  191. @blur="keyupEvent('price', item.price, index, 2)" border="none" type="digit"
  192. labelWidth="">
  193. <template #suffix>
  194. <text class="unit">元/g</text>
  195. </template>
  196. </up-input>
  197. </view>
  198. <view class="cate-ipt">
  199. <up-input class="" v-model="item.stock" placeholder="请输入库存" inputAlign="left"
  200. @blur="keyupEvent('stock', item.stock, index, 2)" border="none" type="number"
  201. labelWidth="">
  202. <template #suffix>
  203. <text class="unit">件</text>
  204. </template>
  205. </up-input>
  206. </view>
  207. </view>
  208. <view class="cate-ipt-line">
  209. <view class="cate-ipt" style="margin-right: 16rpx;">
  210. <up-input class="" v-model="item.weight" placeholder="请输入重量" inputAlign="left"
  211. @blur="keyupEvent('weight', item.weight, index, 2)" border="none" type="digit"
  212. labelWidth="">
  213. <template #suffix>
  214. <text class="unit">g</text>
  215. </template>
  216. </up-input>
  217. </view>
  218. <view class="cate-ipt">
  219. <up-input class="" v-model="item.additionalAmount" placeholder="请输入附加金额"
  220. inputAlign="left"
  221. @blur="keyupEvent('additionalAmount', item.additionalAmount, index, 2)"
  222. border="none" type="number" labelWidth="">
  223. <template #suffix>
  224. <text class="unit">元</text>
  225. </template>
  226. </up-input>
  227. </view>
  228. </view>
  229. <view class="cate-ipt-line" style="padding: 0;">
  230. <view class="cate-ipt">
  231. <up-input class="" v-model="item.barCode" placeholder="请输入商品编号" inputAlign="left"
  232. @blur="keyupEvent('barCode', item.barCode, index, 2)" border="none" type="digit"
  233. labelWidth="">
  234. </up-input>
  235. </view>
  236. </view>
  237. </view>
  238. </view>
  239. <view class="cate-add" @click="editCatePage">
  240. 管理规格
  241. </view>
  242. </view>
  243. <!-- <view class="card-title">商品规格</view>
  244. <up-form-item label="工费" prop="laborCost" :borderBottom="false" :required="true">
  245. <up-input v-model="formData.laborCost" placeholder="请输入工费" inputAlign="right" border="none"
  246. type="digit"></up-input>
  247. <template #right>
  248. <text class="unit">元/g</text>
  249. </template>
  250. </up-form-item>
  251. <up-form-item label="库存" prop="stock" :borderBottom="false" :required="true">
  252. <up-input v-model="formData.stock" placeholder="请输入库存" inputAlign="right" border="none"
  253. type="number"></up-input>
  254. </up-form-item>
  255. <up-form-item label="重量" prop="weight" :borderBottom="false" :required="true">
  256. <up-input v-model="formData.weight" placeholder="请输入重量" inputAlign="right" border="none"
  257. type="digit"></up-input>
  258. <template #right>
  259. <text class="unit">g</text>
  260. </template>
  261. </up-form-item>
  262. <up-form-item label="附加费" prop="additionalFee" :borderBottom="false" :required="true">
  263. <up-input v-model="formData.additionalFee" placeholder="请输入附加费" inputAlign="right" border="none"
  264. type="digit"></up-input>
  265. <template #right>
  266. <text class="unit">元</text>
  267. </template>
  268. </up-form-item>
  269. <up-form-item label="商品编号" prop="barCode" :borderBottom="false" :required="true">
  270. <up-input v-model="formData.barCode" placeholder="请输入商品编号" inputAlign="right" border="none"
  271. type="number"></up-input>
  272. </up-form-item>
  273. <view class="upload-section">
  274. <view class="upload-item">
  275. <view class="upload-label"><text class="required">*</text>规格图片</view>
  276. <up-upload :fileList="productImagesGg" @afterRead="async (e) => {
  277. await afterRead(e);
  278. getImageProductGg();
  279. }" @delete="onProductImageGgDelete" name="productGg" :maxCount="1">
  280. <view class="upload-btn">
  281. <up-icon name="plus" size="20" color="#ccc"></up-icon>
  282. <text class="upload-tip">点击上传</text>
  283. </view>
  284. </up-upload>
  285. <text class="format-tip">支持上传PNG、JPG格式图片,每张不超过5MB,最多可上传1张。</text>
  286. </view>
  287. </view> -->
  288. <up-form-item label="商品排序" prop="sort" :borderBottom="false">
  289. <up-input v-model="formData.sort" placeholder="请输入排序号" inputAlign="right" border="none"
  290. type="number"></up-input>
  291. </up-form-item>
  292. </up-form>
  293. </view>
  294. <!-- 发布按钮 -->
  295. <view class="btn-view">
  296. <button class="submit" @click="submitForm">立即发布</button>
  297. </view>
  298. <!-- 类目选择弹窗 -->
  299. <up-popup :show="showCategory" @close="showCategory = false" mode="bottom" round="20" :closeable="true">
  300. <view class="popup-content">
  301. <view class="popup-header">
  302. <text class="popup-title">选择商品分类</text>
  303. </view>
  304. <category-selector :categoryList="categoryData" :selectedIds="formData.categoryIds"
  305. @change="onCategoryChange" ref="categoryRef" />
  306. <view class="popup-actions">
  307. <button class="action-btn cancel" @click="showCategory = false">取消</button>
  308. <button class="action-btn confirm" @click="confirmCategory">确定</button>
  309. </view>
  310. </view>
  311. </up-popup>
  312. <!-- 运费模板-->
  313. <up-picker :show="showTemp" v-model="formData.tempIds" :columns="tempColumns" keyName="name" valueName="id"
  314. confirmColor="#F8C008" @close="showTemp = false" @confirm="tempConfirm"
  315. @cancel="showTemp = false"></up-picker>
  316. </view>
  317. </template>
  318. <script setup>
  319. import {
  320. ref,
  321. computed,
  322. watch,
  323. nextTick
  324. } from 'vue';
  325. import {
  326. onShow,
  327. onLoad,
  328. onUnload
  329. } from "@dcloudio/uni-app";
  330. import {
  331. productCategory,
  332. productSave,
  333. productUpdate,
  334. templatesList,
  335. productInfo
  336. } from "@/api/merchant";
  337. import CategorySelector from '@/components/CategorySelector';
  338. import {
  339. useAppStore
  340. } from "@/stores/app";
  341. import {
  342. useImageUpload
  343. } from "@/hooks/useImageUpload";
  344. import {
  345. useToast
  346. } from "@/hooks/useToast";
  347. const {
  348. Toast
  349. } = useToast();
  350. const {
  351. imageList,
  352. afterRead,
  353. deletePic,
  354. uploadLoading
  355. } = useImageUpload({
  356. pid: 1,
  357. model: "product",
  358. });
  359. const appStore = useAppStore();
  360. const merchantInfo = appStore.userInfo.merchant
  361. // 表单验证和提交
  362. const formRef = ref(null);
  363. // 表单数据
  364. const formData = ref({
  365. categoryIds: [], // 选中的分类ID数组
  366. categoryDisplayName: '', // 显示的分类名称
  367. tempName: '', // 显示的运费模板名称
  368. tempIds: [],
  369. storeName: '',
  370. keyword: '',
  371. storeInfo: '',
  372. unitName: '',
  373. metalType: '',
  374. weight: '',
  375. laborCost: '',
  376. additionalFee: '',
  377. sort: '',
  378. content: '',
  379. stock: '',
  380. barCode: ''
  381. });
  382. // 编辑器相关
  383. const editorIns = ref(null);
  384. const readOnly = ref(false);
  385. const descriptionText = ref('');
  386. const toolbarConfig = ref({
  387. iconSize: '20px',
  388. iconColumns: 10,
  389. excludeKeys: ['direction', 'date', 'lineHeight', 'letterSpacing', 'listCheck']
  390. });
  391. // 分类相关
  392. const showCategory = ref(false);
  393. const categoryData = ref([]);
  394. const categoryRef = ref();
  395. const showTemp = ref(false);
  396. const tempColumns = ref([])
  397. // 图片列表
  398. const previewImages = ref([]);
  399. const productImages = ref([]);
  400. const productImagesGg = ref([]);
  401. const productId = ref(null);
  402. const isProductCenter = ref(null);
  403. // 规格
  404. const attr = ref([]);
  405. // 规格列表
  406. const attrTable = ref([]);
  407. // 规格数据模板
  408. const oneTemplate = ref({
  409. image: '',
  410. imgs: [],
  411. price: '',
  412. additionalAmount: '',
  413. stock: '',
  414. barCode: '',
  415. weight: '',
  416. })
  417. // 规格历史数据,防止刷新数据时已填数据丢失
  418. let historyAttr = []
  419. // 规格表历史数据
  420. let historyTable = {}
  421. // 材质列表
  422. const materialList = ref([{
  423. name: '黄金',
  424. code: 'au'
  425. },
  426. {
  427. name: '铂金',
  428. code: 'pt'
  429. },
  430. {
  431. name: '白银',
  432. code: 'ag'
  433. }
  434. ]);
  435. // 验证规则
  436. const rules = ref({
  437. categoryIds: {
  438. type: 'array',
  439. required: true,
  440. message: '请选择商品分类',
  441. trigger: ['blur', 'change']
  442. },
  443. storeName: {
  444. type: 'string',
  445. required: true,
  446. message: '请输入商品名称',
  447. trigger: ['blur', 'change']
  448. },
  449. keyword: {
  450. type: 'string',
  451. required: true,
  452. message: '请输入商品关键字',
  453. trigger: ['blur', 'change']
  454. },
  455. storeInfo: {
  456. type: 'string',
  457. required: true,
  458. message: '请输入商品关键字',
  459. trigger: ['blur', 'change']
  460. },
  461. unitName: {
  462. type: 'string',
  463. required: true,
  464. message: '请输入单位',
  465. trigger: ['blur', 'change']
  466. },
  467. tempIds: {
  468. type: 'array',
  469. required: true,
  470. message: '请选择运费模板',
  471. trigger: ['blur', 'change']
  472. },
  473. metalType: {
  474. type: 'string',
  475. required: true,
  476. message: '请选择材质',
  477. trigger: ['blur', 'change']
  478. },
  479. sort: {
  480. type: 'string',
  481. pattern: /^\d*$/,
  482. message: '排序号必须是整数',
  483. trigger: ['blur', 'change'],
  484. validator: (rule, value) => {
  485. if (!value || value.trim().length === 0) return true; // 可选字段,为空时通过
  486. return /^\d+$/.test(value);
  487. }
  488. },
  489. // additionalFee: {
  490. // type: 'string',
  491. // required: true,
  492. // pattern: /^\d+(\.\d+)?$/,
  493. // message: '附加费必须是数字,可以是小数',
  494. // trigger: ['blur', 'change']
  495. // },
  496. // weight: {
  497. // type: 'string',
  498. // required: true,
  499. // pattern: /^\d+(\.\d+)?$/,
  500. // message: '重量必须是数字,可以是小数',
  501. // trigger: ['blur', 'change']
  502. // },
  503. // laborCost: {
  504. // type: 'string',
  505. // required: true,
  506. // pattern: /^\d+(\.\d+)?$/,
  507. // message: '工费必须是数字,可以是小数',
  508. // trigger: ['blur', 'change']
  509. // },
  510. // stock: {
  511. // type: 'string',
  512. // required: true,
  513. // pattern: /^\d*$/,
  514. // message: '库存必须是整数',
  515. // trigger: ['blur', 'change'],
  516. // validator: (rule, value) => {
  517. // if (!value || value.trim().length === 0) return false;
  518. // return /^\d+$/.test(value);
  519. // }
  520. // },
  521. // barCode: {
  522. // type: 'string',
  523. // required: true,
  524. // pattern: /^\d*$/,
  525. // message: '商品编号必须是整数',
  526. // trigger: ['blur', 'change'],
  527. // validator: (rule, value) => {
  528. // if (!value || value.trim().length === 0) return false;
  529. // return /^\d+$/.test(value);
  530. // }
  531. // },
  532. });
  533. // 页面加载
  534. onShow(() => {
  535. })
  536. onLoad(async (options) => {
  537. appStore.SET_CPATTR([])
  538. appStore.SET_CPATTRVALUE([])
  539. await getProductCategory();
  540. await getTempData();
  541. console.log(options)
  542. if (options.id) {
  543. productId.value = options.id;
  544. isProductCenter.value = options.isProductCenter || null;
  545. await getProductDetail(options.id);
  546. } else {
  547. resetForm();
  548. }
  549. uni.$on("updateAttr", onUpdateAttr)
  550. })
  551. onUnload(() => {
  552. uni.$off("updateAttr")
  553. })
  554. // 规格设置页面点击确认,更新当前页规格表格
  555. const onUpdateAttr = (data) => {
  556. // console.log("onUpdateAttr", data)
  557. const table = attrFormat(data)
  558. attr.value = data.concat();
  559. attrTable.value = table
  560. // console.log("table==>", table)
  561. }
  562. // 获取商品分类
  563. async function getProductCategory() {
  564. let obj = {
  565. type: 1,
  566. status: 1
  567. }
  568. try {
  569. const {
  570. data
  571. } = await productCategory(obj)
  572. console.log('原始分类数据:', data);
  573. const newArr = []
  574. data.forEach((value, index) => {
  575. newArr[index] = value
  576. if (value.child) newArr[index].child = value.child.filter(item => item.status === true)
  577. })
  578. // 过滤商品分类设置为隐藏的子分类不出现在树形列表里
  579. categoryData.value = newArr.filter(item => item.code !== 'bb_mall')
  580. console.log('转换后的分类数据:', categoryData.value);
  581. } catch (error) {
  582. console.error('获取商品分类失败:', error);
  583. uni.showToast({
  584. title: '获取分类失败',
  585. icon: 'none'
  586. });
  587. }
  588. }
  589. // 获取运费模板
  590. async function getTempData() {
  591. let obj = {
  592. page: 1,
  593. limit: 9999
  594. }
  595. try {
  596. const {
  597. data
  598. } = await templatesList()
  599. tempColumns.value[0] = data.list;
  600. } catch (error) {
  601. console.error('获取商品分类失败:', error);
  602. uni.showToast({
  603. title: '获取分类失败',
  604. icon: 'none'
  605. });
  606. }
  607. }
  608. const initFormValidation = async () => {
  609. await nextTick();
  610. if (!formRef.value) {
  611. console.error('表单引用不存在');
  612. return;
  613. }
  614. try {
  615. // 方法1:清除所有验证状态,然后重新验证
  616. formRef.value.clearValidate();
  617. // 方法2:延迟触发字段验证
  618. setTimeout(async () => {
  619. // 逐个触发必填字段的验证
  620. // const requiredFields = ['categoryIds', 'storeName', 'keyword', 'storeInfo', 'unitName',
  621. // 'tempIds', 'metalType', 'weight', 'laborCost', 'additionalFee',
  622. // 'stock', 'barCode'
  623. // ];
  624. const requiredFields = ['categoryIds', 'storeName', 'keyword', 'storeInfo', 'unitName',
  625. 'tempIds', 'metalType'
  626. ];
  627. for (const field of requiredFields) {
  628. try {
  629. await formRef.value.validateField(field);
  630. } catch (error) {
  631. console.log(`⚠️ 字段 ${field} 验证状态:`, error);
  632. }
  633. }
  634. }, 300);
  635. } catch (error) {
  636. console.error('初始化表单验证状态失败:', error);
  637. }
  638. };
  639. // 获取商品详情
  640. async function getProductDetail(id) {
  641. try {
  642. const {
  643. data
  644. } = await productInfo(id)
  645. console.log('获取商品详情:', data);
  646. // 使用Object.assign确保响应式更新
  647. Object.assign(formData.value, {
  648. ...data,
  649. // 确保categoryIds是数组格式
  650. categoryIds: data.cateId ? (Array.isArray(data.cateId) ? data.cateId : data.cateId.split(
  651. ',')) : [],
  652. // 确保tempIds是数组格式
  653. tempIds: data.tempId ? [data.tempId] : []
  654. });
  655. productImages.value = [];
  656. console.log('分类IDs:', formData.value.categoryIds);
  657. console.log('运费模板IDs:', formData.value.tempIds);
  658. // 等待DOM更新
  659. await nextTick();
  660. // 更新显示名称
  661. formData.value.categoryDisplayName = getCategoryDisplayName(formData.value.categoryIds);
  662. formData.value.tempName = formatterTemp(data.tempId);
  663. console.log('分类显示名称:', formData.value.categoryDisplayName);
  664. // 图片处理
  665. previewImages.value = data.image ? [{
  666. url: data.image
  667. }] : [];
  668. // 商品轮播图
  669. if (data.sliderImage) {
  670. try {
  671. const urlArr = typeof data.sliderImage === 'string' ? JSON.parse(data.sliderImage) : data
  672. .sliderImage;
  673. if (urlArr && urlArr.length > 0) {
  674. productImages.value = urlArr.map(url => ({
  675. url
  676. }));
  677. }
  678. } catch (error) {
  679. console.error('解析轮播图失败:', error);
  680. productImages.value = [];
  681. }
  682. }
  683. // 商品属性
  684. if (data.attrValue && data.attrValue.length > 0) {
  685. data.attr.forEach(item => {
  686. attr.value.push({
  687. attrName: item.attrName,
  688. attrValue: item.attrValues.split(',')
  689. })
  690. })
  691. data.attrValue.forEach(item => {
  692. let attrName = item.suk;
  693. attrTable.value.push({
  694. ...item,
  695. attrName: attrName,
  696. imgs: item?.image ? [{
  697. url: item.image
  698. }] : []
  699. })
  700. })
  701. appStore.SET_CPATTR(attr.value)
  702. }
  703. // 商品属性
  704. // if (data.attrValue && data.attrValue.length > 0) {
  705. // formData.value.additionalFee = data.attrValue[0].additionalAmount;
  706. // formData.value.laborCost = data.attrValue[0].price;
  707. // formData.value.barCode = data.attrValue[0].barCode;
  708. // formData.value.stock = data.attrValue[0].stock;
  709. // formData.value.weight = data.attrValue[0].weight;
  710. // // 规格图片
  711. // productImagesGg.value = data.attrValue[0].image ? [{
  712. // url: data.attrValue[0].image
  713. // }] : [];
  714. // }
  715. // 商品描述
  716. descriptionText.value = data.content ? data.content.replace(/<[^>]*>/g, '').substring(0, 500) : '';
  717. formData.value.content = data.content || '';
  718. // 设置分类选择器
  719. if (categoryRef.value && formData.value.categoryIds.length > 0) {
  720. // 使用nextTick确保组件已渲染
  721. await nextTick();
  722. if (categoryRef.value.setSelectedIds) {
  723. categoryRef.value.setSelectedIds(formData.value.categoryIds);
  724. }
  725. }
  726. // 重要:手动初始化表单验证状态
  727. await initFormValidation();
  728. } catch (error) {
  729. console.error('获取商品详情失败:', error);
  730. uni.showToast({
  731. title: '获取商品详情失败',
  732. icon: 'none'
  733. });
  734. }
  735. }
  736. // 运费模板id获取中文名
  737. function formatterTemp(id) {
  738. // 假设 tempColumns 是一个数组
  739. const foundItem = tempColumns.value[0].find(item => item.id == id);
  740. return foundItem ? foundItem.name : '';
  741. }
  742. function tempConfirm(obj) {
  743. formData.value.tempId = obj.value[0].id;
  744. formData.value.tempName = obj.value[0].name;
  745. showTemp.value = false;
  746. setTimeout(() => {
  747. if (formRef.value) {
  748. formRef.value.validateField('tempIds');
  749. }
  750. }, 100);
  751. }
  752. // 分类选择变化
  753. const onCategoryChange = (result) => {
  754. console.log('分类选择变化:', result);
  755. // 这里只更新显示,不直接更新表单数据,等用户点击确定
  756. }
  757. // 确认分类选择
  758. const confirmCategory = () => {
  759. if (categoryRef.value) {
  760. const selectedIds = categoryRef.value.getSelectedIds()
  761. if (selectedIds.length === 0) {
  762. uni.showToast({
  763. title: '请至少选择一个分类',
  764. icon: 'none'
  765. })
  766. return
  767. }
  768. // 更新表单数据
  769. formData.value.categoryIds = selectedIds
  770. formData.value.categoryDisplayName = getCategoryDisplayName(selectedIds)
  771. showCategory.value = false
  772. console.log('最终选中的分类ID:', selectedIds)
  773. // 触发校验
  774. setTimeout(() => {
  775. if (formRef.value) {
  776. formRef.value.validateField('categoryIds');
  777. }
  778. }, 100);
  779. }
  780. }
  781. // 根据选中的ID生成显示名称
  782. const getCategoryDisplayName = (selectedIds) => {
  783. if (!selectedIds || selectedIds.length === 0) return ''
  784. const names = []
  785. selectedIds.forEach(id => {
  786. // 检查是否是一级分类(表示全选)
  787. const firstLevel = categoryData.value.find(item => item.id == id)
  788. if (firstLevel) {
  789. // 如果是一级分类,显示"分类名称(全部)"
  790. names.push(`${firstLevel.name}`)
  791. } else {
  792. // 查找二级分类
  793. for (const parent of categoryData.value) {
  794. if (parent.child) {
  795. const secondLevel = parent.child.find(child => child.id == id)
  796. if (secondLevel) {
  797. names.push(`${parent.name}-${secondLevel.name}`)
  798. break
  799. }
  800. }
  801. }
  802. // 如果没有子分类的一级分类
  803. const singleLevel = categoryData.value.find(item =>
  804. !item.child && item.id == id
  805. )
  806. if (singleLevel) {
  807. names.push(singleLevel.name)
  808. }
  809. }
  810. })
  811. return names.join('、')
  812. }
  813. async function getImage() {
  814. console.log(imageList.value)
  815. if (imageList.value.length > 0) {
  816. if (imageList.value[0].status == "success") {
  817. previewImages.value = imageList.value;
  818. console.log('previewImages.value', previewImages.value)
  819. // change();
  820. } else {
  821. Toast({
  822. title: "上传失败"
  823. });
  824. }
  825. }
  826. imageList.value = [];
  827. }
  828. async function getImageProduct() {
  829. if (imageList.value.length > 0) {
  830. if (imageList.value[0].status == "success") {
  831. productImages.value = [...productImages.value, ...imageList.value];
  832. // change();
  833. } else {
  834. Toast({
  835. title: "上传失败"
  836. });
  837. }
  838. }
  839. imageList.value = [];
  840. }
  841. async function getImageProductGg(index) {
  842. if (imageList.value.length > 0) {
  843. if (imageList.value[0].status == "success") {
  844. console.log(imageList.value)
  845. if (index == "oneTemplate") {
  846. oneTemplate.value.imgs = imageList.value
  847. oneTemplate.value.image = imageList.value[0].info.url
  848. } else {
  849. attrTable.value[index].imgs = imageList.value
  850. attrTable.value[index].image = imageList.value[0].info.url
  851. }
  852. // productImagesGg.value = imageList.value;
  853. // change();
  854. } else {
  855. Toast({
  856. title: "上传失败"
  857. });
  858. }
  859. }
  860. imageList.value = [];
  861. }
  862. const onPreviewImageDelete = (e) => {
  863. previewImages.value.splice(e.index, 1);
  864. };
  865. const onProductImageDelete = (e) => {
  866. productImages.value.splice(e.index, 1);
  867. };
  868. const onProductImageGgDelete = (e) => {
  869. console.log(e)
  870. if (e == "oneTemplate") {
  871. oneTemplate.value.imgs = []
  872. } else {
  873. attrTable.value[e].imgs = []
  874. }
  875. productImagesGg.value.splice(e.index, 1);
  876. };
  877. // 编辑器相关方法
  878. const onEditorInput = (e) => {
  879. descriptionText.value = e.text || '';
  880. formData.value.content = e.html || '';
  881. };
  882. const onEditorInit = (editor) => {
  883. editorIns.value = editor;
  884. };
  885. const onOverMax = (e) => {
  886. uni.showToast({
  887. title: '内容长度超出限制',
  888. icon: 'none'
  889. });
  890. };
  891. const onUpinImage = (tempFiles, editorCtx) => {
  892. const filePath = tempFiles[0].tempFilePath || tempFiles[0].path;
  893. editorCtx.insertImage({
  894. src: filePath,
  895. width: '80%',
  896. success: () => {
  897. uni.showToast({
  898. title: '图片插入成功',
  899. icon: 'success'
  900. });
  901. }
  902. });
  903. };
  904. const validateForm = () => {
  905. try {
  906. if (!isProductCenter.value) {
  907. // 检查图片上传
  908. if (previewImages.value.length === 0) {
  909. uni.showToast({
  910. title: '请上传商品预览图',
  911. icon: 'none'
  912. });
  913. return;
  914. }
  915. if (productImages.value.length === 0) {
  916. uni.showToast({
  917. title: '请上传商品图片',
  918. icon: 'none'
  919. });
  920. return;
  921. }
  922. if (formData.value.sort && !/^\d+$/.test(formData.value.sort)) {
  923. uni.showToast({
  924. title: '排序号必须是整数',
  925. icon: 'none'
  926. });
  927. return;
  928. }
  929. }
  930. // if (productImagesGg.value.length === 0) {
  931. // uni.showToast({
  932. // title: '请上传商品规格图片',
  933. // icon: 'none'
  934. // });
  935. // return;
  936. // }
  937. // 验证数字字段
  938. // if (!/^\d+(\.\d+)?$/.test(formData.value.weight)) {
  939. // uni.showToast({
  940. // title: '重量格式不正确',
  941. // icon: 'none'
  942. // });
  943. // return;
  944. // }
  945. // if (!/^\d+(\.\d+)?$/.test(formData.value.laborCost)) {
  946. // uni.showToast({
  947. // title: '工费格式不正确',
  948. // icon: 'none'
  949. // });
  950. // return;
  951. // }
  952. // if (!/^\d+(\.\d+)?$/.test(formData.value.additionalFee)) {
  953. // uni.showToast({
  954. // title: '附加费格式不正确',
  955. // icon: 'none'
  956. // });
  957. // return;
  958. // }
  959. // if (!/^\d+$/.test(formData.value.stock)) {
  960. // uni.showToast({
  961. // title: '库存必须是整数',
  962. // icon: 'none'
  963. // });
  964. // return;
  965. // }
  966. // if (!/^\d+$/.test(formData.value.barCode)) {
  967. // uni.showToast({
  968. // title: '商品编号必须是整数',
  969. // icon: 'none'
  970. // });
  971. // return;
  972. // }
  973. const valid = formRef.value.validate();
  974. return valid;
  975. } catch (error) {
  976. console.error('表单验证失败:', error);
  977. return false;
  978. }
  979. };
  980. const validateAttr = (item) => {
  981. if (!item.image) {
  982. uni.showToast({
  983. title: '请上传商品规格图片',
  984. icon: 'none'
  985. });
  986. return false;
  987. }
  988. // 验证数字字段
  989. if (!/^\d+(\.\d+)?$/.test(item.weight)) {
  990. uni.showToast({
  991. title: '重量格式不正确',
  992. icon: 'none'
  993. });
  994. return false;
  995. }
  996. if (!/^\d+(\.\d+)?$/.test(item.price)) {
  997. uni.showToast({
  998. title: '工费格式不正确',
  999. icon: 'none'
  1000. });
  1001. return false;
  1002. }
  1003. if (!/^\d+(\.\d+)?$/.test(item.additionalAmount)) {
  1004. uni.showToast({
  1005. title: '附加费格式不正确',
  1006. icon: 'none'
  1007. });
  1008. return false;
  1009. }
  1010. if (!/^\d+$/.test(item.stock)) {
  1011. uni.showToast({
  1012. title: '库存必须是整数',
  1013. icon: 'none'
  1014. });
  1015. return false;
  1016. }
  1017. if (!/^\d+$/.test(item.barCode)) {
  1018. uni.showToast({
  1019. title: '商品编号必须是整数',
  1020. icon: 'none'
  1021. });
  1022. return false;
  1023. }
  1024. return true
  1025. }
  1026. const submitForm = async () => {
  1027. const valid = validateForm();
  1028. if (valid) {
  1029. const submitData = {
  1030. ...formData.value
  1031. };
  1032. let urlString = '';
  1033. console.log(previewImages.value[0])
  1034. if (isProductCenter.value) {
  1035. submitData.image = previewImages.value[0].url;
  1036. urlString = JSON.stringify(productImages.value.map(item => item.url));
  1037. } else {
  1038. submitData.image = previewImages.value[0]?.info?.url || previewImages.value[0]?.url;
  1039. urlString = JSON.stringify(productImages.value.map(item => item?.info?.url || item?.url));
  1040. }
  1041. submitData.sliderImage = urlString;
  1042. submitData.cateId = formData.value.categoryIds.join(',');
  1043. submitData.merchantId = parseInt(merchantInfo.id);
  1044. submitData.specType = true;
  1045. submitData.isSub = false;
  1046. // 规格长度为0
  1047. console.log(attr.value, attrTable.value)
  1048. if (!attr.value || attr.value.length <= 0 || !attrTable.value || attrTable.value.length <= 0) {
  1049. return Toast({
  1050. title: "请添加商品规格!"
  1051. });
  1052. }
  1053. let pass = true;
  1054. submitData.attr = attr.value.map(item => {
  1055. return {
  1056. attrName: item.attrName,
  1057. attrValues: item.attrValue.join(','),
  1058. }
  1059. })
  1060. submitData.attrValue = attrTable.value.map(item => {
  1061. if (!validateAttr(item)) {
  1062. pass = false;
  1063. }
  1064. console.log(typeof(item.attrValue))
  1065. return {
  1066. ...item,
  1067. id: 0,
  1068. productId: 0,
  1069. attrValue: typeof(item.attrValue) == 'object' ? JSON.stringify(item.attrValue) : item
  1070. .attrValue,
  1071. image: item.image || item.imgs[0]?.info?.url || item.imgs[0]?.url
  1072. }
  1073. })
  1074. console.dir(submitData)
  1075. if (!pass) return false;
  1076. // return false;
  1077. // submitData.attr = [{
  1078. // "attrName": "规格",
  1079. // "attrValues": "默认",
  1080. // "id": 0
  1081. // }];
  1082. // submitData.attrValue = [{
  1083. // additionalAmount: formData.value.additionalFee,
  1084. // attrValue: "{\"规格\":\"默认\"}",
  1085. // barCode: formData.value.barCode,
  1086. // image: productImagesGg.value[0]?.info?.url || productImagesGg.value[0].url,
  1087. // price: formData.value.laborCost,
  1088. // stock: formData.value.stock,
  1089. // weight: formData.value.weight
  1090. // }];
  1091. if (productId.value && !isProductCenter.value) {
  1092. const {
  1093. data
  1094. } = await productUpdate(submitData);
  1095. uni.showToast({
  1096. title: '修改成功',
  1097. icon: 'success'
  1098. });
  1099. } else {
  1100. const {
  1101. data
  1102. } = await productSave(submitData);
  1103. uni.showToast({
  1104. title: '发布成功',
  1105. icon: 'success'
  1106. });
  1107. }
  1108. // 编辑时,返回上个页面
  1109. if (!!productId.value && !isProductCenter.value) {
  1110. uni.navigateBack()
  1111. } else {
  1112. uni.redirectTo({
  1113. url: '/pages/merchantCenters/productManagement'
  1114. })
  1115. }
  1116. }
  1117. }
  1118. // 重置表单(用于新建场景)
  1119. const resetForm = () => {
  1120. formData.value = {
  1121. categoryIds: [],
  1122. categoryDisplayName: '',
  1123. tempName: '',
  1124. tempIds: [],
  1125. storeName: '',
  1126. keyword: '',
  1127. storeInfo: '',
  1128. unitName: '',
  1129. metalType: '',
  1130. weight: '',
  1131. laborCost: '',
  1132. additionalFee: '',
  1133. sort: '',
  1134. content: '',
  1135. stock: '',
  1136. barCode: ''
  1137. };
  1138. previewImages.value = [];
  1139. productImages.value = [];
  1140. productImagesGg.value = [];
  1141. descriptionText.value = '';
  1142. // 清除校验状态
  1143. if (formRef.value) {
  1144. formRef.value.clearValidate();
  1145. }
  1146. }
  1147. const editCatePage = () => {
  1148. if (attrTable.value && attrTable.value.length) {
  1149. historyAttr = []
  1150. historyTable = {}
  1151. // 防止刷新数据时已填数据丢失
  1152. attrTable.value.forEach(item => {
  1153. historyAttr.push(item.attrName)
  1154. historyTable[item.attrName] = {
  1155. ...item
  1156. }
  1157. })
  1158. }
  1159. console.log(historyAttr, historyTable)
  1160. uni.navigateTo({
  1161. url: "/pages/merchantCenters/productCate/productCate"
  1162. })
  1163. }
  1164. const setAllAttr = () => {
  1165. if (!validateAttr(oneTemplate.value)) return false;
  1166. uni.showModal({
  1167. title: '提示',
  1168. content: '所有商品规格将修改为当前配置',
  1169. confirmText: '确认',
  1170. cancelText: "取消",
  1171. success(res) {
  1172. if (res.confirm) {
  1173. console.log(oneTemplate.value)
  1174. console.log("确认了", attrTable.value);
  1175. if (attrTable.value && attrTable.value.length > 0) {
  1176. attrTable.value = attrTable.value.map(item => {
  1177. return {
  1178. ...item,
  1179. ...oneTemplate.value
  1180. }
  1181. })
  1182. }
  1183. }
  1184. }
  1185. })
  1186. }
  1187. function attrFormat(arr) {
  1188. // console.log('arr', arr)
  1189. let data = [];
  1190. const res = [];
  1191. return format(arr);
  1192. function format(arr) {
  1193. console.log("attrFormat", arr)
  1194. if (arr.length > 1) {
  1195. arr.forEach((v, i) => {
  1196. if (i === 0) data = arr[i]["attrValue"];
  1197. const tmp = [];
  1198. if (!data) return;
  1199. data.forEach(function(vv) {
  1200. arr[i + 1] &&
  1201. arr[i + 1]["attrValue"] &&
  1202. arr[i + 1]["attrValue"].forEach(g => {
  1203. const rep2 =
  1204. (i !== 0 ? "" : arr[i]["attrName"] + "_") +
  1205. vv +
  1206. "$&" +
  1207. arr[i + 1]["attrName"] +
  1208. "_" +
  1209. g;
  1210. tmp.push(rep2);
  1211. if (i === arr.length - 2) {
  1212. const rep4 = {
  1213. additionalAmount: "",
  1214. image: "",
  1215. price: "",
  1216. cost: 0,
  1217. otPrice: 0,
  1218. stock: "",
  1219. barCode: "",
  1220. weight: "",
  1221. volume: 0,
  1222. brokerage: 0,
  1223. brokerage_two: 0,
  1224. imgs: []
  1225. };
  1226. rep2.split("$&").forEach((h, k) => {
  1227. const rep3 = h.split("_");
  1228. if (!rep4["attrValue"]) rep4["attrValue"] = {};
  1229. rep4["attrValue"][rep3[0]] =
  1230. rep3.length > 1 ? rep3[1] : "";
  1231. });
  1232. for (const attrValueKey in rep4.attrValue) {
  1233. rep4[attrValueKey] = rep4.attrValue[attrValueKey];
  1234. }
  1235. res.push(rep4);
  1236. }
  1237. });
  1238. });
  1239. data = tmp.length ? tmp : [];
  1240. });
  1241. } else {
  1242. const dataArr = [];
  1243. arr.forEach((v, k) => {
  1244. // console.log("v['attrValue']", v['attrValue'])
  1245. v["attrValue"] &&
  1246. v["attrValue"].forEach((vv, kk) => {
  1247. dataArr[kk] = v["attrName"] + "_" + vv;
  1248. res[kk] = {
  1249. additionalAmount: "",
  1250. image: "",
  1251. price: "",
  1252. cost: 0,
  1253. otPrice: 0,
  1254. stock: "",
  1255. barCode: "",
  1256. weight: "",
  1257. volume: 0,
  1258. brokerage: 0,
  1259. brokerage_two: 0,
  1260. attrValue: {
  1261. [v["attrName"]]: vv
  1262. },
  1263. imgs: []
  1264. };
  1265. // Object.values(res[kk].attrValue).forEach((v, i) => {
  1266. // res[kk]['value' + i] = v
  1267. // })
  1268. for (const attrValueKey in res[kk].attrValue) {
  1269. res[kk][attrValueKey] = res[kk].attrValue[attrValueKey];
  1270. }
  1271. });
  1272. });
  1273. data.push(dataArr.join("$&"));
  1274. }
  1275. // console.log(res)
  1276. const result = []
  1277. res.forEach(item => {
  1278. let attrName = [];
  1279. for (const name in item.attrValue) {
  1280. attrName.push(item.attrValue[name]);
  1281. }
  1282. item.attrName = attrName.join(',');
  1283. // 防止刷新数据时已填数据丢失
  1284. if (historyAttr.includes(item.attrName)) {
  1285. console.log(historyAttr, item.attrName, historyTable[item.attrName])
  1286. item = {
  1287. ...historyTable[item.attrName]
  1288. }
  1289. }
  1290. result.push(item)
  1291. console.log("res-item==>", item)
  1292. })
  1293. return result;
  1294. }
  1295. }
  1296. // 校验输入框不能输入0,保留2位小数,库存为正整数
  1297. const keyupEvent = (key, val, index, num) => {
  1298. // console.log(key, val, index, num)
  1299. if (key === "barCode") return;
  1300. var re = /^\D*([0-9]\d*\.?\d{0,2})?.*$/;
  1301. switch (num) {
  1302. case 1:
  1303. if (Number(val) === 0) {
  1304. oneTemplate.value[key] = [
  1305. "stock",
  1306. "cost",
  1307. "otPrice",
  1308. "price",
  1309. "additionalAmount"
  1310. ].includes(key) ?
  1311. 0 :
  1312. 0.01;
  1313. } else {
  1314. oneTemplate.value[key] =
  1315. key === "stock" ?
  1316. parseInt(val) :
  1317. val.toString().replace(re, "$1")
  1318. }
  1319. break;
  1320. case 2:
  1321. if (Number(val) === 0) {
  1322. attrTable.value[index][key] = [
  1323. "stock",
  1324. "cost",
  1325. "otPrice",
  1326. "price",
  1327. "additionalAmount"
  1328. ].includes(key) ?
  1329. 0 :
  1330. 0.01;
  1331. } else {
  1332. attrTable.value[index][key] =
  1333. key === "stock" ?
  1334. parseInt(val) :
  1335. val.toString().replace(re, "$1")
  1336. }
  1337. break;
  1338. default:
  1339. break
  1340. }
  1341. }
  1342. </script>
  1343. <style scoped lang="scss">
  1344. .container {
  1345. background-color: #f9f7f0;
  1346. min-height: 100vh;
  1347. padding-bottom: 140rpx;
  1348. }
  1349. .form-container {
  1350. padding: 16rpx;
  1351. }
  1352. .upload-section {
  1353. margin-top: 20rpx;
  1354. margin-bottom: 20rpx;
  1355. background: white;
  1356. border-radius: 16rpx;
  1357. padding: 30rpx;
  1358. }
  1359. .upload-item {
  1360. margin-bottom: 40rpx;
  1361. &:last-child {
  1362. margin-bottom: 0;
  1363. }
  1364. }
  1365. .upload-label {
  1366. display: block;
  1367. font-size: 28rpx;
  1368. color: #333;
  1369. margin-bottom: 20rpx;
  1370. position: relative;
  1371. }
  1372. .required {
  1373. position: absolute;
  1374. left: -9px;
  1375. color: #f56c6c;
  1376. line-height: 20px;
  1377. font-size: 20px;
  1378. top: 3px;
  1379. }
  1380. .upload-btn {
  1381. display: flex;
  1382. flex-direction: column;
  1383. align-items: center;
  1384. justify-content: center;
  1385. width: 5rem;
  1386. height: 5rem;
  1387. border: 1rpx dashed #DCDFE6;
  1388. border-radius: 8rpx;
  1389. background: #fff;
  1390. }
  1391. .upload-tip {
  1392. font-size: 20rpx;
  1393. color: #666;
  1394. margin-top: 6rpx;
  1395. }
  1396. .format-tip {
  1397. display: block;
  1398. font-size: 24rpx;
  1399. color: #999;
  1400. margin-top: 15rpx;
  1401. }
  1402. .card-title {
  1403. font-size: 36rpx;
  1404. color: #333;
  1405. line-height: 60rpx;
  1406. margin-bottom: 20rpx;
  1407. }
  1408. :deep(.u-form-item) {
  1409. background-color: #fff;
  1410. border-radius: 16rpx;
  1411. padding: 8rpx 30rpx;
  1412. box-sizing: border-box;
  1413. margin-bottom: 20rpx;
  1414. }
  1415. :deep(.u-form-item__body__left__content__label) {
  1416. font-size: 28rpx !important;
  1417. color: #333 !important;
  1418. }
  1419. :deep(.u-radio-group--row) {
  1420. justify-content: flex-end;
  1421. }
  1422. .btn-view {
  1423. position: fixed;
  1424. bottom: 0;
  1425. left: 0;
  1426. right: 0;
  1427. background-color: #FFF;
  1428. padding: 20rpx 30rpx;
  1429. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.1);
  1430. z-index: 5;
  1431. }
  1432. .submit {
  1433. height: 88rpx;
  1434. line-height: 88rpx;
  1435. background: #F8C008;
  1436. border-radius: 16rpx;
  1437. font-size: 32rpx;
  1438. font-weight: bold;
  1439. color: #333333;
  1440. border: none;
  1441. width: 100%;
  1442. &:active {
  1443. opacity: 0.8;
  1444. }
  1445. }
  1446. .unit {
  1447. color: #999;
  1448. font-size: 28rpx;
  1449. margin-left: 10rpx;
  1450. }
  1451. /* 编辑器样式 */
  1452. .editor-section {
  1453. background: white;
  1454. border-radius: 16rpx;
  1455. margin-bottom: 20rpx;
  1456. overflow: hidden;
  1457. }
  1458. .editor-header {
  1459. display: flex;
  1460. justify-content: space-between;
  1461. align-items: center;
  1462. padding: 30rpx;
  1463. border-bottom: 1rpx solid #f0f0f0;
  1464. }
  1465. .editor-label {
  1466. font-size: 28rpx;
  1467. color: #333;
  1468. font-weight: 500;
  1469. }
  1470. .word-count {
  1471. font-size: 24rpx;
  1472. color: #999;
  1473. }
  1474. :deep(.sp-editor) {
  1475. min-height: 400rpx;
  1476. }
  1477. /* 弹窗样式 */
  1478. .popup-content {
  1479. background: #fff;
  1480. border-radius: 20rpx 20rpx 0 0;
  1481. padding-bottom: env(safe-area-inset-bottom);
  1482. }
  1483. .popup-header {
  1484. padding: 30rpx;
  1485. text-align: center;
  1486. border-bottom: 1rpx solid #f0f0f0;
  1487. position: relative;
  1488. }
  1489. .popup-title {
  1490. font-size: 32rpx;
  1491. font-weight: 600;
  1492. color: #333;
  1493. }
  1494. .popup-actions {
  1495. display: flex;
  1496. padding: 30rpx;
  1497. gap: 20rpx;
  1498. }
  1499. .action-btn {
  1500. flex: 1;
  1501. height: 80rpx;
  1502. line-height: 80rpx;
  1503. border-radius: 12rpx;
  1504. font-size: 28rpx;
  1505. border: none;
  1506. &.cancel {
  1507. background: #f0f0f0;
  1508. color: #666;
  1509. }
  1510. &.confirm {
  1511. background: #F8C008;
  1512. color: #333;
  1513. }
  1514. }
  1515. :deep(.u-tabs__wrapper__nav__line) {
  1516. background-color: #F8C008 !important;
  1517. }
  1518. :deep(.u-form-item__body__right__message) {
  1519. text-align: right;
  1520. }
  1521. :deep(.u-textarea__field) {
  1522. text-align: right;
  1523. }
  1524. .pro-cate-container {
  1525. width: 100%;
  1526. padding: 16rpx;
  1527. background-color: #fff;
  1528. border-radius: 16rpx;
  1529. margin-bottom: 16rpx;
  1530. .cate-title {
  1531. font-size: 28rpx;
  1532. line-height: 44rpx;
  1533. color: #333;
  1534. margin-bottom: 16rpx;
  1535. }
  1536. .cate-add {
  1537. width: 100%;
  1538. height: 88rpx;
  1539. line-height: 88rpx;
  1540. text-align: center;
  1541. border-radius: 16rpx;
  1542. background-color: #FEF8E6;
  1543. color: #F8C008;
  1544. font-size: 32rpx;
  1545. font-weight: bold;
  1546. margin-top: 16rpx;
  1547. }
  1548. .cate-list {
  1549. .cate-item {
  1550. width: 100%;
  1551. background-color: #F9F7F0;
  1552. border-radius: 16rpx;
  1553. padding: 16rpx;
  1554. margin-bottom: 16rpx;
  1555. .upload-btn {
  1556. display: flex;
  1557. flex-direction: column;
  1558. align-items: center;
  1559. justify-content: center;
  1560. width: 100rpx;
  1561. height: 100rpx;
  1562. border: 1rpx dashed #DCDFE6;
  1563. border-radius: 8rpx;
  1564. background: #fff;
  1565. }
  1566. .cate-pic-line {
  1567. width: 100%;
  1568. height: 100rpx;
  1569. display: flex;
  1570. justify-content: space-between;
  1571. align-items: center;
  1572. margin-bottom: 16rpx;
  1573. ::v-deep .u-upload {
  1574. flex: 0;
  1575. margin-right: 16rpx;
  1576. width: 100rpx;
  1577. height: 100rpx;
  1578. }
  1579. ::v-deep .u-upload__wrap__preview {
  1580. margin: 0;
  1581. width: 100rpx;
  1582. height: 100rpx;
  1583. }
  1584. ::v-deep .u-upload__wrap__preview__image {
  1585. width: 100rpx !important;
  1586. height: 100rpx !important;
  1587. }
  1588. .cate-pic {
  1589. width: 100rpx;
  1590. height: 100rpx;
  1591. border-radius: 8rpx;
  1592. background-color: #fff;
  1593. margin-right: 16rpx;
  1594. }
  1595. .cate-name {
  1596. width: 100rpx;
  1597. flex: 1;
  1598. color: #333;
  1599. font-size: 28rpx;
  1600. line-height: 44rpx;
  1601. }
  1602. }
  1603. .cate-ipt-line {
  1604. width: 100%;
  1605. display: flex;
  1606. align-items: center;
  1607. justify-content: space-between;
  1608. flex-wrap: nowrap;
  1609. padding-bottom: 16rpx;
  1610. ::v-deep .u-input {
  1611. height: 100rpx;
  1612. line-height: 100rpx;
  1613. }
  1614. .cate-ipt {
  1615. width: 48%;
  1616. flex: 1;
  1617. height: 100rpx;
  1618. background-color: #fff;
  1619. border-radius: 16rpx;
  1620. padding: 0 16rpx !important;
  1621. font-size: 28rpx;
  1622. .unit {
  1623. color: #333;
  1624. }
  1625. }
  1626. }
  1627. }
  1628. }
  1629. }
  1630. </style>