Browse Source

更新模板消息接口

刘忠健 2 years ago
parent
commit
fcd25ebf62

+ 17 - 0
api/app/api/controller/WechatHprose.php

@@ -0,0 +1,17 @@
+<?php
+
+
+namespace app\api\controller;
+
+
+use app\api\controller\hprose\WechatMp;
+use Hprose\Http\Server;
+
+class WechatHprose
+{
+    public function server(){
+        $server=new Server();
+        $server->addInstanceMethods(new WechatMp());
+        return $server->start();
+    }
+}

+ 33 - 0
api/app/api/controller/hprose/WechatMp.php

@@ -0,0 +1,33 @@
+<?php
+
+
+namespace app\api\controller\hprose;
+
+
+use app\api\controller\hprose\util\WechatMpHelper;
+use app\common\model\WechatConfig;
+use EasyWeChat\Factory;
+use think\facade\Log;
+
+class WechatMp
+{
+    /**
+     * 发送1条模板消息
+     * @param $authInfo,数组,索引值为:code ,time,sign
+     * @param $data ,数组,每个元素为1条模板消息
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function templateMessageSend($authInfo, $data)
+    {
+        Log::record("接收参数:authInfo:" . json_encode($authInfo, JSON_UNESCAPED_UNICODE));
+        Log::record("接收参数:data:" . json_encode($data, JSON_UNESCAPED_UNICODE));
+        $wechatMpHelper = new WechatMpHelper($authInfo);
+        if ($wechatMpHelper->res["code"] != 0) {
+            return $wechatMpHelper->res["code"];
+        }
+        return $wechatMpHelper->templateMessageSend($data);
+    }
+
+}

+ 49 - 0
api/app/api/controller/hprose/util/WechatMpHelper.php

@@ -0,0 +1,49 @@
+<?php
+
+
+namespace app\api\controller\hprose\util;
+
+
+use app\common\model\WechatConfig;
+use EasyWeChat\Factory;
+use think\facade\Log;
+
+class WechatMpHelper
+{
+    protected $app = null;
+
+    public $res = ["code" => -1];
+
+    public function __construct($authInfo)
+    {
+        $sign = $authInfo["sign"];
+        $time = $authInfo["time"];
+        $code = $authInfo["code"];
+        $wechatConfig = WechatConfig::getEasyWechatConfig($code);
+        $sign2 = md5($code . $time . $wechatConfig["sign_key"]);
+        if (strtoupper($sign2) != strtoupper($sign)) {
+            $this->res = returnFormat(500, "签名失败", ["sign" => $sign, "sign2" => $sign2]);
+        }
+        $this->app = Factory::officialAccount($wechatConfig);
+
+        $this->res = returnFormat(0);
+    }
+
+    /**
+     * 发送模板消息
+     * @param $data
+     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
+     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
+     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
+     * @throws \GuzzleHttp\Exception\GuzzleException
+     */
+    public function templateMessageSend($data)
+    {
+
+        $res = $this->app->template_message->send($data);
+
+        $this->res = array_merge($res, returnFormat($res["errcode"], $res["errmsg"]));
+        Log::record(json_encode($this->res, JSON_UNESCAPED_UNICODE), "debug");
+        return $this->res;
+    }
+}

+ 1 - 0
api/app/common/model/WechatConfig.php

@@ -25,6 +25,7 @@ class WechatConfig extends Base
         $config = [
             'app_id' => $wechatConfig->app_id,
             'secret' => $wechatConfig->secret,
+            'sign_key'=>$wechatConfig->sign_key,
         ];
         return $config;
     }