www_fpvone_cn/application/index/controller/Leagueqr.php

195 lines
5.3 KiB
PHP

<?php
namespace app\index\controller;
use fast\Random;
use addons\shopro\facade\Wechat;
use app\admin\model\shopro\ThirdOauth;
use app\admin\controller\shopro\Common;
use app\admin\model\shopro\ClubThirdOauth;
class Leagueqr extends Common
{
protected $wechat;
protected $noNeedRight = ['getQrcode', 'checkScan', 'unbind'];
public function _initialize()
{
parent::_initialize();
$this->wechat = Wechat::officialAccountManage();
}
public function getleagueQrcode()
{
$event = $this->request->param('event');
// var_dump($event);exit;
if (!in_array($event, ['bind'])) {
$this->error('参数错误');
}
$adminId = $this->auth->id;
var_dump($adminId);exit;
$thirdOauth = ClubThirdOauth::where([
'provider' => 'wechat',
'platform' => 'admin',
'user_id' => $adminId
])->find();
if ($thirdOauth) {
error_stop('已绑定微信账号', -2, $thirdOauth);
}
// 二维码和缓存过期时间
$expireTime = 1 * 60;
// 事件唯一标识
$eventId = Random::uuid();
// var_dump($eventId);exit;
$cacheKey = "wechatAdmin.{$event}.{$eventId}";
cache($cacheKey, ['id' => 0], $expireTime);
try {
$result = $this->wechat->qrcode->temporary($cacheKey, $expireTime);
// var_dump($result);exit;
$qrcode = $this->wechat->qrcode->url($result['ticket']);
// var_dump($qrcode);exit;
} catch (\Exception $e) {
$this->error($e->getMessage());
}
// return json($qrcode);exit;
// var_dump($qrcode);exit;
$this->success('', null, [
'url' => $qrcode,
'eventId' => $eventId
]);
}
// 获取公众号二维码
public function getQrcode()
{
$event = $this->request->param('event');
// var_dump($event);exit;
if (!in_array($event, ['bind'])) {
$this->error('参数错误');
}
$adminId = $this->auth->id;
// var_dump($adminId);exit;
$thirdOauth = ThirdOauth::where([
'provider' => 'wechat',
'platform' => 'admin',
'admin_id' => $adminId
])->find();
if ($thirdOauth) {
error_stop('已绑定微信账号', -2, $thirdOauth);
}
// 二维码和缓存过期时间
$expireTime = 1 * 60;
// 事件唯一标识
$eventId = Random::uuid();
// var_dump($eventId);exit;
$cacheKey = "wechatAdmin.{$event}.{$eventId}";
cache($cacheKey, ['id' => 0], $expireTime);
try {
$result = $this->wechat->qrcode->temporary($cacheKey, $expireTime);
// var_dump($result);exit;
$qrcode = $this->wechat->qrcode->url($result['ticket']);
// var_dump($qrcode);exit;
} catch (\Exception $e) {
$this->error($e->getMessage());
}
// return json($qrcode);exit;
// var_dump($qrcode);exit;
$this->success('', null, [
'url' => $qrcode,
'eventId' => $eventId
]);
}
// 检查扫码结果
public function checkScan()
{
$event = $this->request->param('event');
$eventId = $this->request->param('eventId');
if (!in_array($event, ['bind'])) {
error_stop('参数错误');
}
$cacheKey = "wechatAdmin.{$event}.{$eventId}";
$cacheValue = cache($cacheKey);
// var_dump($cacheValue);exit;
if (empty($cacheValue)) {
error_stop('二维码已过期, 请重新扫码');
}
if ($cacheValue['id'] === 0) {
error_stop('等待扫码', -1);
}
if ($cacheValue['id'] !== 0) {
switch ($event) {
case 'bind':
$adminId = $this->auth->id;
$thirdOauth = ThirdOauth::where([
'provider' => 'wechat',
'platform' => 'admin',
'openid' => $cacheValue['id'],
])->find();
if ($thirdOauth && $thirdOauth->admin_id !== 0) {
error_stop('该微信账号已被绑定');
}
if (!$thirdOauth) {
$thirdOauth = ThirdOauth::create([
'provider' => 'wechat',
'platform' => 'admin',
'openid' => $cacheValue['id'],
'admin_id' => $adminId
]);
} else {
$thirdOauth->admin_id = $adminId;
$thirdOauth->save();
}
break;
}
$this->success();
}
}
// 解绑
public function unbind()
{
$adminId = $this->auth->id;
$thirdOauth = ThirdOauth::where([
'provider' => 'wechat',
'platform' => 'admin',
'admin_id' => $adminId
])->find();
if ($thirdOauth) {
$thirdOauth->admin_id = 0;
$thirdOauth->save();
}
$this->success();
}
}