// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\console\service; use app\common\library\express\Usps; use app\common\library\helper; use app\console\library\Tools; use app\common\service\BaseService; use app\console\model\Order as OrderModel; use think\facade\Log; /** * 服务类:会员等级 * Class UserGrade * @package app\console\service */ class OrderGetYundan extends BaseService { /** * 设置用户的会员等级 * @param int $storeId * @return array|bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function orderCreateYundans(int $storeId) { $Usps = new Usps(); // 请求查询接口 $token = $Usps->getItdidaToken(); if (empty($token)) { Log::error('无有效的Iddida token'); return false; } // 获取所有待发货订单 $list = $this->getUsableList($storeId); // 遍历等级,根据升级条件 查询满足消费金额的用户列表,并且他的等级小于该等级 $data = []; $url = 'http://f1.itdida.com/itdida-api/yundans'; $params = [ "baoGuanFangShi" => 0, "baoGuoLeiXing" => 1, "cardType" => 0, "clientId" => $token, "danJianList" => [ [ "chang" => 1, "gao" => 1, "kuan" => 1, "origBoardNum" => 0, "shiZhong" => 0.5, "stackable" => true ] ], "eori" => "", "extraFreight" => 0, "freightPaymentWay" => 0, "fuShuiJin" => 0, "guoJia" => "US", "huoWuTeXing" => "带电", "ioss" => "", "jianShu" => 1,//to fix "kapaiWaybill" => false, "keHuDanHao" => "",//todo fill "maiJiaId" => "",//to fill "needDispatch" => false, "needValidateAddress" => false, "pushBackLabelAndInvoice" => false, "qiTaDanHao1" => "JCX0415047275YQ", "requiredChangeNo" => false, "requiredTrackNo" => true, "requiredZhuanDanFaPiao" => false, "shenBaoXinXiList" => [ [ "shenBaoBiZhong" => "USD", "shenBaoDanJia" => 0,//todo fill 申报单价 "shenBaoHaiGuanBianMa" => "", "shenBaoJinE" => 0,////todo fill 申报金额 "shenBaoPinMing" => "",//todo fill 品名 "shenBaoShuLiang" => 1, "zhongWenPinMing" => ""//to fill ], ], "shouHuoQuDao" => "美国专线小包普货", "shouHuoShiZhong" => 0.500, "shouJianRenChengShi" => "",//to fill收货城市 "shouJianRenDiZhi1" => "",//to fill收货地址 "shouJianRenDiZhi2" => "", "shouJianRenDiZhi3" => "", "shouJianRenDianHua" => "",//to fill收货电话 "shouJianRenGongSi" => "", "shouJianRenXingMing" => "",//to fill收货人姓名 "shouJianRenYouBian" => "",//to fill收货人邮编 "skuText" => "null,null,null", "synPushBill" => false, "tradeTerms" => "", "useReferenceApi" => false, "wuLiuFangShi" => 2, "zhouMing" => ""//to fill收货人州名 ]; $headers = [ "Content-type: application/json;charset=utf-8", //'Content-Length: ' . strlen($data_string)), "Authorization: {$token}", "Accept: */*" ]; foreach ($list as $item) { $params['keHuDanHao'] = $item['order_no']; $params['maiJiaId'] = $item['user_id']; $params['zhouMing'] = $item['address']['region']['code']; $params['shouJianRenChengShi'] = $item['address']['city']; $params['shouJianRenYouBian'] = $item['address']['zip_code']; $params['shouJianRenDianHua'] = $item['address']['phone']; $params['shouJianRenDiZhi1'] = $item['address']['detail']; $params['shouJianRenXingMing'] = $item['address']['name'] . ' ' . $item['address']['last_name']; $goodsList = []; foreach ($item['goods'] as $good) { $goodsList[] = [ "shenBaoBiZhong" => "USD", "shenBaoDanJia" => $good['goods_price'],//todo fill 申报单价 "shenBaoHaiGuanBianMa" => "", "shenBaoJinE" => $good['total_price'],//todo fill 申报金额 "shenBaoPinMing" => "vape",//$good['goods_name'],//todo fill 品名 "shenBaoShuLiang" => $good['total_num'], "zhongWenPinMing" => "电子烟"//todo fill ]; } $params['shenBaoXinXiList'] = $goodsList; $resJson = curl_post($url, [$params], $headers); Log::write('itdida::' . $resJson); $res = json_decode($resJson, true); // 遍历整理数据 if (isset($res['data']) && $res['data'][0]['code'] == 200) { $model = \app\store\model\Order::detail($item['order_id']); if (!$model->delivery(['express_id' => 10001, 'express_no' => $res['data']['zhuanDanHao']])) { Log::write('发货失败orderId:' . $item['order_id'],'error'); } }else{ Log::write('itdida运单失败::orderNo:' . $item['order_no'] . $resJson,'error'); } sleep(1); } // 记录日志 Tools::taskLogs('UserGrade', 'setUserGrade', [ 'storeId' => $storeId, 'data' => $data ]); } /** * 获取所有会员等级 * @param int $storeId 商城ID * @return false|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ private function getUsableList(int $storeId) { $orderModel = new OrderModel(); return $orderModel->getUsableList($storeId); } }