123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729 |
- <?php
- namespace app\api\controller;
- use app\event\Msg;
- use app\logics\EmployeeLogic;
- use app\logics\OrgLogic;
- use app\model\Company;
- use app\model\CustomerClue;
- use app\model\Employee;
- use app\model\Miniprogram;
- use app\model\Org;
- use app\model\User as ModelUser;
- use app\model\UserSignLog;
- use Firebase\JWT\JWT;
- use openssl\Aes;
- use app\model\CampEmployee;
- use app\model\Camp as CampModel;
- use wx\miniprogram\User as MiniprogramUser;
- use app\model\AgentUser;
- use app\model\Wechat;
- use app\model\CreditsSetting;
- use app\model\AgentIntegral;
- use toolkits\Aec;
- use app\model\AgentType;
- class Users extends Base
- {
- /**
- * 用户登陆
- * @return json [code:状态,token:jwt_token, exist:是否已注册]
- *
- * 装修宝mini是薛鹊主体下的小程序,unionid 不同
- */
- public function code2session($code)
- {
- // 小程序类型获取
- $miniType = $this->request->param('client_type');
- $client = $this->request->param('client');
- // 查找小程序根部门
- if (!empty($miniType)) {
- $miniprogram = Miniprogram::where([['notify', '=', $miniType]])->findOrEmpty();
- $rootOrg = $miniprogram->root_id;
- $miniprogramId = Company::where('root_id', $rootOrg)->value('mini');
- $miniprogram = Wechat::findOrEmpty($miniprogramId);
- } else {
- $defaultRootOrg = Miniprogram::where([['notify', '=', config('app.cgi')]])->value('root_id');
- if (empty($client)) {
- $miniprogram = Wechat::findOrEmpty(1);
- } else {
- $miniprogram = Wechat::where('appid', $client)->findOrEmpty();
- }
- }
- if ($miniprogram->isEmpty()) return json(['code' => self::error_msg, 'msg' => '未授权的小程序']);
- // code获取用户信息
- $rs = (new MiniprogramUser())->code2session($miniprogram->appid, $miniprogram->secret, $code);
- if (isset($rs['errcode']) && $rs['errcode'] != 0) {
- return json(['code' => self::error_debug_msg, 'msg' => $rs['errmsg']]);
- }
- // 初始token
- $token = [
- 'openid' => $rs['openid'],
- 'unionid' => $rs['unionid'],
- 'session_key' => $rs['session_key'],
- 'client_type' => $miniType,
- 'isEmployee' => false,
- 'mini' => $miniprogram->id
- ];
- $multi = ModelUser::whereOr([['unionid', '=', $rs['unionid']], ['mini_openid', '=', $rs['openid']]])->count(); // 查询用户数量
- if ($multi == 1) {
- $user = ModelUser::whereOr([['unionid', '=', $rs['unionid']], ['mini_openid', '=', $rs['openid']]])->find();
- if (!empty($rootOrg) && $user->root_id != $rootOrg) $user = null;
- } elseif ($multi > 1) {
- if (!empty($rootOrg)) {
- $uidList = ModelUser::whereOr([[['unionid', '=', $rs['unionid']], ['root_id', '=', $rootOrg]], [['mini_openid', '=', $rs['openid']], ['root_id', '=', $rootOrg]]])->column('id');
- $condition = [['uid', 'in', $uidList], ['state', 'in', ['在职', '待确认']]];
- } else { // 未标记登录的企业
- /** 检查是否是员工 */
- $uidList = ModelUser::whereOr([['unionid', '=', $rs['unionid']], ['mini_openid', '=', $rs['openid']]])->column('id');
- $condition = [['uid', 'in', $uidList], ['state', 'in', ['在职']], ['disable', '=', 0]];
- }
- // 查询所属企业并判断企业是否正常
- $rootIdList = Employee::where($condition)->column('root_id');
- if (!empty($rootIdList)) {
- $rootIdList = Company::where([['root_id', 'in', $rootIdList], ['status', '=', 0], ['end_date', '>=', date('Y-m-d')]])->column('root_id');
- }
- // 获取最近登录的uid;
- if (!empty($rootIdList)) {
- $uidList = Employee::where([['uid', 'in', $uidList], ['state', 'in', ['在职']], ['root_id', 'in', $rootIdList]])->column('uid');
- }
- $user = ModelUser::where([['id', 'in', $uidList]])->order('updatetime desc')->find();
- }
- if (empty($user)) {
- $data = [
- 'mini_openid' => $rs['openid'],
- 'unionid' => $rs['unionid'],
- 'root_id' => $rootOrg ?? $defaultRootOrg,
- 'updatetime' => date('Y-m-d H:i:s')
- ];
- $user = ModelUser::create($data);
- } else {
- $user->updatetime = date('Y-m-d H:i:s');
- if (empty($user->unionid)) $user->unionid = $rs['unionid'];
- $user->mini_openid = $rs['openid'];
- $user->save();
- }
- $rootOrg = $user->root_id;
- $token['root_org'] = $rootOrg;
- $token['client_type'] = Miniprogram::where(['root_id' => $rootOrg])->value('notify');
- $token['uid'] = $user->id;
- $userData = $user->visible(['id', 'nickname', 'headimgurl', 'sex', 'subscribe', 'phone'])->toArray();
- $userData['sign'] = UserSignLog::where([['user_id', '=', $user->id], ['date', '=', date('Y-m-d')]])->count();
- $info = Company::where('root_id', $rootOrg)->find();
- if ($info['end_date'] < date('Y-m-d') || $info['status'] == 1) { //账号过期或关闭
- $employee = false;
- } else {
- $root_id = Company::where("status=0 and end_date>='" . date('Y-m-d') . "'")->column('root_id');
- $ew[] = ['root_id', 'in', $root_id];
- $employee = Employee::where($ew)->where([['uid', '=', $user->id], ['state', 'in', ['在职', '待审核']]])->order('state asc')->find();
- }
- if ($employee) {
- $updateData = ['updatetime' => date('Y-m-d H:i:s')];
- if (empty($employee->unionid)) $updateData['unionid'] = $user->unionid;
- $employee->save($updateData);
- //更新uid关联所有店面的公众号id
- $this->updateOfficialOpenid($user->unionid, $employee->id);
- // 查询是设计部门还是销售部门
- $orgType = Org::where('id', $employee->org_id)->value('org_type');
- // 判断是否小程序禁入
- if ($employee->disable == 0) {
- $userData['binded'] = [
- 'id' => $employee->id,
- 'name' => $employee->name,
- 'org_name' => $employee->org->name,
- 'org_id' => $employee->org_id,
- 'isManager' => $employee->is_manager,
- 'isNewbie' => $employee->is_newbie,
- 'state' => $employee->state,
- 'qrcode' => $employee->qrcode,
- 'org_type' => $orgType
- ];
- }
- $token['isEmployee'] = true;
- $token['employee_id'] = $employee->id;
- $token['org_id'] = $employee->org_id;
- $token['org_type'] = $orgType;
- $token['isManager'] = $employee->is_manager;
- $token['name'] = $employee->name;
- }
- $check = $token;
- $shareuser = null;
- $share = $this->request->param('share');
- $share_agent_id = $this->request->param('share_agent_id'); //通过经纪人分享打开的
- $share_agent_id = isset($share_agent_id) && !empty($share_agent_id) ? $share_agent_id : 0;
- $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
- if ($share) {
- $shareuser = ModelUser::where(['id' => $share, 'root_id' => $rootOrg])->field('id,nickname,headimgurl,phone')->find();
- if ($shareuser) {
- $shareBind = Employee::where(['uid' => $shareuser->id, 'state' => '在职', 'root_id' => $rootOrg])->find();
- if (!$shareBind) {
- // 如果分享人已离职
- $shareBind = Employee::where([['uid', '=', $shareuser->id], ['state', 'in', ['离职', '待审核']], ['root_id', '=', $rootOrg]])->order('id desc')->find();
- if ($shareBind) {
- $shareBind = Employee::with(['user'])->where(['id' => $shareBind['left_to_empid']])->find();
- $shareuser->nickname = $shareBind->name;
- $shareuser->phone = $shareBind->phone;
- $shareuser->headimgurl = $shareBind->user->headimgrul;
- $shareuser->id = $shareBind->user->id;
- $shareuser->str = '';
- }
- } else {
- $shareuser->str = $aec->encrypt($rootOrg.'#'.$shareBind->id);
- $shareuser->nickname = $shareBind->name;
- $shareuser->phone = $shareBind->phone;
- }
- $token['share'] = $shareuser->id;
- if ($shareBind && $shareBind['state'] == '在职') {
- $token['share_employee'] = $shareBind->id;
- $token['share_org'] = $shareBind->org_id;
- $token['share_agent_id'] = $share_agent_id; //经纪人分享的
- $shareuser['name'] = $shareBind->name;
- $shareuser['qrcode'] = $shareBind->qrcode;
- }
- if (!$token['isEmployee']) {
- $this->addClue([
- 'uid' => $userData['id'],
- 'employee_id' => $token['share_employee'] ?? 0,
- 'org_id' => $token['share_org'] ?? $token['root_org'],
- 'agent_id' => $share_agent_id
- ], $token['root_org']);
- }
- }
- }
- $company = Company::where('root_id', $rootOrg)->find()->toArray();
- $company['client_type'] = $token['client_type'];
- // 信息加密
- $data = http_build_query($token);
- $aes = new Aes(config('app.jwt_key'));
- $key = $aes->encrypt($data);
- // token数据设置
- $payload = array(
- "iss" => "https://" . $this->request->domain(),
- "aud" => 'mini',
- "iat" => time(),
- "nbf" => time(),
- "data" => $key
- );
- // 自定义登陆状态
- $token = JWT::encode($payload, config('app.jwt_key'));
- $share_str = '';
- if ($check['isEmployee']) $share_str = $aec->encrypt($rootOrg.'#'.$check['employee_id']);
- $returnData = ['code' => self::success, 'token' => $token, 'user' => $userData, 'company' => $company['company_name'], 'company_data' => $company,'str'=>$share_str];
- empty($shareuser) ?: $returnData['share'] = $shareuser;
- //是否在训练中
- if ($check['isEmployee']) {
- $camps = CampModel::where([['root_id', '=', $check['root_org']], ['del', '=', 0]])->column('id');
- if (!$camps) {
- $returnData['camp'] = 0;
- } else {
- $tw[] = ['camp_id', 'in', $camps];
- $returnData['camp'] = CampEmployee::where([['state', '<>', '转正'], ['now', '=', 1], ['employee_id', '=', $check['employee_id']], ['root_id', '=', $check['root_org']]])->where($tw)->count();
- }
- } else {
- $returnData['camp'] = 0;
- }
- //查询是否是经纪人
- $agent = AgentUser::where([['uid', '=', $check['uid']], ['root_id', '=', $check['root_org']], ['status', '=', 1]])->find();
- $returnData['isagent'] = !empty($agent) ? $agent['id'] : 0;
- $returnData['agent_name'] = !empty($agent) ? $agent['agent_name'] : '';
- //如果是经纪人返回关联业务员的信息,用于在经纪人端使用
- if (!empty($agent)) {
- $agent_employee = Employee::with(['user'])->where(['id' => $agent['agent_employee_id']])->find();
- $returnData['share'] = [
- 'id' => $agent_employee->user->id,
- 'nickname' => $agent_employee->user->nickname,
- 'name' => $agent_employee->name,
- 'phone' => $agent_employee->phone,
- 'headimgurl' => $agent_employee->user->headimgrul,
- 'qrcode' => $agent_employee->qrcode
- ];
- $returnData['agent_employee_id'] = !empty($agent_employee) ? $agent_employee->uid : 0;
- }
- //是否关注公众号
- if ($check['isEmployee']) {
- $returnData['official_openid'] = Employee::where('id', $check['employee_id'])->value('official_openid') ? 1 : 0;
- } else {
- $returnData['official_openid'] = 1;
- }
- return json($returnData);
- }
- /**
- * 用户登陆
- * @return json [code:状态,token:jwt_token, exist:是否已注册]
- *
- * 装修宝mini是薛鹊主体下的小程序,unionid 不同
- */
- public function code2sessionmini($code)
- {
- // 小程序类型获取
- $miniType = $this->request->param('client_type');
- $client = $this->request->param('client');
- if (!empty($miniType) && !empty($client)) {
- //同时传client 和 client_type 表示 进入 client 小程序下的 client_type 店面
- $miniprogram = Miniprogram::where([['notify', '=', $miniType]])->findOrEmpty();
- $rootOrg = $miniprogram->root_id;
- $miniprogram = Wechat::where('id', $client)->findOrEmpty();
- }elseif (!empty($miniType)) {
- // 查找小程序根部门
- $miniprogram = Miniprogram::where([['notify', '=', $miniType]])->findOrEmpty();
- $rootOrg = $miniprogram->root_id;
- $miniprogramId = Company::where('root_id', $rootOrg)->value('mini');
- $miniprogram = Wechat::findOrEmpty($miniprogramId);
- } else {
- $defaultRootOrg = Miniprogram::where([['notify', '=', config('app.cgi')]])->value('root_id');
- if (empty($client)) {
- $miniprogram = Wechat::findOrEmpty(1);
- } else {
- $miniprogram = Wechat::where('id', $client)->findOrEmpty();
- }
- }
- if ($miniprogram->isEmpty()) return json(['code' => self::error_msg, 'msg' => '未授权的小程序']);
- // code获取用户信息
- $rs = (new MiniprogramUser())->code2session($miniprogram->appid, $miniprogram->secret, $code);
- if (isset($rs['errcode']) && $rs['errcode'] != 0) {
- return json(['code' => self::error_debug_msg, 'msg' => $rs['errmsg']]);
- }
- // 初始token
- $token = [
- 'openid' => $rs['openid'],
- 'unionid' => $rs['unionid'],
- 'session_key' => $rs['session_key'],
- 'client_type' => $miniType,
- 'isEmployee' => false,
- 'mini' => $miniprogram->id
- ];
- //原有主体
- $unionid_field = 'unionid';
- $openid_field = 'mini_openid';
-
- $multi = ModelUser::whereOr([[$unionid_field, '=', $rs['unionid']], [$openid_field, '=', $rs['openid']]])->count(); // 查询用户数量
- if ($multi == 1) {
- $user = ModelUser::whereOr([[$unionid_field, '=', $rs['unionid']], [$openid_field, '=', $rs['openid']]])->find();
- if (!empty($rootOrg) && $user->root_id != $rootOrg) $user = null;
- } elseif ($multi > 1) {
- if (!empty($rootOrg)) {
- $uidList = ModelUser::whereOr([[[$unionid_field, '=', $rs['unionid']], ['root_id', '=', $rootOrg]], [[$openid_field, '=', $rs['openid']], ['root_id', '=', $rootOrg]]])->column('id');
- $condition = [['uid', 'in', $uidList], ['state', 'in', ['在职', '待确认']]];
- } else { // 未标记登录的企业
- /** 检查是否是员工 */
- $uidList = ModelUser::whereOr([[$unionid_field, '=', $rs['unionid']], [$openid_field, '=', $rs['openid']]])->column('id');
- $condition = [['uid', 'in', $uidList], ['state', 'in', ['在职']], ['disable', '=', 0]];
- }
- // 查询所属企业并判断企业是否正常
- $rootIdList = Employee::where($condition)->column('root_id');
- if (!empty($rootIdList)) {
- $rootIdList = Company::where([['root_id', 'in', $rootIdList], ['status', '=', 0], ['end_date', '>=', date('Y-m-d')]])->column('root_id');
- }
- // 获取最近登录的uid;
- if (!empty($rootIdList)) {
- $uidList = Employee::where([['uid', 'in', $uidList], ['state', 'in', ['在职']], ['root_id', 'in', $rootIdList]])->column('uid');
- }
- $user = ModelUser::where([['id', 'in', $uidList]])->order('updatetime desc')->find();
- }
- if (empty($user)) {
- $data = [
- $openid_field => $rs['openid'],
- $unionid_field => $rs['unionid'],
- 'root_id' => $rootOrg ?? $defaultRootOrg,
- 'updatetime' => date('Y-m-d H:i:s')
- ];
- $user = ModelUser::create($data);
- } else {
- $user->updatetime = date('Y-m-d H:i:s');
- if (empty($user->$unionid_field)) $user->$unionid_field = $rs['unionid'];
- $user->$openid_field = $rs['openid'];
- $user->save();
- }
- $rootOrg = $user->root_id;
- $token['root_org'] = $rootOrg;
- $token['client_type'] = Miniprogram::where(['root_id' => $rootOrg])->value('notify');
- $token['uid'] = $user->id;
- $userData = $user->visible(['id', 'nickname', 'headimgurl', 'sex', 'subscribe', 'phone'])->toArray();
- $userData['sign'] = UserSignLog::where([['user_id', '=', $user->id], ['date', '=', date('Y-m-d')]])->count();
- $info = Company::where('root_id', $rootOrg)->find();
- if ($info['end_date'] < date('Y-m-d') || $info['status'] == 1) { //账号过期或关闭
- $employee = false;
- } else {
- $root_id = Company::where("status=0 and end_date>='" . date('Y-m-d') . "'")->column('root_id');
- $ew[] = ['root_id', 'in', $root_id];
- $employee = Employee::where($ew)->where([['uid', '=', $user->id], ['state', 'in', ['在职', '待审核']]])->order('state asc')->find();
- }
- if ($employee) {
- $updateData = ['updatetime' => date('Y-m-d H:i:s')];
- if (empty($employee->$unionid_field)) $updateData[$unionid_field] = $user->$unionid_field;
- $employee->save($updateData);
- //更新uid关联所有店面的公众号id
- $this->xqupdateOfficialOpenid($user->$unionid_field, $employee->id);
- // 查询是设计部门还是销售部门
- $orgType = Org::where('id', $employee->org_id)->value('org_type');
- // 判断是否小程序禁入
- if ($employee->disable == 0) {
- $userData['binded'] = [
- 'id' => $employee->id,
- 'name' => $employee->name,
- 'org_name' => $employee->org->name,
- 'org_id' => $employee->org_id,
- 'isManager' => $employee->is_manager,
- 'isNewbie' => $employee->is_newbie,
- 'state' => $employee->state,
- 'qrcode' => $employee->qrcode,
- 'org_type' => $orgType
- ];
- }
- $token['isEmployee'] = true;
- $token['employee_id'] = $employee->id;
- $token['org_id'] = $employee->org_id;
- $token['org_type'] = $orgType;
- $token['isManager'] = $employee->is_manager;
- $token['name'] = $employee->name;
- }
- $check = $token;
- $shareuser = null;
- $share = $this->request->param('share');
- $share_agent_id = $this->request->param('share_agent_id'); //通过经纪人分享打开的
- $share_agent_id = isset($share_agent_id) && !empty($share_agent_id) ? $share_agent_id : 0;
- $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
- if ($share) {
- $shareuser = ModelUser::where(['id' => $share, 'root_id' => $rootOrg])->field('id,nickname,headimgurl,phone')->find();
- if ($shareuser) {
- $shareBind = Employee::where(['uid' => $shareuser->id, 'state' => '在职', 'root_id' => $rootOrg])->find();
- if (!$shareBind) {
- // 如果分享人已离职
- $shareBind = Employee::where([['uid', '=', $shareuser->id], ['state', 'in', ['离职', '待审核']], ['root_id', '=', $rootOrg]])->order('id desc')->find();
- if ($shareBind) {
- $shareBind = Employee::with(['user'])->where(['id' => $shareBind['left_to_empid']])->find();
- $shareuser->nickname = $shareBind->name;
- $shareuser->phone = $shareBind->phone;
- $shareuser->headimgurl = $shareBind->user->headimgrul;
- $shareuser->id = $shareBind->user->id;
- $shareuser->str = '';
- }
- } else {
- $shareuser->str = $aec->encrypt($rootOrg.'#'.$shareBind->id);
- $shareuser->nickname = $shareBind->name;
- $shareuser->phone = $shareBind->phone;
- }
- $token['share'] = $shareuser->id;
- if ($shareBind && $shareBind['state'] == '在职') {
- $token['share_employee'] = $shareBind->id;
- $token['share_org'] = $shareBind->org_id;
- $token['share_agent_id'] = $share_agent_id; //经纪人分享的
- $shareuser['name'] = $shareBind->name;
- $shareuser['qrcode'] = $shareBind->qrcode;
- }
- if (!$token['isEmployee']) {
- $this->addClue([
- 'uid' => $userData['id'],
- 'employee_id' => $token['share_employee'] ?? 0,
- 'org_id' => $token['share_org'] ?? $token['root_org'],
- 'agent_id' => $share_agent_id
- ], $token['root_org']);
- }
- }
- }
- $company = Company::where('root_id', $rootOrg)->find()->toArray();
- $company['client_type'] = $token['client_type'];
- // 信息加密
- $data = http_build_query($token);
- $aes = new Aes(config('app.jwt_key'));
- $key = $aes->encrypt($data);
- // token数据设置
- $payload = array(
- "iss" => "https://" . $this->request->domain(),
- "aud" => 'mini',
- "iat" => time(),
- "nbf" => time(),
- "data" => $key
- );
- // 自定义登陆状态
- $share_str = '';
- $token = JWT::encode($payload, config('app.jwt_key'));
- if ($check['isEmployee']) $share_str = $aec->encrypt($rootOrg.'#'.$check['employee_id']);
- $returnData = ['code' => self::success, 'token' => $token, 'user' => $userData, 'company' => $company['company_name'], 'company_data' => $company,'str'=>$share_str];
- empty($shareuser) ?: $returnData['share'] = $shareuser;
- //是否在训练中
- if ($check['isEmployee']) {
- $camps = CampModel::where([['root_id', '=', $check['root_org']], ['del', '=', 0]])->column('id');
- if (!$camps) {
- $returnData['camp'] = 0;
- } else {
- $tw[] = ['camp_id', 'in', $camps];
- $returnData['camp'] = CampEmployee::where([['state', '<>', '转正'], ['now', '=', 1], ['employee_id', '=', $check['employee_id']], ['root_id', '=', $check['root_org']]])->where($tw)->count();
- }
- } else {
- $returnData['camp'] = 0;
- }
- //查询是否是经纪人
- $agent = AgentUser::where([['uid', '=', $check['uid']], ['root_id', '=', $check['root_org']], ['is_review', '=', 2], ['status', '=', 1]])->find();
- $returnData['isagent'] = !empty($agent) ? $agent['id'] : 0;
- //如果是经纪人返回关联业务员的信息,用于在经纪人端使用
- if (!empty($agent)) {
- $agent_employee = Employee::with(['user'])->where(['id' => $agent['agent_employee_id']])->find();
- $returnData['share'] = [
- 'id' => $agent_employee->user->id,
- 'nickname' => $agent_employee->user->nickname,
- 'name' => $agent_employee->name,
- 'phone' => $agent_employee->phone,
- 'headimgurl' => $agent_employee->user->headimgrul,
- 'qrcode' => $agent_employee->qrcode
- ];
- $returnData['agent_employee_id'] = !empty($agent_employee) ? $agent_employee->uid : 0;
- }
- //是否关注公众号
- if ($check['isEmployee']) {
- $returnData['official_openid'] = Employee::where('id', $check['employee_id'])->value('official_openid') ? 1 : 0;
- if (isset($employee)) event('SysOperate', [$employee, '小程序登录']);//登录日志
- } else {
- $returnData['official_openid'] = 1;
- }
- //test测试号无法关联小程序
- $returnData['official_openid'] = 1;
- return json($returnData);
- }
- /**
- * 关注公众号
- */
- private function updateOfficialOpenid($unionid, $eid)
- {
- $where[] = ['unionid', '=', $unionid];
- $where[] = ['official_openid', '<>', ''];
- $official_openid = Employee::where($where)->order('id desc')->value('official_openid');
- if ($official_openid) {
- Employee::where('id', $eid)->update(['official_openid' => $official_openid]);
- }
- return;
- }
- /**
- * 关注公众号
- */
- private function xqupdateOfficialOpenid($unionid, $eid)
- {
- return;
- }
- private function addClue($data, $root_id)
- {
- // 如果线索是员工(包含运维,已离职,待审核)
- $isEmployee = Employee::where([['uid', '=', $data['uid']], ['root_id', '=', $root_id], ['state', 'in', ['在职', '待审核']]])->find();
- if ($isEmployee) return;
- // 如果线索已经获取过,更新时间
- if ($data['employee_id'] == 0) { // 判断是否有员工已获取
- CustomerClue::where([['uid', '=', $data['uid']]])->update(['updatetime' => date('Y-m-d H:i:s')]);
- } else { // 判读是否重复获取
- $clue = CustomerClue::where([['uid', '=', $data['uid']], ['employee_id', '=', $data['employee_id']]])->find();
- if (empty($clue) && $data['employee_id'] !== 0) {
- CustomerClue::create($data);
- //增加经纪人分享的产生线索增加积分功能
- if(!empty($data['agent_id'])){
- $this->agent_clue($data,$root_id);
- }
- } else {
- $clue->updatetime = date('Y-m-d H:i:s');
- $clue->save();
- }
- }
- //查询是否获取过手机号
- $phone = ModelUser::where('id',$data['uid'])->value('phone');
- if ($phone) {
- CustomerClue::where([['uid', '=', $data['uid']],['phone','NULL',null]])->update(['phone' =>$phone]);
- }
- }
- /**
- * 经纪人分享产生线索计算
- */
- public function agent_clue($data,$root_id)
- {
- $rule_type = 'produceclue_integral';
- $agent = AgentUser::where([['root_id','=',$root_id],['id','=',$data['agent_id']]])->field('id,type')->find();
- $rule = AgentType::where([['root_id','=',$root_id],['id','=',$agent['type']]])->find();
- $rule_list = !empty($rule['count']) ? json_decode($rule['count'],true) : '';
- $itg_rule = !empty($rule_list) ? $rule_list[$rule_type] : 0;
- //$itg_rule = CreditsSetting::where([['code', '=', $rule_type], ['root_id', '=', $root_id]])->value('value');
- $state = '经纪人分享产生线索';
- $add = array(
- 'agent_id' => $data['agent_id'],
- 'type' => 6,
- 'integral' => $itg_rule,
- 'addtime' => time(),
- 'state' => $state,
- 'customer_id' => 0
- );
- //如果设置了产生线索送积分再增加记录
- if(!empty($itg_rule)){
- AgentIntegral::insert($add);
- }
-
- }
- /**
- * 授权获取用户信息(客户小程序调用)
- */
- public function setUserInfo($encryptedData, $iv, $signature, $rawData)
- {
- $request = request();
- $token = $request->token;
- // 计算签名是否正确
- $sign = sha1($rawData . $token['session_key']);
- if ($sign != $signature) {
- return json(['code' => self::error_debug_msg, 'msg' => '签名验证失败']);
- }
- $miniprogram = Wechat::where([['id', '=', $token['mini']]])->find();
- // 解析获取用户数据
- $mini = new MiniprogramUser();
- $data = $mini->getInfo($miniprogram->appid, $token['session_key'], $encryptedData, $iv);
- ModelUser::where([['unionid', '=', $request->token['unionid']], ['root_id', '=', $token['root_org']]])->update([
- 'city' => $data['city'],
- 'province' => $data['province'],
- 'country' => $data['country'],
- 'sex' => $data['gender'],
- 'nickname' => $data['nickName'],
- 'headimgurl' => $data['avatarUrl']
- ]);
- // 更新token
- if (!$token['isEmployee']) {
- // 添加客户线索xg
- $this->addClue([
- 'uid' => $token['uid'],
- 'employee_id' => $token['share_employee'] ?? 0,
- 'org_id' => $token['share_org'] ?? $token['root_org'],
- 'agent_id' => $token['share_agent_id'] ?? 0
- ], $token['root_org']);
- }
- return json(['code' => self::success, 'msg' => '信息保存成功']);
- }
- /**
- * 授权获取并保存用户手机号
- */
- public function setUserMobile($encryptedData, $iv)
- {
- $request = request();
- $miniprogram = Wechat::where([['id', '=', $request->token['mini']]])->find();
- $mini = new MiniprogramUser();
- $data = $mini->getInfo($miniprogram->appid, $request->token['session_key'], $encryptedData, $iv);
- ModelUser::where([['unionid|xq_unionid', '=', $request->token['unionid']], ['root_id', '=', $request->token['root_org']]])->update(['phone' => $data['phoneNumber']]);
- // 线索更新手机号
- if (!$request->token['isEmployee'] && !empty($request->token['share_employee'])) {
- CustomerClue::where([
- 'uid' => $request->token['uid'],
- 'employee_id' => $request->token['share_employee']
- ])->update(['phone' => $data['phoneNumber']]);
- }
- return json(['code' => self::success, 'msg' => '信息保存成功', 'mobile' => $data['phoneNumber']]);
- }
- /**
- * 员工注册
- */
- public function register()
- {
- $params = request()->param(['is_manager', 'name', 'orgid', 'recruit' => '']);
- $result = EmployeeLogic::addemployee($params, $msg);
- if ($result) {
- $leader = Employee::where(['org_id' => $params['orgid'], 'is_manager' => 1])->column('id');
- event(new Msg($leader, '您接收到一条新审批,请点击前往审阅', 'register'));
- return json(['code' => 0, 'msg' => '成功提交', 'data' => $result]);
- } else {
- return json(['code' => 1, 'msg' => $msg]);
- }
- }
- /**
- * 组织结构
- */
- public function org()
- {
- $token = request()->token;
- $data = OrgLogic::struc($token['root_org']);
- return json(['code' => 0, 'data' => $data]);
- }
- /**
- * 装修宝mini 装修宝pro数据同步
- */
- public function synchronizationData()
- {
-
- $code = input('code','');
- $encryptedData = input('encrypted','');
- $iv = input('iv','');
- //装修宝mini 配置
- $miniprogram = Wechat::where([['id', '=', 3]])->find();
- //code 获取用户信息
- $rs = (new MiniprogramUser())->code2session($miniprogram->appid, $miniprogram->secret, $code);
- if(!isset($rs['session_key'])) return json(['code' => 1, 'data' => 'code无效', 'msg' => 'code无效']);
- $mini = new MiniprogramUser();
- $data = $mini->getInfo($miniprogram->appid, $rs['session_key'], $encryptedData, $iv);
- if(!isset($data['phoneNumber'])) return json(['code' => 1, 'data' => '解析失败', 'msg' => '解析失败']);
- //手机号查询 装修宝pro 账号信息
- $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
- $phone = $aec->encrypt($data['phoneNumber']);
- $where = [['phone','=',$phone],['uid','>',0]];
- $emp = Employee::where($where)->column('id,uid');
- if($emp){
- $eids = array_column($emp,'id');
- $uids = array_column($emp,'uid');
- Employee::where([['id','in',$eids]])->update(['xq_unionid'=>$rs['unionid'],'xq_mini_openid'=>$rs['openid']]);
- ModelUser::where([['id','in',$uids]])->update(['xq_unionid'=>$rs['unionid'],'xq_mini_openid'=>$rs['openid']]);
- }else{
- return json(['code' => 0, 'msg' => '未查询到关联账号']);
- }
-
- return json(['code' => 0, 'data' => '同步完成']);
- }
- }
|