Explorar el Código

微信二维码登录示例代码

Sven dunn hace 2 años
padre
commit
4d85fe95e9

+ 178 - 0
其它规范示例/微信相关接口/小程序二维码登录/接口样例.php

@@ -0,0 +1,178 @@
+<?php
+
+namespace app\index\controller;
+
+use EasyWeChat\Factory;
+use think\facade\Config;
+use think\facade\Log;
+use think\facade\Cache;
+use app\common\util\wechat\WxAccountConfig;
+
+class Test extends Base
+{
+
+	private $wechat_config;
+    public function __construct()
+    {
+        parent::__construct();
+        $this->wechat_config 	= WxAccountConfig::getMiniProgramConfig();
+        $this->wechat_config['app_id'] 	= Config::get('wechat.request.app_id'); //微信appid,推荐写在配置文件或环境变量
+        $this->wechat_config['secret'] 	= Config::get('wechat.request.secret'); //微信appid对应secret,推荐写在配置文件或环境变量
+		
+    }
+
+	//二维码被扫判断是否已登录
+	public function miniProgram_qrcode_login(){
+		$my_token	= trim(input('my_token', ''));//二维码token
+		//获取缓存中的对应token
+		$check 	= Cache::get('mini_logged_'.$my_token);
+		$id 	= intval($check);
+		if($id==0){
+			$this->error('未登录');
+		}
+		/*
+		*   获取账号信息result 业务部分	
+		*/
+		$this->success($result, "登录成功");
+	}
+
+	//小程序已扫码标记
+	public function miniProgram_scan(){
+		$my_token	= trim(input('my_token', ''));//二维码token
+		if($my_token != ''){//通过扫二维码登录
+			$check 	= Cache::get('mini_login_'.$my_token);
+			if(!isset($check)){
+				//113可以自定义
+				$this->error('二维码失效', 113);
+			}		
+			Cache::set('mini_login_'.$my_token, 1, 60);//把二维码标记为已扫
+			$this->success([], "扫描成功");
+		}
+		$this->error("二维码token不能为空");
+	}
+	
+	//小程序扫码后请求登录接口
+	public function miniProgram_login(){
+		$my_token	= trim(input('my_token', ''));//二维码token
+		$code	= trim(input('code', ''));//微信码
+		if($code == ''){
+			$this->error("微信码不能为空");
+		}
+        $app = Factory::miniProgram($this->wechat_config);
+        //$result = $app->auth->session($code);
+		$params = ['code'=>$code];
+		$result = $app->plugin->httpPostJson('wxa/business/getuserphonenumber', $params);
+		if (!isset($result['phone_info']['phoneNumber'])) {
+            $this->error('获取手机号失败');
+        }
+		$phone 	= $result['phone_info']['phoneNumber'];
+
+		/*
+		 *   获取登录账号id 业务部分	
+		*/
+		
+		if($my_token != ''){//通过扫二维码登录
+			$check 	= Cache::get('mini_logged_'.$my_token);
+			Cache::set('mini_login_'.$my_token, 1, 60);//把二维码标记为已扫
+			if(isset($check)){//已登录
+				$id 	= intval($check);
+				
+				/*
+				*   获取账号信息result 业务部分	
+				*/				
+				
+				$this->success($result, "登录成功");
+			}
+			else{
+				$check 	= Cache::get('mini_login_'.$my_token);
+				if(!isset($check)){
+					//113可以自定义
+					$this->error('二维码失效', 113);
+				}
+				else {
+					/*
+					*   获取账号信息result 业务部分	
+					*/
+					Cache::set('mini_logged_'.$my_token, $id, 3600*24);//把二维码绑定扫码id
+					$this->success($result, "登录成功");
+				}
+			}
+		}
+		//直接授权登录
+		/*
+		*   获取账号信息result 业务部分	
+		*/
+		$this->success($result, "登录成功");
+
+	}
+
+	//页面判断二维码是否已被扫
+	public function miniProgram_qrcode_check(){
+		$my_token	= trim(input('my_token', ''));//二维码token
+		//获取缓存中的对应token
+		$check 	= Cache::get('mini_login_'.$my_token);
+		//其中113与114可以自定义
+		if(!isset($check)){
+			$this->error('二维码失效', 113);
+		}
+		else if($check == 1){
+			$this->error('二维码已被扫', 114);
+		}
+		$this->success([], "二维码未被扫");
+	}
+
+	//获取小程序登录二维码图片文件
+	public function miniProgram_qrcode(){
+		$page	= trim(input('page', ''));//前端定义的页面
+		$width	= intval(input('width', 600));//前端定义的二维码的宽度,单位 px,最小 280,最大 1280
+		$env_version	= trim(input('env_version', 'release'));//二维码版本,正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
+		//判断wechat.log文件是否存在,不存在则创建
+		// $fileHelp = new FileHelp();
+		// if(!is_file($fileHelp->qrcode_path.'wechat.log')){
+			// file_put_contents($fileHelp->qrcode_path.'wechat.log', "");
+		// }		
+		$config = [
+            'app_id' => Config::get('wechat.request.app_id'), //微信appid,推荐写在配置文件或环境变量
+            'secret' => Config::get('wechat.request.secret'), //微信appid对应secret,推荐写在配置文件或环境变量
+ 
+			// 下面为可选项
+			// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
+			'response_type' => 'array',
+
+			// 'log' => [
+				// 'level' => 'debug',
+				// 'file' => $fileHelp->qrcode_path.'wechat.log',
+			// ],
+		];
+		$filename 	= time().'_qrcode.jpg';//自定义二维码图片文件名
+		$app = Factory::miniProgram($config);
+		$opt 	= [
+			'page'  => $page,
+			'width' => $width,
+			'env_version' => $env_version,
+		];
+		//生成二维码token
+		$my_token 	= substr(md5(time()), 0, 16);
+		//token写入缓存
+		Cache::set('mini_login_'.$my_token, 0, 60);
+		$scene 	= $my_token;
+		$response = $app->app_code->getUnlimit($scene, $opt);
+		Log::info(' scene:'.json_encode($scene).'  opt:'.json_encode($opt).' response:'.json_encode($response));
+		// 保存小程序码到文件
+		if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
+			//目录不存在则创建
+			// if(!is_dir($fileHelp->qrcode_path)){
+				// mkdir($fileHelp->qrcode_path, 0777);
+			// }
+			//保存二维码图片文件
+			$new_filename = $response->saveAs($fileHelp->qrcode_path, $filename);
+			//转换图片地址
+			$result['qrcode_url'] 	= $fileHelp->getQrcodeUrlByPath($fileHelp->qrcode_path.$new_filename);
+			$result['my_token'] 	= $my_token;
+			//生成二维码成功,返回图片与token
+			$this->success($result, "生成成功");
+		}
+		$this->error("二维码生成失败");		
+	}
+	
+}