Quellcode durchsuchen

删除商品功能,解决商品分类页的分页bug,当商品已删除时返回上一页

nnkwrik vor 6 Jahren
Ursprung
Commit
ca3b78f5c9

+ 18 - 1
goods-service/src/main/java/io/github/nnkwrik/goodsservice/controller/GoodsController.java

@@ -122,7 +122,7 @@ public class GoodsController {
     public Response<List<Goods>> getGoodsRelated(@PathVariable("goodsId") int goodsId,
                                                  @RequestParam(value = "page", defaultValue = "1") int page,
                                                  @RequestParam(value = "size", defaultValue = "10") int size) {
-        List<Goods> goodsList = goodsService.getGoodsRelated(goodsId,page,size);
+        List<Goods> goodsList = goodsService.getGoodsRelated(goodsId, page, size);
         log.info("获取与 goodsId=[{}] 相关的商品 : 展示{}个商品", goodsId, goodsList.size());
 
         return Response.ok(goodsList);
@@ -158,5 +158,22 @@ public class GoodsController {
         return Response.ok();
     }
 
+    @DeleteMapping("/delete/{goodsId}")
+    public Response postGoods(@PathVariable int goodsId,
+                              @JWT(required = true) JWTUser user) {
+        try {
+            goodsService.deleteGoods(goodsId, user.getOpenId());
+        } catch (Exception e) {
+            if (e.getMessage().equals(Response.SELLER_AND_GOODS_IS_NOT_MATCH + "")) {
+                String msg = "删除商品失败.当前用户信息和卖家信息不匹配";
+                return Response.fail(Response.SELLER_AND_GOODS_IS_NOT_MATCH, msg);
+            }
+            e.printStackTrace();
+
+        }
+        log.info("用户删除商品: 用户id=【{}】,商品Id=【{}】", user.getOpenId(), goodsId);
+        return Response.ok();
+    }
+
 
 }

+ 6 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/dao/GoodsMapper.java

@@ -167,4 +167,10 @@ public interface GoodsMapper {
             "  and is_selling = false\n" +
             "  and is_delete = false")
     Integer getSellerHistory(@Param("seller_id") String sellerId);
+
+    @Select("SELECT EXISTS(SELECT 1 FROM goods WHERE seller_id=#{seller_id} and id=#{goods_id})")
+    Boolean validateSeller(@Param("goods_id") int goodsId, @Param("seller_id") String userId);
+
+    @Delete("delete from goods where id = ${goods_id}")
+    void deleteGoods(@Param("goods_id") int goodsId);
 }

+ 1 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/GoodsService.java

@@ -32,4 +32,5 @@ public interface GoodsService {
 
     void postGoods(Goods goods);
 
+    void deleteGoods(int goodsId, String userId) throws Exception;
 }

+ 10 - 1
goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/impl/GoodsServiceImpl.java

