wesmiler 2 months ago
parent
commit
2e6e135407
1 changed files with 26 additions and 13 deletions
  1. 26 13
      app/Services/MpService.php

+ 26 - 13
app/Services/MpService.php

@@ -32,7 +32,9 @@ class MpService extends BaseService
         // 获取二维码
         'getQrcode' => 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s&scene=%s&page=%s&env_version=%s',
         // 生成小程序分享链接
-        'getShareLink'=>'https://api.weixin.qq.com/wxa/generatescheme?access_token=%s',
+        'getSchemeLink'=>'https://api.weixin.qq.com/wxa/generatescheme?access_token=%s',
+        // 短链接
+        'getShortLink'=>'https://api.weixin.qq.com/wxa/genwxashortlink?access_token=%s',
         // 获取用户信息
         'getUserInfo' => 'https://api.weixin.qq.com/sns/jscode2session',
         // 获取公众号accessToken和openid
@@ -192,7 +194,7 @@ class MpService extends BaseService
      * @param false $refresh
      * @return false|string
      */
-    public function getMiniShareLink($path, $query,$isExpire=false, $refresh=false)
+    public function getMiniShareLink($path, $query,$isExpire=false,$linkType=2, $refresh=false)
     {
         try {
             if(empty($path) || empty($query)){
@@ -211,27 +213,38 @@ class MpService extends BaseService
                 return false;
             }
 
-            $cacheKey = "caches:members:mpShare_{$this->mpAppid}:".md5($path.$query);
+            $cacheKey = "caches:members:mpShare_{$this->mpAppid}:{$linkType}_".md5($path.$query);
             $link = RedisService::get($cacheKey);
             if($link && !$refresh){
                 return $link;
             }
 
-            $data = [
-                'jump_wxa'=>[
-                    'path'=>$path,
-                    'query'=>$query,
-                ],
-                'is_expire'=> $isExpire,
-            ];
+            if($linkType == 1){
+                $data = [
+                    'jump_wxa'=>[
+                        'path'=>$path,
+                        'query'=>$query,
+                    ],
+                    'is_expire'=> $isExpire,
+                ];
+            }else if($linkType ==2){
+                $data=[
+                    'page_url'=> $path,
+                    'page_title'=> $query,
+                    'is_permanent'=>$isExpire
+                ];
+            }
+
 
-            $url = sprintf($this->apiUrls['getShareLink'], $token);
+            $linkTypes = [1=>'getSchemeLink',2=>'getShortLink'];
+            $name = isset($linkTypes[$linkType])?$linkTypes[$linkType]: 'getShortLink';
+            $url = sprintf($this->apiUrls[$name], $token);
             var_dump($url);
             $result = curl_post($url, json_encode($data));
             var_dump($result);
             $datas = $result? json_decode($result, true) : [];
             var_dump($datas);
-            $this->saveLog($cacheKey.'_result', ['path'=>$path,'query'=>$query,'url'=>$url,'result'=>$result,'date'=>date('Y-m-d H:i:s')]);
+            $this->saveLog($cacheKey.'_result', ['path'=>$path,'query'=>$query,'linkType'=>$linkType,'url'=>$url,'result'=>$result,'date'=>date('Y-m-d H:i:s')]);
             $errcode = isset($datas['errcode'])? $datas['errcode'] : '';
             $errmsg = isset($datas['errmsg'])? $datas['errmsg'] : '';
             $link ='';
@@ -244,7 +257,7 @@ class MpService extends BaseService
             return $result;
         }catch (\Exception $e){
             $this->error = $e->getMessage();
-            $this->saveLog($cacheKey.'_error', ['path'=>$path,'query'=>$query,'error'=>$this->error,'trace'=>$e->getTrace(),'date'=>date('Y-m-d H:i:s')]);
+            $this->saveLog($cacheKey.'_error', ['path'=>$path,'query'=>$query,'linkType'=>$linkType,'error'=>$this->error,'trace'=>$e->getTrace(),'date'=>date('Y-m-d H:i:s')]);
             return false;
         }
     }