Ver Fonte

有赞信息

wufeng há 2 anos atrás
pai
commit
3a2513d87c

+ 61 - 0
app/youzan/controller/Index.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace app\youzan\controller;
+
+use app\admin\controller\Base;
+use app\youzan\logic\Cat;
+use app\youzan\logic\Goodup;
+use think\facade\Validate;
+
+
+//有赞对接类
+class Index extends Base
+{
+
+    //获取有赞的类目列表
+    public function getYzCatList()
+    {
+
+        $parent_cid = $this->request->post('parent_cid/d', 0, 'trim');
+
+        $rs = Cat::getYzCatList($parent_cid);
+
+        return app_show(0, '请求成功', $rs);
+
+    }
+
+    //商品上线—有赞平台对接
+    public function youzanUpOnline()
+    {
+        $param = $this->request->only(['good_platform_id', 'yz_cat_id', 'cat_id', 'support_refund', 'origin', 'price', 'token']);
+
+        $val = Validate::rule([
+            'good_platform_id|商品上线记录ID' => 'require|number|gt:0',
+            'yz_cat_id|有赞分类' => 'require|number|gt:0',
+            'cat_id|自有分类' => 'require|number|gt:0',
+            'support_refund|是否可退货' => 'require|number|in:0,1',
+            'origin|系统售价' => 'require|float',
+            'price|最后售价' => 'require|float',
+            'token' => 'require',
+        ]);
+
+        if ($val->check($param)) return Goodup::youzanGoodUpOnline($param['good_platform_id'], $param);
+        else return error_show(1005, $val->getError());
+    }
+
+    //商品下线—有赞平台对接
+    public function youzanOffline()
+    {
+        $param = $this->request->only(['good_platform_id', 'token']);
+
+        $val = Validate::rule([
+            'good_platform_id|商品上线记录ID' => 'require|number|gt:0',
+            'token' => 'require',
+        ]);
+
+        if ($val->check($param)) return Goodup::youzanGoodOffline($param['good_platform_id'], $param);
+        else return error_show(1005, $val->getError());
+    }
+
+
+}

+ 39 - 0
app/youzan/logic/Cat.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace app\youzan\logic;
+
+use app\youzan\model\YzCat;
+use think\facade\Cache;
+use think\facade\Db;
+
+
+//分类处理层
+class Cat
+{
+
+    private static $cache_key = 'yz_cat_list_pid_';
+
+    //获取分类列表
+    public static function getYzCatList(int $parent_cid = 0)
+    {
+
+        $key = self::$cache_key . (string)$parent_cid;
+
+        $rs = Cache::get($key);
+
+        if (empty($rs)) {
+
+            $rs = YzCat::field('id,yz_cat_id,yz_cat_name,parent_cid')
+                ->where('status', YzCat::$status_normal)
+                ->where('parent_cid', $parent_cid)
+                ->order('id')
+                ->select()
+                ->toArray();
+
+            Cache::set($key, $rs, 3600);
+        }
+
+
+        return $rs;
+    }
+}

+ 130 - 0
app/youzan/logic/Goodup.php