@@ -63,7 +63,7 @@ public class GoodsServiceImpl implements GoodsService {
 
     @Override
     public SimpleUser getSellerInfo(String sellerId) {
-                Response<SimpleUser> response = userClient.getSimpleUser(sellerId);
+        Response<SimpleUser> response = userClient.getSimpleUser(sellerId);
         if (response.getErrno() == Response.USER_IS_NOT_EXIST) {
             log.info("没有搜索到商品卖家的相关信息");
             return null;
@@ -103,6 +103,15 @@ public class GoodsServiceImpl implements GoodsService {
         goodsMapper.addGoods(goods);
     }
 
+    @Override
+    public void deleteGoods(int goodsId, String userId) throws Exception {
+        if (goodsMapper.validateSeller(goodsId, userId)) {
+            goodsMapper.deleteGoods(goodsId);
+        } else {
+            throw new Exception(Response.SELLER_AND_GOODS_IS_NOT_MATCH + "");
+        }
+    }
+
     @Override
     public List<CommentVo> getGoodsComment(int goodsId) {
         List<GoodsComment> baseCommentPo = goodsMapper.findBaseComment(goodsId);

+ 9 - 2
wx-front/config/api.js

@@ -1,8 +1,9 @@
-const ApiRootUrl = 'https://5d876383.ngrok.io/';
-// const ApiRootUrl = 'http://127.0.0.1:8080/';
+// const ApiRootUrl = 'https://5d876383.ngrok.io/';
+const ApiRootUrl = 'http://127.0.0.1:8080/';
 
 module.exports = {
   IndexUrl: ApiRootUrl + 'index/index', //首页数据接口
+  IndexMore: ApiRootUrl + 'index/more', //首页展示更多推荐商品
   CatalogList: ApiRootUrl + 'catalog/index',  //分类目录全部分类数据接口
   CatalogCurrent: ApiRootUrl + 'catalog',  //分类目录当前分类数据接口
 
@@ -16,6 +17,7 @@ module.exports = {
   GoodsHot: ApiRootUrl + 'goods/hot',  //热门
   GoodsRelated: ApiRootUrl + 'goods/related',  //商品详情页的关联商品(大家都在看)
   GoodsPost: ApiRootUrl + 'goods/post', //发布商品
+  GoodsDelete: ApiRootUrl + 'goods/delete', //删除商品
 
   PostCateList: ApiRootUrl + 'category/post', //发布商品时选择分类
 
@@ -35,6 +37,11 @@ module.exports = {
 
   CollectList: ApiRootUrl + 'collect/list',  //收藏列表
   CollectAddOrDelete: ApiRootUrl + 'collect/addordelete',  //添加或取消收藏
+  
+  PostedList: ApiRootUrl + 'goods/posted',  //发布的商品
+  BoughtList: ApiRootUrl + 'goods/bought',  //买过的商品
+  SoldList: ApiRootUrl + 'goods/sold',  //卖出的商品
+  
 
   CommentList: ApiRootUrl + 'comment/list',  //评论列表
   CommentCount: ApiRootUrl + 'comment/count',  //评论总数

+ 3 - 1
wx-front/pages/category/category.js

@@ -111,7 +111,9 @@ Page({
       });
     }
     this.setData({
-      id: event.currentTarget.dataset.id
+      id: event.currentTarget.dataset.id,
+      page:1,
+      goodsList:[]
     });
 
     this.getGoodsList();

+ 59 - 5
wx-front/pages/goods/goods.js

@@ -15,6 +15,7 @@ Page({
     // brand: {},
     // specificationList: [],
     // productList: [],
+    isSeller:false,
     seller: {},
     sellerDates: 0,
     sellerHistory: 0,
@@ -25,6 +26,7 @@ Page({
     checkedSpecText: '请选择规格数量',
     openAttr: false,
     openComment: false,
+    openDelete:false,
     replyId: '',
     replyUserId: '',
     replyUserName: '',
@@ -40,6 +42,16 @@ Page({
     let that = this;
     util.request(api.GoodsDetail + '/' + that.data.id).then(function(res) {
       if (res.errno === 0) {
+        if (res.data.info.isDelete) {
+          util.showErrorToast('商品不存在')
+          setTimeout(function callback() {
+            
+            wx.navigateBack({
+              delta: 1
+            })
+          }, 1000)
+          return 
+        }
 
         //计算卖家来平台第几天
         let registerTime = res.data.seller.registerTime
@@ -51,12 +63,7 @@ Page({
           seller: res.data.seller,
           sellerHistory: res.data.sellerHistory,
           sellerDates: dates,
-          // attribute: res.data.attribute,
-          // issueList: res.data.issue,
           comment: res.data.comment,
-          // brand: res.data.brand,
-          // specificationList: res.data.specificationList,
-          // productList: res.data.productList,
           userHasCollect: res.data.userHasCollect
         });
 
@@ -70,6 +77,13 @@ Page({
           });
         }
 
+        if (that.data.seller.openId == wx.getStorageSync('userInfo').openId){
+          console.log("当前用户是卖家")
+          that.setData({
+            isSeller: true
+          });
+        }
+
         WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
 
         that.getGoodsRelated();
@@ -90,6 +104,26 @@ Page({
       }
     });
 
+  },
+  deleteGoods: function () {
+    let that = this;
+    util.request(api.GoodsDelete + '/' + that.data.id,{},'DELETE').then(function (res) {
+      if (res.errno === 0) {
+
+        setTimeout(function goback() {
+          wx.reLaunch({
+            url: '/pages/index/index'
+          })
+        }, 1000)
+
+        wx.showToast({
+          title: '删除成功'
+        })
+      }else{
+        console.log(res.errmsg)
+      }
+    });
+
   },
   // clickSkuValue: function (event) {
   //   let that = this;
@@ -211,6 +245,7 @@ Page({
 
     var that = this;
     this.getGoodsInfo();
+
     // util.request(api.CartGoodsCount).then(function (res) {
     //   if (res.errno === 0) {
     //     that.setData({
@@ -257,6 +292,18 @@ Page({
 
   },
 
+  switchDeletetPop: function (event) {
+    let that = this
+    user.checkLoginAndNav().then(() => {
+      if (this.data.openDelete == false) {
+        this.setData({
+          openDelete: !this.data.openDelete
+        });
+      }
+    })
+
+  },
+
 
   closeComment: function() {
     this.setData({
@@ -265,6 +312,13 @@ Page({
     });
   },
 
+  closeDelete: function () {
+    this.setData({
+
+      openDelete: false,
+    });
+  },
+
   postComment: function(event) {
     let that = this
     if (event.detail.value.trim() == '') {

+ 12 - 0
wx-front/pages/goods/goods.wxml

@@ -257,6 +257,16 @@
 </view>
 
 
+<view class="attr-pop-box" hidden="{{!openDelete}}">
+  <view class="delete-pop ">
+    <view class="close" bindtap="closeDelete">
+      <image class="icon" src="/static/images/clear_input.png"></image>
+    </view>
+    <button class='delete-btn' bindtap="deleteGoods" >确定删除</button>
+  </view>
+</view>
+
+
 <view class="bottom-btn">
   <view class="l" bindtap="addCannelCollect">
     <image class="icon" src="{{ collectBackImage }}"></image>
@@ -278,5 +288,7 @@
   </view> -->
   <!-- <view class="c">立即购买</view> -->
   <view class="r" wx:if='{{!goods.isSelling}}' bindtap="addToCart">该商品已出售</view>
+  <view class="r" wx:elif="{{isSeller && !goods.isDelete && goods.isSelling}}" bindtap="switchDeletetPop">删除该商品</view>
   <view class="r" wx:else bindtap="addToCart">想要</view>
+  
 </view>

+ 28 - 40
wx-front/pages/goods/goods.wxss

@@ -687,55 +687,43 @@
   bottom: 100rpx;
 }
 
-.attr-pop .close {
-  position: absolute;
-  width: 48rpx;
-  height: 48rpx;
-  right: 31.25rpx;
-  overflow: hidden;
-  top: 31.25rpx;
+.delete-pop {
+  width: 100%;
+  height: auto;
+  max-height: 780rpx;
+  padding: 10rpx;
+  background: #fff;
+  position: fixed;
+  z-index: 9;
+  bottom: 50%;
 }
 
-.attr-pop .close .icon {
+.delete-pop .close {
+  position: absolute;
   width: 48rpx;
   height: 48rpx;
-}
-
-.attr-pop .img-info {
-  width: 687.5rpx;
-  height: 177rpx;
+  right: 31.25rpx;
   overflow: hidden;
-  margin-bottom: 41.5rpx;
-}
-
-.attr-pop .img {
-  float: left;
-  height: 177rpx;
-  width: 177rpx;
-  background: #f4f4f4;
-  margin-right: 31.25rpx;
+  /* top: 31.25rpx; */
 }
 
-.attr-pop .info {
-  float: left;
-  height: 177rpx;
-  display: flex;
-  align-items: center;
-}
-
-.attr-pop .p {
-  font-size: 33rpx;
-  color: #333;
-  height: 33rpx;
-  line-height: 33rpx;
-  margin-bottom: 10rpx;
+.delete-pop .close .icon {
+  width: 60rpx;
+  height: 60rpx;
 }
 
-.attr-pop .a {
-  font-size: 29rpx;
-  color: #333;
-  height: 40rpx;
-  line-height: 40rpx;
+.delete-pop .delete-btn {
+  width: 600rpx;
+  background: #f94b4b;
+  margin-top: 25px;
+  border: 1px solid #f94b4b;
+  height: 100rpx;
+  line-height: 96rpx;
+  text-align: center;
+  color: #fff;
+  font-size: 30rpx;
+  border-radius: 10rpx;
+  margin-bottom: 30rpx;
 }
 
 .spec-con {