siwenlei 1 年間 前
コミット
0b7510ad9e

+ 63 - 0
application/api/controller/Coupon.php

@@ -0,0 +1,63 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+
+/**
+ * 优惠券
+ */
+class Coupon extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+
+    /**
+     * 获取优惠券详情
+     * @return void
+     */
+    public function getCouponDetails(){
+
+        $coupon_id = input('coupon_id',0);
+
+        $res = Db::name("unishop_coupon")->where('id',$coupon_id)->whereNull("deletetime")->find();
+
+        if(empty($res)){
+            $this->error("优惠券已下架");
+        }
+        if(!empty($res)){
+
+            $res['starttime'] = date('Y-m-d H:i:s',$res['starttime']);
+            $res['endtime'] = date('Y-m-d H:i:s',$res['endtime']);
+
+        }
+
+        $this->success("商品详情",$res);
+    }
+
+    /**
+     * 获取优惠券列表
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function getCouponList(){
+
+        //判断是否存在优惠券
+        $time = time();
+
+        $coupon = Db::name("unishop_coupon")->where('switch',1)->where("starttime",'<=',$time)->where("endtime",">=",$time)->whereNull("deletetime")->order('weigh desc')->select();
+
+        if(!empty($coupon)){
+            foreach ($coupon as $key=>$value){
+                $coupon[$key]['starttime'] = date('Y-m-d H:i:s',$value['starttime']);
+                $coupon[$key]['endtime'] = date('Y-m-d H:i:s',$value['endtime']);
+            }
+        }
+
+        $this->success("优惠券列表",$coupon);
+    }
+
+}

+ 94 - 0
application/api/controller/Goods.php

@@ -0,0 +1,94 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+
+/**
+ * 商品类接口
+ */
+class Goods extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+
+    /**
+     * 获取零售商品详情
+     * @return void
+     */
+    public function getGoodsDetails(){
+
+        $gid = input('gid',0);
+
+        $res = Db::name("unishop_product")->where('id',$gid)->whereNull("deletetime")->find();
+
+        if(empty($res)){
+            $this->error("商品已售空或已下架");
+        }
+
+        //判断是否存在优惠券
+        $time = time();
+
+        $coupon = Db::name("unishop_coupon")->field('id,title,value,least')->where('switch',1)->where("starttime",'<=',$time)->where("endtime",">=",$time)->whereNull("deletetime")->limit(1)->select();
+
+        if(!empty($coupon)){
+            $res['coupon'] = $coupon;
+        }
+
+        //查询商品评论
+        $evaluate = Db::name("unishop_evaluate")->alias('e')->join('fa_user u','e.user_id=u.id')->field('nickname,avatar,comment,rate')->where("product_id",$gid)->whereNull("deletetime")->limit(2)->select();
+        if(!empty($evaluate)){
+
+            $res['evaluate'] =  $evaluate;
+            $res['evaluate_count'] = Db::name("unishop_evaluate")->alias('e')->join('fa_user u','e.user_id=u.id')->field('nickname,avatar')->where("product_id",$gid)->whereNull("deletetime")->count();
+
+        }
+
+        //计算平均分
+        $rate = Db::name("unishop_evaluate")->alias('e')->join('fa_user u','e.user_id=u.id')->field('nickname,avatar')->where("product_id",$gid)->whereNull("deletetime")->avg('rate');
+        $res['level'] =  $rate;
+
+        $this->success("获取零售商品详情",$res);
+    }
+
+    /**
+     * 批发商品详情
+     * @return void
+     */
+    public function getWholesaleGoodsDetails(){
+
+        $gid = input('gid',0);
+
+        $res = Db::name("unishop_wholesale_goods")->where('goods_id',$gid)->find();
+
+        if(empty($res)){
+            $this->error("商品已售空或已下架");
+        }
+
+        //判断是否存在优惠券
+        $time = time();
+
+        $coupon = Db::name("unishop_coupon")->field('id,title,value,least')->where('switch',1)->where("starttime",'<=',$time)->where("endtime",">=",$time)->whereNull("deletetime")->limit(1)->select();
+
+        if(!empty($coupon)){
+            $res['coupon'] = $coupon;
+        }
+
+        //查询商品评论
+        $evaluate = Db::name("unishop_evaluate")->alias('e')->join('fa_user u','e.user_id=u.id')->field('nickname,avatar,comment,rate')->where("product_id",$gid)->whereNull("deletetime")->limit(2)->select();
+        if(!empty($evaluate)){
+
+            $res['evaluate'] =  $evaluate;
+            $res['evaluate_count'] = Db::name("unishop_evaluate")->alias('e')->join('fa_user u','e.user_id=u.id')->field('nickname,avatar')->where("product_id",$gid)->whereNull("deletetime")->count();
+
+        }
+
+        //计算平均分
+        $rate = Db::name("unishop_evaluate")->alias('e')->join('fa_user u','e.user_id=u.id')->field('nickname,avatar')->where("product_id",$gid)->whereNull("deletetime")->avg('rate');
+        $res['level'] =  $rate;
+
+        $this->success("批发商品详情",$res);
+    }
+
+}

