From 7dbf23b44473b112bf3af93eba6927469f128384 Mon Sep 17 00:00:00 2001
From: Anyon <zoujingli@qq.com>
Date: Fri, 4 May 2018 17:39:28 +0800
Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E5=A2=9E=E5=8A=A0=E5=BE=AE?=
 =?UTF-8?q?=E4=BF=A1=E6=89=AB=E7=A0=81=E6=94=AF=E4=BB=98=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 application/store/controller/wechat/Demo.php | 57 +++++++++++++++++++-
 application/wechat/view/config/index.html    | 10 +++-
 2 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/application/store/controller/wechat/Demo.php b/application/store/controller/wechat/Demo.php
index 36733d653..89b38c278 100644
--- a/application/store/controller/wechat/Demo.php
+++ b/application/store/controller/wechat/Demo.php
@@ -14,6 +14,7 @@
 
 namespace app\store\controller\wechat;
 
+use Endroid\QrCode\QrCode;
 use service\WechatService;
 use WeChat\Pay;
 
@@ -24,6 +25,45 @@ use WeChat\Pay;
  */
 class Demo
 {
+
+    /**
+     * 扫码支付测试
+     * @return \think\Response
+     * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
+     * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
+     * @throws \Endroid\QrCode\Exceptions\ImageTypeInvalidException
+     * @throws \WeChat\Exceptions\InvalidResponseException
+     */
+    public function scan()
+    {
+        $wechat = new Pay(config('wechat.'));
+        $options = [
+            'body'             => '测试商品',
+            'out_trade_no'     => time(),
+            'total_fee'        => '1',
+            'trade_type'       => 'NATIVE',
+            'notify_url'       => url('@wx-demo-notify', '', true, true),
+            'spbill_create_ip' => request()->ip(),
+        ];
+        // 生成预支付码
+        $result = $wechat->createOrder($options);
+        return $this->createQrc($result['code_url']);
+    }
+
+
+    /**
+     * 公众号JSAPI支付二维码
+     * @return \think\Response
+     * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
+     * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
+     * @throws \Endroid\QrCode\Exceptions\ImageTypeInvalidException
+     */
+    public function jsapiQrc()
+    {
+        $url = url('@wx-demo-jsapi', '', true, true);
+        return $this->createQrc($url);
+    }
+
     /**
      * 公众号JSAPI支付测试
      * @link wx-demo-jsapi
@@ -43,7 +83,7 @@ class Demo
             'openid'           => $openid,
             'trade_type'       => 'JSAPI',
             'notify_url'       => url('@wx-demo-notify', '', true, true),
-            'spbill_create_ip' => '127.0.0.1',
+            'spbill_create_ip' => request()->ip(),
         ];
         // 生成预支付码
         $result = $wechat->createOrder($options);
@@ -90,4 +130,19 @@ class Demo
         return 'SUCCESS';
     }
 
+    /**
+     * 显示二维码
+     * @param string $url
+     * @return \think\Response
+     * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
+     * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
+     * @throws \Endroid\QrCode\Exceptions\ImageTypeInvalidException
+     */
+    protected function createQrc($url)
+    {
+        $qrCode = new QrCode();
+        $qrCode->setText($url)->setSize(300)->setPadding(20)->setImageType(QrCode::IMAGE_TYPE_PNG);
+        return \think\facade\Response::header('Content-Type', 'image/png')->data($qrCode->get());
+    }
+
 }
\ No newline at end of file
diff --git a/application/wechat/view/config/index.html b/application/wechat/view/config/index.html
index 026fb0652..c8f308fa2 100644
--- a/application/wechat/view/config/index.html
+++ b/application/wechat/view/config/index.html
@@ -33,10 +33,18 @@
                 <img class="notselect" data-tips-image src="{:url('@wechat/api.tools/oauth_qrc')}" style="width:80px;margin-left:-4px">
                 <p class="text-center" style="margin-left:-10px">网页授权</p>
             </div>
-            <div class="pull-left padding-left-0">
+            <div class="pull-left padding-left-0 padding-right-15">
                 <img class="notselect" data-tips-image src="{:url('@wechat/api.tools/jssdk_qrc')}" style="width:80px;">
                 <p class="text-center">JSSDK签名</p>
             </div>
+            <div class="pull-left padding-left-0 padding-right-15">
+                <img class="notselect" data-tips-image src="{:url('@wx-demo-jsapiqrc')}" style="width:80px;">
+                <p class="text-center">JSAPI支付</p>
+            </div>
+            <div class="pull-left padding-left-0">
+                <img class="notselect" data-tips-image src="{:url('@wx-demo-scan')}" style="width:80px;">
+                <p class="text-center">扫码2支付</p>
+            </div>
         </div>
     </div>