specs.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. <template>
  2. <view class="content_specs">
  3. <view class="main_specs">
  4. <view class="title_specs box-pack-between">
  5. <text>常用规格</text>
  6. <view size="mini" type="primary" class="SetupComplete flex-center" @click="Finish">设置完成</view>
  7. </view>
  8. <view class="flex wrap">
  9. <view class="item" v-for="(item,index) of defaultList" :key='index' @click="regular(item)">
  10. {{item}}
  11. </view>
  12. </view>
  13. </view>
  14. <view class="main_specs">
  15. <view class="title_specs">
  16. <text class="red">*</text>我的规格
  17. </view>
  18. <view class="box-pack-between">
  19. <view class="flex wrap">
  20. <view class="item" v-for="(item,index) of mySpecList" :key='index'>
  21. {{item.title}}
  22. <view class='del flex-center' @click="del(index,item.id)"> <text class="iconfont iconquchu"></text> </view>
  23. </view>
  24. </view>
  25. <view>
  26. <view @click="addClickSpec" v-if="!is_spec" class="addSpec flex-center">
  27. 添加规格值
  28. </view>
  29. <block v-else>
  30. <form @submit="addClickFinish">
  31. <view class="flex flex-y-center addcontent">
  32. <input class="tc" type="text" confirm-type="done" @confirm="onConfirm" name='name'
  33. v-model="spec_name" placeholder-class="placeholderinput" placeholder="请输入规格" />
  34. <button form-type="submit" class="addgou flex-center"
  35. style="background: none;max-width: 60rpx;">
  36. <image src="../static/images/gou.png"></image>
  37. </button>
  38. </view>
  39. </form>
  40. </block>
  41. </view>
  42. </view>
  43. </view>
  44. <view class="main_specs">
  45. <view class="title_specs">
  46. <text class="red">*</text>设置规格值
  47. </view>
  48. <view class="box-pack-between main_specs_item" v-for="(item,index) of mySpecList" :key='index'>
  49. <view class="flex flex-y-center wrap">
  50. <text class="item-title">{{item.title}}</text>
  51. <view class="item" v-if="item.arr" v-for="(spec,idx) of item.arr" :key='idx'>
  52. {{spec.title}}
  53. <view @click="delChildren(index,idx,spec.id)" class='del flex-center'> <text class="iconfont iconquchu"></text> </view>
  54. </view>
  55. </view>
  56. <view>
  57. <view @click="addClickSpecChild(index)" v-if="!item.is_spec_children" class="addSpec flex-center">
  58. 添加规格值
  59. </view>
  60. <block v-else>
  61. <form @submit="addClickFinishChild" :data-index='index' :data-id='item.id'>
  62. <view class="flex flex-y-center addcontent">
  63. <input class="tc" v-model="item.spec_name_child" :data-index='index' :data-id='item.id' @confirm="onConfirmChild" type="text" name='name' placeholder="请输入规格"
  64. placeholder-class="placeholderinput" />
  65. <button form-type="submit" class="addgou flex-center"
  66. style="background: none;max-width: 60rpx;">
  67. <image src="../static/images/gou.png"></image>
  68. </button>
  69. </view>
  70. </form>
  71. </block>
  72. </view>
  73. </view>
  74. </view>
  75. <view class="main_specs">
  76. <view class="title_specs box-pack-between">
  77. <view class="flex-y-center">
  78. <text class="red">*</text><text>设置规格详情</text>
  79. </view>
  80. <view class="flex-y-center" @click="enit(null)">一键设置 <image class="enit" src="../static/images/enit.png"></image> </view>
  81. </view>
  82. <view class="flex main_detail box-pack-between" v-for="(item,index) of tableList" :key='index'>
  83. <view class="flex">
  84. <!-- <view class="main_round"></view> -->
  85. <view class="main_item">
  86. <view class="main_title">
  87. {{item.spec}}
  88. </view>
  89. <view class="">
  90. 售价:{{item.growth}}, 库存:{{item.inventory}}
  91. </view>
  92. </view>
  93. </view>
  94. <view class="flex-y-center" @click="enit(index)">
  95. 编辑 <image class="enit" src="../static/images/enit.png"></image>
  96. </view>
  97. </view>
  98. </view>
  99. <uni-popup ref="popup" type="center">
  100. <view class="popup-content">
  101. <form @submit="setup">
  102. <view class="popup-content-setup">设置</view>
  103. <view class="flex-center popup-content-title">
  104. <text class="ft26">售价:</text>
  105. <view class="popup-content-price">
  106. <input type="digit" name='growth' placeholder="请输入价格" />
  107. </view>
  108. </view>
  109. <view class="flex-center popup-content-title">
  110. <text class="ft26">库存:</text>
  111. <view class="popup-content-price">
  112. <input type="number" name='inventory' placeholder="请输入库存"/>
  113. </view>
  114. </view>
  115. <button form-type="submit" class="btn" type="default">确定</button>
  116. </form>
  117. </view>
  118. </uni-popup>
  119. </view>
  120. </template>
  121. <script>
  122. export default {
  123. data() {
  124. return {
  125. defaultList: ['规格','口味','主食','套餐','款式', '尺寸', '颜色'],
  126. mySpecList: [],
  127. is_spec: false,
  128. is_spec_children:false,
  129. spec_name_child:'',
  130. spec_name:'',
  131. edit_value: null,
  132. tableList:[],
  133. set_index:null,
  134. id:null,
  135. }
  136. },
  137. onLoad(e) {
  138. if(e.id){
  139. this.id = e.id=='spec'?'':e.id;
  140. let {edit_value,spec,spec_val} = uni.getStorageSync('obj'+ e.id)
  141. console.log(uni.getStorageSync('obj'+ e.id))
  142. spec.forEach((v)=>{
  143. v.is_spec_children = false
  144. v.spec_name_child=''
  145. })
  146. this.edit_value = edit_value
  147. this.mySpecList = spec
  148. this.tableList = spec_val
  149. }
  150. },
  151. methods: {
  152. // 删除父规格
  153. del(index,id){
  154. this.request({
  155. url: "Smdcshop/delspec",
  156. data:{
  157. edit_value:this.edit_value,
  158. id,
  159. goods_id:this.id||'',
  160. },
  161. }).then(res => {
  162. if (res.code == '200') {
  163. this.edit_value = res.data;
  164. this.mySpecList.splice(index,1);
  165. this.setTableList();
  166. }
  167. }).catch((res) => {
  168. uni.showToast({
  169. title: res.message,
  170. icon: 'none'
  171. })
  172. });
  173. },
  174. // 删除子规格
  175. delChildren(index,idx,id){
  176. this.request({
  177. url: "Smdcshop/delspecson",
  178. data:{
  179. edit_value:this.edit_value,
  180. id,
  181. goods_id:this.id||'',
  182. },
  183. }).then(res => {
  184. if (res.code == '200') {
  185. this.edit_value = res.data;
  186. this.mySpecList[index].arr.splice(idx,1);
  187. this.setTableList();
  188. }
  189. }).catch((res) => {
  190. uni.showToast({
  191. title: res.message,
  192. icon: 'none'
  193. })
  194. });
  195. },
  196. // 完成
  197. Finish(e){
  198. if(!this.tableList.length){
  199. uni.showToast({
  200. title: "请设置规格值",
  201. icon: "none"
  202. })
  203. return
  204. }
  205. let hasGoodStudent = this.mySpecList.some((v, i) => v.arr.length <= 0)
  206. // console.log(hasGoodStudent); // true
  207. if(hasGoodStudent){
  208. uni.showToast({
  209. title: "规格缺少规格值",
  210. icon: "none"
  211. })
  212. return
  213. }
  214. let hasGoodStudentType = this.tableList.some((v, i) => Number(v.growth) <= 0)
  215. // console.log(hasGoodStudentType); // true
  216. if(hasGoodStudentType){
  217. uni.showToast({
  218. title: "库存价格不能小于0",
  219. icon: "none"
  220. })
  221. return
  222. }
  223. let pages = getCurrentPages(); //获取所有页面栈实例列表
  224. let nowPage = pages[pages.length - 1]; //当前页页面实例
  225. let prevPage = pages[pages.length - 2]; //上一页页面实例
  226. prevPage.$vm.tableList = JSON.stringify(this.tableList); //修改上一页data里面的sh参数值为100
  227. prevPage.$vm.edit_value = this.edit_value;
  228. prevPage.$vm.mySpecList = JSON.stringify(this.mySpecList);
  229. uni.navigateBack({
  230. delta: 1
  231. })
  232. },
  233. enit(index){
  234. if(!this.tableList.length && index==null){
  235. uni.showToast({
  236. title: "请设置规格",
  237. icon: "none"
  238. })
  239. return
  240. }
  241. this.set_index = index;
  242. this.$refs.popup.open();
  243. },
  244. // 设置价格,库存
  245. setup(e){
  246. let {growth,inventory} = e.detail.value
  247. if(this.set_index == null){
  248. this.tableList.forEach((v)=>{
  249. v.growth = growth
  250. v.inventory = inventory
  251. })
  252. }else{
  253. this.tableList[this.set_index].growth = growth
  254. this.tableList[this.set_index].inventory = inventory
  255. }
  256. this.$refs.popup.close();
  257. },
  258. addClickSpec() {
  259. this.is_spec = !this.is_spec
  260. },
  261. addClickSpecChild(index) {
  262. this.mySpecList[index].is_spec_children = !this.mySpecList[index].is_spec_children
  263. },
  264. // 常用规格
  265. regular(e){
  266. let data = {
  267. value: e,
  268. edit_value: this.edit_value,
  269. }
  270. this.addspec(data)
  271. },
  272. // 新增父规格
  273. onConfirm({detail}){
  274. if(!detail.value){
  275. uni.showToast({
  276. title: "请输入规格",
  277. icon: "none"
  278. })
  279. return
  280. }
  281. let data = {
  282. value: detail.value,
  283. edit_value: this.edit_value,
  284. }
  285. this.addspec(data)
  286. },
  287. addClickFinish({
  288. detail
  289. }) {
  290. if(!detail.value.name){
  291. uni.showToast({
  292. title: "请输入规格",
  293. icon: "none"
  294. })
  295. return
  296. }
  297. let data = {
  298. value: detail.value.name,
  299. edit_value: this.edit_value,
  300. }
  301. this.addspec(data)
  302. },
  303. addspec(data){
  304. this.request({
  305. url: "Smdcshop/addspec",
  306. data,
  307. }).then(res => {
  308. if (res.code == '200') {
  309. this.mySpecList.push({
  310. title: data.value,
  311. id:res.data.id,
  312. is_spec_children:false,
  313. arr:[],
  314. })
  315. this.edit_value = res.data.edit_value
  316. this.is_spec = false
  317. this.spec_name='';
  318. }
  319. }).catch((res) => {
  320. uni.showToast({
  321. title: res.message,
  322. icon: 'none'
  323. })
  324. });
  325. },
  326. // 新增子规格
  327. onConfirmChild({detail,currentTarget}){
  328. let {index,id} = currentTarget.dataset;
  329. if(!detail.value){
  330. uni.showToast({
  331. title: "请输入规格值",
  332. icon: "none"
  333. })
  334. return
  335. }
  336. let data = {
  337. value: detail.value,
  338. edit_value: this.edit_value,
  339. id,
  340. }
  341. this.addspecson(data,index);
  342. },
  343. addClickFinishChild({detail,currentTarget}) {
  344. let {index,id} = currentTarget.dataset;
  345. if(!detail.value.name){
  346. uni.showToast({
  347. title: "请输入规格值",
  348. icon: "none"
  349. })
  350. return
  351. }
  352. let data = {
  353. value: detail.value.name,
  354. edit_value: this.edit_value,
  355. id,
  356. }
  357. this.addspecson(data,index)
  358. },
  359. addspecson(data,index){
  360. this.request({
  361. url: "Smdcshop/addspecson",
  362. data,
  363. }).then(res => {
  364. if (res.code == '200') {
  365. this.mySpecList[index].arr.push({
  366. title:data.value,
  367. id:res.data.id
  368. })
  369. this.edit_value = res.data.edit_value
  370. this.mySpecList[index].spec_name_child=''
  371. this.setTableList();
  372. }
  373. }).catch((res) => {
  374. uni.showToast({
  375. title: res.message,
  376. icon: 'none'
  377. })
  378. });
  379. },
  380. /**
  381. * 设置表格数据 该方法在新增规格、新增规格值、规格和规格值的文本改变都要调用
  382. */
  383. setTableList(){
  384. let newList=[];
  385. this.getRowList(null,0,newList)
  386. this.tableList = newList;
  387. // console.log(this.tableList)
  388. },
  389. /**
  390. * 获取行数据 mySpecList规格集合 tableList表格集合
  391. * @param spec 多规格值通过逗号拼接组成每一行唯一值
  392. * @param specIndex 规格索引
  393. * @param newList 新表格数据
  394. */
  395. getRowList(spec,specIndex,newList){
  396. if(specIndex < this.mySpecList.length){
  397. this.mySpecList[specIndex].arr.forEach((t)=>{
  398. let newValues=(spec===null?t.title:(spec+":"+t.title))
  399. //当前规格值,一直往下规格值循环完
  400. this.getRowList(newValues,specIndex+1,newList)
  401. })
  402. //当添加新规格(第二个规格的时候)没有规格值时,默认一个空值
  403. if(this.mySpecList[specIndex].arr.length===0 && spec!==null){
  404. let newValues = spec+":"
  405. this.getRowList(newValues,specIndex+1,newList)
  406. }
  407. }
  408. //当最后规格的规格值循环到最后后添加行数据
  409. else{
  410. //判断表格行数据是否已经存在,存在直接添加不创建新行
  411. let row = this.tableList.find((r)=>{return r.spec===spec});
  412. // console.log(row,"row")
  413. if(row){
  414. newList.push({...row})
  415. }
  416. else{
  417. //默认的字段
  418. row = {
  419. spec:spec,
  420. growth:0,
  421. inventory:0,
  422. vipprice:0
  423. }
  424. //通过规格值循环出规格值列数
  425. // spec.split(':').forEach((c,index)=>{
  426. // row["sku-"+index]=c
  427. // })
  428. newList.push(row);
  429. }
  430. }
  431. },
  432. },
  433. }
  434. </script>
  435. <style lang="scss" scoped>
  436. .content_specs {
  437. background: #F1F1F1;
  438. }
  439. .tc{
  440. text-align: center;
  441. }
  442. .enit {
  443. width: 40rpx;
  444. height: 40rpx
  445. }
  446. .popup-content{
  447. background: #FFFFFF;
  448. width: 600rpx;
  449. height: 450rpx;
  450. padding: 20rpx;
  451. border-radius: 10rpx;
  452. .popup-content-setup{
  453. text-align: center;
  454. padding-bottom: 20rpx;
  455. }
  456. .popup-content-title{
  457. padding-bottom: 20rpx;
  458. .popup-content-price{
  459. padding: 15rpx;
  460. border: 1rpx solid #F1F1F1;
  461. }
  462. }
  463. .btn {
  464. width: 400rpx;
  465. height: 80rpx;
  466. background: linear-gradient(315deg, #3387ff 2%, #569bff 99%);
  467. border-radius: 8rpx;
  468. box-shadow: 0px 0px 8rpx 0px rgba(51, 135, 255, 0.2);
  469. font-size: 32rpx;
  470. font-weight: 500;
  471. text-align: center;
  472. color: #fff;
  473. line-height: 80rpx;
  474. margin: 0 auto;
  475. margin-top:50rpx;
  476. margin-bottom: 30rpx;
  477. }
  478. }
  479. .SetupComplete{
  480. padding: 10rpx 20rpx;
  481. height: 60rpx;
  482. background: linear-gradient(315deg, #3387ff 2%, #569bff 99%);
  483. border-radius: 8rpx;
  484. box-shadow: 0px 0px 8rpx 0px rgba(51, 135, 255, 0.2);
  485. font-size: 32rpx;
  486. font-weight: 500;
  487. text-align: center;
  488. color: #fff;
  489. }
  490. .content_specs {
  491. .main_specs {
  492. padding: 20rpx 30rpx;
  493. background: #fff;
  494. margin-bottom: 1rpx;
  495. .title_specs {
  496. font-size: 30rpx;
  497. padding: 10rpx 0;
  498. margin-bottom: 20rpx;
  499. }
  500. .main_specs_item {
  501. margin-bottom: 20rpx;
  502. }
  503. }
  504. .main_detail {
  505. font-size: 26rpx;
  506. margin-bottom: 20rpx;
  507. border-bottom: 1px solid #F1F1F1;
  508. padding-bottom: 10rpx;
  509. .main_round {
  510. width: 100rpx;
  511. height: 100rpx;
  512. border: 1px dashed #ccc;
  513. margin-right: 20rpx;
  514. }
  515. .main_item {
  516. margin-left: 25rpx;
  517. .main_title {
  518. margin-bottom: 20rpx;
  519. max-width: 450rpx;
  520. }
  521. }
  522. }
  523. }
  524. .item-title {
  525. font-size: 28rpx;
  526. margin-right: 30rpx;
  527. }
  528. .item {
  529. position: relative;
  530. border: 1px solid #ccc;
  531. border-radius: 50rpx;
  532. padding: 10rpx 20rpx;
  533. font-size: 26rpx;
  534. margin-right: 30rpx;
  535. margin-bottom: 4rpx;
  536. .del {
  537. position: absolute;
  538. right: 0;
  539. top: -6rpx;
  540. width: 30rpx;
  541. height: 30rpx;
  542. background: #DD524D;
  543. border-radius: 100rpx;
  544. color: #FFFFFF;
  545. .iconquchu {
  546. font-size: 14rpx;
  547. }
  548. }
  549. }
  550. .addSpec {
  551. font-size: 26rpx;
  552. color: #000000;
  553. border-radius: 40px;
  554. border: 1rpx dashed #ccc;
  555. min-width: 180rpx;
  556. padding: 10rpx 20rpx;
  557. }
  558. .addcontent {
  559. border-radius: 40px;
  560. font-size: 26rpx;
  561. max-width: 230rpx;
  562. min-width: 230rpx;
  563. border: 1rpx solid #DD524D;
  564. padding-left: 10rpx;
  565. height: 68rpx;
  566. .addgou {
  567. height: 100%;
  568. image {
  569. width: 37rpx;
  570. height: 29rpx;
  571. margin-bottom: 8rpx;
  572. }
  573. }
  574. }
  575. .red {
  576. color: red;
  577. }
  578. .main_specs button {
  579. // display: block !important;
  580. background: none !important;
  581. height: 100% !important;
  582. width: 100% !important;
  583. border: none !important;
  584. border-radius: 0 !important;
  585. padding: 0 !important;
  586. margin: 0 !important;
  587. line-height: inherit !important;
  588. font-family: inherit !important;
  589. color: inherit !important;
  590. text-align: inherit;
  591. }
  592. .main_specs button:before,
  593. .main_specs button:after {
  594. border-radius: 0 !important;
  595. border: none !important;
  596. }
  597. .placeholderinput{
  598. font-size: 20rpx;
  599. }
  600. </style>