增加接口配置

This commit is contained in:
Anyon 2020-12-13 12:34:41 +08:00
parent 51308e7b24
commit cd1c8a2a9d
6 changed files with 54 additions and 15 deletions

View File

@ -15,8 +15,10 @@ abstract class Auth extends Controller
{ {
/** /**
* 当前接口类型 * 当前接口类型
* 小程序使用 wxapp * -- 手机浏览器访问 wap
* 服务号使用 wechat * -- 电脑浏览器访问 web
* -- 微信小程序访问 wxapp
* -- 微信服务号访问 wechat
* @var string * @var string
*/ */
protected $type; protected $type;
@ -38,18 +40,21 @@ abstract class Auth extends Controller
*/ */
protected function initialize() 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->user = $this->getUser();
$this->uuid = $this->user['id']; $this->uuid = $this->user['id'];
} }
/** /**
* 获取用户数据 * 获取用户数据
* @return array|void * @return array
*/ */
protected function getUser(): array protected function getUser(): array
{ {
try { try {
$this->type = input('api', 'web');
$service = UserService::instance(); $service = UserService::instance();
if (empty($this->uuid)) { if (empty($this->uuid)) {
$token = input('token') ?: $this->request->header('token'); $token = input('token') ?: $this->request->header('token');

View File

@ -13,6 +13,12 @@ use think\admin\Controller;
*/ */
class Login extends Controller class Login extends Controller
{ {
/**
* 接口认证类型
* @var string
*/
private $type = UserService::APITYPE_WAP;
/** /**
* 绑定数据表 * 绑定数据表
* @var string * @var string
@ -73,7 +79,7 @@ class Login extends Controller
$this->error('手机号已注册,请使用其它手机号!'); $this->error('手机号已注册,请使用其它手机号!');
} }
$data['password'] = md5($data['password']); $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); empty($user) ? $this->error('手机注册失败!') : $this->success('用户注册成功!', $user);
} }

View File

@ -20,6 +20,12 @@ use think\Response;
class Wechat extends Controller class Wechat extends Controller
{ {
/**
* 接口认证类型
* @var string
*/
private $type = UserService::APITYPE_WECHAT;
/** /**
* 粉丝OPNEID * 粉丝OPNEID
* @var string * @var string
@ -70,8 +76,8 @@ class Wechat extends Controller
$data['openid2'] = $data['openid']; $data['openid2'] = $data['openid'];
$data['base_sex'] = ['未知', '男', '女'][$data['sex']] ?? '未知'; $data['base_sex'] = ['未知', '男', '女'][$data['sex']] ?? '未知';
if (isset($data['headimgurl'])) $data['headimg'] = $data['headimgurl']; if (isset($data['headimgurl'])) $data['headimg'] = $data['headimgurl'];
$map = isset($data['unionid']) ? ['unionid' => $data['unionid']] : ['openid2' => $this->openid]; $map = isset($data['unionid']) ? ['unionid' => $data['unionid']] : [UserService::AUTHS[$this->type] => $this->openid];
$this->userInfo = UserService::instance()->save($map, array_merge($map, $data), 'wechat', true); $this->userInfo = UserService::instance()->save($map, array_merge($map, $data), $this->type, true);
$content = $this->_buildContent(); $content = $this->_buildContent();
} }
return Response::create($content)->contentType('application/x-javascript'); return Response::create($content)->contentType('application/x-javascript');

View File

@ -17,6 +17,12 @@ use WeMini\Qrcode;
*/ */
class Wxapp extends Controller class Wxapp extends Controller
{ {
/**
* 接口认证类型
* @var string
*/
protected $type = UserService::APITYPE_WXAPP;
/** /**
* 小程序配置参数 * 小程序配置参数
* @var array * @var array
@ -34,8 +40,6 @@ class Wxapp extends Controller
$this->config = [ $this->config = [
'appid' => sysconf('data.wxapp_appid'), 'appid' => sysconf('data.wxapp_appid'),
'appsecret' => sysconf('data.wxapp_appkey'), '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', 'cache_path' => $this->app->getRuntimePath() . 'wechat',
]; ];
} }
@ -53,7 +57,7 @@ class Wxapp extends Controller
[$openid, $unionid, $sessionKey] = $this->_getSessionKey($input['code']); [$openid, $unionid, $sessionKey] = $this->_getSessionKey($input['code']);
$map = empty($unionid) ? ['openid1' => $openid] : ['unionid' => $unionid]; $map = empty($unionid) ? ['openid1' => $openid] : ['unionid' => $unionid];
$data = array_merge($map, ['openid1' => $openid, 'session_key' => $sessionKey]); $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'])) { if (is_array($result) && isset($result['openId']) && isset($result['avatarUrl']) && isset($result['nickName'])) {
$sex = ['未知', '男', '女'][$result['gender']] ?? '未知'; $sex = ['未知', '男', '女'][$result['gender']] ?? '未知';
$map = empty($result['unionId']) ? ['openid1' => $result['openId']] : ['unionid' => $result['unionId']]; $map = empty($result['unionId']) ? ['openid1' => $result['openId']] : ['unionid' => $result['unionId']];
$data = ['openid1' => $result['openId'], 'headimg' => $result['avatarUrl'], 'nickname' => $result['nickName'], 'base_sex' => $sex]; $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), 'wxapp', true)); $this->success('数据解密成功!', UserService::instance()->save($map, array_merge($map, $data), $this->type, true));
} elseif (is_array($result) && isset($result['phoneNumber'])) { } elseif (is_array($result) && isset($result['phoneNumber'])) {
$this->success('数据解密成功!', $result); $this->success('数据解密成功!', $result);
} else { } else {

View File

@ -114,8 +114,7 @@ class Center extends Auth
*/ */
public function getFrom() public function getFrom()
{ {
$query = $this->_query($this->table); $query = $this->_query($this->table)->field('id,from,username,nickname,headimg,create_at');
$query->where(['from' => $this->uuid])->field('id,from,username,nickname,headimg,create_at'); $this->success('获取我邀请的朋友', $query->where(['from' => $this->uuid])->order('id desc')->page(true, false, false, 15));
$this->success('获取我邀请的朋友', $query->order('id desc')->page(true, false, false, 15));
} }
} }

View File

@ -11,6 +11,25 @@ use think\admin\Service;
*/ */
class UserService extends 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 * @var integer