123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?php
- namespace addons\shopro\library\easywechatPlus;
- use addons\shopro\exception\ShoproException;
- use addons\shopro\service\order\shippingInfo\OrderShippingInfo;
- use addons\shopro\service\order\shippingInfo\TradeOrderShippingInfo;
- use app\admin\model\shopro\data\WechatExpress;
- class WechatMiniProgramShop extends EasywechatPlus
- {
-
- public function uploadShippingInfos($order, $express = null, $type = 'send')
- {
- try {
- $orderShippingInfo = new OrderShippingInfo($order);
- if ($type == 'change') {
- $uploadParams = $orderShippingInfo->getChangeShippingParams($express);
- } else {
- $uploadParams = $orderShippingInfo->getShippingParams();
- }
-
- $this->setMessageJumpPath();
- $length = count($uploadParams);
- foreach ($uploadParams as $key => $params) {
- $params['delivery_mode'] = 1;
- $params['is_all_delivered'] = true;
- if ($params['logistics_type'] == 1 && count($params['shipping_list']) > 1) {
-
- $params['delivery_mode'] = 2;
- }
- if ($length > 1) {
- if ($key == ($length - 1)) {
-
- $params['is_all_delivered'] = true;
- } else {
- $params['is_all_delivered'] = false;
- }
- }
- $params['upload_time'] = date(DATE_RFC3339);
- \think\Log::error('发货信息录入' . json_encode($params));
- $result = $this->uploadShippingInfo($params);
- if ($result['errcode'] != 0) {
- throw new ShoproException('获取失败: errcode:' . $result['errcode'] . '; errmsg:' . $result['errmsg']);
- }
- }
- } catch (\Exception $e) {
- format_log_error($e, 'upload_shipping_info', '发货信息录入错误');
- }
- }
-
- public function tradeUploadShippingInfos($order)
- {
- try {
- $orderShippingInfo = new TradeOrderShippingInfo($order);
- $uploadParams = $orderShippingInfo->getShippingParams();
-
- $this->setMessageJumpPath(true, '');
- $length = count($uploadParams);
- foreach ($uploadParams as $key => $params) {
- $params['delivery_mode'] = 1;
- $params['is_all_delivered'] = true;
- $params['upload_time'] = date(DATE_RFC3339);
- \think\Log::error('发货信息录入' . json_encode($params));
- $result = $this->uploadShippingInfo($params);
- if ($result['errcode'] != 0) {
- throw new ShoproException('获取失败: errcode:' . $result['errcode'] . '; errmsg:' . $result['errmsg']);
- }
- }
- } catch (\Exception $e) {
- format_log_error($e, 'trade_upload_shipping_info', '发货信息录入错误');
- }
- }
-
- private function uploadShippingInfo($params)
- {
- $access_token = $this->getAccessToken();
- $add_template_url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info";
- $result = \addons\shopro\facade\HttpClient::request('post', $add_template_url, [
- 'body' => json_encode($params, JSON_UNESCAPED_UNICODE),
- 'query' => ["access_token" => $access_token['access_token']],
- 'headers' => ['Content-Type' => 'application/json']
- ]);
- $result = $result->getBody()->getContents();
- return json_decode($result, true);
- }
-
- public function checkAndSetMessageJumpPath($exception = false, $is_force = false)
- {
- try {
-
- if ($this->isTradeManaged($is_force)) {
-
- $this->setMessageJumpPath($is_force);
- }
- return true;
- } catch (\Exception $e) {
- format_log_error($e, 'checkAndSetMessageJumpPath', '自动设置微信小程序发货信息管理消息跳转路径失败');
- if ($exception) {
-
- throw new ShoproException($e->getMessage());
- }
- }
- return false;
- }
-
- public function isTradeManaged($is_force = false)
- {
- $key = 'wechat:is_trade_managed';
- if (!$is_force && redis_cache('?' . $key)) {
- return redis_cache($key);
- }
- $access_token = $this->getAccessToken();
- $add_template_url = "https://api.weixin.qq.com/wxa/sec/order/is_trade_managed";
- $mini_appid = sheep_config('shop.platform.WechatMiniProgram.app_id');
- if (!$mini_appid) {
-
- throw new ShoproException('微信小程序发货管理查询失败,没有配置微信小程序');
- }
- $params = [
- 'appid' => $mini_appid
- ];
- $result = \addons\shopro\facade\HttpClient::request('post', $add_template_url, [
- 'body' => json_encode($params, JSON_UNESCAPED_UNICODE),
- 'query' => ["access_token" => $access_token['access_token']],
- 'headers' => ['Content-Type' => 'application/json']
- ]);
- $result = $result->getBody()->getContents();
- $result = json_decode($result, true);
- if ($result['errcode'] != 0 && $result['errcode'] != '48001') {
-
- throw new ShoproException('查询是否有微信发货信息管理权限失败: errcode:' . $result['errcode'] . '; errmsg:' . $result['errmsg']);
- }
- $is_trade_managed = isset($result['is_trade_managed']) ? intval($result['is_trade_managed']) : 0;
- redis_cache($key, $is_trade_managed, 7200);
- return $is_trade_managed;
- }
-
- public function setMessageJumpPath($is_force = false, $path = 'pages/order/detail?comein_type=wechat')
- {
- if (!$is_force && redis_cache('?wechat:set_message_jump_path')) {
-
- return true;
- }
- $access_token = $this->getAccessToken();
- $add_template_url = "https://api.weixin.qq.com/wxa/sec/order/set_msg_jump_path";
- $params = [
- 'path' => $path
- ];
- $result = \addons\shopro\facade\HttpClient::request('post', $add_template_url, [
- 'body' => json_encode($params, JSON_UNESCAPED_UNICODE),
- 'query' => ["access_token" => $access_token['access_token']],
- 'headers' => ['Content-Type' => 'application/json']
- ]);
- $result = $result->getBody()->getContents();
- $result = json_decode($result, true);
- if ($result['errcode'] != 0) {
- throw new ShoproException('设置微信发货消息跳转地址失败: errcode:' . $result['errcode'] . '; errmsg:' . $result['errmsg']);
- }
- if ($is_force) {
-
- redis_cache('wechat:set_message_jump_path', null);
- } else {
- redis_cache('wechat:set_message_jump_path', time());
- }
- return $result;
- }
-
- public function getDelivery($is_force = false)
- {
- if (!$is_force && redis_cache('?wechat:get_delivery_list')) {
-
- return true;
- }
- $access_token = $this->getAccessToken();
- $get_delivery_url = "https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/get_delivery_list";
- $result = \addons\shopro\facade\HttpClient::request('post', $get_delivery_url, [
- 'body' => '{}',
- 'query' => ["access_token" => $access_token['access_token']],
- 'headers' => ['Content-Type' => 'application/json']
- ]);
- $result = $result->getBody()->getContents();
- $result = json_decode($result, true);
- if ($result['errcode'] != 0) {
- throw new ShoproException('获取微信 delivery 列表失败: errcode:' . $result['errcode'] . '; errmsg:' . $result['errmsg']);
- }
-
- $datas = $result['delivery_list'];
- foreach ($datas as $data) {
- $wechatExpress = WechatExpress::where('code', $data['delivery_id'])->find();
- $current = [
- 'name' => $data['delivery_name'] ?? '',
- 'code' => $data['delivery_id'] ?? '',
- ];
- if (!$wechatExpress) {
- $wechatExpress = new WechatExpress();
- }
- $wechatExpress->save($current);
- }
- redis_cache('wechat:get_delivery_list', time());
- return $result;
- }
-
- public function __call($funcname, $arguments)
- {
-
-
-
- return $this->app->{$funcname}(...$arguments);
- }
- }
|