Bladeren bron

生成小程序码

dyjh 6 jaren geleden
bovenliggende
commit
10e7a523de
1 gewijzigde bestanden met toevoegingen van 63 en 1 verwijderingen
  1. 63 1
      app/Http/Controllers/Api/V1/AlbumPosterController.php

+ 63 - 1
app/Http/Controllers/Api/V1/AlbumPosterController.php

xqd xqd
@@ -8,6 +8,7 @@
 
 namespace App\Http\Controllers\Api\V1;
 
+use App\Models\AlbumManufacturerModel;
 use App\Models\AlbumPosterModel;
 use App\Services\Base\ErrorCode;
 use Validator, Response,Auth;
@@ -63,9 +64,70 @@ class AlbumPosterController extends Controller
         if ($validator->fails()) {
             return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
         }
-        $info = AlbumPosterModel::where('store_id', $request->input('store_id'))->first()->toArray();
+        $store_id = $request->input('store_id');
+        $info = AlbumPosterModel::where('store_id', $store_id)->first()->toArray();
+        $wechat_app = AlbumManufacturerModel::where('store_id', $store_id)->first();
+        if (!file_exists(public_path() . '/upload/QrCodee/' . $store_id . '.gif')) {
+            $this->getQrCode($wechat_app->xyx_id, $wechat_app->xyx_secret, $store_id);
+        }
         $info['phone'] = $userAuth->phone;
         $info['username'] = $userAuth->username;
+        $info['QrCode'] = env('APP_URL') . '/upload/QrCode/' . $store_id . '.gif';
         return $this->api(compact('info'));
     }
+
+    private function getQrCode($appId, $appSecret, $store_id)
+    {
+        $access = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";
+        $res_access = $this->curlGet($access);
+        $res_access = json_decode($res_access, true);
+        if (isset($res_access['access_token'])) {
+            $ACCESS_TOKEN = $res_access['access_token'];
+            $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $ACCESS_TOKEN;
+            $data = array();
+            $data['scene'] = "scene";//自定义信息,可以填写诸如识别用户身份的字段,注意用中文时的情况
+            $data['page'] = "pages/index/index";//扫描后对应的path
+            $data['width'] = 800;//自定义的尺寸
+            $data['auto_color'] = false;//是否自定义颜色
+            $color = array(
+                "r" => "0",
+                "g" => "0",
+                "b" => "0",
+            );
+            $data['line_color'] = $color;//自定义的颜色值
+            //dd($data,$url);
+            $data = json_encode($data);
+            $this->getHttpArray($url, $data, $store_id);
+            return true;
+        } else {
+            \Log::error($res_access);
+            return false;
+        }
+    }
+
+    private function curlGet($url)
+    {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($ch, CURLOPT_HEADER, 0);
+        $output = curl_exec($ch);
+        curl_close($ch);
+        return $output;
+    }
+
+    private function getHttpArray($url, $post_data, $store_id)
+    {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   //没有这个会自动输出,不用print_r()也会在后面多个1
+        curl_setopt($ch, CURLOPT_POST, 1);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
+        $output = curl_exec($ch);
+        file_put_contents(public_path() . '/upload/QrCode/' . $store_id . '.gif', $output . PHP_EOL, FILE_APPEND);
+        curl_close($ch);
+        //$out = json_decode($output);
+        return true;
+    }
 }