123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- namespace app\command;
- use app\model\Company;
- use app\model\Customer;
- use app\model\Employee;
- use app\model\WeworksingleCompanySetting;
- use app\model\WeworksingleCustomer;
- use app\model\WeworksingleExternalMessage;
- use app\model\WeworksingleExternalMessageRecord;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\facade\Config;
- use think\facade\Env;
- use weworkapi\api\apiSingle;
- class WeworksingleSendMessage extends Command
- {
- protected function configure()
- {
- $this->setName('weworksingle_send_message')
- ->setDescription('weworksingle send group message');
- }
- protected function execute(Input $input, Output $output)
- {
- $db_config = [
- // 默认使用的数据库连接配置
- 'default' => env('database.driver', 'mysql'),
- // 自定义时间查询规则
- 'time_query_rule' => [],
- // 自动写入时间戳字段
- // true为自动识别类型 false关闭
- // 字符串则明确指定时间字段类型 支持 int timestamp datetime date
- 'auto_timestamp' => true,
- // 时间字段取出后的默认时间格式
- 'datetime_format' => 'Y-m-d H:i:s',
- // 时间字段配置 配置格式:create_time,update_time
- 'datetime_field' => '',
- // 数据库连接配置信息
- 'connections' => [
- 'mysql' => [
- // 数据库类型
- 'type' => Env::get('database.type', 'mysql'),
- // 服务器地址
- 'hostname' => Env::get('database.hostname', '127.0.0.1'),
- // 数据库名
- 'database' => Env::get('database.database', ''),
- // 用户名
- 'username' => Env::get('database.username', 'root'),
- // 密码
- 'password' => Env::get('database.password', ''),
- // 端口
- 'hostport' => Env::get('database.hostport', '3306'),
- // 数据库连接参数
- 'params' => [],
- // 数据库编码默认采用utf8
- 'charset' => Env::get('database.charset', 'utf8mb4'),
- // 数据库表前缀
- 'prefix' => Env::get('database.prefix', ''),
- // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
- 'deploy' => 0,
- // 数据库读写是否分离 主从式有效
- 'rw_separate' => false,
- // 读写分离后 主服务器数量
- 'master_num' => 1,
- // 指定从服务器序号
- 'slave_no' => '',
- // 是否严格检查字段是否存在
- 'fields_strict' => true,
- // 是否需要断线重连
- 'break_reconnect' => false,
- // 监听SQL
- 'trigger_sql' => false, // env('app_debug', true),
- // 开启字段缓存
- 'fields_cache' => false,
- ],
- // 更多的数据库配置信息
- ]
- ];
- Config::set($db_config, 'database');
- $where[] = ['status', '=', 0];
- $where[] = ['sendtime', '<=', time()];
- $list = WeworksingleExternalMessage::where($where)->select();
- $group_arr[1] = '确认量房';
- $group_arr[2] = '交定';
- $group_arr[3] = '签单';
- $record_data = []; //需要执行记录
- foreach($list as $v){
- // 发送的消息
- unset($msg);
- $msg = [];
- // 单聊标签组
- unset($select_group_name);
- $select_group_name = [];
- // 群发对象 (单聊)
- unset($receive_list);
- $receive_list = [];
- $company_id = Company::where('root_id', '=', $v['root_org'])->value('id');
- $setting = WeworksingleCompanySetting::where('company_id', '=', $company_id)->find();
- $config['corp_id'] = $setting['corp_id'];
- $config['agent_id'] = $setting['agentid'];
- $config['secret'] = $setting['customer_secret'];
- $single_send = []; // 发送单聊
- // 单聊
- if ($v['send_type'] == 1) {
- $select_group = array_unique(array_filter(explode(',', $v['customer_group'])));
- $select_group_name = [];
- foreach ($select_group as $s) {
- $select_group_name = array_merge($select_group_name, Customer::changeState($group_arr[$s], 'chaos'));
- }
- $c_where[] = ['state', 'in', $select_group_name];
- $orgids = orgSubIds($v['root_org']);
- $c_where[] = ['org_id', 'in', $orgids];
- $c_where[] = ['employee_id', '>', 0];
- $customer_list = Customer::where($c_where)->field('id,employee_id')->select()->toArray();
- if (!empty($customer_list)) {
- // 所属的员工
- $employee_ids = array_values(array_unique(array_column($customer_list, 'employee_id')));
- $employee_list = Employee::with(['weworksingleUser'])->where([['id', 'in', $employee_ids], ['weworksingle_uid', '>', 0]])->select();
- // 客户外部联系人
- $customer_ids = array_values(array_unique(array_column($customer_list, 'id')));
- $external_users = WeworksingleCustomer::where([['customer_id', 'in', $customer_ids], ['employee_id', '>', 0], ['status', '=', 0]])->select();
- if (!empty($employee_list) && !empty($external_users)) {
- foreach ($external_users as $c){
- foreach ($employee_list as $e) {
- if ($c['employee_id'] == $e['id']) {
- if (!empty($e['weworksingleUser'])) {
- $receive_list[] = $c['external_userid'];
- $single_send[$e['weworksingleUser']['userid']][] = [
- 'employee_id' => $e['id'],
- 'external_userid' => $c['external_userid']
- ];
- }
- }
- }
- }
- }
- }
- // 处理群发内容
- if (!empty($single_send)) {
- foreach ($single_send as $key => $val) {
- $msg = [
- 'chat_type' => 'single',
- 'external_userid' => array_column($val, 'external_userid'),
- 'sender' => $key,
- 'text' => [
- 'content'=> $v['content']
- ],
- 'attachments' => [
- /*[
- "msgtype" => "image",
- "image" =>[]
- ],
- [
- 'msgtype' => 'link',
- 'link' => []
- ],
- [
- 'msgtype' => 'miniprogram',
- 'miniprogram' => []
- ],
- [
- 'msgtype' => 'video',
- 'video' => []
- ],
- [
- 'msgtype' => 'file',
- 'file' => []
- ]*/
- ]
- ];
- $app = (new apiSingle($config));
- $result = $app->sendExternalMessage($msg);
- if ($result) {
- $employee_id = array_values(array_unique(array_column($val, 'employee_id')));
- $record_data[] = [
- 'message_id' => $v['id'],
- 'employee_id' => $employee_id[0],
- 'sender' => $key,
- 'external_userid' => '',
- 'msgid' => $result['msgid'],
- 'fail_list' => !empty($result['fail_list']) ? json($result['fail_list']) : ''
- ];
- }
- }
- }
- }
- // 群聊
- if ($v['send_type'] == 2) {
- $employee_id_arr = array_values(array_unique(array_filter(explode(',', $v['employee_id']))));
- $employee_list = Employee::with(['weworksingleUser'])->where([['id', 'in', $employee_id_arr], ['weworksingle_uid', '>', 0]])->select();
- if (!empty($employee_list)) {
- foreach ($employee_list as $key => $val) {
- if (!empty($val['weworksingleUser'])) {
- $msg = [
- 'chat_type' => 'group',
- 'external_userid' => [],
- 'sender' => $val['weworksingleUser']['userid'],
- 'text' => [
- 'content'=> $v['content']
- ],
- 'attachments' => [
- /*[
- "msgtype" => "image",
- "image" =>[]
- ],
- [
- 'msgtype' => 'link',
- 'link' => []
- ],
- [
- 'msgtype' => 'miniprogram',
- 'miniprogram' => []
- ],
- [
- 'msgtype' => 'video',
- 'video' => []
- ],
- [
- 'msgtype' => 'file',
- 'file' => []
- ]*/
- ]
- ];
- $app = (new apiSingle($config));
- $result = $app->sendExternalMessage($msg);
- if ($result) {
- $record_data[] = [
- 'message_id' => $v['id'],
- 'employee_id' => $val['id'],
- 'sender' => $val['weworksingleUser']['userid'],
- 'external_userid' => '',
- 'msgid' => $result['msgid'],
- 'fail_list' => !empty($result['fail_list']) ? json($result['fail_list']) : ''
- ];
- }
- }
- }
- }
- }
- $v->status = 1;
- $v->executetime = time();
- $v->receive_userid = implode(',', $receive_list);
- $v->save();
- }
- $s_result = (new WeworksingleExternalMessageRecord())->saveAll($record_data);
- }
- }
|