Browse Source

增加活动分享链接接口

15954078560 2 years ago
parent
commit
ac8359f40e
1 changed files with 45 additions and 0 deletions
  1. 45 0
      application/api/controller/Index.php

+ 45 - 0
application/api/controller/Index.php

@@ -3,6 +3,7 @@
 namespace app\api\controller;
 
 use app\common\controller\Api;
+use Think\Cache;
 use think\Db;
 use wxpay;
 
@@ -928,4 +929,48 @@ class Index extends Api
         $this->success('开通成功');
     }
 
+    //获取活动分享链接
+    public function activelink() {
+        $id = input('id', 0, 'intval'); //活动id
+        if (!$id) {
+            $this->error('参数缺失');
+        }
+        $info = Db::name('active')->find($id);
+        if (!$info) {
+            $this->error('活动不存在');
+        }
+        if ($info['status'] == 2) {
+            $this->error('活动已结束');
+        }
+        if ($info['status'] == 3) {
+            $this->error('活动已取消');
+        }
+
+        $access_token = Cache::get('access_token');
+        if (!$access_token) {
+            //获取$access_token
+            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.config('wxchatpay.app_id').'&secret='.config('wxchatpay.app_secret');
+            $result = file_get_contents($url);
+            $result = json_decode($result, true);
+            $access_token = $result['access_token'];
+            //缓存
+            Cache::set('access_token', $access_token, 7000);
+        }
+        if (!$access_token) {
+            $this->error('参数缺失');
+        }
+
+        $data['page_url'] = 'pages/home/detail?id='.$id.'&code='.$this->auth->invite_no;
+        $data['page_title'] = $info['title'];
+        $data = json_encode($data, 320);
+
+        $url = 'https://api.weixin.qq.com/wxa/genwxashortlink?access_token='.$access_token;
+        $rs = httpRequest($url, 'POST', $data);
+        $rs = json_decode($rs, true);
+        if ($rs['errcode'] != 0) {
+            $this->error('网络延迟');
+        }
+
+        $this->success('分享链接', $rs['link']);
+    }
 }