| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils\Delivery;
- use app\models\DeliveryInfo;
- use app\models\District;
- use app\models\Option;
- use app\models\Order;
- use app\models\SaasUser;
- use app\models\Store;
- use app\models\User;
- use app\utils\address\Address;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- /**
- * 即时配送
- * Class Delivery
- * @package app\utils\Delivery
- */
- class Delivery extends DeliveryService
- {
- /**
- * @var array $args 下单参数
- */
- private static $args;
- /**
- * 顺丰
- */
- const SF = 1;
- /**
- * 达达
- */
- const DD = 2;
- /**
- * 闪送
- */
- const SS = 3;
- /**
- * 美团
- */
- const MT = 4;
- /**
- * uu跑腿
- */
- const UU = 5;
- /**
- * 测试
- */
- const TEST = 6;
- /**
- * @var string[] $deliveryIdArr 数组
- */
- public static $deliveryIdArr = [
- self::SF => 'SFTC', // 顺丰同城急送
- self::DD => 'DADA', // 达达
- self::SS => 'SS', // 闪送
- self::MT => 'MTPS', // 美团配送
- self::UU => 'UU', // uu跑腿
- self::TEST => 'TEST' // TEST
- ];
- /**
- * @var string[] $deliveryType 数组
- */
- public static $deliveryType = [
- self::SF => '顺丰同城急送',
- self::DD => '达达',
- self::SS => '闪送',
- self::MT => '美团配送',
- self::UU => 'uu跑腿',
- self::TEST => '测试' // TEST
- ];
- /**
- * order_status 枚举值
- */
- const DELIVERY_STATUS_101 = 101;
- const DELIVERY_STATUS_102 = 102;
- const DELIVERY_STATUS_103 = 103;
- const DELIVERY_STATUS_201 = 201;
- const DELIVERY_STATUS_202 = 202;
- const DELIVERY_STATUS_203 = 203;
- const DELIVERY_STATUS_204 = 204;
- const DELIVERY_STATUS_205 = 205;
- const DELIVERY_STATUS_301 = 301;
- const DELIVERY_STATUS_302 = 302;
- const DELIVERY_STATUS_303 = 303;
- const DELIVERY_STATUS_304 = 304;
- const DELIVERY_STATUS_305 = 305;
- const DELIVERY_STATUS_401 = 401;
- const DELIVERY_STATUS_501 = 501;
- const DELIVERY_STATUS_502 = 502;
- public static $validOrderStatus = [
- self::DELIVERY_STATUS_101 => '等待分配骑手',
- self::DELIVERY_STATUS_102 => '分配骑手成功',
- self::DELIVERY_STATUS_103 => '商家取消订单',
- self::DELIVERY_STATUS_201 => '骑手到店开始取货',
- self::DELIVERY_STATUS_202 => '取货成功',
- self::DELIVERY_STATUS_203 => '取货失败,商家取消订单',
- self::DELIVERY_STATUS_204 => '取货失败,骑手因自身原因取消订单',
- self::DELIVERY_STATUS_205 => '取货失败,骑手因商家原因取消订单',
- self::DELIVERY_STATUS_301 => '配送中',
- self::DELIVERY_STATUS_302 => '配送成功',
- self::DELIVERY_STATUS_303 => '商家取消订单,配送物品开始返还商家',
- self::DELIVERY_STATUS_304 => '无法联系收货人,配送物品开始返还商家',
- self::DELIVERY_STATUS_305 => '收货人拒收,配送物品开始返还商家',
- self::DELIVERY_STATUS_401 => '货品返还商户成功',
- self::DELIVERY_STATUS_501 => '因运力系统原因取消',
- self::DELIVERY_STATUS_502 => '因不可抗拒因素(天气,道路管制等原因)取消'
- ];
- /**
- * @var int[] 骑手已接单
- */
- public static $validRiderReceive = [
- self::DELIVERY_STATUS_102
- ];
- /**
- * @var int[] 骑手已取货
- */
- public static $validRiderGetGoods = [
- self::DELIVERY_STATUS_202,
- self::DELIVERY_STATUS_301
- ];
- /**
- * @var int[] 配送已完成
- */
- public static $validComplete = [
- self::DELIVERY_STATUS_302,
- ];
- /**
- * @var int[] 配送异常
- */
- public static $validError = [
- self::DELIVERY_STATUS_103,
- self::DELIVERY_STATUS_203,
- self::DELIVERY_STATUS_204,
- self::DELIVERY_STATUS_205,
- self::DELIVERY_STATUS_303,
- self::DELIVERY_STATUS_304,
- self::DELIVERY_STATUS_305,
- self::DELIVERY_STATUS_501,
- self::DELIVERY_STATUS_502
- ];
- /**
- * 取消配送单原因枚举
- */
- const REASON_ID_1 = 1;
- const REASON_ID_2 = 2;
- const REASON_ID_3 = 3;
- const REASON_ID_4 = 4;
- const REASON_ID_5 = 5;
- const REASON_ID_6 = 6;
- public static $validReason = [
- self::REASON_ID_1 => '暂时不需要邮寄',
- self::REASON_ID_2 => '价格不合适',
- self::REASON_ID_3 => '订单信息有误,重新下单',
- self::REASON_ID_4 => '骑手取货不及时',
- self::REASON_ID_5 => '骑手配送不及时',
- self::REASON_ID_6 => '其他原因'
- ];
- /**
- * 分类品目
- */
- const CATEGORY_1 = 1; // 美食夜宵(一级)
- const CATEGORY_2 = 2; // 零食小吃
- const CATEGORY_3 = 3; // 香锅/烤鱼
- const CATEGORY_4 = 4; // 西餐
- const CATEGORY_5 = 5; // 日韩料理
- const CATEGORY_6 = 6; // 海鲜/烧烤
- const CATEGORY_7 = 7; // 快餐/地方菜
- const CATEGORY_8 = 8; // 小龙虾
- const CATEGORY_9 = 9; // 披萨
- const CATEGORY_10 = 10; // 甜品饮料(一级)
- const CATEGORY_11 = 11; // 甜品
- const CATEGORY_12 = 12; // 奶茶果汁
- const CATEGORY_13 = 13; // 咖啡
- const CATEGORY_14 = 14; // 面包/甜点
- const CATEGORY_15 = 15; // 冰淇淋
- const CATEGORY_16 = 16; // 蛋糕(一级)
- const CATEGORY_17 = 17; // 蛋糕
- const CATEGORY_18 = 18; // 日用百货(一级)
- const CATEGORY_19 = 19; // 便利店
- const CATEGORY_20 = 20; // 水站/奶站
- const CATEGORY_21 = 21; // 零食/干果
- const CATEGORY_22 = 22; // 五金日用
- const CATEGORY_23 = 23; // 粮油调味
- const CATEGORY_24 = 24; // 文具店
- const CATEGORY_25 = 25; // 酒水行
- const CATEGORY_26 = 26; // 地方特产
- const CATEGORY_27 = 27; // 进口食品
- const CATEGORY_28 = 28; // 宠物用品
- const CATEGORY_29 = 29; // 超市
- const CATEGORY_30 = 30; // 书店
- const CATEGORY_31 = 31; // 宠物食品用品
- const CATEGORY_32 = 32; // 办公家居用品
- const CATEGORY_33 = 33; // 果蔬生鲜
- const CATEGORY_34 = 34; // 果蔬
- const CATEGORY_35 = 35; // 海鲜水产
- const CATEGORY_36 = 36; // 冷冻速食
- const CATEGORY_37 = 37; // 鲜花
- const CATEGORY_38 = 38; // 鲜花
- const CATEGORY_39 = 39; // 医药健康
- const CATEGORY_40 = 40; // 送药
- const CATEGORY_41 = 41; // 器材器具
- const CATEGORY_42 = 42; // 美妆护肤
- const CATEGORY_43 = 43; // 日化美妆
- const CATEGORY_44 = 44; // 母婴
- const CATEGORY_45 = 45; // 孕婴用品
- const CATEGORY_46 = 46; // 文件或票务
- const CATEGORY_47 = 47; // 保单
- const CATEGORY_48 = 48; // 票务文件
- const CATEGORY_49 = 49; // 政府文件
- const CATEGORY_50 = 50; // 证件
- const CATEGORY_51 = 51; // 服饰鞋帽
- const CATEGORY_52 = 52; // 服饰鞋帽综合
- const CATEGORY_53 = 53; // 洗涤
- const CATEGORY_54 = 54; // 脏衣服收
- const CATEGORY_55 = 55; // 干净衣服派
- const CATEGORY_56 = 56; // 珠宝奢侈品
- const CATEGORY_57 = 57; // 珠宝饰品
- const CATEGORY_58 = 58; // 奢侈品
- const CATEGORY_59 = 59; // 家居家装
- const CATEGORY_60 = 60; // 家具
- const CATEGORY_61 = 61; // 装修建材
- const CATEGORY_62 = 62; // 厨房卫浴
- const CATEGORY_63 = 63; // 数码产品
- const CATEGORY_64 = 64; // 数码产品
- const CATEGORY_65 = 65; // 配件器材
- const CATEGORY_66 = 66; // 配件器材
- const CATEGORY_67 = 67; // 电商
- const CATEGORY_68 = 68; // 电视购物
- const CATEGORY_69 = 69; // 线上商城
- const CATEGORY_70 = 70; // 现场勘查
- const CATEGORY_71 = 71; // 现场勘查
- const CATEGORY_72 = 72; // 快递业务
- const CATEGORY_73 = 73; // 快递配送
- const CATEGORY_74 = 74; // 其他
- const CATEGORY_75 = 75; // 其他
- public static $validLevelOneCategory = [
- self::CATEGORY_1 => '美食夜宵',
- self::CATEGORY_10 => '甜品饮料',
- self::CATEGORY_16 => '蛋糕',
- self::CATEGORY_18 => '日用百货',
- self::CATEGORY_33 => '果蔬生鲜',
- self::CATEGORY_37 => '鲜花',
- self::CATEGORY_39 => '医药健康',
- self::CATEGORY_42 => '美妆护肤',
- self::CATEGORY_44 => '母婴',
- self::CATEGORY_46 => '文件或票务',
- self::CATEGORY_51 => '服饰鞋帽',
- self::CATEGORY_53 => '洗涤',
- self::CATEGORY_56 => '珠宝奢侈品',
- self::CATEGORY_59 => '家居家装',
- self::CATEGORY_63 => '数码产品',
- self::CATEGORY_65 => '配件器材',
- self::CATEGORY_67 => '电商',
- self::CATEGORY_70 => '现场勘查',
- self::CATEGORY_72 => '快递业务',
- self::CATEGORY_74 => '其他'
- ];
- public static $validLevelCategory = [
- self::CATEGORY_1 => [
- self::CATEGORY_2 => '零食小吃',
- self::CATEGORY_3 => '香锅/烤鱼',
- self::CATEGORY_4 => '西餐',
- self::CATEGORY_5 =>' 日韩料理',
- self::CATEGORY_6 => '海鲜/烧烤',
- self::CATEGORY_7 => '快餐/地方菜',
- self::CATEGORY_8 => '小龙虾',
- self::CATEGORY_9 => '披萨'
- ],
- self::CATEGORY_10 => [
- self::CATEGORY_11 => '甜品',
- self::CATEGORY_12 => '奶茶果汁',
- self::CATEGORY_13 => '咖啡',
- self::CATEGORY_14 => '面包/甜点',
- self::CATEGORY_15 => '冰淇淋'
- ],
- self::CATEGORY_16 => [
- self::CATEGORY_17 => '蛋糕'
- ],
- self::CATEGORY_18 => [
- self::CATEGORY_19 => '便利店',
- self::CATEGORY_20 => '水站/奶站',
- self::CATEGORY_21 => '零食/干果',
- self::CATEGORY_22 => '五金日用',
- self::CATEGORY_23 => '粮油调味',
- self::CATEGORY_24 => '文具店',
- self::CATEGORY_25 => '酒水行',
- self::CATEGORY_26 => '地方特产',
- self::CATEGORY_27 => '进口食品',
- self::CATEGORY_28 => '宠物用品',
- self::CATEGORY_29 => '超市',
- self::CATEGORY_30 => '书店',
- self::CATEGORY_31 => '宠物食品用品',
- self::CATEGORY_32 => '办公家居用品',
- ],
- self::CATEGORY_33 => [
- self::CATEGORY_34 => '果蔬',
- self::CATEGORY_35 => '海鲜水产',
- self::CATEGORY_36 => '冷冻速食'
- ],
- self::CATEGORY_37 => [
- self::CATEGORY_38 => '鲜花',
- ],
- self::CATEGORY_39 => [
- self::CATEGORY_40 => '送药',
- self::CATEGORY_41 => '器材器具',
- ],
- self::CATEGORY_42 => [
- self::CATEGORY_43 => '日化美妆',
- ],
- self::CATEGORY_44 => [
- self::CATEGORY_45 => '孕婴用品',
- ],
- self::CATEGORY_46 => [
- self::CATEGORY_47 => '保单',
- self::CATEGORY_48 => '票务文件',
- self::CATEGORY_49 => ' 政府文件',
- self::CATEGORY_50 => '证件'
- ],
- self::CATEGORY_51 => [
- self::CATEGORY_52 => '服饰鞋帽综合'
- ],
- self::CATEGORY_53 => [
- self::CATEGORY_54 => '脏衣服收',
- self::CATEGORY_55 => '干净衣服派'
- ],
- self::CATEGORY_56 => [
- self::CATEGORY_57 => '珠宝饰品',
- self::CATEGORY_58 => '奢侈品',
- ],
- self::CATEGORY_59 => [
- self::CATEGORY_60 => '家具',
- self::CATEGORY_61 => '装修建材',
- self::CATEGORY_62 => '厨房卫浴'
- ],
- self::CATEGORY_63 => [
- self::CATEGORY_64 => '数码产品',
- ],
- self::CATEGORY_65 => [
- self::CATEGORY_66 => '配件器材',
- ],
- self::CATEGORY_67 => [
- self::CATEGORY_68 => '电视购物',
- self::CATEGORY_69 => '线上商城'
- ],
- self::CATEGORY_70 => [
- self::CATEGORY_71 => '现场勘查'
- ],
- self::CATEGORY_72 => [
- self::CATEGORY_73 => '快递配送'
- ],
- self::CATEGORY_74 => [
- self::CATEGORY_75 => '其他'
- ],
- ];
- /**
- * @var DeliveryService $service
- */
- public static $service;
- /**
- * @var string $prefix 请求url前缀
- */
- public static $prefix = 'https://api.weixin.qq.com/cgi-bin/express/local/business/';
- /**
- * @var string $preAddOrderEndpoint 预下单接口
- */
- public static $preAddOrderEndpoint = 'order/pre_add';
- /**
- * @var string $preCancelOrderEndpoint 预取消接口
- */
- public static $preCancelOrderEndpoint = 'order/precancel';
- /**
- * @var string $cancelOrderEndpoint 取消接口
- */
- public static $cancelOrderEndpoint = 'order/cancel';
- /**
- * @var string $addOrderEndpoint 下单接口
- */
- public static $addOrderEndpoint = 'order/add';
- /**
- * @var string $getAllImmeDeliveryEndpoint 获取已支持的配送公司接口列表
- */
- public static $getAllImmeDeliveryEndpoint = 'delivery/getall';
- /**
- * @var string $getBindAccountEndpoint 拉取已绑定账号
- */
- public static $getBindAccountEndpoint = 'shop/get';
- /**
- * @var string $getOrderEndpoint 拉取配送单信息
- */
- public static $getOrderEndpoint = 'order/get';
- /**
- * @var string $openEndpoint 第三方代商户发起开通即时配送权限
- */
- public static $openEndpoint = 'open';
- /**
- * @var string $mockUpdateOrderEndpoint 模拟配送公司更新配送单状态, 该接口只用于沙盒环境,即订单并没有真实流转到运力方
- */
- public static $mockUpdateOrderEndpoint = 'test_update_order';
- /**
- * @var string $realMockUpdateOrderEndpoint 模拟配送公司更新配送单状态, 该接口用于测试账户下的单,将请求转发到运力测试环境
- */
- public static $realMockUpdateOrderEndpoint = 'realmock_update_order';
- /**
- * 测试数据
- */
- // public static $delivery_id = 'TEST'; // 测试运力
- // public static $shop_id = 'test_shop_id'; // 测试商户(appkey)
- // public static $app_secret = 'test_app_secrect'; // 测试商户
- // public static $shop_no = '123456'; // shop_no
- /**
- * 顺丰测试账号
- */
- // public static $delivery_id = 'SFTC'; // 测试运力
- // public static $shop_id = '1534713176'; // 测试商户(appkey)
- // public static $app_secret = 'd80400f91e156f63b38886e616d84590'; // 测试商户
- // public static $shop_no = '3243279847393'; // shop_no
- // public static $shop_order_id = 'tianxin100'; // 订单id
- // public static $openid = 'or1pO5afofkMrp94tXwIJjAdoCQE';
- /**
- * @var string $shop_id appkey
- */
- protected static $shop_id;
- /**
- * @var string $shop_no 商铺号
- */
- protected static $shop_no;
- /**
- * @var string $app_secret 配送方侧密钥
- */
- protected static $app_secret;
- /**
- * @var string category
- */
- protected static $category;
- /**
- * @var Store $store
- */
- protected static $store;
- /**
- * @var integer $delivery_type
- */
- protected static $delivery_type;
- /**
- * @param $method
- * @param $args
- * @return false|mixed
- */
- public static function __callStatic($method, $args) {
- $store = Store::findOne(get_store_id());
- self::$delivery_type = $store->delivery_type;
- $delivery_config = Option::getDeliveryConfig();
- if (self::$delivery_type == 1) {
- self::$shop_no = $delivery_config['sf']['sf_shop_no'];
- self::$shop_id = $delivery_config['sf']['sf_app_key'];
- self::$app_secret = $delivery_config['sf']['sf_app_secret'];
- } else if (self::$delivery_type == 2){
- self::$shop_no = $delivery_config['dd']['dd_shop_no'];
- self::$shop_id = $delivery_config['dd']['dd_app_key'];
- self::$app_secret = $delivery_config['dd']['dd_app_secret'];
- } else if (self::$delivery_type == 3) {
- self::$shop_no = $delivery_config['ss']['ss_shop_no'];
- self::$shop_id = $delivery_config['ss']['ss_app_key'];
- self::$app_secret = $delivery_config['ss']['ss_app_secret'];
- } else if (self::$delivery_type == 4) {
- self::$shop_no = $delivery_config['mt']['mt_shop_no'];
- self::$shop_id = $delivery_config['mt']['mt_app_key'];
- self::$app_secret = $delivery_config['mt']['mt_app_secret'];
- } else if (self::$delivery_type == 5) {
- self::$shop_no = $delivery_config['uu']['uu_shop_no'];
- self::$shop_id = $delivery_config['uu']['uu_app_key'];
- self::$app_secret = $delivery_config['uu']['uu_app_secret'];
- } else {
- self::$shop_no = '123456';
- self::$shop_id = 'test_shop_id';
- self::$app_secret = 'test_app_secret';
- }
- self::$store = $store;
- self::$category = $store->delivery_category;
- self::$service = new DeliveryService();
- return call_user_func_array(array(Delivery::class, $method), $args);
- }
- /**
- * 预下单接口
- * @param array $goods_list
- * @param integer $order_no
- * @return array
- */
- protected static function preAddOrder($goods_list, $order_no) {
- if (!$order_no) {
- return [
- 'code' => 1,
- 'msg' => '缺少订单号参数'
- ];
- }
- $res = self::format($goods_list, self::$delivery_type, $order_no);
- if (isset($res['code'])) {
- return $res;
- }
- $res = self::$service->request(self::getUrl(self::$preAddOrderEndpoint), self::$args);
- $res = Json::decode($res);
- if (isset($res['resultcode']) && $res['resultcode'] == 0) {
- DeliveryInfo::deleteAll(['order_no' => $order_no, 'store_id' => get_store_id(), 'status' => 0]);
- // 入表
- $delivery_info = new DeliveryInfo();
- $delivery_info->store_id = get_store_id();
- $delivery_info->order_no = $order_no;
- $delivery_info->fee = $res['fee'];
- $delivery_info->delivery_token = $res['delivery_token'];
- $delivery_info->delivery_type = self::$delivery_type;
- $delivery_info->created_at = time();
- $delivery_info->save();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res
- ];
- }
- return [
- 'code' => 1,
- 'errorno' => $res['resultcode'],
- 'msg' => $res['resultmsg'],
- 'args' => self::$args
- ];
- }
- /**
- * 下单接口
- * @param array $goods_list
- * [
- * [
- * 'name' => '苹果',
- * 'price' => 10,
- * 'num' => 10,
- * 'pic' => 'https://chidian.cyyvip.com/web/uploads/images/store_8/2021-07-22/ad8537bd77ddc367e30591e62d97b2e46eb08ff4.jpg'
- * ],
- * [
- * 'name' => '香蕉',
- * 'price' => 5,
- * 'num' => 2,
- * 'pic' => 'https://chidian.cyyvip.com/web/uploads/images/store_8/2021-07-22/ad8537bd77ddc367e30591e62d97b2e46eb08ff4.jpg'
- * ],
- * ]
- * @param string $order_no
- * @return array
- */
- protected static function addOrder($goods_list, $order_no) {
- if (!$order_no) {
- return [
- 'code' => 1,
- 'msg' => '缺少订单号参数'
- ];
- }
- $res = self::format($goods_list, self::$delivery_type, $order_no);
- if (isset($res['code'])) {
- return $res;
- }
- $res = self::$service->request(self::getUrl(self::$addOrderEndpoint), self::$args);
- $res = Json::decode($res);
- \Yii::error(['同城配送下单数据', $res]);
- if (isset($res['resultcode']) && $res['resultcode'] == 0) {
- // 入表
- $delivery_info = DeliveryInfo::findOne(['store_id' => get_store_id(), 'order_no' => $order_no, 'delivery_type' => self::$delivery_type]);
- if ($delivery_info) {
- $delivery_info->status = $res['order_status'];
- $delivery_info->waybill_id = $res['waybill_id'];
- // $delivery_info->fee = $res['fee'];ML20211101195946262565
- $delivery_info->updated_at = time();
- $delivery_info->save();
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res
- ];
- }
- return [
- 'code' => 1,
- 'errorno' => $res['resultcode'],
- 'msg' => $res['resultmsg'],
- ];
- }
- /**
- * 组装数据
- * @param array $goods_list 商品列表
- * @param integer $type 运力类型
- * @param string $order_no 订单号
- * @return array
- */
- protected static function format($goods_list, $type, $order_no) {
- if (empty($goods_list) || !is_array($goods_list) || empty($order_no)) {
- return [
- 'code' => 1,
- 'msg' => '商品列表有空或数据格式错误'
- ];
- }
- if (!in_array($type, array_keys(self::$deliveryIdArr))) {
- return [
- 'code' => 1,
- 'msg' => '非法运力类型'
- ];
- }
- $order = Order::findOne(['order_no' => $order_no]);
- if ($order) {
- $user = User::findOne($order->user_id);
- $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
- }
- $args = [
- 'shopid' => self::$shop_id,
- 'shop_order_id' => $order_no,
- 'shop_no' => self::$shop_no,
- 'shop' => [
- 'goods_count' => count($goods_list),
- 'goods_name' => $goods_list[0]['name'],
- 'img_url' => $goods_list[0]['pic'],
- 'wxa_path' => "/pages/order/order"
- ],
- 'delivery_id' => self::$deliveryIdArr[$type],
- 'delivery_sign' => self::getDeliverySign(self::$shop_id, $order_no, self::$app_secret),
- 'openid' => isset($user) && !empty($user) ? $user->wechat_open_id : get_user()->wechat_open_id,
- // 开发者的open_id
- // 'openid' => 'or1pO5afofkMrp94tXwIJjAdoCQE'
- ];
- $delivery_info_token = DeliveryInfo::findOne(['order_no' => $order_no])->delivery_token;
- if ($delivery_info_token) {
- $args['delivery_token'] = $delivery_info_token;
- }
- $goods = [];
- $total_price = 0;
- foreach ($goods_list as $value) {
- $goods[] = [
- 'good_count' => $value['num'],
- 'good_price' => $value['price'],
- 'good_name' => $value['name'],
- 'good_unit' => '元'
- ];
- $total_price += $value['price'];
- }
- $category_arr = explode(',', self::$category);
- $first_class = self::$validLevelOneCategory[$category_arr[0]];
- $second_class = self::$validLevelCategory[$category_arr[0]][$category_arr[1]];
- // 货物信息
- $args['cargo'] = [
- 'cargo_first_class' => $first_class,
- 'cargo_second_class' => $second_class,
- 'goods_detail' => [
- 'goods' => $goods
- ],
- 'goods_value' => $total_price,
- ];
- // 商家门店编号,在配送公司登记,美团、闪送必填 TODO
- if (in_array($type, [self::MT, self::SS])) {
- $args['shop_no'] = self::$shop_no;
- if (empty($args['shop_no'])) {
- return [
- 'code' => 1,
- 'msg' => '请配置美团或者闪送的门店编号'
- ];
- }
- }
- // order_info
- $args['order_info'] = [
- 'order_time' => time(),
- 'order_type' => 0
- ];
- // 商家门店编号,在配送公司登记,闪送必填 TODO
- if (in_array($type, [self::SS])) {
- $args['order_info']['is_direct_delivery'] = 1;
- }
- // sender :uu跑腿 || 发件人信息,闪送、顺丰同城急送必须填写 || 美团配送、达达,不传shop_no的值需填该字段
- if ($type == self::UU || in_array($type, [self::SS, self::SF])
- || (in_array($type, [self::MT, self::DD]) && empty($args['shop_no']))) {
- $coordinate = self::$store->coordinate;
- $province = District::findOne(self::$store->province_id)->name;
- $city = District::findOne(self::$store->city_id)->name;
- $district = District::findOne(self::$store->district_id)->name;
- if (empty($coordinate) || empty(self::$store->address) || empty($province) || empty($city) || empty($district)) {
- return [
- 'code' => 1,
- 'msg' => '店铺位置信息未配置'
- ];
- }
- if (empty(self::$store->user_name) || empty(self::$store->contact_tel)) {
- return [
- 'code' => 1,
- 'msg' => '店铺联系人信息未配置'
- ];
- }
- $args['sender'] = [
- 'address' => $province . $district . $city,
- 'address_detail' => self::$store->address,
- 'city' => $city,
- 'lat' => explode(',', $coordinate)[0],
- 'lng' => explode(',', $coordinate)[1],
- 'name' => self::$store->user_name,
- 'phone' => self::$store->contact_tel,
- 'coordinate_type' => 1,
- ];
- }
- // 收货人信息
- $address_info = \app\models\Address::findOne(['is_delete' => 0, 'is_default' => 1, 'user_id' => get_saas_user_id()]);
- if (!$address_info) {
- $address_info = \app\models\Address::findOne(['is_delete' => 0, 'user_id' => get_saas_user_id()]);
- }
- if (!$address_info) {
- $address_info = \app\models\Address::findOne(['is_delete' => 0, 'user_id' => get_user_id()]);
- }
- if (!$address_info) {
- return [
- 'code' => 1,
- 'msg' => '请先设置收货地址'
- ];
- }
-
- $args['receiver'] = [
- 'address' => $address_info->province . $address_info->city . $address_info->district,
- 'address_detail' => $address_info->detail,
- 'city' => $address_info->city,
- 'lat' => $address_info->latitude,
- 'lng' => $address_info->longitude,
- 'name' => $address_info->name,
- 'phone' => $address_info->mobile,
- 'coordinate_type' => 1,
- ];
- \Yii::error($args);
- self::$args = $args;
- }
- /**
- * 获取完整url
- * @param string $endpoint
- * @return string
- */
- protected static function getUrl(string $endpoint) {
- return self::$prefix . $endpoint;
- }
- /**
- * sha1 加签
- * @param string $shop_id
- * @param string $shop_order_id
- * @param string $app_secret
- * @return string
- */
- private static function getDeliverySign($shop_id, $shop_order_id, $app_secret) {
- return SHA1($shop_id . $shop_order_id . $app_secret);
- }
- /**
- * 获取已支持的配送公司列表接口
- * @return array
- */
- protected static function getAllDelivery() {
- $res = self::$service->request(self::getUrl(self::$getAllImmeDeliveryEndpoint), []);
- $res = Json::decode($res);
- if (isset($res['resultcode']) && $res['resultcode'] == 0) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res
- ];
- } else {
- return [
- 'code' => 1,
- 'errorno' => $res['resultcode'],
- 'msg' => $res['resultmsg'],
- ];
- }
- }
- /**
- * 获取已支持的配送公司列表接口
- * @return array
- */
- protected static function getBindAccount() {
- $res = self::$service->request(self::getUrl(self::$getBindAccountEndpoint), []);
- $res = Json::decode($res);
- if (isset($res['resultcode']) && $res['resultcode'] == 0) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res
- ];
- } else {
- return [
- 'code' => 1,
- 'errorno' => $res['errcode'],
- 'msg' => $res['errmsg'],
- ];
- }
- }
- /**
- * 拉取配送单信息
- * @return array
- */
- protected static function getOrder($order_no) {
- if (empty($order_no)) {
- return [
- 'code' => 1,
- 'msg' => '订单号不能为空'
- ];
- }
- $args = [
- 'shopid' => self::$shop_id,
- 'shop_order_id' => $order_no,
- 'delivery_sign' => self::getDeliverySign(self::$shop_id, $order_no, self::$app_secret),
- 'shop_no' => self::$shop_no
- ];
- \Yii::error($args);
- $res = self::$service->request(self::getUrl(self::$getOrderEndpoint), $args);
- $res = Json::decode($res);
- if (isset($res['resultcode']) && $res['resultcode'] == 0) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res
- ];
- } else {
- return [
- 'code' => 1,
- 'errorno' => $res['resultcode'],
- 'msg' => $res['resultmsg'],
- ];
- }
- }
- /**
- * 预取消配送单接口
- * @param string $order_no
- * @param integer $reason_id
- * @param string $reason
- * @return array
- */
- protected static function preCancelOrder($order_no, $reason_id = 0, $reason = '') {
- if (empty($order_no)) {
- return [
- 'code' => 1,
- 'msg' => '缺少订单号参数'
- ];
- }
- $delivery_info = DeliveryInfo::findOne(['store_id' => get_store_id(), 'order_no' => $order_no]);
- if (!$delivery_info) {
- return [
- 'code' => 1,
- 'msg' => '未找到配送单信息'
- ];
- }
- $args = [
- 'shopid' => self::$shop_id,
- 'shop_order_id' => $order_no,
- 'delivery_sign' => self::getDeliverySign(self::$shop_id, $order_no, self::$app_secret),
- 'shop_no' => self::$shop_no,
- 'delivery_id' => self::$deliveryIdArr[$delivery_info->delivery_type],
- 'waybill_id' => $delivery_info->waybill_id,
- 'cancel_reason_id' => $reason_id,
- 'cancel_reason' => $reason
- ];
- \Yii::error($args);
- $res = self::$service->request(self::getUrl(self::$preCancelOrderEndpoint), $args);
- $res = Json::decode($res);
- if (isset($res['resultcode']) && $res['resultcode'] == 0) {
- /**
- * @var array $res
- * [
- * "resultcode" => 0,
- * "resultmsg" => "ok",
- * "deduct_fee" => 5, // 费用
- * "desc" => "blabla" // 描述
- * ]
- */
- $delivery_info->deduct_fee = $res['deduct_fee'];
- $delivery_info->save();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res
- ];
- } else {
- return [
- 'code' => 1,
- 'errorno' => $res['resultcode'],
- 'msg' => $res['resultmsg'],
- ];
- }
- }
- /**
- * 取消配送单接口
- * @param string $order_no
- * @param integer $reason_id
- * @param string $reason
- * @return array
- */
- protected static function cancelOrder($order_no, $reason_id = 0, $reason = '') {
- if (empty($order_no)) {
- return [
- 'code' => 1,
- 'msg' => '缺少订单号参数'
- ];
- }
- $delivery_info = DeliveryInfo::findOne(['store_id' => get_store_id(), 'order_no' => $order_no]);
- if (!$delivery_info) {
- return [
- 'code' => 1,
- 'msg' => '未找到配送单信息'
- ];
- }
- $args = [
- 'shopid' => self::$shop_id,
- 'shop_order_id' => $order_no,
- 'delivery_sign' => self::getDeliverySign(self::$shop_id, $order_no, self::$app_secret),
- 'shop_no' => self::$shop_no,
- 'delivery_id' => self::$deliveryIdArr[$delivery_info->delivery_type],
- 'waybill_id' => $delivery_info->waybill_id,
- 'cancel_reason_id' => $reason_id,
- 'cancel_reason' => $reason
- ];
- \Yii::error($args);
- $res = self::$service->request(self::getUrl(self::$cancelOrderEndpoint), $args);
- $res = Json::decode($res);
- if (isset($res['resultcode']) && $res['resultcode'] == 0) {
- /**
- * @var array $res
- * [
- * "resultcode" => 0,
- * "resultmsg" => "ok",
- * "deduct_fee" => 5, // 费用
- * "desc" => "blabla" // 描述
- * ]
- */
- $delivery_info->deduct_fee = $res['deduct_fee'];
- $delivery_info->reason_id = $reason_id;
- $delivery_info->save();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res
- ];
- } else {
- return [
- 'code' => 1,
- 'errorno' => $res['resultcode'],
- 'msg' => $res['resultmsg'],
- ];
- }
- }
- /**
- * 第三方代商户发起开通即时配送权限
- * @return array
- */
- protected static function open() {
- $res = self::$service->request(self::getUrl(self::$openEndpoint), []);
- $res = Json::decode($res);
- if (isset($res['resultcode']) && $res['resultcode'] == 0) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res
- ];
- } else {
- return [
- 'code' => 1,
- 'errorno' => $res['resultcode'],
- 'msg' => $res['resultmsg'],
- ];
- }
- }
- /**
- * 模拟配送公司更新配送单状态(测试账户下单)
- * @return array
- */
- protected static function realMockUpdateOrder($order_no) {
- if (empty($order_no)) {
- return [
- 'code' => 1,
- 'msg' => '缺少订单号参数'
- ];
- }
- $args = [
- 'shopid' => self::$shop_id,
- 'shop_order_id' => $order_no,
- 'delivery_sign' => self::getDeliverySign(self::$shop_id, $order_no, self::$app_secret),
- 'action_time' => time(),
- 'order_status' => 102,
- 'action_msg' => '', // 附加信息
- ];
- \Yii::error($args);
- $res = self::$service->request(self::getUrl(self::$realMockUpdateOrderEndpoint), $args);
- $res = Json::decode($res);
- if (isset($res['resultcode']) && $res['resultcode'] == 0) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res
- ];
- } else {
- return [
- 'code' => 1,
- 'errorno' => $res['resultcode'],
- 'msg' => $res['resultmsg'],
- ];
- }
- }
- /**
- * 模拟配送公司更新配送单状态(不转发到运力环境)
- * @return array
- */
- protected static function mockUpdateOrder($order_no) {
- if (empty($order_no)) {
- return [
- 'code' => 1,
- 'msg' => '缺少订单号参数'
- ];
- }
- $args = [
- 'shopid' => self::$shop_id,
- 'shop_order_id' => $order_no,
- 'delivery_sign' => self::getDeliverySign(self::$shop_id, $order_no, self::$app_secret),
- 'action_time' => time(),
- 'order_status' => 102,
- 'action_msg' => '', // 附加信息
- ];
- \Yii::error($args);
- $res = self::$service->request(self::getUrl(self::$mockUpdateOrderEndpoint), $args);
- $res = Json::decode($res);
- if (isset($res['resultcode']) && $res['resultcode'] == 0) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res
- ];
- } else {
- return [
- 'code' => 1,
- 'errorno' => $res['resultcode'],
- 'msg' => $res['resultmsg'],
- ];
- }
- }
- }
|