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' => '同步完成']); } }