+ 154 - 0
application/api/controller/Index.php

@@ -3,6 +3,7 @@
 namespace app\api\controller;
 
 use app\common\controller\Api;
+use think\Db;
 
 /**
  * 首页接口
@@ -20,4 +21,157 @@ class Index extends Api
     {
         $this->success('请求成功');
     }
+
+    /**
+     * 首页-banner图列表
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function bannerList()
+    {
+        $list = Db::name("unishop_banner")->where('status',1)->whereNull("deletetime")->order('weigh desc')->select();
+
+        $this->success('请求成功',$list);
+    }
+
+    /**
+     * 首页-四方商品图列表
+     * @return void
+     */
+    public function GoodSquare(){
+
+        $list_new = Db::name("unishop_product")->field('image')->where("switch",1)->where("is_new",1)->whereNull("deletetime")->order('weigh desc')->limit(1)->select();
+        $list_preferred = Db::name("unishop_product")->field('image')->where("switch",1)->where("is_preferred",1)->whereNull("deletetime")->order('weigh desc')->limit(1)->select();
+        $list_hot = Db::name("unishop_product")->field('image')->where("switch",1)->where("is_hot",1)->whereNull("deletetime")->order('weigh desc')->limit(1)->select();
+        $list_discount = Db::name("unishop_product")->field('image')->where("switch",1)->where("is_discount",1)->whereNull("deletetime")->order('weigh desc')->limit(1)->select();
+
+        $data = [
+            "new"=>$list_new,
+            "preferred"=>$list_preferred,
+            "hot"=>$list_hot,
+            "discount"=>$list_discount,
+        ];
+
+        $this->success("四方商品图列表",$data);
+
+    }
+
+    /**
+     * 首页-分类模块
+     * @return void
+     */
+    public function getCatList(){
+
+        $list = Db::name("unishop_category")->field('id,name,keywords,description')->where('status','normal')->whereNull("deletetime")->order('weigh desc')->select();
+
+        $this->success("分类商品",$list);
+
+    }
+
+    /**
+     * 首页-商品列表
+     * @return void
+     */
+    public function getGoodsList(){
+        $page = input('page',1); //页码
+        $limit = input('limit',10); //条数
+        $offset = ($page - 1) * $limit;
+        $cid = input('cid',0); //分类ID
+
+        $where = [];
+        $where['switch'] = 1;
+        if(!empty($cid)){
+            $where['category_id'] = $cid;
+        }
+        $list = Db::name("unishop_product")->field('id,title,image,sales_price')->where($where)->whereNull("deletetime")->order("weigh desc")->limit($offset,$limit)->select();
+
+        $this->success("分类商品列表",$list);
+
+
+    }
+
+    /**
+     * 首页-推荐模版商品列表
+     * @return void
+     */
+    public function getModList(){
+
+        $type = input('type',1); //类型 1=今日推荐,2=热门榜单,3=首发新品,4=促销单品
+        $page = input('page',1); //页码
+        $limit = input('limit',10); //条数
+        $offset = ($page - 1) * $limit;
+        $where = [];
+        $where['switch'] = 1;
+        switch ($type) {
+            case 1:
+                $where['is_preferred'] = 1;
+            case 2:
+                $where['is_hot'] = 1;
+            case 3:
+                $where['is_new'] = 1;
+            case 4:
+                $where['is_discount'] = 1;
+            default:
+                $where['is_preferred'] = 1;
+
+        }
+        $list = Db::name("unishop_product")->field('id,title,image,sales_price')->where($where)->whereNull("deletetime")->order('weigh desc')->limit($offset,$limit)->select();
+
+        $this->success("推荐商品列表",$list);
+    }
+
+    /**
+     * 零售商品列表
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function getRetailList(){
+
+        $page = input('page',1); //页码
+        $limit = input('limit',10); //条数
+        $offset = ($page - 1) * $limit;
+        $where = [];
+        $where['switch'] = 1;
+        $list = Db::name("unishop_product")->field('id,title,image,sales_price')->where($where)->whereNull("deletetime")->order('weigh desc')->limit($offset,$limit)->select();
+
+        $this->success("零售商品列表",$list);
+
+    }
+
+    /**
+     * 批发商品列表
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function getWholesaleList(){
+
+        $page = input('page',1); //页码
+        $limit = input('limit',10); //条数
+        $offset = ($page - 1) * $limit;
+        $where = [];
+        $where['p.switch'] = 1;
+        $list = Db::name("unishop_wholesale_goods")->alias('p')->join('fa_user u','u.id=p.user_id')->field('p.user_id,ANY_VALUE(p.id) as id,ANY_VALUE(p.title) as  title,ANY_VALUE(p.image) as image,ANY_VALUE( p.sales_price) as sales_price,p.goods_id,ANY_VALUE( u.nickname) as nickname,ANY_VALUE( u.avatar) as avatar')->where($where)->whereNull("p.deletetime")->limit($offset,$limit)->group('p.user_id,p.goods_id')->select();
+
+        if(!empty($list)){
+            foreach ($list as $key=>$value){
+
+                $cout = Db::name("unishop_wholesale_goods")->where("switch",1)->where("user_id",$value['user_id'])->where("goods_id",$value['goods_id'])->whereNull("deletetime")->sum("stock");
+
+                $list[$key]['sales'] = $cout;
+
+            }
+        }
+
+        $this->success("批发商品列表",$list);
+
+
+    }
+
+
 }

+ 68 - 0
application/api/controller/News.php

@@ -0,0 +1,68 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+use think\Exception;
+
+/**
+ * 文章资讯接口
+ */
+class News extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+
+    /**
+     * 文章资讯列表
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function getNewsList(){
+
+        $list = Db::name("unishop_news")->field("id,title,look,image,createtime")->where("status",'1')->whereNull("deletetime")->order("weigh desc")->select();
+
+        if(!empty($list)){
+            foreach ($list as $key=>$value){
+                $list[$key]['createtime'] = date("Y-m-d H:i:s",$value['createtime']);
+            }
+        }
+
+        $this->success("文章资讯列表",$list);
+    }
+
+    /**
+     * 文章资讯详情
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function getNewsDetails(){
+
+        try {
+
+            $nid = input("nid",'0');
+
+            $res = Db::name("unishop_news")->where("id",$nid)->where("status",'1')->whereNull("deletetime")->find();
+
+            if(empty($res)){
+                $this->error("文章已删除");
+            }
+
+            $res['createtime'] = date("Y-m-d H:i:s",$res['createtime']);
+
+            //增加文章阅读量
+            Db::name("unishop_news")->where("id",$nid)->setInc('look');
+
+            $this->success("文章资讯详情",$res);
+
+        } catch (Exception $e){
+            $this->error($e->getMessage());
+        }
+    }
+
+}

+ 279 - 0
application/api/controller/User.php

@@ -7,7 +7,9 @@ use app\common\library\Ems;
 use app\common\library\Sms;
 use fast\Random;
 use think\Config;
+use think\Exception;
 use think\Validate;
+use think\Db;
 
 /**
  * 会员接口
@@ -345,4 +347,281 @@ class User extends Api
             $this->error($this->auth->getError());
         }
     }
+
+    /**
+     * 用户申请提现
+     * @return void
+     */
+    public function userwithdraw(){
+        try {
+            $usrr_model = new \app\common\model\User();
+
+            $user_id = $this->auth->id;
+
+            $params = $this->request->param();
+
+            $money = isset($params['money'])?$params['money']:0;
+            if(!is_numeric($money) || $money <= 0){
+                $this->error("非法金额");
+            }
+            $user_money = $usrr_model->where("id",$user_id)->value("money");
+            if($money > $user_money){
+                $this->error("可提现余额不足");
+            }
+            $out_trade_no = $params['out_trade_no'];
+
+            //插入提现表
+            $add = $usrr_model->add($user_id,$money,$out_trade_no);
+
+            if($add){
+                $this->success("提现申请成功");
+            }else{
+                $this->error("提现申请失败");
+            }
+
+
+
+        } catch (Exception $e){
+
+            $this->error($e->getMessage());
+        }
+
+    }
+
+    /**
+     * 获取我的积分和邀请列表  --待定
+     * @return void
+     */
+    public function getUserScoreList(){
+
+        try {
+
+            $user_id = $this->auth->id;
+            $usrr_model = new \app\common\model\User();
+            $score = $usrr_model->where('id',$user_id)->value("score");
+
+            $list = Db::name("hu_points_log")->where("user_id",$user_id)->whereNull("deletetime")->order('id desc')->select();
+            $data = [
+                'list'=>$list,
+                'score'=>$score,
+            ];
+
+            $this->success("我的积分",$data);
+
+        } catch (Exception $e){
+            $this->error($e->getMessage());
+        }
+
+
+
+
+    }
+
+    /**
+     * 用户邀请列表和邀请码   --待定
+     * @return void
+     */
+    public function getUserInviteList(){
+
+        try {
+
+            $user_id = $this->auth->id;
+
+            //判断有无邀请码
+            $usrr_model = new \app\common\model\User();
+            $code = $usrr_model->where('id',$user_id)->value("code");
+            if(empty($code)){
+                //生成邀请码
+                $code =  generateUniqueInvitationCode(6);
+                $usrr_model->where('id',$user_id)->update(['code'=>$code]);
+            }
+
+            //我邀请的人
+            $list = Db::name("hu_user_invite")->alias('i')->join('fa_user u','i.user_id=u.id')->field("nickname,avatar,i.createtime")->where("invite_id",$user_id)->where("i.status",'1')->whereNull("i.deletetime")->select();
+
+            if(!empty($list)){
+
+                foreach ($list as $key=>$value){
+                    $list[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
+                    //奖励积分
+
+                }
+
+            }
+
+            $data = [
+                'code'=>$code,
+                'list'=>$list
+            ];
+
+            $this->success("邀请码和邀请的人",$data);
+
+        } catch (Exception $e){
+            $this->error($e->getMessage());
+        }
+
+
+    }
+
+    /**
+     * 我的收藏列表
+     * @return void
+     */
+    public function getUsetLoveList(){
+
+        try {
+
+            $user_id = $this->auth->id;
+
+            $list = Db::name("unishop_favorite")->where("user_id",$user_id)->order('id desc')->select();
+
+            if(!empty($list)){
+                foreach ($list as $key=>$value){
+                    $goods = json_decode($value['snapshot'],true);
+                    $list[$key]['id'] = $goods['id'];
+                    $list[$key]['image'] = $goods['image'];
+                    $list[$key]['title'] = $goods['title'];
+                    $list[$key]['sales_price'] = $goods['sales_price'];
+                }
+            }
+
+            $this->success("收藏列表",$list);
+
+        } catch (Exception $e){
+
+            $this->error($e->getMessage());
+        }
+
+    }
+
+    /**
+     * 商品收藏添加|取消
+     * @return void
+     */
+    public function userLove(){
+
+        try {
+
+            $user_id = $this->auth->id;
+
+            $gid = input("gid",'0');
+            $goods = Db::name("unishop_product")->where("id",$gid)->whereNull("deletetime")->find();
+            if(empty($goods)){
+                $this->error("商品不存在");
+            }
+
+            $love = Db::name("unishop_favorite")->where("user_id",$user_id)->where("product_id",$goods['id'])->find();
+
+            if(!empty($love)){
+
+                $del = Db::name("unishop_favorite")->where("user_id",$user_id)->where("product_id",$goods['id'])->delete();
+                if($del){
+                    $this->success("取消收藏成功");
+                }else{
+                    $this->error("取消收藏失败");
+                }
+            }else{
+
+                $data['user_id'] = $user_id;
+                $data['product_id'] = $goods['id'];
+                $data['snapshot'] = json_encode($goods,JSON_UNESCAPED_UNICODE);
+                $data['createtime'] = time();
+
+                $add = Db::name("unishop_favorite")->insert($data);
+
+                if($add){
+                    $this->success("收藏成功");
+                }else{
+                    $this->error("收藏失败");
+                }
+
+            }
+
+        } catch (Exception $e){
+            $this->error($e->getMessage());
+        }
+
+    }
+
+    /**
+     * 我的地址管理
+     * @return void
+     */
+    public function getAddressList(){
+
+        try {
+
+            $user_id = $this->auth->id;
+
+            $list = Db::name("unishop_address")->where("user_id",$user_id)->order("is_default desc,id desc")->select();
+
+            if(!empty($list)){
+                foreach ($list as $key=>$value){
+
+                    $list[$key]['province'] = Db::name("unishop_area")->where("id",$value['province_id'])->value("name");
+                    $list[$key]['city'] = Db::name("unishop_area")->where("id",$value['city_id'])->value("name");
+                    $list[$key]['area'] = Db::name("unishop_area")->where("id",$value['area_id'])->value("name");
+
+                }
+            }
+
+            $this->success("地址列表",$list);
+        } catch (Exception $e){
+
+            $this->error($e->getMessage());
+
+        }
+
+
+
+    }
+
+    /**
+     * 地址新增
+     * @return void
+     */
+    public function userAddressAdd(){
+
+        try {
+            $user_id = $this->auth->id;
+            $name = input('name','');
+            $mobile = input('mobile','');
+            $address = input('address','');
+            $province_id = input('province_id','');
+            $city_id = input('city_id','');
+            $area_id = input('area_id','');
+
+            if(empty($name)){
+                $this->error("请填写收件人姓名");
+            }
+
+            if (!preg_match('/^1[3456789]\d{9}$/', $mobile)) {
+                $this->error("手机号码格式正确");;
+            }
+            if(empty($address)){
+                $this->error("请填写具体收件地址");
+            }
+
+            $data['user_id'] = $user_id;
+            $data['name'] = $name;
+            $data['mobile'] = $mobile;
+            $data['address'] = $address;
+            $data['province_id'] = $province_id;
+            $data['city_id'] = $city_id;
+            $data['area_id'] = $area_id;
+            $data['createtime'] = time();
+            $data['updatetime'] = time();
+
+            $add = Db::name("unishop_address")->insert($data);
+            if($add){
+                $this->success("添加成功");
+            }else{
+                $this->error("添加失败");
+            }
+
+        } catch (Exception $e){
+            $this->error($e->getMessage());
+        }
+
+    }
 }

