123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- namespace app\api\controller;
- use app\logics\SmsLogic;
- use app\logics\UserLogic;
- use app\model\Employee;
- use app\model\Miniprogram;
- use app\model\User as ModelUser;
- use app\model\WeworkBindTicket;
- use app\model\WeworkCompany;
- use app\model\WeworkUser;
- use EasyWeChat\Factory;
- use Firebase\JWT\JWT;
- use openssl\Aes;
- use think\facade\Config;
- use think\facade\Log;
- use app\model\Company;
- use toolkits\Aec;
- /**
- * 企业微信登录相关 (第三方应用)
- * Class Wework
- * @package app\apiSingle\controller
- */
- class Wework extends Base
- {
- /**
- * 获取授权链接
- */
- public function getAuthLink(){
- $config = Config::get('app.wework');
- $app = Factory::openWork($config);
- $redirectUrl = input('redirectUrl', '', 'trim');
- $link = $app->corp->getOAuthRedirectUrl($redirectUrl, 'snsapi_userinfo', $state = null);
- return json(['code' => self::success, 'data' => $link, 'msg' => '获取成功']);
- }
- /**
- * 根据授权code换取用户信息,查询到信息则返回token,没有则返回对应的状态节点信息。
- * @return \think\response\Json
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getUserByCode(){
- $code = input('code', '', 'trim');
- $config = Config::get('app.wework');
- $app = Factory::openWork($config);
- $info = $app->corp->getUserByCode($code);
- if ($info['errcode'] == 0) {
- $where['corpid'] = $info['CorpId'];
- $where['userid'] = $info['UserId'];
- $find = (new WeworkUser())->where($where)->find();
- $wx_uid = 0;
- if (empty($find)) {
- $data['corpid'] = $info['CorpId'];
- $data['userid'] = $info['UserId'];
- $data['deviceid'] = !empty($info['DeviceId']) ? $info['DeviceId'] : '';
- $data['open_userid'] = !empty($info['open_userid']) ? $info['open_userid'] : '';
- $wx_result = WeworkUser::create($data);
- $wx_uid = $wx_result->id;
- } else {
- $wx_uid = $find['id'];
- }
- // 查询用户所在企业是否有开通应用
- $wework_company = WeworkCompany::where('auth_corpid', '=', $info['CorpId'])->find();
- $company_find = Company::where(['wework_company'=> $wework_company['id']])->find();
- if (empty($company_find)) {
- // return json(['code'=> 2, 'msg'=> '所在企业未绑定装企公司']);
- return json(['code'=> 3, 'msg'=> '请先登录', 'data'=> ['wx_cid'=> $wework_company['id'], 'wx_uid'=> $wx_uid]]);
- }
- // 查询是否有绑定员工
- $employee = Employee::where('wework_uid', '=', $wx_uid)->find();
- if (empty($employee)) {
- return json(['code'=> 3, 'msg'=> '请先登录', 'data'=> ['wx_cid'=> $wework_company['id'], 'wx_uid'=> $wx_uid]]);
- } else {
- // 查询到员工,返回token
- $token = [
- 'root_org' => $employee['root_id'],
- 'isEmployee' => true,
- 'uid' => $employee['uid'],
- 'employee_id'=> $employee['id'],
- 'org_id' => $employee['org_id']
- ];
- // 信息加密
- $data = http_build_query($token);
- $aes = new Aes(config('app.jwt_key'));
- $key = $aes->encrypt($data);
- // token数据设置
- $payload = array(
- "iss" => "https://" . request()->domain(),
- "aud" => 'mini',
- "iat" => time(),
- "nbf" => time(),
- "data" => $key
- );
- // 自定义登陆状态
- $token = JWT::encode($payload, config('app.jwt_key'));
- $logic = new UserLogic();
- $user = $logic->getInfo([['id', '=', $employee['uid']]], ['id', 'nickname', 'headimgurl', 'sex', 'subscribe', 'phone']);
- $company = Company::where('root_id', $employee['root_id'])->find();
- $company['client_type'] = Miniprogram::where('root_id', '=', $employee['root_id'])->value('notify');
- $returnData = ['code' => self::success, 'token' => $token, 'user' => $user, 'company' =>$company->company_name, 'company_data'=>$company];
- return json($returnData);
- }
- } else {
- return json(['code'=> self::error_msg, 'msg'=> '获取授权信息失败', 'data'=> '']);
- }
- }
- /**
- * 获取手机验证码
- */
- public function sms($phone)
- {
- $content = '验证码:%code%(请勿转告他人),有效时间15分钟,请尽快完成验证';
- $smsLogic = new SmsLogic();
- $rs = $smsLogic->sendSms($phone, 'employee_login_phone', $content);
- if (!$rs) return json(['code' => 1, 'msg' => '短信发送失败']);
- return json(['code' => 0, 'msg' => '短信发送成功']);
- }
- /**
- * 手机号验证码登陆
- */
- public function phone_login($phone)
- {
- $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
- $phone = $aec->encrypt($phone);
- $wx_cid = input('wx_cid', '', 'intval');
- $wx_uid = input('wx_uid', '', 'intval');
- // 判断是否是违法请求
- $wx_company = WeworkCompany::find($wx_cid);
- $wx_user = WeworkUser::find($wx_uid);
- if ($wx_company['auth_corpid'] !== $wx_user['corpid']){
- return json(['code'=> self::error_msg, 'msg'=> '登录失败,请联系管理员']);
- }
- $company = Company::where(['wework_company'=> $wx_cid])->find();
- // 无绑定公司,判断是否是管理员
- if (empty($company)) {
- $companys = Employee::with(['company', 'companys'])->where('phone', '=', $phone)->where('grant_id', '<>', 0)->count();
- if ($companys > 0) {
- $ticket = md5(time());
- $t_data['wx_uid'] = $wx_uid;
- $t_data['wx_cid'] = $wx_cid;
- $t_data['phone'] = $phone;
- $t_data['ticket'] = $ticket;
- $t_data['status'] = 0;
- WeworkBindTicket::create($t_data);
- return json(['code'=> 2, 'msg'=> '请绑定企业', 'data'=> $ticket]);
- } else {
- return json(['code'=> self::error_msg, 'msg'=> '登录失败,请联系管理员']);
- }
- } else {
- if ($company['status'] == 1) {
- return json(['code' => self::error_msg, 'msg' => '账号被禁用,请联系管理员']);
- } elseif ($company['end_date'] . ' 23:59:59' < date('Y-m-d H:i:s', time())) {
- return json(['code' => self::error_msg, 'msg' => '账号已过期,请联系管理员']);
- }
- $employee = Employee::where([['phone', '=', $phone], ['root_id', '=', $company['root_id']]])->find();
- if (empty($employee)) return json(['code' => self::error_msg, 'msg' => '账户不存在']);
- // 关联企业微信用户信息到系统用户
- Employee::where(['id'=> $employee['id']])->update(['wework_uid'=> $wx_uid]);
- // 查询到员工,返回token
- $token = [
- 'root_org' => $employee['root_id'],
- 'isEmployee' => true,
- 'uid' => $employee['uid'],
- 'employee_id'=> $employee['id'],
- 'org_id' => $employee['org_id']
- ];
- // 信息加密
- $data = http_build_query($token);
- $aes = new Aes(config('app.jwt_key'));
- $key = $aes->encrypt($data);
- // token数据设置
- $payload = array(
- "iss" => "https://" . request()->domain(),
- "aud" => 'mini',
- "iat" => time(),
- "nbf" => time(),
- "data" => $key
- );
- // 自定义登陆状态
- $token = JWT::encode($payload, config('app.jwt_key'));
- $logic = new UserLogic();
- $user = $logic->getInfo([['id', '=', $employee['uid']]], ['id', 'nickname', 'headimgurl', 'sex', 'subscribe', 'phone']);
- $company = Company::where('root_id', $employee['root_id'])->find();
- $company['client_type'] = Miniprogram::where('root_id', '=', $employee['root_id'])->value('notify');
- $returnData = ['code' => self::success, 'token' => $token, 'user' => $user, 'company' =>$company->company_name, 'company_data'=>$company];
- return json($returnData);
- }
- }
- /**
- * 获取手机号所管理企业列表
- */
- public function getCompanys(){
- $ticket = input('ticket', '', 'trim');
- $find = WeworkBindTicket::where(['ticket'=> $ticket, 'status'=> 0])->find();
- if (empty($find)) {
- return json(['code' => self::error_msg, 'msg' => '没有可绑定的企业']);
- }
- $employees = Employee::where([['phone', '=', $find['phone']], ['state', '=', '在职'], ['is_manager', '=', 1]])->select()->toArray();
- $companyList = Company::with(['brand'])->where([['root_id', 'in', array_column($employees, 'root_id')]])->select()->visible(['id','company_name', 'logo','root_id','status','end_date'])->toArray();
- foreach ($companyList as &$item) {
- $item['client_type'] = Miniprogram::where('root_id', '=', $item['root_id'])->value('notify');
- if ($item['status']==1) {
- $item['off'] = 1;
- $item['off_remark'] = '账号被禁用';
- }elseif ($item['end_date']<date('Y-m-d')) {
- $item['off'] = 1;
- $item['off_remark'] = '账号已过期';
- }else{
- $item['off'] = 0;
- $item['off_remark'] = '账号正常';
- }
- }
- return json(['code'=> self::success, 'data'=> $companyList, 'msg'=> '请求成功']);
- }
- /**
- * 绑定企业
- */
- public function bindCompany(){
- $ticket = input('ticket', '', 'trim');
- $company_id = input('company_id', '', 'intval');
- $t_find = WeworkBindTicket::where('ticket', '=', $ticket)->find();
- $t_find->status = 1;
- $t_find->save();
- $result = Company::where('id', '=', $company_id)->update(['wework_company'=> $t_find['wx_cid']]);
- if ($result !== false) {
- $company = Company::find($company_id);
- $employee = Employee::where([['phone', '=', $t_find['phone']], ['root_id', '=', $company['root_id']]])->find();
- if (empty($employee)) return json(['code' => self::error_msg, 'msg' => '账户不存在']);
- // 关联企业微信用户信息到系统用户
- Employee::where(['id'=> $employee['id']])->update(['wework_uid'=> $t_find['wx_uid']]);
- // 查询到员工,返回token
- $token = [
- 'root_org' => $employee['root_id'],
- 'isEmployee' => true,
- 'uid' => $employee['uid'],
- 'employee_id'=> $employee['id'],
- 'org_id' => $employee['org_id']
- ];
- // 信息加密
- $data = http_build_query($token);
- $aes = new Aes(config('app.jwt_key'));
- $key = $aes->encrypt($data);
- // token数据设置
- $payload = array(
- "iss" => "https://" . request()->domain(),
- "aud" => 'mini',
- "iat" => time(),
- "nbf" => time(),
- "data" => $key
- );
- // 自定义登陆状态
- $token = JWT::encode($payload, config('app.jwt_key'));
- $company = Company::where('root_id', $employee['root_id'])->find();
- $company['client_type'] = Miniprogram::where('root_id', '=', $employee['root_id'])->value('notify');
- $logic = new UserLogic();
- $user = $logic->getInfo([['id', '=', $employee['uid']]], ['id', 'nickname', 'headimgurl', 'sex', 'subscribe', 'phone']);
- $returnData = ['code' => self::success, 'msg'=> '绑定成功', 'token' => $token, 'user' => $user, 'company' =>$company->company_name, 'company_data'=>$company];
- return json($returnData);
- } else {
- return json(['code'=> self::error_msg, 'msg'=> '绑定失败']);
- }
- }
- }
|