学而实习之 不亦乐乎

积分商城功能数据表及模块设计

2024-01-15 22:35:25

一、积分商城功能表结构设计

1、会员积分记录表(user_points)

来自的用户 user_id,签到和兑换记录字段 points(签到为正值,兑换为负值),积分信息 info

CREATE TABLE `user_points` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `user_id` int(10) NOT NULL,
  `points` int(10) NOT NULL,
  `time` int(10) NOT NULL,
  `info` varchar(30) DEFAULT NULL,
  `source` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1签到,2推荐',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=72 DEFAULT CHARSET=utf8 COMMENT='会员积分';

2、积分兑换商品表user_points_goods

记录兑换的商品需要多少积分,字段points表示需要的积分数量才能兑换的商品

CREATE TABLE `user_points_goods` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(110) NOT NULL,
  `thumb` varchar(110) NOT NULL,
  `description` varchar(100) DEFAULT NULL,
  `content` text,
  `time` int(10) NOT NULL,
  `points` int(10) NOT NULL,
  `listorder` int(10) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='积分商品表';

3、积分换取商品记录表points_record

user_id记录哪个用户,goode_id记录哪个商品,points使用的积分数

CREATE TABLE `user_points_record` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `points` int(11) NOT NULL,
  `goods_id` int(11) NOT NULL,
  `status` int(11) NOT NULL DEFAULT '1',
  `empress` varchar(100) DEFAULT NULL,
  `time` int(11) NOT NULL,
  `user_id` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='积分兑换记录';

二、积分商城功能模块设计

1、会员签到功能实现

需要如下两步:

  • 检测已登录的该用户今天是否签到,已经签到则今天不再签到
  • 今天未签到则插入points积分记录表中
//会员签到
public function sign(){

    //验证用户是否登录
    $sessionUserData=$this->isLogin();

    //先检测今天是否签到
    $pointsData=Db::name('points')->where('user_id',$sessionUserData['id'])->whereDay('time')->where('source',1)->order('id desc')->find();

    if(!empty($pointsData)){
        return alert('你今天已经签到了','index',5);
    }

    $res=Db::name('points')->insert([
        'user_id'=>$sessionUserData['id'],
        'time'=>time(),
        'points'=>10,
        'info'=>'签到赚取积分'
    ]);

    if($res){
        return alert('签到成功','index',6);
    }else{
        return alert('签到失败','index',5);
    }
}

2、积分换购流程

1)积分商品详情

  • 根据id查找积分商品
  • 统计该用户积分

实现代码:

public function points_goods_detail(){

    $sessionUserData=$this->isLogin();
    $id=input('id');

    //1、根据id查找积分商品
    $pointsGoodsData=Db::name('points_goods')->find($id);
    if(empty($pointsGoodsData)){
        return alert('没有改商品','points_shop',5);
    }

    //2、统计该用户积分
    $total_socre=Db::name('points')->where('user_id',$sessionUserData['id'])->sum('points');

    return view('',[
        'pointsGoodsData'=>$pointsGoodsData,
        'total_socre'=>$total_socre,
        'left_menu'=>32
    ]);
}

2)积分换购

  • 根据id查找积分商品
  • 统计用户积分并判断该用户积分是否大于积分商品里选中的商品的积分
  • 开启异常捕捉,积分换购的积分换取商品记录表 points_record 插入一条记录,同时插入会员积分记录表负数积分信息

实现代码:

public function points_exchange(){

    $sessionUserData=$this->isLogin();
    $id=input('id');

    //1、根据id查找积分商品
    $pointsGoodsData=Db::name('points_goods')->find($id);

    if(empty($pointsGoodsData)){
        return json(['status'=>-1,'msg'=>'没有该商品']);
    }

    //2、统计用户积分并判断该用户积分是否大于积分商品里选中的商品的积分
    $total_socre=Db::name('points')->where('user_id',$sessionUserData['id'])->sum('points');
    if($total_socre>$pointsGoodsData['points']){

    //3、开启异常捕捉,积分换购的积分换取商品记录表points_record插入一条记录,同时插入会员积分记录表负数积分信息
        try{
            //积分换购表
            Db::name('points_record')->insert([
                'user_id'=>$sessionUserData['id'],
                'points'=>$pointsGoodsData['points'],
                'time'=>time(),
                'goods_id'=>$id
            ]);

            Db::name('points')->insert([
                'user_id'=>$sessionUserData['id'],
                'points'=>0-$pointsGoodsData['points'],
                'time'=>time(),
                'info'=>'商品换购'
            ]);
        }catch (\Exception $e){
            return json(['status'=>-1,'msg'=>'服务端异常,换购失败']);
        }

        return json(['status'=>1,'msg'=>'商品换购成功']);
    }else{
        return json(['status'=>-1,'msg'=>'积分不足']);
    }
}