corpid = $corpid; $group->name = $name; $group->type = $type; $group->save(); $group->sort_val = $group->id; $result = $group->save(); return $result ? 0 : 4609; } /** * 修改活码分组 */ public static function editSqgroup($id, $name) { $group = SourceQrcodeGroups::where('id', $id)->first(); if(empty($group)){ return 4607; } $group->name = $name; $group->save(); return 0; } /** * @param site: 1上移 2下移 3置顶 4置底 */ public static function editGroupSortOld($id, $site) { $group = SourceQrcodeGroups::where('id', $id)->first(); $group_sort_val = $group->sort_val; if( $site==1 ){ //上移 查上一个交换位置 $target = SourceQrcodeGroups::where('sort_val', '<', $group_sort_val)->where('enable', 1)->orderBy('sort_val', 'desc')->first(); if( empty($target) ){ //已经是最高,不需操作 return true; } $target_sort_val = $target->sort_val; $group->sort_val = $target_sort_val; $target->sort_val = $group_sort_val; $group->save(); $target->save(); } elseif( $site==2 ){ //下移 $target = SourceQrcodeGroups::where('sort_val', '>', $group_sort_val)->where('enable', 1)->orderBy('sort_val', 'asc')->first(); if( empty($target) ){ //已经是最低,不需操作 return true; } $target_sort_val = $target->sort_val; $group->sort_val = $target_sort_val; $target->sort_val = $group_sort_val; $group->save(); $target->save(); } elseif( $site==3 ){ //置顶 $target = SourceQrcodeGroups::where('enable', 1)->orderBy('sort_val', 'asc')->first(); $sort_val = $target->sort_val - 1; $group->sort_val = $sort_val; $group->save(); } elseif( $site==4 ){ //置底 $target = SourceQrcodeGroups::where('enable', 1)->orderBy('sort_val', 'desc')->first(); $sort_val = $target->sort_val + 1; $group->sort_val = $sort_val; $group->save(); } return true; } public static function editGroupSort($ids) { try { foreach($ids as $k=>$v){ SourceQrcodeGroups::where('id', $v)->update([ 'sort_val' => $k ]); } } catch (\Exception $e) { return 4610; } return 0; } public static function delSqGroup($id, $del_sqs = 0) { DB::beginTransaction(); try{ $res = SourceQrcodeGroups::where('id', $id)->where('enable', 1)->update(['enable' => 0]); if($res && $del_sqs){ $sqs = SourceQrcodes::where('group_id', $id)->where('enable', 1)->get(); if( !$sqs->isEmpty() ){ //删除数据 SourceQrcodes::where('group_id', $id)->where('enable', 1)->update(['enable' => 0]); //企微删除接口 foreach($sqs as $item){ self::delSourceQrcode($item->$id); } } } DB::commit(); } catch (\Exception $e) { DB::rollBack(); Log::logError('删除渠道活码分组-过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'data' => ['id'=>$id, 'del_sqs'=>$del_sqs] ], 'sourceQrcodes-Exception'); return 4602; } return 0; } public static function sqGroupList($corpid, $type, $page, $pagesize) { $offset = ($page-1) * $pagesize; $listQuery = SourceQrcodeGroups::where('enable', 1) ->where('type', $type) ->where('corpid', $corpid); $total = (clone $listQuery)->count(); $list = $listQuery->select(['id', 'name', 'corpid', 'sort_val', 'enable'])->orderBy('sort_val') ->offset($offset) ->limit($pagesize) ->get(); return [$list, $total]; } public static function addSourceQrcode($corpid, $name, $group_id, $user_json, $tag_list, $customer_prefix, $customer_prefix_type, $up_toplimit, $warn_user, $msgData) { if( !self::verifyUserJson($user_json) ){ return [4600, '客服数据格式有误']; } # 验证欢迎语内容 if(!is_array($msgData)) $msgData = json_decode($msgData, true); $errno = WelcomeMsgService::msgDataVerify($msgData); if($errno) return [$errno, '欢迎语格式有误']; $staffs = array(); $user_info = json_decode($user_json, true); foreach($user_info as $staff){ if( $staff['is_open'] ){ $staffs[] = $staff['user_id']; //开启的客服 } } #渠道活码请求 $state = uniqid('sq-') . mt_rand(1000, 9999); //生成唯一state $params = [ "type" => 2, //联系方式类型,1-单人, 2-多人 "scene" => 2, //场景,1-在小程序中联系,2-通过二维码联系 "remark" => "渠道活码", //联系方式的备注信息 "skip_verify" => true, //外部客户添加时是否无需验证,默认为true "state" => $state, //企业自定义的state参数,用于区分不同的添加渠道,不超过30字符 "user" => $staffs //客服列表 ]; DB::beginTransaction(); try{ $result = QyCommon::momentCommon('add_contact_way', $corpid, $params); if( !$result ){ return [4601, '生成渠道二维码失败']; } $sqParams = array(); $sqParams['corpid'] = $corpid; $sqParams['name'] = $name; $sqParams['group_id'] = $group_id; $sqParams['state'] = $state; $sqParams['tag_list'] = $tag_list; $sqParams['customer_prefix'] = $customer_prefix; $sqParams['customer_prefix_type'] = $customer_prefix_type; $sqParams['up_toplimit'] = (int)$up_toplimit; $sqParams['warn_user'] = $warn_user; $sqParams['user_id'] = \Auth::id(); $sqParams['config_id'] = $result['config_id']; $sqParams['qrcode'] = $result['qr_code']; $sqModel = new SourceQrcodes($sqParams); if(!$sqModel->save()){ DB::rollBack(); return [4602, '系统错误']; } $sq_id = $sqModel->id; $sqUsers = array(); foreach($user_info as $staff){ $sqUsers[] = [ 'corpid' => $corpid, 'sq_id' => $sq_id, 'user_id' => $staff['user_id'], 'is_open' => $staff['is_open'], 'toplimit' => $staff['toplimit'] ]; } $ures = SourceQrcodeUsers::insert($sqUsers); if(!$ures){ DB::rollBack(); return [4602, '系统错误']; } $sres = self::setWelcomeMsg($msgData, $corpid, $sq_id); if(!$sres){ DB::rollBack(); return [4602, '系统错误']; } DB::commit(); } catch (\Exception $e) { Log::logError('创建渠道活码-过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'data' => $msgData ], 'sourceQrcodes-Exception'); return [4602, '系统错误']; } return [0, '添加成功']; } private static function verifyUserJson($user_json) { $user_info = json_decode($user_json, true); if( !is_array($user_info) ){ return false; } $is_open = 0; foreach($user_info as $user){ if( !isset($user['user_id']) || !isset($user['is_open']) || !isset($user['toplimit']) || $user['toplimit']<0 ){ return false; } if($user['is_open']) $is_open = 1; } if($is_open == 0){ return false; } return true; } public static function setWelcomeMsg($msgData, $corpid, $sq_id) { # 设置时间规则及消息内容到消息表 foreach ($msgData as $msgRule) { $msgId = isset($msgRule['msg_id']) ? $msgRule['msg_id'] : 0; $weeks = $msgRule['weeks']; $isDayParting = $msgRule['is_day_parting']; $startTime = $msgRule['start_time']; $endTime = $msgRule['end_time']; $content = $msgRule['content']; $attachments = $msgRule['attachments']; $operate = isset($msgRule['operate']) ? $msgRule['operate'] : ''; $key_val = isset($msgRule['key_val']) ? $msgRule['key_val'] : 0; if($msgId && $operate=='del') { // 删除子规则操作 $result = SourceQrcodeWelcomeMsg::where('id', $msgId)->update(['enable' => 0]); if(!$result) { return false; } continue; } $result = SourceQrcodeWelcomeMsg::setMsg( $msgId, $weeks, $startTime, $endTime, $content, $attachments, $isDayParting, $corpid, $sq_id, $key_val ); if(!$result) { return false; } } return true; } public static function editSourceQrcode($id, $name, $group_id, $user_json, $tag_list, $customer_prefix, $customer_prefix_type, $up_toplimit, $warn_user, $msgData) { $sqInfo = SourceQrcodes::where('id', $id)->first(); if(!isset($sqInfo->id)){ return [4603, '参数有误']; } if( !self::verifyUserJson($user_json) ){ return [4600, '客服数据格式有误']; } $corpid = $sqInfo->corpid; # 验证欢迎语内容 if(!is_array($msgData)) $msgData = json_decode($msgData, true); $errno = WelcomeMsgService::msgDataVerify($msgData); if($errno) return [$errno, '欢迎语格式有误']; $staffs = array(); $user_info = json_decode($user_json, true); foreach($user_info as $staff){ if( $staff['is_open'] ){ $staffs[] = $staff['user_id']; //开启的客服 } } DB::beginTransaction(); try{ #原有所有配置客服信息 $old_all = SourceQrcodeUsers::where('sq_id', $sqInfo->id)->get()->keyBy('user_id')->toArray(); $old_enable_staffs = array(); //原有未删除客服,用来判断本次删除了哪些 //$old_up_staffs = array(); //原有企微配置客服 foreach($old_all as $val){ if($val['enable'] == 1){ $old_enable_staffs[] = $val['user_id']; } /*if($val['enable'] == 1 && $val['is_open'] == 1 && $val['is_shut'] == 0){ $old_up_staffs[] = $val['user_id']; }*/ } #判断是否需要更新企微接口 $final_up_staffs = array(); //获取最新二维码配置客服 foreach($user_info as $staff){ $staff_id = $staff['user_id']; if($staff['is_open'] == 0){ continue; //未开启直接跳过,若最终qw客服为空,判断操作有误 } #判断数据表是否已有 if( isset($old_all[$staff_id]) ){ #未设上限,直接开启 if($staff['toplimit'] == 0){ $final_up_staffs[] = $staff_id; } else { $user_add_count = CustomerDetails::suffix($corpid) ->where('corpid', $corpid) ->where('state', $sqInfo->state) ->where('user_id', $staff_id) ->where('scan_time', '>', date('Y-m-d')) ->count(); //已添加客户数 if($user_add_count < $staff['toplimit']){ //未达新限 $final_up_staffs[] = $staff_id; } } } else { //全新 qw客服 $final_up_staffs[] = $staff_id; } } if( empty($final_up_staffs)){ return [4608, '没有配置符合条件的客服']; } //更新渠道二维码 $params = [ "config_id" => $sqInfo->config_id, //企业联系方式的配置id "user" => $final_up_staffs //客服列表 ]; $result = QyCommon::momentCommon('update_contact_way', $corpid, $params); if( !$result ){ return [4604, '更新渠道二维码失败']; } $sqParams = array(); $sqParams['name'] = $name; $sqParams['group_id'] = $group_id; $sqParams['tag_list'] = $tag_list; $sqParams['customer_prefix'] = $customer_prefix; $sqParams['customer_prefix_type'] = $customer_prefix_type; $sqParams['up_toplimit'] = (int)$up_toplimit; $sqParams['warn_user'] = $warn_user; $upRes = SourceQrcodes::where('id', $id)->update($sqParams); $sq_id = $sqInfo->id; $final_users = array(); //最终保存入库客服 foreach($user_info as $staff){ $con = [ 'corpid' => $corpid, 'sq_id' => $sq_id, 'user_id' => $staff['user_id'] ]; $attr = [ 'is_open' => $staff['is_open'], 'toplimit' => (int)$staff['toplimit'], 'enable' => 1 ]; if(in_array($staff['user_id'], $final_up_staffs)){ $attr['is_shut'] = 0; } SourceQrcodeUsers::updateOrCreate($con, $attr); $final_users[] = $staff['user_id']; } #需要删除的客服 $del_staffs = array_diff($old_enable_staffs, $final_users); SourceQrcodeUsers::whereIn('user_id', $del_staffs)->where('sq_id', $sq_id)->update(['enable' => 0]); $sres = self::setWelcomeMsg($msgData, $corpid, $sq_id); if(!$sres){ DB::rollBack(); return [4602, '系统错误']; } DB::commit(); } catch (\Exception $e) { Log::logError('编辑渠道活码-过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'data' => $msgData ], 'sourceQrcodes-Exception'); return [4602, '系统错误']; } return [0, '修改成功']; } public static function sourceQrcodeList($corpid, $group_id, $name, $user_ids, $page, $pagesize) { #筛选客服 $sq_ids = array(); if(!empty($user_ids)){ $sq_ids = SourceQrcodeUsers::where('corpid', $corpid)->whereIn('user_id', $user_ids)->where('enable', 1)->pluck('sq_id')->all(); if( empty($sq_ids) ){ return [[], 0]; } } $listQuery = SourceQrcodes::where('enable', 1) ->where('corpid', $corpid) ->where(function($query) use($group_id, $name, $sq_ids){ if($group_id) $query->where('group_id', $group_id); if($name) $query->where('name', 'like', '%'.$name.'%'); if(!empty($sq_ids)) $query->whereIn('id', $sq_ids); }); $total = (clone $listQuery)->count(); if(!$total){ return [[], 0]; } $offset = ($page-1) * $pagesize; $list = $listQuery->orderBy('id', 'desc') ->offset($offset) ->limit($pagesize) ->get(); $create_user_ids = $list->pluck('user_id')->all(); $create_users = Users::whereIn('id', $create_user_ids)->pluck('name', 'id')->all(); $group_ids = $list->pluck('group_id')->all(); $groups = SourceQrcodeGroups::whereIn('id', $group_ids)->pluck('name', 'id')->all(); foreach($list as $item){ #创建人 $item->creater = $create_users[$item->user_id] ?? ''; #分组名称 $item->group_name = $groups[$item->group_id] ?? ''; #扫码总人数 $item->user_add_count = CustomerDetails::suffix($corpid) ->where('corpid', $corpid) ->where('state', $item->state) ->count(); } return [$list, $total]; } //活码详情 public static function sourceQrcodeDetail($id) { $sqInfo = SourceQrcodes::find($id); #客服信息 $staffs = SourceQrcodeUsers::where('sq_id', $id)->where('enable', 1)->get(); $user_ids = $staffs->pluck('user_id')->all(); $user_names = DjUser::whereIn('user_id', $user_ids)->where('corpid', $sqInfo->corpid)->pluck('name', 'user_id')->all(); foreach($staffs as $k=>$item){ #客服名称 $item->staff_name = $user_names[$item->user_id] ?? ''; #今日添加客户数 $item->user_add_count = CustomerDetails::suffix($sqInfo->corpid) ->where('corpid', $sqInfo->corpid) ->where('state', $sqInfo->state) ->where('user_id', $item->user_id) ->where('scan_time', '>', date('Y-m-d')) ->count(); } #欢迎语信息 $welcomeMsg = SourceQrcodeWelcomeMsg::where('sq_id', $id)->where('enable', 1)->orderBy('is_day_parting')->get(); foreach ($welcomeMsg as $item) { $item->msg_id = $item->id; # 附件信息处理 $attachments = json_decode($item->attachments, true); if(!empty($attachments)) { foreach ($attachments as $key=>&$attachment) { if(isset($attachment['msgtype']) && $attachment['msgtype'] == 'radar') { // 雷达附件信息回显 $radarId = $attachment['radar']['radar_id'] ?? 0; $radarInfo = RadarService::getRadarContent($sqInfo->corpid, $radarId); if(empty($radarInfo)) { unset($attachment[$key]); continue; } $attachment['radar'] = $radarInfo; } } } $item->attachments = json_encode($attachments, 256); } $sqInfo->staff_users = $staffs; $sqInfo->welcomeMsg = $welcomeMsg; return $sqInfo; } //禁用渠道活码 public static function delSourceQrcode($id) { DB::beginTransaction(); try{ $sqInfo = SourceQrcodes::find($id); $corpid = $sqInfo->corpid; $sqInfo->del_time = date('Y-m-d H:i:s'); $sqInfo->status = 0; $sqInfo->save(); $config_id = $sqInfo->config_id; # 企微删除接口 $params = [ "config_id" => $sqInfo->config_id, //企业联系方式的配置id ]; $result = QyCommon::momentCommon('del_contact_way', $corpid, $params); if( !$result ){ DB::rollBack(); return [4605, '删除渠道二维码失败']; } DB::commit(); } catch (\Exception $e) { DB::rollBack(); Log::logError('删除渠道活码-过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'data' => $id ], 'sourceQrcodes-Exception'); return [4602, '系统错误']; } return [0, '禁用成功']; } public static function dataView($id) { $sqInfo = SourceQrcodes::find($id); $tTime = date('Y-m-d 00:00:00', time()); $corpid = $sqInfo->corpid; $state = $sqInfo->state; $cusDetailTalInfo = CustomerDetails::suffix($corpid) ->selectRaw("COUNT(id) AS scan_num_total") ->selectRaw("SUM(IF(enable = 1, 1, 0)) AS keep_num_total") ->selectRaw("SUM(IF(scan_user_type = 2, 1, 0)) AS incr_num_total") ->where('state', $state) ->first(); $total = [ 'scan_num_total' => $cusDetailTalInfo->scan_num_total ?? 0, 'keep_num_total' => $cusDetailTalInfo->keep_num_total ?? 0, 'incr_num_total' => $cusDetailTalInfo->incr_num_total ?? 0, ]; $total['loss_num_total'] = $total['scan_num_total'] - $total['keep_num_total']; $cusDetailTdInfo = CustomerDetails::suffix($corpid) ->selectRaw("COUNT(id) AS scan_num_td") ->selectRaw("SUM(IF(enable = 1, 1, 0)) AS keep_num_td") ->selectRaw("SUM(IF(scan_user_type = 2, 1, 0)) AS incr_num_td") ->where('state', $state) ->where('scan_time', '>=', $tTime) ->first(); $today = [ 'scan_num_td' => $cusDetailTdInfo->scan_num_td ?? 0, 'keep_num_td' => $cusDetailTdInfo->keep_num_td ?? 0, 'incr_num_td' => $cusDetailTdInfo->incr_num_td ?? 0, ]; $today['loss_num_td'] = $today['scan_num_td'] - $today['keep_num_td']; return [ 'tal' => $total, 'td' => $today ]; } public static function dataTrend($id, $stDate, $enDate) { $list = self::getTrendFormatData($stDate, $enDate); $statisticList = SourceQrcodeDailyStatistics::query() ->select(['date']) ->selectRaw("SUM(scan_num) AS scan_num") ->selectRaw("SUM(keep_num) AS keep_num") ->where('sq_id', $id) ->whereBetween('date', [$stDate, $enDate]) ->groupBy('date') ->get(); if ($statisticList->isNotEmpty()) { foreach ($statisticList as $item) { $list[$item->date]['scan_num'] = intval($item->scan_num); $list[$item->date]['keep_num'] = intval($item->keep_num); } } return array_values($list); } protected static function getTrendFormatData($stDate, $enDate) { $dayArr = []; $stDateStr = strtotime($stDate); $enDateStr = strtotime($enDate); while ($stDateStr <= $enDateStr) { $date = date('Y-m-d', $stDateStr); $dayArr[$date] = [ 'date' => $date, 'scan_num' => 0, 'keep_num' => 0 ]; $stDateStr += 86400; } return $dayArr; } public static function customerList($sq_id, $name, $page, $pagesize) { $sqInfo = SourceQrcodes::find($sq_id); $corpid = $sqInfo->corpid; $state = $sqInfo->state; $customerQuery = CustomerDetails::suffix($corpid) ->where('corpid', $corpid) ->where('state', $state); $total = (clone $customerQuery)->count(); if($total == 0){ return [[], 0]; } $offset = ($page-1) * $pagesize; $list = $customerQuery->where(function($query) use($name){ if($name) $query->where('name', 'like', '%'. $name .'%'); }) ->select('id', 'name', 'external_userid', 'remark', 'scan_time', 'scan_user_type', 'enable', 'user_id') ->orderBy('id', 'desc') ->offset($offset) ->limit($pagesize) ->get(); //补充客服名称 $user_ids = $list->pluck('user_id')->all(); $user_names = DjUser::where('corpid', $corpid)->whereIn('user_id', $user_ids)->pluck('name', 'user_id')->all(); //客户头像 $external_userids = $list->pluck('external_userid')->all(); $customers = Customer::suffix($corpid) ->where('corpid', $corpid) ->whereIn('external_userid', $external_userids) ->pluck('avatar', 'external_userid') ->all(); foreach($list as $item){ $item->user_name = $user_names[$item->user_id] ?? ''; $item->avatar = $customers[$item->external_userid] ?? ''; } return [$list, $total]; } public static function staffList($sq_id) { $sqInfo = SourceQrcodes::find($sq_id); if(!isset($sqInfo->id)){ return null; } $corpid = $sqInfo->corpid; $state = $sqInfo->state; $list = SourceQrcodeUsers::where('enable', 1) ->where('sq_id', $sq_id) ->get(); if($list->isEmpty()){ return null; } $user_ids = $list->pluck('user_id')->all(); $user_names = DjUser::where('corpid', $corpid)->whereIn('user_id', $user_ids)->pluck('name', 'user_id')->all(); foreach($list as $item){ #客服名称 $item->user_name = $user_names[$item->user_id] ?? ''; #添加客户数 $item->user_add_total = CustomerDetails::suffix($corpid) ->where('corpid', $corpid) ->where('state', $state) ->where('user_id', $item->user_id) ->count(); #今日添加客户数 $item->user_add_count = CustomerDetails::suffix($corpid) ->where('corpid', $corpid) ->where('state', $state) ->where('user_id', $item->user_id) ->where('scan_time', '>', date('Y-m-d')) ->count(); } return $list; } }