2021-12-23 00:19:55 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Models\PushDeerUser;
|
2022-01-24 13:40:50 +08:00
|
|
|
use App\Models\PushDeerKey;
|
|
|
|
use App\Models\PushDeerDevice;
|
|
|
|
use App\Models\PushDeerMessage;
|
2021-12-23 00:19:55 +08:00
|
|
|
|
|
|
|
class PushDeerUserController extends Controller
|
|
|
|
{
|
|
|
|
public function info(Request $request)
|
|
|
|
{
|
|
|
|
//return $_SESSION;
|
2022-01-04 22:11:03 +08:00
|
|
|
return http_result(PushDeerUser::where('id', $_SESSION['uid'])->get()->first());
|
2021-12-23 00:19:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function fakeLogin(Request $request)
|
|
|
|
{
|
2022-01-17 13:08:45 +08:00
|
|
|
if (!env('APP_DEBUG')) {
|
|
|
|
return send_error('Debug only', ErrorCode('ARGS'));
|
|
|
|
}
|
|
|
|
|
2021-12-23 00:19:55 +08:00
|
|
|
$info = [
|
|
|
|
'uid' => 'theid999',
|
|
|
|
'email' => 'easychen+new@gmail.com',
|
|
|
|
];
|
|
|
|
|
|
|
|
if (isset($info['uid'])) {
|
|
|
|
// 通过 uid 查询 user 表,如果存在自动登入,如果不存在自动注册并登入
|
|
|
|
$pd_user = PushDeerUser::where('apple_id', $info['uid'])->get()->first();
|
|
|
|
if (!$pd_user) {
|
|
|
|
// 用户不存在,创建用户
|
|
|
|
$the_user = [];
|
|
|
|
$the_user['apple_id'] = $info['uid'];
|
|
|
|
$the_user['email'] = $info['email'];
|
|
|
|
$the_user['name'] = @reset(explode('@', $info['email']));
|
|
|
|
$the_user['level'] = 1;
|
|
|
|
|
|
|
|
$pd_user = PushDeerUser::create($the_user);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 将数据写到session
|
|
|
|
session_start();
|
|
|
|
$_SESSION['uid'] = $pd_user['id'];
|
|
|
|
$_SESSION['name'] = $pd_user['name'];
|
|
|
|
$_SESSION['email'] = $pd_user['email'];
|
|
|
|
$_SESSION['level'] = $pd_user['level'];
|
|
|
|
|
|
|
|
session_regenerate_id(true);
|
|
|
|
$token = session_id();
|
|
|
|
return http_result(['token'=>$token]);
|
|
|
|
}
|
|
|
|
|
2021-12-27 18:02:48 +08:00
|
|
|
return send_error('id_token解析错误', ErrorCode('ARGS'));
|
2021-12-23 00:19:55 +08:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:53:31 +08:00
|
|
|
public function wecode2unionid(Request $request)
|
2022-01-24 12:00:09 +08:00
|
|
|
{
|
|
|
|
$validated = $request->validate(
|
|
|
|
[
|
2022-02-06 12:53:31 +08:00
|
|
|
'code' => 'required|string',
|
2022-01-24 12:00:09 +08:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($validated['code'])) {
|
|
|
|
// 解码并进行验证
|
|
|
|
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
|
|
|
|
.urlencode(env("WECHAT_APPID"))
|
|
|
|
."&secret="
|
|
|
|
.urlencode(env("WECHAT_APPSECRET"))
|
|
|
|
."&code="
|
|
|
|
.urlencode($validated['code'])
|
|
|
|
."&grant_type=authorization_code";
|
|
|
|
|
|
|
|
$code_info = json_decode(file_get_contents($url), true);
|
|
|
|
|
2022-01-24 13:40:50 +08:00
|
|
|
if (!$code_info || !isset($code_info['access_token']) || !isset($code_info['unionid'])) {
|
2022-01-24 12:00:09 +08:00
|
|
|
return send_error("错误的Code", ErrorCode('REMOTE'));
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:53:31 +08:00
|
|
|
return http_result(['unionid'=>$code_info['unionid']]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return send_error('微信Code错误', ErrorCode('ARGS'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function wechatLogin(Request $request)
|
|
|
|
{
|
|
|
|
$validated = $request->validate(
|
|
|
|
[
|
|
|
|
'code' => 'required|string',
|
|
|
|
'self_hosted' => 'integer|nullable',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($validated['code'])) {
|
|
|
|
if (intval(@$validated['self_hosted']) != 1) {
|
|
|
|
// 解码并进行验证
|
|
|
|
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
|
|
|
|
.urlencode(env("WECHAT_APPID"))
|
|
|
|
."&secret="
|
|
|
|
.urlencode(env("WECHAT_APPSECRET"))
|
|
|
|
."&code="
|
|
|
|
.urlencode($validated['code'])
|
|
|
|
."&grant_type=authorization_code";
|
|
|
|
|
|
|
|
$code_info = json_decode(file_get_contents($url), true);
|
|
|
|
|
|
|
|
if (!$code_info || !isset($code_info['access_token']) || !isset($code_info['unionid'])) {
|
|
|
|
return send_error("错误的Code", ErrorCode('REMOTE'));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$url = "https://api2.pushdeer.com/login/unoinid?code=".urlencode($validated['code']);
|
|
|
|
$ret = json_decode(file_get_contents($url), true);
|
2022-02-06 13:01:56 +08:00
|
|
|
if (!$ret || !isset($ret['content']) || !isset($ret['content']['unionid'])) {
|
2022-02-06 12:53:31 +08:00
|
|
|
return send_error("错误的Code", ErrorCode('REMOTE'));
|
|
|
|
}
|
|
|
|
|
2022-02-06 13:01:56 +08:00
|
|
|
$code_info = ['unionid'=>$ret['content']['unionid']];
|
2022-02-06 12:53:31 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:40:50 +08:00
|
|
|
// 现在拿到unionid了
|
2022-01-24 12:00:09 +08:00
|
|
|
|
2022-01-24 13:40:50 +08:00
|
|
|
$pd_user = PushDeerUser::where('wechat_id', $code_info['unionid'])->get()->first();
|
2022-01-24 12:00:09 +08:00
|
|
|
if (!$pd_user) {
|
|
|
|
// 用户不存在,创建用户
|
|
|
|
$the_user = [];
|
2022-01-24 13:40:50 +08:00
|
|
|
$the_user['wechat_id'] = $code_info['unionid'];
|
|
|
|
$the_user['email'] = $code_info['unionid'].'@'.'fake.pushdeer.com';
|
|
|
|
$the_user['name'] = '微信用户'.substr($code_info['unionid'], 0, 6);
|
2022-01-24 12:00:09 +08:00
|
|
|
$the_user['level'] = 1;
|
|
|
|
|
|
|
|
$pd_user = PushDeerUser::create($the_user);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 将数据写到session
|
|
|
|
session_start();
|
|
|
|
$_SESSION['uid'] = $pd_user['id'];
|
|
|
|
$_SESSION['name'] = $pd_user['name'];
|
|
|
|
$_SESSION['email'] = $pd_user['email'];
|
|
|
|
$_SESSION['level'] = $pd_user['level'];
|
|
|
|
|
|
|
|
session_regenerate_id(true);
|
|
|
|
$token = session_id();
|
|
|
|
return http_result(['token'=>$token]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return send_error('微信Code错误', ErrorCode('ARGS'));
|
|
|
|
}
|
|
|
|
|
2021-12-23 00:19:55 +08:00
|
|
|
//
|
|
|
|
public function login(Request $request)
|
|
|
|
{
|
|
|
|
$validated = $request->validate(
|
|
|
|
[
|
2022-02-06 12:53:31 +08:00
|
|
|
'idToken' => 'required|string',
|
2021-12-23 00:19:55 +08:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($validated['idToken'])) {
|
|
|
|
// 解码并进行验证
|
|
|
|
$info = getUserDataFromIdentityToken($validated['idToken']);
|
|
|
|
if (isset($info['uid'])) {
|
|
|
|
// 通过 uid 查询 user 表,如果存在自动登入,如果不存在自动注册并登入
|
|
|
|
$pd_user = PushDeerUser::where('apple_id', $info['uid'])->get()->first();
|
|
|
|
if (!$pd_user) {
|
|
|
|
// 用户不存在,创建用户
|
|
|
|
$the_user = [];
|
|
|
|
$the_user['apple_id'] = $info['uid'];
|
|
|
|
$the_user['email'] = $info['email'];
|
|
|
|
$the_user['name'] = @reset(explode('@', $info['email']));
|
|
|
|
$the_user['level'] = 1;
|
|
|
|
|
|
|
|
$pd_user = PushDeerUser::create($the_user);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 将数据写到session
|
|
|
|
session_start();
|
|
|
|
$_SESSION['uid'] = $pd_user['id'];
|
|
|
|
$_SESSION['name'] = $pd_user['name'];
|
|
|
|
$_SESSION['email'] = $pd_user['email'];
|
|
|
|
$_SESSION['level'] = $pd_user['level'];
|
|
|
|
|
|
|
|
session_regenerate_id(true);
|
|
|
|
$token = session_id();
|
|
|
|
return http_result(['token'=>$token]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-27 18:02:48 +08:00
|
|
|
return send_error('id_token解析错误', ErrorCode('ARGS'));
|
2021-12-23 00:19:55 +08:00
|
|
|
}
|
2022-01-24 13:40:50 +08:00
|
|
|
|
|
|
|
public function merge(Request $request)
|
|
|
|
{
|
|
|
|
$validated = $request->validate(
|
|
|
|
[
|
2022-02-06 12:53:31 +08:00
|
|
|
'tokenorcode' => 'required|string',
|
|
|
|
'type' => 'required|string', // apple or wechat
|
2022-01-24 13:40:50 +08:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2022-01-24 19:11:00 +08:00
|
|
|
|
2022-01-25 12:58:05 +08:00
|
|
|
|
2022-01-24 13:40:50 +08:00
|
|
|
$type_field = strtolower($validated['type']) == 'apple' ? 'apple_id' : 'wechat_id';
|
|
|
|
|
2022-01-25 12:58:05 +08:00
|
|
|
|
|
|
|
|
2022-01-24 13:40:50 +08:00
|
|
|
$identiy_string = false;
|
|
|
|
if ($type_field == 'apple_id') {
|
|
|
|
$info = getUserDataFromIdentityToken($validated['tokenorcode']);
|
|
|
|
if ($info && isset($info['uid'])) {
|
|
|
|
$identiy_string = $info['uid'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// wechat
|
|
|
|
// 解码并进行验证
|
|
|
|
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
|
|
|
|
.urlencode(env("WECHAT_APPID"))
|
|
|
|
."&secret="
|
|
|
|
.urlencode(env("WECHAT_APPSECRET"))
|
|
|
|
."&code="
|
|
|
|
.urlencode($validated['tokenorcode'])
|
|
|
|
."&grant_type=authorization_code";
|
|
|
|
|
|
|
|
$info = json_decode(file_get_contents($url), true);
|
|
|
|
|
|
|
|
if ($info && $info['unionid']) {
|
|
|
|
$identiy_string = $info['unionid'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$identiy_string) {
|
|
|
|
return send_error('错误的token 或者 code', ErrorCode('ARGS'));
|
|
|
|
}
|
|
|
|
|
2022-01-24 13:49:11 +08:00
|
|
|
$user2delete = PushDeerUser::where($type_field, $identiy_string)->get()->first();
|
2022-01-24 13:40:50 +08:00
|
|
|
$current_user = PushDeerUser::where('id', uid())->get()->first();
|
2022-01-24 13:49:11 +08:00
|
|
|
|
|
|
|
|
2022-01-24 13:40:50 +08:00
|
|
|
|
|
|
|
// 如果存在旧用户,合并并删除
|
|
|
|
if ($user2delete) {
|
2022-01-25 12:58:05 +08:00
|
|
|
if ($user2delete['id'] == $current_user['id']) {
|
|
|
|
return send_error("不能合并当前账号本身", ErrorCode('ARGS'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-24 13:40:50 +08:00
|
|
|
// 删除Key
|
|
|
|
PushDeerKey::where('uid', $user2delete['id'])->delete();
|
|
|
|
|
|
|
|
// message合并
|
2022-01-25 12:58:05 +08:00
|
|
|
PushDeerMessage::where('uid', $user2delete['id'])->update(['uid'=>uid()]);
|
2022-01-24 13:40:50 +08:00
|
|
|
|
|
|
|
// 设备合并
|
2022-01-25 12:58:05 +08:00
|
|
|
PushDeerDevice::where('uid', $user2delete['id'])->update(['uid'=>uid()]);
|
2022-01-24 13:40:50 +08:00
|
|
|
|
|
|
|
// 删除用户
|
|
|
|
$user2delete->delete();
|
|
|
|
}
|
|
|
|
|
2022-01-25 12:58:05 +08:00
|
|
|
$current_user[$type_field] = $identiy_string;
|
|
|
|
$current_user->save();
|
|
|
|
|
2022-01-24 13:40:50 +08:00
|
|
|
return http_result(['result'=>'done']);
|
|
|
|
}
|
2021-12-23 00:19:55 +08:00
|
|
|
}
|