mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
增加接口配置
This commit is contained in:
parent
51308e7b24
commit
cd1c8a2a9d
@ -15,8 +15,10 @@ abstract class Auth extends Controller
|
||||
{
|
||||
/**
|
||||
* 当前接口类型
|
||||
* 小程序使用 wxapp
|
||||
* 服务号使用 wechat
|
||||
* -- 手机浏览器访问 wap
|
||||
* -- 电脑浏览器访问 web
|
||||
* -- 微信小程序访问 wxapp
|
||||
* -- 微信服务号访问 wechat
|
||||
* @var string
|
||||
*/
|
||||
protected $type;
|
||||
@ -38,18 +40,21 @@ abstract class Auth extends Controller
|
||||
*/
|
||||
protected function initialize()
|
||||
{
|
||||
$this->type = input('api', UserService::APITYPE_WXAPP);
|
||||
if (empty(UserService::TYPES[$this->type])) {
|
||||
$this->error("接口通道[{$this->type}]未定义规则!");
|
||||
}
|
||||
$this->user = $this->getUser();
|
||||
$this->uuid = $this->user['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户数据
|
||||
* @return array|void
|
||||
* @return array
|
||||
*/
|
||||
protected function getUser(): array
|
||||
{
|
||||
try {
|
||||
$this->type = input('api', 'web');
|
||||
$service = UserService::instance();
|
||||
if (empty($this->uuid)) {
|
||||
$token = input('token') ?: $this->request->header('token');
|
||||
|
@ -13,6 +13,12 @@ use think\admin\Controller;
|
||||
*/
|
||||
class Login extends Controller
|
||||
{
|
||||
/**
|
||||
* 接口认证类型
|
||||
* @var string
|
||||
*/
|
||||
private $type = UserService::APITYPE_WAP;
|
||||
|
||||
/**
|
||||
* 绑定数据表
|
||||
* @var string
|
||||
@ -73,7 +79,7 @@ class Login extends Controller
|
||||
$this->error('手机号已注册,请使用其它手机号!');
|
||||
}
|
||||
$data['password'] = md5($data['password']);
|
||||
$user = UserService::instance()->save($map, $data, 'web', true);
|
||||
$user = UserService::instance()->save($map, $data, $this->type, true);
|
||||
empty($user) ? $this->error('手机注册失败!') : $this->success('用户注册成功!', $user);
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,12 @@ use think\Response;
|
||||
class Wechat extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* 接口认证类型
|
||||
* @var string
|
||||
*/
|
||||
private $type = UserService::APITYPE_WECHAT;
|
||||
|
||||
/**
|
||||
* 粉丝OPNEID
|
||||
* @var string
|
||||
@ -70,8 +76,8 @@ class Wechat extends Controller
|
||||
$data['openid2'] = $data['openid'];
|
||||
$data['base_sex'] = ['未知', '男', '女'][$data['sex']] ?? '未知';
|
||||
if (isset($data['headimgurl'])) $data['headimg'] = $data['headimgurl'];
|
||||
$map = isset($data['unionid']) ? ['unionid' => $data['unionid']] : ['openid2' => $this->openid];
|
||||
$this->userInfo = UserService::instance()->save($map, array_merge($map, $data), 'wechat', true);
|
||||
$map = isset($data['unionid']) ? ['unionid' => $data['unionid']] : [UserService::AUTHS[$this->type] => $this->openid];
|
||||
$this->userInfo = UserService::instance()->save($map, array_merge($map, $data), $this->type, true);
|
||||
$content = $this->_buildContent();
|
||||
}
|
||||
return Response::create($content)->contentType('application/x-javascript');
|
||||
|
@ -17,6 +17,12 @@ use WeMini\Qrcode;
|
||||
*/
|
||||
class Wxapp extends Controller
|
||||
{
|
||||
/**
|
||||
* 接口认证类型
|
||||
* @var string
|
||||
*/
|
||||
protected $type = UserService::APITYPE_WXAPP;
|
||||
|
||||
/**
|
||||
* 小程序配置参数
|
||||
* @var array
|
||||
@ -34,8 +40,6 @@ class Wxapp extends Controller
|
||||
$this->config = [
|
||||
'appid' => sysconf('data.wxapp_appid'),
|
||||
'appsecret' => sysconf('data.wxapp_appkey'),
|
||||
'mch_id' => sysconf('data.wxapp_mch_id'),
|
||||
'mch_key' => sysconf('data.wxapp_mch_key'),
|
||||
'cache_path' => $this->app->getRuntimePath() . 'wechat',
|
||||
];
|
||||
}
|
||||
@ -53,7 +57,7 @@ class Wxapp extends Controller
|
||||
[$openid, $unionid, $sessionKey] = $this->_getSessionKey($input['code']);
|
||||
$map = empty($unionid) ? ['openid1' => $openid] : ['unionid' => $unionid];
|
||||
$data = array_merge($map, ['openid1' => $openid, 'session_key' => $sessionKey]);
|
||||
$this->success('授权换取成功!', UserService::instance()->save($map, $data, 'wxapp', true));
|
||||
$this->success('授权换取成功!', UserService::instance()->save($map, $data, $this->type, true));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -77,8 +81,8 @@ class Wxapp extends Controller
|
||||
if (is_array($result) && isset($result['openId']) && isset($result['avatarUrl']) && isset($result['nickName'])) {
|
||||
$sex = ['未知', '男', '女'][$result['gender']] ?? '未知';
|
||||
$map = empty($result['unionId']) ? ['openid1' => $result['openId']] : ['unionid' => $result['unionId']];
|
||||
$data = ['openid1' => $result['openId'], 'headimg' => $result['avatarUrl'], 'nickname' => $result['nickName'], 'base_sex' => $sex];
|
||||
$this->success('数据解密成功!', UserService::instance()->save($map, array_merge($map, $data), 'wxapp', true));
|
||||
$data = [UserService::AUTHS[$this->type] => $result['openId'], 'headimg' => $result['avatarUrl'], 'nickname' => $result['nickName'], 'base_sex' => $sex];
|
||||
$this->success('数据解密成功!', UserService::instance()->save($map, array_merge($map, $data), $this->type, true));
|
||||
} elseif (is_array($result) && isset($result['phoneNumber'])) {
|
||||
$this->success('数据解密成功!', $result);
|
||||
} else {
|
||||
|
@ -114,8 +114,7 @@ class Center extends Auth
|
||||
*/
|
||||
public function getFrom()
|
||||
{
|
||||
$query = $this->_query($this->table);
|
||||
$query->where(['from' => $this->uuid])->field('id,from,username,nickname,headimg,create_at');
|
||||
$this->success('获取我邀请的朋友', $query->order('id desc')->page(true, false, false, 15));
|
||||
$query = $this->_query($this->table)->field('id,from,username,nickname,headimg,create_at');
|
||||
$this->success('获取我邀请的朋友', $query->where(['from' => $this->uuid])->order('id desc')->page(true, false, false, 15));
|
||||
}
|
||||
}
|
@ -11,6 +11,25 @@ use think\admin\Service;
|
||||
*/
|
||||
class UserService extends Service
|
||||
{
|
||||
const APITYPE_WAP = 'wap';
|
||||
const APITYPE_WEB = 'web';
|
||||
const APITYPE_WXAPP = 'wxapp';
|
||||
const APITYPE_WECHAT = 'wechat';
|
||||
|
||||
const AUTHS = [
|
||||
UserService::APITYPE_WAP => 'phone,password',
|
||||
UserService::APITYPE_WEB => 'phone,password',
|
||||
UserService::APITYPE_WXAPP => 'openid1',
|
||||
UserService::APITYPE_WECHAT => 'openid2',
|
||||
];
|
||||
|
||||
const TYPES = [
|
||||
UserService::APITYPE_WAP => '手机浏览器访问',
|
||||
UserService::APITYPE_WEB => '电脑浏览器访问',
|
||||
UserService::APITYPE_WXAPP => '微信小程序访问',
|
||||
UserService::APITYPE_WECHAT => '微信服务号访问',
|
||||
];
|
||||
|
||||
/**
|
||||
* 认证有效时间
|
||||
* @var integer
|
||||
|
Loading…
x
Reference in New Issue
Block a user