@@ -0,0 +1,130 @@
+<?php
+
+namespace app\youzan\logic;
+
+use app\youzan\model\GoodPlatform;
+use think\Exception;
+use think\facade\Cache;
+use think\facade\Db;
+
+
+//商品处理层
+class Goodup
+{
+
+    //redis队列对应的key,要与有赞对接项目中的key保持一致,不要轻易修改
+    private static $redis_good_up_online_key = 'yz_good_up_online';
+    private static $redis_good_offline_key = 'yz_good_offline';
+
+    //添加商品上线到处理队列中
+    public static function youzanGoodUpOnline(int $good_platform_id = 0, array $data = [])
+    {
+
+        Db::startTrans();
+
+        try {
+
+            $db = new GoodPlatform();
+
+            $rs = $db
+                ->fetchSql()
+                ->field('gp.id,gp.spuCode,gp.skuCode,gp.exam_status,gp.status,gb.good_name,gb.weight,gb.good_img')
+                ->alias('gp')
+                ->where(['gp.id' => $good_platform_id, 'gp.is_del' => $db::$del_normal])
+                ->leftJoin('good_basic gb', 'gb.spuCode=gp.spuCode')
+                ->findOrEmpty();
+
+            halt($rs);
+
+            if ($rs->isEmpty()) throw new Exception('该商品上线记录不存在');
+            if ($rs->exam_status != $db::$exam_status_6) throw new Exception('该商品尚未上线成功');
+            if ($rs->status == $db::$status_online_success) throw new Exception('该商品已经在有赞平台上线了');
+
+            $userinfo = GetUserInfo($data['token']);
+
+            //入队列
+            Cache::store("redis")->handler()->lPush(self::$redis_good_up_online_key, json_encode([
+                'item_no' => $rs->skuCode,//商品自定义编码 skuCode
+                'item_type' => 0,//0实物商品
+                'title' => $rs->good_name,//商品标题
+                'is_support_barter' => $data['support_refund'],//是否支持换货。1:支持;0:不支持
+                'desc' => '我是商品描述',//商品描述
+                'item_weight' => $rs->weight,
+                'category_id' => $data['cat_id'],
+                'auto_listing_time' => '0',//0立即售出,传值表示定时(要大于当前时间戳)
+                'stock_deduct_mode' => 0,//0拍下减库存
+                'is_display' => 1,//1上架商品
+                'quantity' => '1111111111',//库存数,先随便写个数,待 @戴 确定
+                'hide_stock' => 0,//0显示库存,1不显示库存
+                'origin' => $data['origin'],//系统售价
+                'price' => $data['price'],//最后售价
+                'uid' => isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
+                'nickname' => isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '',
+                'good_platform_id' => $good_platform_id,
+                'good_img' => $rs->good_img,//图片集合
+                'post_fee' => 0,//运费,单位分,整数
+                'sell_point' => '',//商品卖点信息
+                'yz_cat_id' => $data['yz_cat_id'],//有赞类目id
+            ]));
+
+            //更新状态值
+            $db->where('id', $good_platform_id)
+                ->where('status', '<>', $db::$status_online_success)
+                ->save([
+                    'status' => $db::$status_online_ing,
+                    'updatetime' => date('Y-m-d H:i:s'),
+                ]);
+
+            Db::commit();
+
+            return app_show(0, '操作成功');
+
+        } catch (Exception $exception) {
+            Db::rollback();
+            return error_show(1005, $exception->getMessage());
+        }
+
+    }
+
+    //添加商品下线到处理队列中
+    public static function youzanGoodOffline(int $good_platform_id = 0, array $data = [])
+    {
+
+        Db::startTrans();
+
+        try {
+
+            $db = new GoodPlatform();
+
+            $rs = $db
+                ->field('gp.id,gp.skuCode,gp.exam_status,gp.status')
+                ->alias('gp')
+                ->where(['gp.id' => $good_platform_id, 'gp.is_del' => $db::$del_normal])
+                ->findOrEmpty();
+
+            if ($rs->isEmpty()) throw new Exception('该商品上线记录不存在');
+            if ($rs->exam_status != $db::$exam_status_8) throw new Exception('该商品尚未下线成功');
+            if ($rs->status == $db::$status_offline) throw new Exception('该商品已经在有赞平台下线了');
+
+            $userinfo = GetUserInfo($data['token']);
+
+            //入队列
+            Cache::store("redis")->handler()->lPush(self::$redis_good_offline_key, json_encode([
+                'item_no' => $rs->skuCode,//商品自定义编码 skuCode
+                'uid' => isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
+                'nickname' => isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '',
+                'good_platform_id' => $good_platform_id,
+            ]));
+
+            Db::commit();
+
+            return app_show(0, '操作成功');
+
+        } catch (Exception $exception) {
+            Db::rollback();
+            return error_show(1005, $exception->getMessage());
+        }
+
+    }
+
+}

+ 33 - 0
app/youzan/model/GoodPlatform.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace app\youzan\model;
+
+use think\Model;
+
+//商品上线平台表
+class GoodPlatform extends Model
+{
+    protected $table = 'wsm_good_platform';
+    protected $pk = 'id';
+    protected $autoWriteTimestamp = false;
+
+    public static $del_normal = 0;//未删除
+    public static $deleted = 1;//已删除
+
+    public static $exam_status_0 = 0;//待提交
+    public static $exam_status_1 = 1;//待完善成本
+    public static $exam_status_2 = 2;//待产品审核
+    public static $exam_status_3 = 3;//待财务定价
+    public static $exam_status_4 = 4;//待财务审核定价
+    public static $exam_status_5 = 5;//待上线
+    public static $exam_status_6 = 6;//上线成功
+    public static $exam_status_7 = 7;//审核失败
+    public static $exam_status_8 = 8;//已下线
+
+    public static $status_online_ing = 1;//有赞待上线(处理中)
+    public static $status_online_success = 2;//有赞上线成功
+    public static $status_online_fail = 3;//有赞上线失败
+    public static $status_offline = 4;//有赞已下线
+
+
+}

+ 18 - 0
app/youzan/model/YzCat.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace app\youzan\model;
+
+use think\Model;
+
+//有赞类目表
+class YzCat extends Model
+{
+    protected $connection = 'mysql_yz';//切换连接参数
+    protected $table = 'yz_cat';
+    protected $pk = 'id';
+    protected $autoWriteTimestamp = false;
+
+    public static $status_normal = 1;//状态:正常
+    public static $status_delete = 2;//状态:删除
+
+}

+ 5 - 1
app/youzan/route/app.php

@@ -1,4 +1,8 @@
 <?php
+
 use think\facade\Route;
 
-route::rule("push","youzan/Push/GetPush");
+route::rule('push', 'youzan/Push/GetPush');
+route::rule('yz_cat_list', 'youzan/Index/getYzCatList');//有赞类目列表
+route::rule('yz_goodup', 'youzan/Index/youzanUpOnline');//将商品推送到有赞平台
+route::rule('yz_goodoff', 'youzan/Index/youzanOffline');//将商品从有赞平台下线