ソースを参照

小程序设置对接消息跳转路径设置接口

萤火科技 2 年 前
親
コミット
8394a2b0f4
2 ファイル変更、51 行追加、1 行削除
  1. 27 0
      app/common/library/wechat/Shipping.php
  2. 24 1
      app/store/model/wxapp/Setting.php

+ 27 - 0
app/common/library/wechat/Shipping.php

@@ -47,4 +47,31 @@ class Shipping extends WxBase
         }
         return $response;
     }
+
+    /**
+     * 消息跳转路径设置接口
+     * api文档: https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html#%E5%85%AD%E3%80%81%E6%B6%88%E6%81%AF%E8%B7%B3%E8%BD%AC%E8%B7%AF%E5%BE%84%E8%AE%BE%E7%BD%AE%E6%8E%A5%E5%8F%A3
+     * @throws BaseException
+     */
+    public function setMsgJumpPath(array $params)
+    {
+        // 微信接口url
+        $accessToken = $this->getAccessToken();
+        $apiUrl = "https://api.weixin.qq.com/wxa/sec/order/set_msg_jump_path?access_token={$accessToken}";
+        // 执行请求
+        $result = $this->post($apiUrl, $this->jsonEncode($params));
+        // 记录日志
+        log_record(['name' => '微信小程序-消息跳转路径设置接口', 'url' => $apiUrl, 'params' => $params, 'result' => $result]);
+        // 返回结果
+        $response = $this->jsonDecode($result);
+        if (!isset($response['errcode'])) {
+            $this->error = 'not found errcode';
+            return false;
+        }
+        if ($response['errcode'] != 0) {
+            $this->error = $response['errmsg'];
+            return false;
+        }
+        return $response;
+    }
 }

+ 24 - 1
app/store/model/wxapp/Setting.php

@@ -13,8 +13,10 @@ declare (strict_types=1);
 namespace app\store\model\wxapp;
 
 use think\facade\Cache;
-use app\common\library\helper;
 use app\common\model\wxapp\Setting as SettingModel;
+use app\common\library\helper;
+use app\common\library\wechat\Shipping as WechatShippingApi;
+use cores\exception\BaseException;
 
 /**
  * 微信小程序设置模型
@@ -34,12 +36,15 @@ class Setting extends SettingModel
      * @param string $key
      * @param array $values
      * @return bool
+     * @throws BaseException
      */
     public function edit(string $key, array $values): bool
     {
         $model = self::detail($key) ?: $this;
         // 删除小程序设置缓存
         Cache::delete('wxapp_setting_' . self::$storeId);
+        // 设置消息跳转路径
+        $values['enableShipping'] && $this->setMsgJumpPath($values);
         // 保存设置
         return $model->save([
             'key' => $key,
@@ -49,4 +54,22 @@ class Setting extends SettingModel
             'store_id' => self::$storeId,
         ]);
     }
+
+    /**
+     * 微信小程序 -> 发货信息管理 -> 消息跳转路径设置接口
+     * @param array $values
+     * @return true
+     * @throws BaseException
+     */
+    private function setMsgJumpPath(array $values): bool
+    {
+        // 请求API数据
+        $WechatShippingApi = new WechatShippingApi($values['app_id'], $values['app_secret']);
+        // 处理返回结果
+        $response = $WechatShippingApi->setMsgJumpPath([
+            'path' => 'pages/order/index?dataType=received'
+        ]);
+        empty($response) && throwError('微信API请求失败:' . $WechatShippingApi->getError());
+        return true;
+    }
 }