authService = $authService;
$this->wechatService = $wechatService;
}
public function loginByAccount(Request $request)
{
//基础信息的检查
$rules = [
'account' => 'required',
'password' => 'required',
];
$messages = [
'account.required' => '请输入用户名',
'password.required' => '请输入密码',
];
$create_data = $request->all();
$validator = Validator::make($create_data, $rules, $messages);
if ($validator->fails()) {
$msg = $validator->errors()->all();
return response()->json(['status' => 0, 'msg' => $msg[0]]);
} else {
if (!$member = $this->authService->loginByAccount($request->account, $request->password, $request->autoLogin)) {
return $this->sendErrorResponse("账号或密码错误", []);
}
if ($member->status == 0) {
Auth::guard('api-member')->logout();
return $this->sendErrorResponse("你的账号处于封禁状态, 请联系管理员");
}
return response()->json([
'message' => 'Successfully created user!',
], 201);
}
}
/**
* 微信登录中转
* $state 自定义参数
* $url 回调地地
*/
public function wechatAuth(Request $request)
{
//获取参数
$app_id = subsite_config('aix.system.oauth.wechat_official.app_id');
$redirect_uri = urlencode(route('api.auth.wechat_auth_back'));
$url = $request->input('url', '');
if (empty($url)) {
return response()->json(['status' => 0, 'msg' => '请调写回调地址']);
}
$state = $request->input('state', '');
//存参数
$auth = WechatAuth::create(['url' => $url, 'state' => $state]);
//微信授权
$wechat_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$auth['id']}#wechat_redirect";
return redirect($wechat_url);
}
/**
* 微信回调
*/
public function wechatAuthBack(Request $request)
{
//微信登录
$officialAccount = $this->wechatService->getOfficialAccount();
$wechatUser = $officialAccount->oauth->user()->getOriginal();
//回调
$id = $request->input('state');
$auth = WechatAuth::where('id', $id)->first();
$wechatUser['state'] = $auth['state'];
unset($wechatUser['privilege']);
//循环拼接表单项
$formItemString = '';
foreach ($wechatUser as $key => $value) {
$formItemString .= "";
}
//构造表单并跳转
$content = <<
{ $formItemString }
EOF;
exit ($content);
}
public function test(Request $request)
{
dd($request->post());
}
/**
* 根据token获取信息
*/
public function getInfoByToken(Request $request)
{
$token = $request->header('token');
if (empty($token)) {
return response()->json([
'code' => 2,
'message' => '请输入token',
]);
}
$token_info = ThirdToken::where('token', $token)->first();
if (empty($token_info)) {
return response()->json([
'code' => 2,
'message' => 'token错误',
]);
}
$expire = strtotime($token_info['expire_at']);
if ($expire < time()) {
return response()->json([
'code' => 2,
'message' => 'token已过期',
]);
}
if ($token_info['id'] != 130) {
$token_info->expire_at = date('Y-m-d H:i:s', time() + 7200);
$token_info->save();
}
if ($token_info['type'] == 1) {
$member = Member::where('id', $token_info['type_id'])->first();
$member_info = MemberInfo::where('uid', $token_info['type_id'])->first();
$info = [
'id' => $token_info['type_id'],
'type' => 1,
'realname' => $member_info['realname'],
'avatar' => $member['avatars'] ? upload_asset($member['avatars']) : '',
'sex' => $member_info['sex'],
'mobile' => $member['mobile'],
'email' => $member['email'],
];
} elseif ($token_info['type'] == 2) {
$company = Company::where('id', $token_info['type_id'])->first();
$info = [
'id' => $token_info['type_id'],
'type' => 2,
'companyname' => $company['companyname'],
'logo' => $company['logo'] ? upload_asset($company['logo']) : '',
'mobile' => $company['mobile'],
'email' => $company['email'],
'address' => $company['address'],
'contact' => $company['contact'],
];
}
return response()->json([
'code' => 1,
'data' => $info,
'message' => '成功',
]);
}
}