request->isPost) { $data = [ 'key' => \post_params('key', ''), 'secret' => \post_params('secret', ''), 'url' => \post_params('url', ''), ]; Option::set('stbz_account', json_encode($data), 0, 'saas'); try { $res = $this->regSupplier(); if ($res['code'] != 0) { return $this->asJson([ 'code' => 1, 'msg' => $res['msg'], ]); } return $this->asJson([ 'code' => 0, 'msg' => '保存成功', ]); } catch (\Exception $e) { return $this->asJson([ 'code' => 0, 'msg' => $e->getMessage(), ]); } } $data = Option::get('stbz_account', 0, 'saas', \json_encode([ 'key' => '', 'secret' => '', 'url' => '' ])); return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => json_decode($data['value']), ]); } // 注册供货商账号 public function regSupplier() { $supplier = Supplier::find()->where(['is_stbz' => 1])->one(); if ($supplier) { return [ 'code' => 0, 'msg' => '账号已创建', ]; } $name = Yii::$app->security->generateRandomString(24); $password = '12345678'; $logo = \Yii::$app->request->hostInfo . '/web/v1/statics/images/stbz.jpg'; $form = new PlatformForm(); $form->supplier_name = '胜天半子'; $form->name = $name; $form->password = $password; $form->tel = '11111111111'; $form->logo = $logo; $form->type = '1'; $form->rate = '0.00'; $form->email = 'stbz@qq.com'; $form->certificate = $logo; $form->id_card_front = $logo; $form->id_card_reverse = $logo; $form->sale_day = 7; $res = $form->createSupplier(); if ($res['code'] == 0) { $supplier_id = $res['data']['cloud_supplier_id']; $form = new PlatformForm(); $form->status = 1; $form->supplier_id = $supplier_id; $ss = $form->setSupplierStatus(); } return $res; } /** * 获取来源列表 * @return \yii\web\Response * @throws \yii\base\InvalidConfigException * @throws \yii\db\Exception * @author Syan mzsongyan@gmail.com * @date 2023-09-14 */ public function actionGetSourceList() { $key = md5(json_encode(['type' => 'stbz_get_source_list'])); if (\Yii::$app->cache->exists($key)) { $data = \Yii::$app->cache->get($key); $returnData = \json_decode($data, true); } else { $data = \stbz_client()->getApiResponse('post', '/v2/GetSource'); $returnData = \json_decode($data, true); if ($returnData['code'] != 1) { return $this->asJson([ 'code' => 1, 'msg' => $returnData['msg'] ?: $returnData['message'], ]); } \Yii::$app->cache->set($key, $data, 600); } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => $returnData['data'], ]); } /** * 获取一级分类列表 * @return \yii\web\Response * @author Syan mzsongyan@gmail.com * @date 2022-09-13 */ public function actionGetCatList() { $source = \get_params('source', 0); $key = md5(json_encode(['type' => 'stbz_get_cat_list', 'source' => $source])); if (\Yii::$app->cache->exists($key)) { $data = \Yii::$app->cache->get($key); $returnData = \json_decode($data, true); } else { $data = \stbz_client()->getApiResponse('get', '/v2/Category/GetCategory', [ 'parent_id' => 0, 'source' => $source, ]); $returnData = \json_decode($data, true); if ($returnData['code'] != 1) { return $this->asJson([ 'code' => 1, 'msg' => $returnData['msg'] ?: $returnData['message'], 'data' => $returnData, ]); } \Yii::$app->cache->set($key, $data, 600); } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => $returnData['data'], ]); } /** * 获取选品库商品列表 * @return \yii\web\Response * @author Syan mzsongyan@gmail.com * @date 2022-09-13 */ public function actionGetGoodsList() { $search_words = \get_params('search_words'); $page = \get_params('page', 1); $limit = \get_params('limit', 10); $category_id = \get_params('category_id', 0); $source = \get_params('source', 0); // 协议价 $price_left = \get_params('price_left', 0); $price_right = \get_params('price_right', 0); // 指导价 $zhidao_price_left = \get_params('zhidao_price_left', 0); $zhidao_price_right = \get_params('zhidao_price_right', 0); // 营销价 $yingxiao_price_left = \get_params('yingxiao_price_left', 0); $yingxiao_price_right = \get_params('yingxiao_price_right', 0); // 常规利润率 $changgui_lirun_left = \get_params('changgui_lirun_left', 0); $changgui_lirun_right = \get_params('changgui_lirun_right', 0); // 营销利润率 $yingxiao_lirun_left = \get_params('yingxiao_lirun_left', 0); $yingxiao_lirun_right = \get_params('yingxiao_lirun_right', 0); // 是否包邮 $is_baoyou = \get_params('is_baoyou', -1); // 排序字段 $sort_field = \get_params('sort_field', ''); $sort_type = \get_params('sort_type', ''); $params = [ 'page' => $page > 50 ? 50 : $page, 'limit' => $limit, 'recommend' => 2, ]; if ($source > 0) { $params['source'] = $source; } if ($search_words) { $params['search_words'] = $search_words; } if ($category_id > 0) { $params['category_id'] = $category_id; } if ($price_left > 0) { // 协议价 $params['agreement_price'] = [ 'from' => $price_left, 'to' => $price_right > 0 ? $price_right : 99999, ]; } if ($zhidao_price_left > 0) { // 指导价 $params['guide_price'] = [ 'from' => $zhidao_price_left, 'to' => $zhidao_price_right > 0 ? $zhidao_price_right : 99999, ]; } if ($yingxiao_price_left > 0) { // 营销价 $params['activity_price'] = [ 'from' => $yingxiao_price_left, 'to' => $yingxiao_price_right > 0 ? $yingxiao_price_right : 99999, ]; } if ($changgui_lirun_left > 0) { // 常规利润率 $params['promotion_rate'] = [ 'from' => $changgui_lirun_left, 'to' => $changgui_lirun_right > 0 ? $changgui_lirun_right : 99999, ]; } if ($yingxiao_lirun_left > 0) { // 营销利润率 $params['activity_rate'] = [ 'from' => $yingxiao_lirun_left, 'to' => $yingxiao_lirun_right > 0 ? $yingxiao_lirun_right : 99999, ]; } if ($is_baoyou > -1) { $params['is_free_shipping'] = $is_baoyou; } if ($sort_field) { $params['type'] = $sort_field; } if ($sort_type) { $params['sort'] = $sort_type; } $key = md5(json_encode(\array_merge($params, ['cache_type' => 'stbz_get_goods_list']))); if (\Yii::$app->cache->exists($key)) { $data = \Yii::$app->cache->get($key); $returnData = \json_decode($data, true); } else { $data = \stbz_client()->getApiResponse('post', '/v2/GoodsStorage/NewList', $params); $returnData = \json_decode($data, true); if ($returnData['code'] != 1) { return $this->asJson([ 'code' => 1, 'msg' => $returnData['msg'] ?: $returnData['message'], ]); } \Yii::$app->cache->set($key, $data, 600); } foreach ($returnData['data']['list'] as &$datum) { $datum['rate'] = 0; $diff_price = $datum['guide_price'] - $datum['agreement_price']; if ($diff_price > 0) { $datum['rate'] = sprintf("%.2f", $diff_price / $datum['agreement_price']); } } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => $returnData['data'], ]); } /** * 获取商品详情 * @return \yii\web\Response * @author Syan mzsongyan@gmail.com * @date 2022-09-16 */ public function actionGetGoodsItem($goodsId = null) { $id = \get_params('id', $goodsId); $params = [ 'id' => $id, ]; $key = md5(json_encode(\array_merge($params, ['type' => 'stbz_get_goods_item']))); if (\Yii::$app->cache->exists($key)) { $data = \Yii::$app->cache->get($key); $returnData = \json_decode($data, true); } else { $data = \stbz_client()->getApiResponse('get', '/v2/Goods/Detail', $params); $returnData = \json_decode($data, true); if ($returnData['code'] != 1) { return $this->asJson([ 'code' => 1, 'msg' => $returnData['msg'] ?: $returnData['message'], ]); } \Yii::$app->cache->set($key, $data, 600); } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => $returnData['data'], ]); } public function actionCheckGoodsImport() { try { $post = \post_params(); $stbz_id = $post['id']; $cloud_store_token = get_platform_token(); $goods_url = "/cloud/user/getStbzGoods"; $goods_data = []; $goods_data['access_token'] = $cloud_store_token; //获取供货商的token信息 $goods_data['id'] = $stbz_id; //默认为 1 $domain = (new OptionSetting)->getCloudDomainName(); $goods_data_info = cloud_post($domain . $goods_url,$goods_data); $goods_data_info = json_decode($goods_data_info,true); if($goods_data_info['code'] != 0){ throw new \Exception($goods_data_info['msg']); }else{ return $this->asJson($goods_data_info); } } catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } //批量添加云仓产品 public function actionAddCloudGoodsGroup() { $post = \post_params(); try { $ids = $post['ids']; if (is_string($ids)) { $ids = explode(',', $ids); } foreach ($ids as $id) { $goods = $this->actionGetGoodsItem($id); $goods = $goods->data; if ($goods['code'] == 1) { throw new \Exception($goods['msg']); } $attr = $this->handleAttr($goods['data']['specs'], $goods['data']['cover']); $data = [ 'cat_id' => \implode(',', $post['cat']), 'name' => $goods['data']['title'], 'goods_no' => (string)$id, 'g_s_pic_url' => $goods['data']['cover'], 'desc' => $goods['data']['description'], 'g_price' => sprintf('%.2f', $goods['data']['cost_price'] * 0.01), 'g_wholesale_price' => sprintf('%.2f', $goods['data']['sale_price'] * 0.01), 'g_wholesale_ladder_rebate' => '[]', 'g_wholesale_rebate_switch' => '0', 'weight' => '', 'g_original_price' => sprintf('%.2f', $goods['data']['sale_price'] * 0.01), 'g_unit' => $goods['data']['unit'], 'g_shop_count' => 1, 'num' => $goods['data']['total_stock'], 'use_attr' => '1', 'g_pic_list' => $goods['data']['covers'], 'g_send_type' => '0', 'attrs' => $attr, 'is_change' => 1, 'stbz_goods_id' => $id, 'market_price' => sprintf('%.2f', $goods['data']['market_price'] * 0.01), ]; $result = $this->commonHandle($data, $id); if (intval($result['code']) !== 0) { throw new \Exception($result['msg']); } } return $this->asJson([ 'code' => 0, 'msg' => '操作成功' ]); } catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() . $e->getLine() ]); } } // 添加云仓商品 public function actionAddCloudGoods() { $post = \post_params(); $goods = $this->actionGetGoodsItem($post['id']); $goods = $goods->data; if ($goods['code'] == 1) { return $this->asJson([ 'code' => 1, 'msg' => $goods['msg'], ]); } if ($post['attr']) { foreach ($post['attr'] as &$attr_item) { $attr_item['original_price'] = $attr_item['wholesale_price']; } } $data = [ 'cat_id' => \implode(',', $post['cat']), 'name' => $post['name'], 'goods_no' => (string)$post['id'], 'g_s_pic_url' => $goods['data']['cover'], 'desc' => $goods['data']['description'], 'g_price' => $post['price'], 'g_wholesale_price' => $post['wholesale_price'], 'g_wholesale_ladder_rebate' => '[]', 'g_wholesale_rebate_switch' => '0', 'weight' => '', 'g_original_price' => $post['original_price'], 'g_unit' => $goods['data']['unit'], 'g_shop_count' => 1, 'num' => $goods['data']['total_stock'], 'use_attr' => '1', 'g_pic_list' => $goods['data']['covers'], 'g_send_type' => '0', 'attrs' => $post['attr'], 'is_change' => 1, 'stbz_goods_id' => $post['id'], 'market_price' => sprintf('%.2f', ($goods['data']['market_price'] * 0.01)) ]; return $this->asJson($this->commonHandle($data, $post['id'])); } public function commonHandle($data, $id = 0) { $supplier = Supplier::findOne(['is_stbz' => 1, 'is_delete' => 0]); if (!$supplier) { return [ 'code' => 1, 'msg' => '供货商未找到', ]; } $params = [ 'goods_ids' => $id ]; $stbz_result = \stbz_client()->getApiResponse('get', '/v2/GoodsStorage/Add', $params); $returnData = \json_decode($stbz_result, true); if ($returnData['code'] != 1 && $returnData['code'] != 0) { return [ 'code' => 1, 'msg' => $returnData['msg'], ]; } $supplierForm = new SupplierForm(); $supplierForm->attributes = $data; $supplierForm->token_supplier_id = $supplier->cloud_supplier_id; $setGoods = $supplierForm->setGoods(); if ($setGoods['code'] == 1) { return $setGoods; } // 提交审核 $supplierForm = new SupplierForm(); $supplierForm->attributes = ['goods_id' => $setGoods['data']['goods_id']]; $supplierForm->token_supplier_id = $supplier->cloud_supplier_id; $submitAudit = $supplierForm->auditSubmit(); if ($submitAudit['code'] == 1) { return $submitAudit; } // 审核商品 $form = new PlatformForm(); $form->attributes = [ 'id' => $submitAudit['data']['goods_id'], 'status' => 1, ]; return $form->auditHandle(); } public function handleAttr($specs, $image) { $attr = []; foreach ($specs['options'] as $i) { $attr_list = []; $value_ids = explode('_', $i['spec_value_ids']); foreach ($value_ids as $ii) { foreach ($specs['values'] as $iii) { if ($ii == $iii['id']) { foreach ($specs['names'] as $iiii) { if ($iiii['id'] == $iii['spec_name_id']) { $attr_list = array_merge($attr_list, [ [ 'attr_group_name' => ($iiii['name'] === '默认' ? '规格' : $iiii['name']), 'attr_name' => $iii['name'] ] ]); } } } } } $attr = array_merge($attr, [ [ 'attr_list' => $attr_list, 'num' => $i['stock'], 'price' => sprintf('%.2f', $i['cost_price'] * 0.01), 'wholesale_price' => sprintf('%.2f', $i['sale_price'] * 0.01), 'original_price' => sprintf('%.2f', $i['sale_price'] * 0.01), 'no' => $i['id'], 'pic' => $i['image'] == '' ? $image : $i['image'], 'share_commission_first' => '', 'share_commission_second' => '', 'share_commission_third' => '', ] ]); } return $attr; } // 获取资金流水 public function actionGetCashFlow() { $page = \get_params('page', 1); $limit = \get_params('limit', 10); $params = [ 'page' => $page, 'limit' => $limit, ]; $key = md5(json_encode(\array_merge($params, ['type' => 'stbz_get_cash']))); if (\Yii::$app->cache->exists($key)) { $data = \Yii::$app->cache->get($key); $returnData = \json_decode($data, true); } else { $data = \stbz_client()->getApiResponse('get', '/v2/cash/flow', [ 'page' => $page, 'limit' => $limit, ]); $returnData = \json_decode($data, true); if ($returnData['code'] != 1) { return $this->asJson([ 'code' => 1, 'msg' => $returnData['msg'], ]); } \Yii::$app->cache->set($key, $data, 600); } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => $returnData['data'], ]); } public function actionSubmitOrder() { $post = \post_params(); $address_data = []; $orderNoHead = substr($post['order_no'], 0, 2); if($orderNoHead == 'PO'){ $address_data = json_decode($post['address_data'] ?? "", true); if (empty($address_data)) { $orderTransit = \app\models\OrderTransit::findOne(['cloud_order_id' => $post['id']]); if ($orderTransit) { $order = \app\models\Order::findOne($orderTransit->order_id); if ($order) { if ($order->is_offline && $order->md_id) { $province_name = ''; $city_name = ''; $district_name = ''; $address = ''; $md = \app\models\Md::findOne($order->md_id); if ($md) { $province_name = District::findOne($md->province)->name; $city_name = District::findOne($md->city)->name; $district_name = District::findOne($md->district)->name; $address = $md->address; } } else { $province_name = District::findOne($order->province_id)->name; $city_name = District::findOne($order->city_id)->name; $district_name = District::findOne($order->district_id)->name; $address = $order->address; } $address_data = [ 'province' => $province_name, 'city' => $city_name ?? '', 'district' => $district_name ?? '', 'address' => $address ?? '' ]; } } } } if (isset($post['pifa']) && $post['pifa']) { $address = $address_data; if (empty($address)) { $address = json_decode($post['address_data'], true); } $spu = []; foreach ($post['goods_list'] as $goods) { $spu[] = [ 'number' => $goods['num'], 'sku' => $goods['attr']['no'], ]; } $params = [ 'spu' => $spu, 'address' => [ 'consignee' => $post['name'], 'phone' => $post['mobile'], 'province' => $address['province'] ?: $post['province'], 'city' => $address['city'] ?: $post['city'], 'area' => $address['district'] ?: $post['district'], 'street' => '', 'description' => $address['address'] ?: $post['address'], ] ]; } else { $spu = []; foreach ($post['goods_list'] as $goods) { $spu[] = [ 'number' => $goods['num'], 'sku' => $goods['attr']['no'], ]; } $params = [ 'spu' => $spu, 'address' => [ 'consignee' => $post['name'], 'phone' => $post['mobile'], 'province' => $post['province'], 'city' => $post['city'], 'area' => $post['district'], 'street' => '', 'description' => $post['address'], ] ]; } $checkData = \stbz_client()->getApiResponse('post', '/v2/order/beforeCheck', $params); $checkData = \json_decode($checkData, true); if ($checkData['code'] != 1) { return $this->asJson([ 'code' => 1, 'msg' => $checkData['msg'], ]); } $params['orderSn'] = $post['order_no']; $data = \stbz_client()->getApiResponse('post', '/v2/order', $params); $data = \json_decode($data, true); if ($data['code'] != 1) { return $this->asJson([ 'code' => 1, 'msg' => $data['msg'], ]); } //获取订单信息 $order_url = "/cloud/supplier/supplierSetStbzOrderStatus"; $order_data['order_id'] = $post['id']; $order_data['status'] = 1; $order_data['access_token'] = get_supplier_token($post['supplier_id']); $orderNoHead = substr($post['order_no'], 0, 2); if ($orderNoHead === 'PO') { $order_data['order_type'] = 1; } elseif ($orderNoHead === 'OR') { $order_data['order_type'] = 2; } $order_data['status'] = 1; $domain = (new OptionSetting)->getCloudDomainName(); $orderInfo = cloud_post($domain. $order_url, $order_data); $orderInfo = json_decode($orderInfo, true); if ((int)$orderInfo['code'] !== 0) { return $this->asJson($orderInfo); } return $this->asJson([ 'code' => 0, 'msg' => '下单成功', ]); } }