|
@@ -6,6 +6,9 @@ use App\Models\Api\Personnel\Employee as EmployeeModel;
|
|
|
use App\Http\Requests\Api\Login as Request;
|
|
use App\Http\Requests\Api\Login as Request;
|
|
|
use App\Facades\Servers\Sms\VerifyCode as Sms;
|
|
use App\Facades\Servers\Sms\VerifyCode as Sms;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
+use App\Facades\Servers\Wechat\OpenPlat;
|
|
|
|
|
+use App\Models\Api\Personnel\EmployeeOpenid as EmployeeOpenidModel;
|
|
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* API登录控制器
|
|
* API登录控制器
|
|
@@ -35,7 +38,7 @@ class Login extends Api
|
|
|
// 接收数据
|
|
// 接收数据
|
|
|
$password = $Request->input('password', '');
|
|
$password = $Request->input('password', '');
|
|
|
// 查询用户
|
|
// 查询用户
|
|
|
- $admin = $EmployeeModel->Where('employee_code', $employee_code)->first(['company_id','id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
|
|
|
|
|
|
|
+ $admin = $EmployeeModel->Where('employee_code', $employee_code)->first(['company_id', 'id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
|
|
|
// 用户不存在
|
|
// 用户不存在
|
|
|
if (!$admin || $admin['status']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
if (!$admin || $admin['status']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
|
// 用户不存在
|
|
// 用户不存在
|
|
@@ -45,10 +48,8 @@ class Login extends Api
|
|
|
// 比对密码
|
|
// 比对密码
|
|
|
if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
|
// 登录
|
|
// 登录
|
|
|
- $accessToken = $EmployeeModel->Login($admin['id'],$admin['company_id'], 'api');
|
|
|
|
|
- // 比对密码
|
|
|
|
|
|
|
+ $accessToken = $EmployeeModel->Login($admin['id'], $admin['company_id'], 'api');
|
|
|
if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
|
|
if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
|
|
|
- // 获取权限列表
|
|
|
|
|
$accessToken['username'] = $admin['name'];
|
|
$accessToken['username'] = $admin['name'];
|
|
|
$accessToken['is_system_admin'] = 0;
|
|
$accessToken['is_system_admin'] = 0;
|
|
|
// 表单令牌
|
|
// 表单令牌
|
|
@@ -94,7 +95,7 @@ class Login extends Api
|
|
|
// 接收数据
|
|
// 接收数据
|
|
|
$password = $Request->input('password', '');
|
|
$password = $Request->input('password', '');
|
|
|
// 查询用户
|
|
// 查询用户
|
|
|
- $user_info = $EmployeeModel->where('mobile', $phone)->first(['company_id','id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
|
|
|
|
|
|
|
+ $user_info = $EmployeeModel->where('mobile', $phone)->first(['company_id', 'id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
|
|
|
// 用户不存在
|
|
// 用户不存在
|
|
|
if (!$user_info || $user_info['status']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
if (!$user_info || $user_info['status']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
|
// 用户不存在
|
|
// 用户不存在
|
|
@@ -104,10 +105,8 @@ class Login extends Api
|
|
|
// 比对密码
|
|
// 比对密码
|
|
|
if (md5($password) != $user_info['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
if (md5($password) != $user_info['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
|
// 登录
|
|
// 登录
|
|
|
- $accessToken = $EmployeeModel->Login($user_info['id'],$user_info['company_id'], 'api');
|
|
|
|
|
- // 比对密码
|
|
|
|
|
|
|
+ $accessToken = $EmployeeModel->Login($user_info['id'], $user_info['company_id'], 'api');
|
|
|
if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
|
|
if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
|
|
|
- // 获取权限列表
|
|
|
|
|
$accessToken['username'] = $user_info['name'];
|
|
$accessToken['username'] = $user_info['name'];
|
|
|
$accessToken['is_system_admin'] = 0;
|
|
$accessToken['is_system_admin'] = 0;
|
|
|
// 表单令牌
|
|
// 表单令牌
|
|
@@ -163,7 +162,7 @@ class Login extends Api
|
|
|
// 接收数据
|
|
// 接收数据
|
|
|
$password = $Request->input('password', '');
|
|
$password = $Request->input('password', '');
|
|
|
// 查询用户
|
|
// 查询用户
|
|
|
- $admin = $EmployeeModel->where('email', $email)->first(['company_id','id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
|
|
|
|
|
|
|
+ $admin = $EmployeeModel->where('email', $email)->first(['company_id', 'id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
|
|
|
// 用户不存在
|
|
// 用户不存在
|
|
|
if (!$admin) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
if (!$admin) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
|
// 用户不存在
|
|
// 用户不存在
|
|
@@ -173,10 +172,8 @@ class Login extends Api
|
|
|
// 比对密码
|
|
// 比对密码
|
|
|
if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
|
// 登录
|
|
// 登录
|
|
|
- $accessToken = $EmployeeModel->Login($admin['id'],$admin['company_id'], 'api');
|
|
|
|
|
- // 比对密码
|
|
|
|
|
|
|
+ $accessToken = $EmployeeModel->Login($admin['id'], $admin['company_id'], 'api');
|
|
|
if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
|
|
if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
|
|
|
- // 获取权限列表
|
|
|
|
|
$accessToken['username'] = $admin['name'];
|
|
$accessToken['username'] = $admin['name'];
|
|
|
$accessToken['is_system_admin'] = 0;
|
|
$accessToken['is_system_admin'] = 0;
|
|
|
// 表单令牌
|
|
// 表单令牌
|
|
@@ -205,7 +202,7 @@ class Login extends Api
|
|
|
if (!$session) return json_send(['code' => 'error', 'msg' => '请先获取手机号验证码']);
|
|
if (!$session) return json_send(['code' => 'error', 'msg' => '请先获取手机号验证码']);
|
|
|
if ($session['code'] != $code || $session['mobile'] != $phone) return json_send(['code' => 'error', 'msg' => '验证码错误']);
|
|
if ($session['code'] != $code || $session['mobile'] != $phone) return json_send(['code' => 'error', 'msg' => '验证码错误']);
|
|
|
// 查询用户
|
|
// 查询用户
|
|
|
- $admin = $EmployeeModel->where('mobile', $phone)->first(['company_id','id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
|
|
|
|
|
|
|
+ $admin = $EmployeeModel->where('mobile', $phone)->first(['company_id', 'id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
|
|
|
// 用户不存在
|
|
// 用户不存在
|
|
|
if (!$admin) return json_send(['code' => 'error', 'msg' => '账号不存在']);
|
|
if (!$admin) return json_send(['code' => 'error', 'msg' => '账号不存在']);
|
|
|
// 用户不存在
|
|
// 用户不存在
|
|
@@ -213,10 +210,78 @@ class Login extends Api
|
|
|
// 转数组
|
|
// 转数组
|
|
|
$admin = $admin->toArray();
|
|
$admin = $admin->toArray();
|
|
|
// 登录
|
|
// 登录
|
|
|
- $accessToken = $EmployeeModel->Login($admin['id'],$admin['company_id'], 'api');
|
|
|
|
|
- // 比对密码
|
|
|
|
|
|
|
+ $accessToken = $EmployeeModel->Login($admin['id'], $admin['company_id'], 'api');
|
|
|
|
|
+ if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
|
|
|
|
|
+ $accessToken['username'] = $admin['name'];
|
|
|
|
|
+ $accessToken['is_system_admin'] = 0;
|
|
|
|
|
+ // 表单令牌
|
|
|
|
|
+ return json_send(['code' => 'success', 'msg' => '登录成功', 'data' => $accessToken]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 小程序手机号授权 /api/login/wechat_phone
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $code 授权码
|
|
|
|
|
+ *
|
|
|
|
|
+ * */
|
|
|
|
|
+ public function wechat_phone(Request $Request, EmployeeModel $EmployeeModel, EmployeeOpenidModel $EmployeeOpenidModel)
|
|
|
|
|
+ {
|
|
|
|
|
+ // 验证规则
|
|
|
|
|
+ $Request->scene('wechat_phone')->validate();
|
|
|
|
|
+ $company_id = request('access_token.company_id', '0');
|
|
|
|
|
+ // 接收数据
|
|
|
|
|
+ $auth_code = request('auth_code', '');
|
|
|
|
|
+ $open_code = $Request->input('open_code', '');
|
|
|
|
|
+ $appId = config('wechat.mini.app_id','');//小程序APPID
|
|
|
|
|
+ // 授权结果
|
|
|
|
|
+ $result = OpenPlat::getUserPhone($auth_code, $appId);
|
|
|
|
|
+ // 如果所需字段不存在
|
|
|
|
|
+ if (isset($result['error'])) return json_send(['code' => 'error', 'msg' => '授权失败', 'data' => ['error' => $result['error']]]);
|
|
|
|
|
+ // 如果所需字段不存在
|
|
|
|
|
+ if (empty($result['purePhoneNumber'])) return json_send(['code' => 'error', 'msg' => '未获取到手机号', 'data' => ['error' => '未获取到手机号']]);
|
|
|
|
|
+ $phone = $result['purePhoneNumber'];
|
|
|
|
|
+ // 查询用户
|
|
|
|
|
+ $phone_user_data = $EmployeeOpenidModel->where(['mobile' => $phone])->first(['company_id', 'id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
|
|
|
|
|
+ if (!$phone_user_data) return json_send(['code' => 'error', 'msg' => '手机号码未授权']);
|
|
|
|
|
+ if ($phone_user_data['status']) return json_send(['code' => 'error', 'msg' => '该账号已停用']);
|
|
|
|
|
+ $uid = $phone_user_data->employee_id;
|
|
|
|
|
+ //获取用户的openid
|
|
|
|
|
+ $tokenData = OpenPlat::jscode2session($open_code, $appId);
|
|
|
|
|
+ if (!isset($tokenData['openid']) || !$tokenData['openid']) return json_send(['code' => 'error', 'msg' => '获取openid失败', 'data' => $tokenData]);
|
|
|
|
|
+ if (!isset($tokenData['unionid']) || !$tokenData['unionid']) return json_send(['code' => 'error', 'msg' => '获取unionid失败', 'data' => $tokenData]);
|
|
|
|
|
+
|
|
|
|
|
+ $user_open_data = $EmployeeOpenidModel->where(['mini_openid' => $tokenData['openid']])->first();
|
|
|
|
|
+ if (!$user_open_data) {
|
|
|
|
|
+ //自动绑定手机号码用户
|
|
|
|
|
+ try {
|
|
|
|
|
+ $phone_user_data->band_wechat = 1; //绑定微信0=未绑定1=已绑定
|
|
|
|
|
+ $phone_user_data->save();
|
|
|
|
|
+ $EmployeeOpenidModel->insertGetId([
|
|
|
|
|
+ 'company_id' => $company_id,
|
|
|
|
|
+ 'employee_id' => $uid,
|
|
|
|
|
+ 'unionid' => $tokenData['unionid'] ?? '',
|
|
|
|
|
+ 'mini_openid' => $tokenData['openid'],
|
|
|
|
|
+ 'type' => '3',
|
|
|
|
|
+ 'insert_time' => time()
|
|
|
|
|
+ ]);
|
|
|
|
|
+ DB::commit();
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ DB::rollBack();
|
|
|
|
|
+ return json_send(['code' => 'error', 'msg' => '绑定失败', 'data' => $e->getMessage()]);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $user_open_data->unionid = $tokenData['unionid'] ?? '';
|
|
|
|
|
+ $user_open_data->mini_openid = $tokenData['openid'];
|
|
|
|
|
+ $user_open_data->type = '3';
|
|
|
|
|
+ $user_open_data->update_time = time();
|
|
|
|
|
+ $user_open_data->save();
|
|
|
|
|
+ }
|
|
|
|
|
+ // 转数组
|
|
|
|
|
+ $admin = $phone_user_data->toArray();
|
|
|
|
|
+ // 登录
|
|
|
|
|
+ $accessToken = $EmployeeModel->Login($admin['id'], $admin['company_id'], 'api');
|
|
|
if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
|
|
if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
|
|
|
- // 获取权限列表
|
|
|
|
|
$accessToken['username'] = $admin['name'];
|
|
$accessToken['username'] = $admin['name'];
|
|
|
$accessToken['is_system_admin'] = 0;
|
|
$accessToken['is_system_admin'] = 0;
|
|
|
// 表单令牌
|
|
// 表单令牌
|