diff --git a/app/data/controller/api/Auth.php b/app/data/controller/api/Auth.php index 2fef462cc..dbca49bc3 100644 --- a/app/data/controller/api/Auth.php +++ b/app/data/controller/api/Auth.php @@ -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'); diff --git a/app/data/controller/api/Login.php b/app/data/controller/api/Login.php index ea8b8c7e8..5c07d7460 100644 --- a/app/data/controller/api/Login.php +++ b/app/data/controller/api/Login.php @@ -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); } diff --git a/app/data/controller/api/Wechat.php b/app/data/controller/api/Wechat.php index dac1cf018..1a3cb59a3 100644 --- a/app/data/controller/api/Wechat.php +++ b/app/data/controller/api/Wechat.php @@ -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'); diff --git a/app/data/controller/api/Wxapp.php b/app/data/controller/api/Wxapp.php index bbcab3fac..dfc8ce969 100644 --- a/app/data/controller/api/Wxapp.php +++ b/app/data/controller/api/Wxapp.php @@ -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 { diff --git a/app/data/controller/api/auth/Center.php b/app/data/controller/api/auth/Center.php index 276451f2d..8671c1d97 100644 --- a/app/data/controller/api/auth/Center.php +++ b/app/data/controller/api/auth/Center.php @@ -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)); } } \ No newline at end of file diff --git a/app/data/service/UserService.php b/app/data/service/UserService.php index c4852ca88..7a5358989 100644 --- a/app/data/service/UserService.php +++ b/app/data/service/UserService.php @@ -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