test.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. <template>
  2. <view class="u-wrap ">
  3. <view class="flex bg-white" style="padding: 30rpx 10rpx;">
  4. <view class="" style="width: 20%;">
  5. <u-button @click="operateCategory(null)" size="mini" type="warning" plain
  6. class=" text-sm text-red flex text-bold">
  7. <text>添加分类</text>
  8. <text class="cuIcon-add u-m-l-4 center"></text>
  9. </u-button>
  10. </view>
  11. <view @click="operateGoods(null)" class="center bg-white" style="width: 80%;color: #007AFF;">
  12. <text class="cuIcon-roundadd margin-right-10"></text>
  13. <text>添加【{{goodsList[current].goodsCategory.name}}】分类下商品</text>
  14. </view>
  15. </view>
  16. <view class="u-menu-wrap">
  17. <scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
  18. <view v-for="(item,index) in goodsList" :key="index" class="u-tab-item"
  19. :class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
  20. @tap.stop="swichMenu(index)">
  21. <view class="">
  22. <text class="u-line-1 u-m-r-4" :style="!item.goodsCategory.showStatus?'text-decoration: line-through;color: #bababa':''">{{item.goodsCategory.name}}</text>
  23. <u-icon color="#EF9944" @click="operateCategory(item.goodsCategory)" name="edit-pen-fill">
  24. </u-icon>
  25. </view>
  26. </view>
  27. </scroll-view>
  28. <block v-for="(item,index) in goodsList" :key="index">
  29. <scroll-view scroll-y class="right-box" v-if="current==index">
  30. <view class="page-view">
  31. <view class="class-item">
  32. <view class="item-title">
  33. <text>{{item.goodsCategory.name}}</text>
  34. </view>
  35. <view class="item-container">
  36. <view class="thumb-box" v-for="(goods, index1) in item.goods" :key="index1">
  37. <image @click="$util.preview(goods.image)" class="item-menu-image"
  38. :src="goods.image" mode=""></image>
  39. <view class="item-content">
  40. <view class="name flex justify-between ">
  41. <view class="text-cut-1 " style="width: 75%;">
  42. <text>{{goods.name}}</text>
  43. </view>
  44. <view class="text-red text-sm" @click="operateGoods(goods)">
  45. <text>编辑</text>
  46. <text class="cuIcon-edit u-m-l-2"></text>
  47. </view>
  48. </view>
  49. <view class="tips">
  50. <view class="text-cut-1 " style="width: 75%;">
  51. {{ goods.description}}
  52. </view>
  53. <view class="text-blue text-sm" @click="operateGoods(goods)">
  54. <text>规格</text>
  55. <text class="cuIcon-goods u-m-l-2"></text>
  56. </view>
  57. </view>
  58. <view class="price_and_action">
  59. <text class="price">¥{{ goods.defaultPrice / 100 }}</text>
  60. <text v-if="!goods.isSell" class="margin-left-20 text-sm text-red">已下架</text>
  61. </view>
  62. </view>
  63. </view>
  64. </view>
  65. </view>
  66. </view>
  67. </scroll-view>
  68. </block>
  69. </view>
  70. <!-- 分类 -->
  71. <u-popup borderRadius="12" v-model="categoryShow" mode="center" width="80%">
  72. <view class="center padding-30 flex-direction">
  73. <text class="text-center text-lg" v-text="categoryEdit?'修改分类':'添加分类'"></text>
  74. <view class="margin-top-30">
  75. <u-form :model="categoryForm" ref="uForm" label-width="140">
  76. <u-form-item label="分类名称">
  77. <u-input :clearable="false" v-model="categoryForm.name" />
  78. </u-form-item>
  79. <u-form-item label="显示顺序">
  80. <u-number-box v-model="categoryForm.displayOrder" ::min="1" :max="100"></u-number-box>
  81. </u-form-item>
  82. <u-form-item label="是否显示">
  83. <view class="center" style="justify-content: flex-start;">
  84. <u-switch active-color="#FF9447" v-model="categoryForm.showStatus" active-value="1"
  85. inactive-value="0"></u-switch>
  86. </view>
  87. </u-form-item>
  88. </u-form>
  89. </view>
  90. <view class="center " style="margin-top: 60rpx;width: 100%;justify-content: space-around;">
  91. <block v-if="categoryEdit">
  92. <view @click="deleteCategory" class="cu-btn flex text-lg line-red radius"
  93. style="padding: 40rpx 0;width: 46%;">
  94. 删除
  95. </view>
  96. <view @click="submitCategory" class="cu-btn flex text-lg bg-base radius"
  97. style="padding: 40rpx 0;width: 46%;">
  98. 确认
  99. </view>
  100. </block>
  101. <view v-else @click="submitCategory" class="cu-btn flex text-lg bg-base radius"
  102. style="padding: 40rpx 0;width: 90%;">
  103. 确认
  104. </view>
  105. </view>
  106. </view>
  107. </u-popup>
  108. <!-- 商品 -->
  109. <u-popup borderRadius="12" v-model="goodsShow" mode="center" width="90%">
  110. <view class="bg-white center flex-direction" style="padding: 30rpx 20rpx;">
  111. <text class="text-center text-bold text-lg" v-text="goodsEdit?'修改商品':'添加商品'"></text>
  112. <view class="margin-top-10">
  113. <u-form :error-type="['toast']" :model="goodsForm" ref="uGoodsForm" label-width="160">
  114. <u-form-item prop="image" :required="true" label="商品图片" label-position="top">
  115. <view class="" style="margin-top: -40rpx;margin-bottom: -20rpx;">
  116. <mp-upload-img ref="mpUploadImg" @delImg="delGoodsImg" @click='uploadGoodsImg'
  117. :count="1" col="3"></mp-upload-img>
  118. </view>
  119. </u-form-item>
  120. <u-form-item :required="true" label="所在分类">
  121. <u-input :clearable="false" disabled placeholder-style="color:#d8d8d8"
  122. v-model="goodsForm.goodsCategoryName" />
  123. </u-form-item>
  124. <u-form-item :required="true" label="商品名称" prop="name">
  125. <u-input :clearable="false" placeholder-style="color:#d8d8d8" v-model="goodsForm.name" />
  126. </u-form-item>
  127. <u-form-item :required="true" label="商品单价">
  128. <u-input :clearable="false" placeholder-style="color:#d8d8d8" v-model="price" />
  129. <text slot="right">元</text>
  130. </u-form-item>
  131. <u-form-item label="显示顺序">
  132. <u-number-box v-model="goodsForm.displayOrder" ::min="1" :max="100"></u-number-box>
  133. </u-form-item>
  134. <u-form-item label="是否在卖">
  135. <view class="center" style="justify-content: flex-start;">
  136. <u-switch active-color="#FF9447" v-model="goodsForm.isSell" active-value="1"
  137. inactive-value="0"></u-switch>
  138. </view>
  139. </u-form-item>
  140. <u-form-item label="商品描述">
  141. <u-input :clearable="false" placeholder-style="color:#d8d8d8"
  142. v-model="goodsForm.description" placeholder="请输入商品描述" />
  143. </u-form-item>
  144. </u-form>
  145. </view>
  146. <view class="center " style="margin-top: 60rpx;width: 100%;justify-content: space-around;">
  147. <block v-if="goodsEdit">
  148. <view @click="deleteGoods" class="cu-btn flex text-lg line-red radius"
  149. style="padding: 40rpx 0;width: 46%;">
  150. 删除
  151. </view>
  152. <view @click="submitGoods" class="cu-btn flex text-lg bg-base radius"
  153. style="padding: 40rpx 0;width: 46%;">
  154. 确认
  155. </view>
  156. </block>
  157. <view v-else @click="submitGoods" class="cu-btn flex text-lg bg-base radius"
  158. style="padding: 40rpx 0;width: 90%;">
  159. 确认
  160. </view>
  161. </view>
  162. </view>
  163. </u-popup>
  164. <toast ref="toast"></toast>
  165. </view>
  166. </template>
  167. <script>
  168. import mpUploadImg from "@/components/mp-uploadImg/mp-uploadImg.vue"
  169. export default {
  170. components: {
  171. mpUploadImg
  172. },
  173. data() {
  174. return {
  175. goodsList: [],
  176. //分类
  177. operateCategoryItem: {},
  178. categoryEdit: false,
  179. categoryShow: false,
  180. categoryForm: {
  181. id: '',
  182. shopId: '',
  183. name: '',
  184. displayOrder: 1,
  185. //是否显示
  186. showStatus: 1,
  187. },
  188. //商品 begin
  189. imgList: [],
  190. price: 0,
  191. operateGoodsItem: {},
  192. goodsEdit: false,
  193. goodsShow: false,
  194. goodsForm: {
  195. id: '',
  196. shopId: '',
  197. goodsCategoryName: '',
  198. name: '',
  199. displayOrder: 1,
  200. defaultPrice: 0,
  201. isSell: 1,
  202. image: '',
  203. description: null
  204. },
  205. goodsRules: {
  206. image: [{
  207. required: true,
  208. message: '请上传商品图片',
  209. trigger: ['blur', 'change']
  210. }],
  211. name: [{
  212. required: true,
  213. message: '请输入商品名称',
  214. trigger: ['blur', 'change']
  215. }],
  216. },
  217. //商品 end
  218. // 菜单
  219. scrollTop: 0, //tab标题的滚动条位置
  220. current: 0, // 预设当前项的值
  221. menuHeight: 0, // 左边菜单的高度
  222. menuItemHeight: 0, // 左边菜单item的高度
  223. }
  224. },
  225. onLoad() {
  226. this.getGoodsList()
  227. },
  228. onReady() {
  229. this.$refs.uGoodsForm.setRules(this.goodsRules);
  230. },
  231. methods: {
  232. getGoodsList() {
  233. let params = {
  234. shopId: this.vuex_shopId
  235. }
  236. this.$api.goods.list(params).then(res => {
  237. this.goodsList = res.data
  238. })
  239. },
  240. //分类 begin
  241. operateCategory(item) {
  242. this.operateCategoryItem = item
  243. this.categoryShow = true
  244. if (this.$isEmpty(item)) {
  245. //添加分类
  246. this.reSetCategoryForm()
  247. this.categoryEdit = false
  248. return
  249. }
  250. //修改分类
  251. this.categoryEdit = true
  252. this.$util.objectCopy(this.categoryForm, item)
  253. },
  254. async submitCategory() {
  255. if (this.$isEmpty(this.categoryForm.name)) {
  256. this.$u.toast('请输入类目名称!')
  257. return
  258. }
  259. if (!this.categoryEdit) {
  260. let flag = this.goodsList.some(item => item.goodsCategory.name == this.categoryForm.name)
  261. if (flag) {
  262. this.$u.toast('已有该名称的类目!')
  263. return
  264. }
  265. }
  266. this.categoryForm.shopId = this.vuex_shopId
  267. let res = await this.$api.goodsCategory.submit(this.categoryForm)
  268. if (res.success) {
  269. this.$u.toast('操作成功')
  270. this.getGoodsList()
  271. this.categoryShow = false
  272. }
  273. },
  274. deleteCategory() {
  275. this.$dialog.showModal('确定删除该分类?').then(() => {
  276. this.$api.goodsCategory.remove(this.operateCategoryItem.id).then(res => {
  277. if (res.success) {
  278. this.$u.toast('删除成功')
  279. this.current = 0
  280. this.categoryShow = false
  281. this.getGoodsList()
  282. }
  283. })
  284. })
  285. },
  286. reSetCategoryForm(){
  287. this.categoryForm={
  288. id: '',
  289. shopId: '',
  290. name: '',
  291. displayOrder: 1,
  292. //是否显示
  293. showStatus: 1,
  294. }
  295. },
  296. //分类 end
  297. //商品begin
  298. uploadGoodsImg(e){
  299. this.goodsForm.image=e[0]
  300. },
  301. operateGoods(item) {
  302. this.goodsForm.goodsCategoryName = this.goodsList[this.current].goodsCategory.name
  303. this.operateGoodsItem = item
  304. this.goodsShow = true
  305. if (this.$isEmpty(item)) {
  306. //添加商品
  307. this.reSetGoodsData()
  308. this.goodsEdit = false
  309. return
  310. }
  311. //修改商品
  312. this.price = item.defaultPrice / 100
  313. this.imgList = [item.image]
  314. this.$refs.mpUploadImg.changeImgList(this.imgList)
  315. this.goodsEdit = true
  316. this.$util.objectCopy(this.goodsForm, item)
  317. },
  318. delGoodsImg(item) {
  319. this.imgList = item[0]
  320. this.goodsForm.image=this.imgList
  321. },
  322. reSetGoodsData(){
  323. this.price=0
  324. this.imgList=[]
  325. this.$refs.mpUploadImg.changeImgList(this.imgList)
  326. let goodsCategoryName=this.goodsForm.goodsCategoryName
  327. this.goodsForm={
  328. id: '',
  329. shopId: '',
  330. goodsCategoryName,
  331. name: '',
  332. displayOrder: 1,
  333. defaultPrice: 0,
  334. isSell: 1,
  335. image: '',
  336. description: null
  337. }
  338. },
  339. submitGoods() {
  340. this.$refs.uGoodsForm.validate(valid => {
  341. if (valid) {
  342. this.goodsForm.defaultPrice=this.price * 100
  343. if (this.$isEmpty(this.goodsForm.defaultPrice)) {
  344. this.$u.toast('请输入商品单价')
  345. return
  346. }
  347. this.doSubmitGoods()
  348. } else {
  349. console.log('验证失败');
  350. }
  351. });
  352. },
  353. async doSubmitGoods(){
  354. this.goodsForm.shopId = this.vuex_shopId
  355. let res = await this.$api.goods.submit(this.goodsForm)
  356. if (res.success) {
  357. this.$u.toast('操作成功')
  358. this.getGoodsList()
  359. this.goodsShow = false
  360. }
  361. },
  362. deleteGoods() {
  363. this.$dialog.showModal('确定删除该商品?').then(() => {
  364. this.$api.goods.remove(this.operateGoodsItem.id).then(res => {
  365. if (res.success) {
  366. this.$u.toast('删除成功')
  367. this.goodsShow = false
  368. this.getGoodsList()
  369. }
  370. })
  371. })
  372. },
  373. //商品end
  374. //菜单 begin
  375. // 点击左边的栏目切换
  376. async swichMenu(index) {
  377. if (index == this.current) return;
  378. this.current = index;
  379. // 如果为0,意味着尚未初始化
  380. if (this.menuHeight == 0 || this.menuItemHeight == 0) {
  381. await this.getElRect('menu-scroll-view', 'menuHeight');
  382. await this.getElRect('u-tab-item', 'menuItemHeight');
  383. }
  384. // 将菜单菜单活动item垂直居中
  385. this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
  386. },
  387. // 获取一个目标元素的高度
  388. getElRect(elClass, dataVal) {
  389. new Promise((resolve, reject) => {
  390. const query = uni.createSelectorQuery().in(this);
  391. query.select('.' + elClass).fields({
  392. size: true
  393. }, res => {
  394. // 如果节点尚未生成,res值为null,循环调用执行
  395. if (!res) {
  396. setTimeout(() => {
  397. this.getElRect(elClass);
  398. }, 10);
  399. return;
  400. }
  401. this[dataVal] = res.height;
  402. }).exec();
  403. })
  404. }
  405. // 菜单end
  406. }
  407. }
  408. </script>
  409. <style lang="scss" scoped>
  410. .u-wrap {
  411. height: calc(100vh);
  412. /* #ifdef H5 */
  413. height: calc(100vh - var(--window-top));
  414. /* #endif */
  415. display: flex;
  416. flex-direction: column;
  417. }
  418. .u-menu-wrap {
  419. flex: 1;
  420. display: flex;
  421. overflow: hidden;
  422. }
  423. .u-tab-view {
  424. width: 200rpx;
  425. height: 100%;
  426. }
  427. .u-tab-item {
  428. height: 110rpx;
  429. background: #f6f6f6;
  430. box-sizing: border-box;
  431. display: flex;
  432. align-items: center;
  433. justify-content: center;
  434. font-size: 26rpx;
  435. color: #444;
  436. font-weight: 400;
  437. line-height: 1;
  438. }
  439. .u-tab-item-active {
  440. position: relative;
  441. color: #000;
  442. font-size: 30rpx;
  443. font-weight: 600;
  444. background: #fff;
  445. }
  446. .u-tab-item-active::before {
  447. content: "";
  448. position: absolute;
  449. border-left: 4px solid $u-type-primary;
  450. height: 32rpx;
  451. left: 0;
  452. top: 39rpx;
  453. }
  454. .u-tab-view {
  455. height: 100%;
  456. }
  457. .right-box {
  458. background-color: rgb(250, 250, 250);
  459. }
  460. .page-view {
  461. padding: 16rpx;
  462. }
  463. .class-item {
  464. margin-bottom: 30rpx;
  465. background-color: #fff;
  466. padding: 16rpx;
  467. border-radius: 8rpx;
  468. }
  469. .item-title {
  470. font-size: 26rpx;
  471. color: $u-main-color;
  472. font-weight: bold;
  473. }
  474. .item-container {
  475. display: flex;
  476. flex-wrap: wrap;
  477. .thumb-box {
  478. width: 100%;
  479. display: flex;
  480. padding: 20rpx 0;
  481. border-bottom: 1rpx solid #EEEEEE;
  482. .item-menu-image {
  483. width: 160rpx;
  484. height: 160rpx;
  485. margin-right: 20rpx;
  486. border-radius: 8rpx;
  487. }
  488. .item-content {
  489. width: calc(100% - 180rpx);
  490. }
  491. .name {
  492. font-size: $font-size-base;
  493. margin-bottom: 10rpx;
  494. }
  495. .tips {
  496. width: 100%;
  497. height: 40rpx;
  498. display: flex;
  499. justify-content: space-between;
  500. line-height: 40rpx;
  501. white-space: nowrap;
  502. font-size: $font-size-sm;
  503. color: $text-color-assist;
  504. margin-bottom: 10rpx;
  505. }
  506. .price_and_action {
  507. width: 100%;
  508. display: flex;
  509. align-items: center;
  510. margin-top: 30rpx;
  511. .price {
  512. font-size: $font-size-base;
  513. font-weight: 600;
  514. }
  515. .btn-group {
  516. display: flex;
  517. justify-content: space-between;
  518. align-items: center;
  519. position: relative;
  520. .btn {
  521. background-color: $base-color;
  522. padding: 0 20rpx;
  523. box-sizing: border-box;
  524. font-size: $font-size-sm;
  525. height: 44rpx;
  526. line-height: 44rpx;
  527. &.property_btn {
  528. border-radius: 24rpx;
  529. }
  530. &.add_btn,
  531. &.reduce_btn {
  532. color: #FFFFFF;
  533. border: $base-color;
  534. padding: 0;
  535. width: 44rpx;
  536. border-radius: 44rpx;
  537. }
  538. }
  539. .dot {
  540. position: absolute;
  541. background-color: #ffffff;
  542. border: 1px solid $color-primary;
  543. color: $color-primary;
  544. font-size: $font-size-sm;
  545. width: 36rpx;
  546. height: 36rpx;
  547. line-height: 36rpx;
  548. text-align: center;
  549. border-radius: 100%;
  550. right: -12rpx;
  551. top: -10rpx;
  552. }
  553. .number {
  554. width: 44rpx;
  555. height: 44rpx;
  556. line-height: 44rpx;
  557. text-align: center;
  558. }
  559. }
  560. }
  561. }
  562. .thumb-box:last-child {
  563. border: none;
  564. }
  565. }
  566. </style>