123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- <?php
- declare(strict_types=1);
- namespace app\mobile\controller;
- use app\model\VrFloder;
- use app\model\VrGroup;
- use app\model\VrView;
- use think\facade\Request;
- use app\model\Employee;
- use app\model\CustomerClue;
- use app\model\UserCollect;
- use app\model\Org;
- use app\model\Customer;
- use app\model\Footprints;
- use app\model\CustomerSubscribe;
- use app\model\User;
- use app\model\Designer;
- use wx\Oplatform;
- use app\model\Setting;
- use app\model\Miniprogram;
- use openssl\Aes;
- use toolkits\Aec;
- use app\model\Camp as CampModel;
- use app\model\Company;
- use app\model\CampEmployee;
- use Firebase\JWT\JWT;
- use app\model\AgentUser;
- use app\model\UserSignLog;
- class Profile extends Base
- {
- /*
- * 获客线索数量
- */
- public function getCustomerCount()
- {
- $conditon = [['employee_id', '=', $this->employeeId]];
- $total = CustomerClue::where($conditon)->count();
- $conditon[] = ['addtime', 'like', date('Y-m-d') . '%'];
- $today = CustomerClue::where($conditon)->count();
- return json(['code' => 0, 'msg' => '获取成功', 'total' => $total, 'today' => $today]);
- }
- /**
- * 线索列表
- */
- public function customernew()
- {
- $param = Request::only(['page'=>1,'limit'=>10,'state'=>'','phone'=>'']);
- $condition[] = ['employee_id', '=', $this->employeeId];
- if($param['state'] !== '') setCondition($param['state'], 'state', '=', $condition);
- $token['root_org'] = $this->rootId;
- if ($param['phone'] == 'has') $condition[] = ['phone', 'not null', ''];
- elseif ($param['phone'] == 'hasno') $condition[] = ['phone', 'null', ''];
- $data = CustomerClue::field('id,uid,phone,addtime,updatetime,state')->with(['user'])->withCount(['subscribe' => function ($query) use ($token) {
- $query->where('root_id', $token['root_org']);
- }, 'footprints'])->where($condition)->order('updatetime desc,id desc')->page((int)$param['page'], (int)$param['limit'])->select();
- foreach ($data as $k => $v) {
- if ($v['nickname'] == '微信用户'){
- $data[$k]['nickname'] = '游客';
- }
- $data[$k]['updatetime'] = $v['updatetime'] ? explode(' ', $v['updatetime']) : explode(' ', $v['addtime']);
- }
- return json(['code' =>0, 'msg' => '获取成功', 'user' => $data]);
- }
- /**
- * 用户收藏记录
- */
- public function collect()
- {
- $param = Request::only(['page'=>1,'limit'=>10]);
- $data = UserCollect::with(['content'])->where('user_id', $this->uid)->order('addtime desc')
- // ->page($page,$limit)
- ->select()->toArray();
- $data = array_filter($data, function ($v) {
- if (isset($v['content']['del']) && $v['content']['del'] == 0) {
- return true;
- } elseif (isset($v['content']['delete_time']) && $v['content']['delete_time'] == 0) {
- return true;
- } else {
- return false;
- }
- });
- $page = (int)$param['page'];
- $limit = (int)$param['limit'];
- $data = array_slice($data, ($page - 1) * $limit, $limit);
- return json(['code' => 0, 'data' => $data]);
- }
- /*
- * 我的团队
- * 公司员工列表
- * screened:company企业,team团队
- * screened = team团队,company公司
- * type= ke获客量 , ding签单量 , credits贡献值 , xuefen学分
- *
- */
- public function employee()
- {
- $param = Request::only(['date'=>'', 'type'=>'ke', 'page'=>1, 'screened'=>'company']);
- $param['date'] = $param['date'] ?: date('Y-m');
- $param['page'] = $param['page'] ?: 1;
- $param['type'] = $param['type'] ?: 'ke';
- $param['screened'] = $param['screened'] ?: 'company';
- $token['root_org'] = $this->rootId;
- $token['employee_id'] = $this->employeeId;
- $info = Employee::where('id',$this->employeeId)->field('id,org_id,uid')->find();
- $token['org_id'] = $info->org_id;
- //选取时间月初和月末时间
- $start_time = $param['date'] . '-01 00:00:00';
- $end_time = date('Y-m-d H:i:s', strtotime('+1 months', strtotime($start_time)));
- $where = [['state', '=', '在职'], ['uid', '<>', 0]];
- if ($param['screened'] == 'company') {
- $where[] = ['root_id', '=', $token['root_org']];
- } else {
- $where[] = ['root_id', '=', $token['root_org']];
- $org_id = orgSubIds($token['org_id']);
- $where[] = ['org_id', 'in', $org_id];
- }
- $searchObj = Employee::with(['user' => function ($query) {
- $query->field('id, headimgurl');
- }])->field('id,uid,name,org_id')->where($where);
- if ($param['type'] == 'ding') {
- $chaos = Customer::changeState('签单', 'chaos');
- $searchObj->withCount([
- 'customer' => function ($query) use ($start_time, $end_time, $chaos) {
- $query->where([['addtime', 'between', [$start_time, $end_time]], ['state', 'in', $chaos]]);
- }
- ]);
- $field = 'customer_count';
- $gdp = 'userdingGdp';
- $totalType = 'total_customer';
- } elseif ($param['type'] == 'ke') {
- $searchObj->withCount([
- 'clue' => function ($query) use ($start_time, $end_time) {
- $query->where([['addtime', 'between', [$start_time, $end_time]]]);
- }
- ]);
- $field = 'clue_count';
- $gdp = 'userFanGdp';
- $totalType = 'total_clue';
- } elseif ($param['type'] == 'xuefen') {
- $searchObj->withSum(['studycredit' => function ($query) use ($start_time, $end_time) {
- $query->where([['addtime', 'between', [$start_time, $end_time]]]);
- }], 'credits');
- $field = 'studycredit_sum';
- $gdp = 'usersdyctGdp';
- $totalType = 'total_studycredit';
- }else {
- $searchObj->withSum(['credits' => function ($query) use ($start_time, $end_time) {
- $query->where([['addtime', 'between', [$start_time, $end_time]], ['type', '=', 1]]);
- }], 'credits');
- $field = 'credits_sum';
- $gdp = 'userdcreditsGdp';
- $totalType = 'total_credits';
- }
- $data = $searchObj->select()->toArray();
- array_multisort(array_column($data, $field), SORT_DESC, $data);
- // 统计总数
- $count = array_sum(array_column($data, $field));
- // 获取我的数据与排名
- $found_arr = array_column($data, 'id');
- $key = array_search($token['employee_id'], $found_arr);
- $myRanking = $data[$key];
- $myRanking['ranking'] = $key + 1;
- if(!isset($myRanking['org_name'])) $myRanking['org_name'] = Org::where('id',$myRanking['org_id'])->value('name');
- // 获取我的占比
- $myRanking[$gdp] = $myRanking['grawth'] = $count == 0 ? 0 : floor($myRanking[$field] / $count * 100);
- $myRanking['value'] = $myRanking[$field];//统一字段
- // 总人数
- $myRanking['count'] = count($data);
- $myRanking[$totalType] = $myRanking['total'] = $count;//统一字段
- $data = array_slice($data, ($param['page'] - 1) * 10, 10);
- if ($data) {
- $org_ids = array_column($data,'org_id');
- $org_names = Org::where([['id','in',$org_ids]])->column('name','id');
- foreach ($data as $key => $value) {
- $data[$key]['org_name'] = isset($org_names[$value['org_id']]) ? $org_names[$value['org_id']] : '';
- }
- }
- if ($param['type'] !== 'ke' && $param['type'] !== 'ding') {
- if($param['type']== 'xuefen'){
- foreach ($data as $k => $v) {
- $data[$k]['studycredit_max'] = $data[$k]['studycredit_sum'];
- }
- }else{
- foreach ($data as $k => $v) {
- $data[$k]['credits_max'] = $data[$k]['credits_sum'];
- }
- }
-
- }
- $companyName = Org::where(['id' => $token['root_org']])->value('name');
- foreach ($data as $key => $value) {
- $data[$key]['value'] = $value[$field];
- }
- return json(['code' => 0, 'msg' => '获取成功', 'company_ranking_list' => $data, 'my_ranking' => $myRanking, 'company' => $companyName]);
- }
- /**
- * 线索无效
- */
- public function invalidClue($id)
- {
- $clue = CustomerClue::where([['id', '=', $id], ['employee_id', '=', $this->employeeId]])->findOrEmpty();
- if ($clue->isEmpty()) return json(['code' => 1, 'msg' => '操作失败']);
- $clue->state = 2;
- $clue->save();
- return json(['code' =>0, 'msg' => '操作成功']);
- }
- /**
- * 客户访问记录(👣)
- */
- public function footprints($id)
- {
- $token['employee_id'] = $this->employeeId;
- $token['root_org'] = $this->rootId;
- $clue = CustomerClue::where([['id', '=', $id], ['employee_id', '=', $token['employee_id']],['uid','>',0]])->findOrEmpty();
- if ($clue->isEmpty()) return json(['code' => 0, 'data' => [], 'msg' => '获取成功']);
- // 查询条件设置
- $condition = [
- ['addtime','>=',$clue->addtime],
- ['uid', '=', $clue->uid],
- ['org_id', 'in', orgSubIds($token['root_org'])]
- ];
- $data = Footprints::where($condition)->order('id desc')->field('id,pipe_type,reg_info,visit_due_time,addtime,uid')->select();
- if (!$data->isEmpty()) {
- //装修案例预约记录
- $w2[] = ['uid', 'in', array_column($data->toArray(), 'uid')];
- $w2[] = ['root_id', '=', $token['root_org']];
- $sub = CustomerSubscribe::where($w2)->field('sucai_id,uid')->select()->toArray();
- foreach ($data as $k => $v) {
- $data[$k]['subscribe'] = 0;
- $data[$k]['group_view'] = [];
- if ($v['pipe_type'] == 'materialCase') {
- $json = json_decode($v->getData('reg_info'), true);
- if(empty($josn) || !isset($json['id'])) break;//json字段缺失导致报错
- foreach ($sub as $k2 => $v2) {
- if ($v2['uid'] == $v['uid'] && $v2['sucai_id'] == $json['id']) {
- $data[$k]['subscribe'] = 1;
- break;
- }
- }
- }elseif($v['pipe_type'] == 'groupVr') {
- $json = json_decode($v->getData('reg_info'), true);
- $data[$k]['group_view'] = isset($json['view_foots']) ? $json['view_foots'] : [];
- }
- }
- }
- return json(['code' =>0, 'data' => $data, 'msg' => '获取成功']);
- }
- /**
- * 更新个人信息
- */
- public function update()
- {
- $param = Request::only(['qrcode'=>'', 'image_photo'=>'','name'=>'','nickname'=>'','wx'=>'','position'=>'','headimg'=>'']);
- if(!array_filter($param)) return json(['code' =>1, 'msg' => '修改失败']);
- $find = Employee::find($this->employeeId);
- if ($param['nickname']) {
- User::where('id', $this->uid)->update(['nickname' => $param['nickname']]);
- unset($param['nickname']);
- }
- if ($param['headimg']) {
- $ali_oss_bindurl = 'https://' . config('app.ali_oss_bindurl') . '/';
- $headimgurl = $ali_oss_bindurl . $param['headimg'] . '?x-oss-process=image/resize,w_132';
- User::where('id', $this->uid)->update(['headimgurl' => $headimgurl]);
- unset($param['headimg']);
- }
- if($param['name']) $param['initials'] = strtoupper(substr(hanzi2pinyin($param['name']),0,1));
- foreach ($param as $k => $v) {
- if($v) $find->$k = $v;
- }
-
- $find->save();
- if($param['image_photo']) Designer::where('employee_id', '=', $this->employeeId)->save(['headimgurl' => $param['image_photo']]);
- return json(['code' =>0, 'msg' => '修改成功']);
- }
- /**
- * 绑定微信
- * 薛鹊开放平台,同时同步数据
- * 绑定所有手机号 同时关联旧数据
- */
- public function wxBind($code)
- {
- $appid = config('app.xqmobile_appid');
- $appscreap = config('app.xqmobile_secret');
- // 授权获取accesstoken
- $tokendata = (new Oplatform())->getAccessToken($appid, $appscreap, $code);
- if(!isset($tokendata['unionid'])) return json(['code' => 1, 'msg' => 'code无效']);
- $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $tokendata['access_token'] . "&openid=" . $tokendata['openid'];
- $info = file_get_contents($url);
- $check = json_decode($info, true);
- if (!isset($check['unionid'])) return json(['code' => 1, 'msg' => 'access_token失效']);
- //手机号
- $phone = Employee::where('id',$this->employeeId)->value('phone');
- $where = [
- ['phone','=',$phone],
- ['uid','>',0],
- ['state','=','在职'],
- ['xq_unionid','<>',''],
- ['xq_unionid','<>',$check['unionid']]
- ];
- $find = Employee::where($where)->value('id');
- if($find) return json(['code' => 1, 'msg' => '已绑定过其他微信,请先解绑']);
- $where = [
- ['phone','=',$phone],
- ['uid','>',0],
- ['state','=','在职']
- ];
- $emps = Employee::where($where)->column('id,uid');
- $ids = array_column($emps,'id');
- $uids = array_column($emps,'uid');
- Employee::where([['id','in',$ids]])->update(['xq_unionid'=>$check['unionid'],'xq_mobile_openid'=>$check['openid']]);
- User::where([['id','in',$uids]])->update(['xq_unionid'=>$check['unionid'],'xq_mobile_openid'=>$check['openid']]);
- return json(['code' => 0, 'msg' => '绑定成功']);
- }
- /**
- * 解绑微信
- */
- public function unbinding()
- {
- //手机号
- $phone = Employee::where('id',$this->employeeId)->value('phone');
- $where = [
- ['phone','=',$phone],
- ['uid','>',0],
- ['state','=','在职']
- ];
- $emps = Employee::where($where)->column('id,uid');
- $ids = array_column($emps,'id');
- $uids = array_column($emps,'uid');
- Employee::where([['id','in',$ids]])->update(['xq_mobile_openid'=>'']);
- User::where([['id','in',$uids]])->update(['xq_mobile_openid'=>'']);
- return json(['code' => 0, 'msg' => '解绑成功']);
- }
- /**
- * 报备客户
- */
- public function add()
- {
- $param = Request::only(['id'=>'', 'name'=>'','phone'=>'']);
- $clue = CustomerClue::where([['id','=',$param['id']],['employee_id','=',$this->employeeId],['state','=',0]])->findOrEmpty();
- if($clue->isEmpty()) return json(['code' => 1, 'msg' => '报备失败']);
- ////判断手机号是否在公海,资源库,已建档,回收站
- $orgids = orgSubIds($this->rootId);
- $tips = $this->checkPepeat([$param['phone']], $orgids, $this->employeeId);
- if (!empty($tips)) return $tips;
- // 检测线索是否已经被其他同事建档过
- $exist = CustomerClue::where([
- ['org_id', 'in', $orgids],
- ['uid', '=', $clue->uid],
- ['state', '=', 1],
- ['id', '<>', $param['id']]
- ])->find();
- if ($exist) return json(['code' => 1, 'msg' => '建档失败,线索已被其他员工先行建档']);
- // 添加数据
- $data['clue_id'] = $clue->id;
- $data['uid'] = $clue->uid;
- $data['employee_id'] = $this->employeeId;
- $data['org_id'] = Employee::where('id',$this->employeeId)->value('org_id');
- $data['agents_id'] = $clue->agent_id ?? NULL; //如果线索是经纪人端获取的记录
- $state = Customer::changeState('待确认');
- $data['state'] = $state;
- $clue->state = 1;
- $clue->save();
- return json(['code' => 0, 'msg' => '报备成功','data'=>'报备成功']);
- }
- /**
- * 添加客户检测手机号的重复
- * 1.客户录入不和资源库和回收站排重 (创艺)
- * 2.客户录入需查询是否和公海重复,如重复,提示可以获取该客户 (创艺) 工单id6077
- */
- public function checkPepeat($pharr, $org_id, $employee_id, $id = null)
- {
- // 同部门能否重复录入开关设置
- $root_id = $this->rootId;
- $empcrm_repeat[] = ['root_id', '=', $root_id];
- $empcrm_repeat[] = ['name', '=', 'empcrm_customer_repeat'];
- $repeat_setting = Setting::where($empcrm_repeat)->findOrEmpty();
- $emp_org = Employee::where('id',$this->employeeId)->value('org_id');
- foreach ($pharr as $val) {
- if (empty($val)) continue;
- // 先判断公海 只有添加时候判断
- if (empty($id)) {
- $poolExist = Customer::where([
- ['phone|phone1|phone2', '=', cypherphone(trim($val))],
- ['org_id', '=', $emp_org],
- ['employee_id', 'null', null],
- ['is_resource', '=', 0]
- ])->column('id');
- if (!empty($poolExist)) {
- $poolCrmIdStr = implode(',', $poolExist);
- return json(['code' => 3, 'data' => $poolCrmIdStr, 'msg' => '录入失败:客户信息已在公海中。']);
- }
- }
- // 新建档,检测手机号是否重复
- $customerExist = Customer::where([
- ['phone|phone1|phone2', '=', cypherphone(trim($val))],
- ['org_id', 'in', $org_id],
- ['employee_id', '>', 0] // 员工客户
- ])->where(
- function($query) {
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['crm_res_id', 'null', null];
- $or2[] = ['crm_res_id', '>', 0];
- $or2[] = ['state', 'not in', $not_sure];
- $query->whereOr([$or1, $or2]);
- }
- )->field('id,employee_id,is_resource,org_id')->select()->toArray();
- $id_arr = array_column($customerExist, 'id');
- $employee_id_arr = array_column($customerExist, 'employee_id');
-
- if (empty($id)) {
- // 对非当前业务员名下重复客户不在判断
- if (in_array($employee_id, $employee_id_arr)) {
- // 是否已存在为自己的客户
- return json(['code' => 2, 'msg' => '录入失败。' . $val . '已经是您的客户。']);
- }
- } else {
- if (!in_array($id, $id_arr) && in_array($employee_id, $employee_id_arr)) {
- // 是否已存在为自己的客户
- return json(['code' => 2, 'msg' => '录入失败。' . $val . '已经是您的客户。']);
- }
- }
- //判断同部门
- if (!$repeat_setting->isEmpty()) {
- $repeat_org = explode(',', $repeat_setting['content']);
- if (in_array($emp_org, $repeat_org)) {
- // 需要验证
- if (!empty($customerExist)) {
- foreach ($customerExist as $ex) {
- if (in_array($ex['org_id'], $repeat_org) && $ex['employee_id'] != $employee_id) {
- // 为了避免开启撞单之前都已经重复的情况,所以要查一下这个id的现数据
- if ($id) {
- $this_customer = Customer::find($id);
- if ($this_customer) {
- $this_customer_phone = array_filter([$this_customer['phone'], $this_customer['phone1'], $this_customer['phone2']]);
- if (!in_array($val, $this_customer_phone)) { // 不是原来的客户手机号,编辑时候判断撞单
- $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
- return json(['code' => 2, 'msg' => '该客户与员工' . $have_emp . '撞单,无法录入']);
- }
- }
- } else {
- $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
- return json(['code' => 2, 'msg' => '该客户与员工' . $have_emp . '撞单,无法录入']);
- }
- }
- }
- }
- }
- }
- }
- }
- /**
- * 获取小程序token
- */
- public function getAppletToken()
- {
- $employee = Employee::where('id',$this->employeeId)->find();
- $user = User::where('id',$this->uid)->field(['id', 'nickname', 'headimgurl', 'sex', 'subscribe', 'phone','mini_openid','unionid'])->find();
- $miniprogram = Miniprogram::where('root_id',$this->rootId)->find();
- // 初始token
- $token = [
- 'openid' => $user->mini_openid,
- 'unionid' => $user->unionid,
- 'session_key' => '',
- 'client_type' => $miniprogram->notify,
- 'isEmployee' => true,
- 'mini' => $miniprogram->id,
- 'uid' => $user->id,
- 'org_id' => $employee->org_id,
- 'org_type' => Org::where('id', $employee->org_id)->value('org_type'),
- 'name' => $employee->name,
- 'root_org' => $this->rootId,
- 'employee_id' => $this->employeeId,
- 'isManager' => $employee->is_manager ? true : false
- ];
- $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'));
- return json(['code' => 0,'token'=>$token,'empid'=>$this->employeeId,'orgid'=>$employee->org_id,'client_type'=>$miniprogram->notify]);
- }
- }
|