使用EasyWechat接入用户登录,获取用户信息
官方文档
<?php
namespace app\index\controller;
use EasyWeChat\Factory;
use think\Controller;
class EasyWechat extends Controller
{
protected $app;
public function _initialize()
{
$config = [
'app_id' => 'wx5d2f01e8ab9532dc',
'secret' => 'f36d7f5c26d76662d533c29072a4f5e5',
'token' => 'JOfggDDrVhODrrGqnfRUvTGvthXdvtzV',
'aes_key' => '',
'response_type' => 'array',
'log' => [
'level' => 'debug',
'file' => __DIR__ . '/wechat.log',
],
'oauth' => [
'scopes' => ['snsapi_userinfo'],
'callback' => 'http://lj.lxx123.club/index/Easy_Wechat/oauth_callback',
],
];
$this->app = Factory::officialAccount($config);
}
public function response_echostr()
{
$this->app->server->serve()->send();
die;
}
public function index()
{
$response = $this->app->oauth->scopes(['snsapi_userinfo'])->redirect();
$response->send();
}
public function oauth_callback()
{
$user = $this->app->oauth->user();
}
}
使用例子
<?php
namespace app\index\controller;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\Image;
use Grafika\Color;
use Grafika\Grafika;
use think\Controller;
use think\Db;
class EasyWechat extends Controller
{
protected $app;
protected $app_pay;
public function _initialize()
{
$config = [
'app_id' => 'wx5d2f01e8ab9532dc',
'secret' => 'f36d7f5c26d76662d533c29072a4f5e5',
'token' => 'JOfggDDrVhODrrGqnfRUvTGvthXdvtzV',
'aes_key' => '',
'response_type' => 'array',
'log' => [
'level' => 'debug',
'file' => __DIR__ . '/wechat.log',
],
'oauth' => [
'scopes' => ['snsapi_userinfo'],
'callback' => 'http://lj.lxx123.club/index/Easy_Wechat/oauth_callback',
],
];
$config_pay = [
'app_id' => '',
'mch_id' => '',
'key' => '',
'cert_path' => 'D:/lxx/qiangdan/extend/wxpay/cert/apiclient_cert.pem',
'key_path' => 'D:/lxx/qiangdan/extend/wxpay/cert/apiclient_key.pem',
'notify_url' => 'http://lj.lxx123.club/index/Easy_Wechat/pay_callback',
];
$this->app = Factory::officialAccount($config);
$this->app_pay = Factory::payment($config_pay);
}
public function response_echostr()
{
if (input('?get.echostr')) {
$this->app->server->serve()->send();
die;
}
$this->app->server->push(function ($message) {
switch ($message['MsgType']) {
case 'event':
switch ($message['Event']) {
case 'SCAN':
$p_openid = Db::table('tp_lxx_users')->where('id', $message['EventKey'])->value('openid');
$this->send_template_new($p_openid);
return '扫码的ID是:' . $message['EventKey'] . ',Ticket是:' . $message['Ticket'] . ',ToUserName,FromUserName';
break;
case 'subscribe':
return '感谢关注!';
break;
case 'CLICK':
switch ($message['EventKey']) {
case 'qrcode':
$data_user_info = Db::table('tp_lxx_users')->where('openid', $message['FromUserName'])->find();
if (empty($data_user_info)) {
$str = <<<string
需要先注册登录平台才能获取推广二维码哟!
???
??
?
string;
return $str;
}
if (!empty($data_user_info['media_id'])) {
return new Image($data_user_info['media_id']);
}
$ret = $this->make_qrcode("{$data_user_info['id']}");
$code_url = $ret['qr_url'];
$head_url = $data_user_info['head_url'];
$name = base64_decode($data_user_info['nickname']);
$base_path = $this->make_image("{$data_user_info['id']}", $name, $code_url, $head_url);
trace(dump($base_path), 'error');
$upload_ret = $this->upload_qrcode($base_path);
Db::table('tp_lxx_users')->where('openid', $message['FromUserName'])->update(['media_id' => $upload_ret['media_id']]);
return new Image($upload_ret['media_id']);
break;
default:
return '其他点击事件!';
break;
}
break;
default:
return '收到event事件消息';
break;
}
return '收到事件消息' . $message['EventKey'];
break;
case 'text':
return '收到文字消息' . $message['FromUserName'];
break;
case 'image':
return '收到图片消息';
break;
case 'voice':
return '收到语音消息';
break;
case 'video':
return '收到视频消息';
break;
case 'location':
return '收到坐标消息';
break;
case 'link':
return '收到链接消息';
break;
case 'file':
return '收到文件消息';
default:
return '收到其它消息';
break;
}
});
$this->app->server->serve()->send();
}
public function download($url, $absolute_path = '')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$file = curl_exec($ch);
curl_close($ch);
$resource = fopen($absolute_path, 'a');
fwrite($resource, $file);
fclose($resource);
}
public function make_image($uid, $text, $code_url, $head_url)
{
$code_url = $code_url;
$code_path = ROOT_PATH . 'public/static/' . $uid . 'code . jpg';
$this->download($code_url, $code_path);
$head_url = $head_url;
$head_path = ROOT_PATH . 'public/static/' . $uid . 'head . jpg';
$this->download($head_url, $head_path);
$text = $text;
$base = ROOT_PATH . 'public/static/base.jpg';
$code = $code_path;
$head = $head_path;
$editor = Grafika::createEditor();
$editor->open($image1, $base);
$editor->open($image2, $code);
$editor->open($image3, $head);
$editor->blend($image1, $image2, 'normal', 0.9, 'center', 0, 300);
$editor->blend($image1, $image3, 'normal', 0.9, 'top-left', 80, 100);
$ttf = ROOT_PATH . '/vendor/topthink/think-captcha/assets/zhttfs/1.ttf';
$editor->text($image1, $text, 30, 250, 150, new Color("#000000"), $ttf, 0);
$absolute_path = ROOT_PATH . 'public/static/' . $uid . 'shareimg.jpg';
$editor->save($image1, $absolute_path);
unlink($code);
unlink($head);
return $absolute_path;
}
public function upload_qrcode($absolute_path = '')
{
if (empty($absolute_path)) {
return false;
}
$result = $this->app->material->uploadImage($absolute_path);
return $result;
}
public function make_qrcode($uid, $path = '')
{
$result = $this->app->qrcode->forever($uid);
$result['qr_url'] = $this->app->qrcode->url($result['ticket']);
if (!empty($path)) {
$path = ROOT_PATH . 'public/static/';
$absolute_path = $path . $uid . ' . jpg';
file_put_contents($absolute_path, $content);
$result['absolute_path'] = $absolute_path;
}
return $result;
}
public function test($value='')
{
$xx = $this->app->auto_reply->current();
dump($xx);
}
public function get_menu_list()
{
$list = $this->app->menu->list();
dump($list);
}
public function set_menu_list()
{
$buttons = [
[
"type" => "click",
"name" => "qrcode",
"key" => "qrcode",
],
[
"name" => "菜单",
"sub_button" => [
[
"type" => "view",
"name" => "搜索",
"url" => "http://www.soso.com/",
],
[
"type" => "view",
"name" => "登录",
"url" => "http://lj.lxx123.club/index/Easy_Wechat/wechat_login",
],
[
"type" => "click",
"name" => "qrcode2",
"key" => "qrcode2",
],
],
],
];
$result = $this->app->menu->create($buttons);
if ($result['errcode'] == 0) {
return true;
} else {
return false;
}
}
public function wechat_login()
{
$response = $this->app->oauth->redirect();
$response->send();
}
public function oauth_callback()
{
$user = $this->app->oauth->user();
$user_info = $user->getOriginal();
$check_user = Db::table('tp_lxx_users')->where('openid', $user_info['openid'])->count();
if ($check_user == 0) {
$data = [
'openid' => $user_info['openid'],
'nickname' => base64_encode($user_info['nickname']),
'head_url' => $user_info['headimgurl'],
];
$is_insert = Db::table('tp_lxx_users')->insert($data);
}
}
public function send_template_new($openid)
{
$this->app->template_message->send([
'touser' => $openid,
'template_id' => 'kXPYggX1fGDLDIuMypAc_nO5Wob684aWdazus6eQuLE',
'url' => 'https://easywechat.org',
'data' => [
'name' => 'name',
'action' => 'action',
'num' => 'num',
'thistime' => 'thistime',
'status' => 'status',
],
]);
}
public function do_order()
{
$result = $this->app_pay->order->unify([
'body' => '腾讯充值中心 - QQ会员充值',
'out_trade_no' => '20150806125346',
'total_fee' => 88,
'spbill_create_ip' => '123.12.12.123',
'notify_url' => '',
'trade_type' => 'JSAPI',
'openid' => 'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o',
]);
dump($result);
}
public function pay_callback()
{
$response = $this->app_pay->handlePaidNotify(function ($message, $fail) {
return true;
});
$response->send();
}
}