+ 25 - 0
application/common.php

@@ -738,6 +738,31 @@ if (!function_exists('getweeknum')) {
 
 
     }
+
+    /**
+     * 生成唯一邀请码
+     */
+    if (!function_exists('generateUniqueInvitationCode')) {
+        function generateUniqueInvitationCode($length) {
+            $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // 仅包含数字字母
+            $code = '';
+            $generatedCodes = [];
+            do {
+                $randomIndex = '';
+                $codeLength = $length - 1; // 减去第一位作为校验码
+
+                for ($i = -1; $i < $codeLength; $i++) {
+                    $randomIndex .= $characters[rand(0, strlen($characters) - 1)];
+                }
+
+                $code = $randomIndex;
+            } while (in_array($code, $generatedCodes));
+
+            $generatedCodes[] = $code; // 存储已生成的邀请码
+
+            return $code;
+        }
+    }
 }
 
 

+ 56 - 0
application/common/model/User.php

@@ -184,4 +184,60 @@ class User extends Model
             return false;
         }
     }
+
+    /**
+     * 添加提现记录
+     * @param int $user_id 用户ID
+     * @param numeric $money  提现金额
+     * @param $out_trade_no 交易凭证|暂定
+     * @return void
+     */
+    public function add(int $user_id,$money,$out_trade_no){
+        //开启事务
+        Db::startTrans();
+        try {
+
+            $data['withdraw_sn'] = getOrderSn('T');
+            $data['user_id'] = $user_id;
+            $data['money'] = $money;
+            $data['out_trade_no'] = $out_trade_no;
+            $data['remark'] = "用户申请提现";
+            $data['createtime'] = time();
+
+            $add = Db::name("unishop_withdraw")->insert($data);
+            if($add){
+
+                //减少用户余额
+                $user_money = self::where("id",$user_id)->value("money");
+                $upd_money = bcsub((string)$user_money,(string)$money,2);
+                $upd = self::where("id",$user_id)->update(['money'=>$upd_money]);
+                if($upd){
+                    $points_log = setUserMoneyLog($user_id,$user_money,$money,$upd_money,2,"用户申请提现");
+                    if($points_log == 1){
+                        Db::commit();
+                        return true;
+                    }else{
+                        Db::rollback();
+                        return false;
+                    }
+                }else{
+                    Db::rollback();
+//                    Logs('批发商品ID为:'.$value['id'].'减少用户余额失败','User_withdraw');
+                    return false;
+                }
+
+            }else{
+
+                Db::rollback();
+                return false;
+            }
+
+
+        } catch (Exception $e){
+
+            Db::rollback();
+            return false;
+        }
+
+    }
 }