1
0

WeworksingleSendMessage.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace app\command;
  3. use app\model\Company;
  4. use app\model\Customer;
  5. use app\model\Employee;
  6. use app\model\WeworksingleCompanySetting;
  7. use app\model\WeworksingleCustomer;
  8. use app\model\WeworksingleExternalMessage;
  9. use app\model\WeworksingleExternalMessageRecord;
  10. use think\console\Command;
  11. use think\console\Input;
  12. use think\console\Output;
  13. use think\facade\Config;
  14. use think\facade\Env;
  15. use weworkapi\api\apiSingle;
  16. class WeworksingleSendMessage extends Command
  17. {
  18. protected function configure()
  19. {
  20. $this->setName('weworksingle_send_message')
  21. ->setDescription('weworksingle send group message');
  22. }
  23. protected function execute(Input $input, Output $output)
  24. {
  25. $db_config = [
  26. // 默认使用的数据库连接配置
  27. 'default' => env('database.driver', 'mysql'),
  28. // 自定义时间查询规则
  29. 'time_query_rule' => [],
  30. // 自动写入时间戳字段
  31. // true为自动识别类型 false关闭
  32. // 字符串则明确指定时间字段类型 支持 int timestamp datetime date
  33. 'auto_timestamp' => true,
  34. // 时间字段取出后的默认时间格式
  35. 'datetime_format' => 'Y-m-d H:i:s',
  36. // 时间字段配置 配置格式:create_time,update_time
  37. 'datetime_field' => '',
  38. // 数据库连接配置信息
  39. 'connections' => [
  40. 'mysql' => [
  41. // 数据库类型
  42. 'type' => Env::get('database.type', 'mysql'),
  43. // 服务器地址
  44. 'hostname' => Env::get('database.hostname', '127.0.0.1'),
  45. // 数据库名
  46. 'database' => Env::get('database.database', ''),
  47. // 用户名
  48. 'username' => Env::get('database.username', 'root'),
  49. // 密码
  50. 'password' => Env::get('database.password', ''),
  51. // 端口
  52. 'hostport' => Env::get('database.hostport', '3306'),
  53. // 数据库连接参数
  54. 'params' => [],
  55. // 数据库编码默认采用utf8
  56. 'charset' => Env::get('database.charset', 'utf8mb4'),
  57. // 数据库表前缀
  58. 'prefix' => Env::get('database.prefix', ''),
  59. // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
  60. 'deploy' => 0,
  61. // 数据库读写是否分离 主从式有效
  62. 'rw_separate' => false,
  63. // 读写分离后 主服务器数量
  64. 'master_num' => 1,
  65. // 指定从服务器序号
  66. 'slave_no' => '',
  67. // 是否严格检查字段是否存在
  68. 'fields_strict' => true,
  69. // 是否需要断线重连
  70. 'break_reconnect' => false,
  71. // 监听SQL
  72. 'trigger_sql' => false, // env('app_debug', true),
  73. // 开启字段缓存
  74. 'fields_cache' => false,
  75. ],
  76. // 更多的数据库配置信息
  77. ]
  78. ];
  79. Config::set($db_config, 'database');
  80. $where[] = ['status', '=', 0];
  81. $where[] = ['sendtime', '<=', time()];
  82. $list = WeworksingleExternalMessage::where($where)->select();
  83. $group_arr[1] = '确认量房';
  84. $group_arr[2] = '交定';
  85. $group_arr[3] = '签单';
  86. $record_data = []; //需要执行记录
  87. foreach($list as $v){
  88. // 发送的消息
  89. unset($msg);
  90. $msg = [];
  91. // 单聊标签组
  92. unset($select_group_name);
  93. $select_group_name = [];
  94. // 群发对象 (单聊)
  95. unset($receive_list);
  96. $receive_list = [];
  97. $company_id = Company::where('root_id', '=', $v['root_org'])->value('id');
  98. $setting = WeworksingleCompanySetting::where('company_id', '=', $company_id)->find();
  99. $config['corp_id'] = $setting['corp_id'];
  100. $config['agent_id'] = $setting['agentid'];
  101. $config['secret'] = $setting['customer_secret'];
  102. $single_send = []; // 发送单聊
  103. // 单聊
  104. if ($v['send_type'] == 1) {
  105. $select_group = array_unique(array_filter(explode(',', $v['customer_group'])));
  106. $select_group_name = [];
  107. foreach ($select_group as $s) {
  108. $select_group_name = array_merge($select_group_name, Customer::changeState($group_arr[$s], 'chaos'));
  109. }
  110. $c_where[] = ['state', 'in', $select_group_name];
  111. $orgids = orgSubIds($v['root_org']);
  112. $c_where[] = ['org_id', 'in', $orgids];
  113. $c_where[] = ['employee_id', '>', 0];
  114. $customer_list = Customer::where($c_where)->field('id,employee_id')->select()->toArray();
  115. if (!empty($customer_list)) {
  116. // 所属的员工
  117. $employee_ids = array_values(array_unique(array_column($customer_list, 'employee_id')));
  118. $employee_list = Employee::with(['weworksingleUser'])->where([['id', 'in', $employee_ids], ['weworksingle_uid', '>', 0]])->select();
  119. // 客户外部联系人
  120. $customer_ids = array_values(array_unique(array_column($customer_list, 'id')));
  121. $external_users = WeworksingleCustomer::where([['customer_id', 'in', $customer_ids], ['employee_id', '>', 0], ['status', '=', 0]])->select();
  122. if (!empty($employee_list) && !empty($external_users)) {
  123. foreach ($external_users as $c){
  124. foreach ($employee_list as $e) {
  125. if ($c['employee_id'] == $e['id']) {
  126. if (!empty($e['weworksingleUser'])) {
  127. $receive_list[] = $c['external_userid'];
  128. $single_send[$e['weworksingleUser']['userid']][] = [
  129. 'employee_id' => $e['id'],
  130. 'external_userid' => $c['external_userid']
  131. ];
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. // 处理群发内容
  139. if (!empty($single_send)) {
  140. foreach ($single_send as $key => $val) {
  141. $msg = [
  142. 'chat_type' => 'single',
  143. 'external_userid' => array_column($val, 'external_userid'),
  144. 'sender' => $key,
  145. 'text' => [
  146. 'content'=> $v['content']
  147. ],
  148. 'attachments' => [
  149. /*[
  150. "msgtype" => "image",
  151. "image" =>[]
  152. ],
  153. [
  154. 'msgtype' => 'link',
  155. 'link' => []
  156. ],
  157. [
  158. 'msgtype' => 'miniprogram',
  159. 'miniprogram' => []
  160. ],
  161. [
  162. 'msgtype' => 'video',
  163. 'video' => []
  164. ],
  165. [
  166. 'msgtype' => 'file',
  167. 'file' => []
  168. ]*/
  169. ]
  170. ];
  171. $app = (new apiSingle($config));
  172. $result = $app->sendExternalMessage($msg);
  173. if ($result) {
  174. $employee_id = array_values(array_unique(array_column($val, 'employee_id')));
  175. $record_data[] = [
  176. 'message_id' => $v['id'],
  177. 'employee_id' => $employee_id[0],
  178. 'sender' => $key,
  179. 'external_userid' => '',
  180. 'msgid' => $result['msgid'],
  181. 'fail_list' => !empty($result['fail_list']) ? json($result['fail_list']) : ''
  182. ];
  183. }
  184. }
  185. }
  186. }
  187. // 群聊
  188. if ($v['send_type'] == 2) {
  189. $employee_id_arr = array_values(array_unique(array_filter(explode(',', $v['employee_id']))));
  190. $employee_list = Employee::with(['weworksingleUser'])->where([['id', 'in', $employee_id_arr], ['weworksingle_uid', '>', 0]])->select();
  191. if (!empty($employee_list)) {
  192. foreach ($employee_list as $key => $val) {
  193. if (!empty($val['weworksingleUser'])) {
  194. $msg = [
  195. 'chat_type' => 'group',
  196. 'external_userid' => [],
  197. 'sender' => $val['weworksingleUser']['userid'],
  198. 'text' => [
  199. 'content'=> $v['content']
  200. ],
  201. 'attachments' => [
  202. /*[
  203. "msgtype" => "image",
  204. "image" =>[]
  205. ],
  206. [
  207. 'msgtype' => 'link',
  208. 'link' => []
  209. ],
  210. [
  211. 'msgtype' => 'miniprogram',
  212. 'miniprogram' => []
  213. ],
  214. [
  215. 'msgtype' => 'video',
  216. 'video' => []
  217. ],
  218. [
  219. 'msgtype' => 'file',
  220. 'file' => []
  221. ]*/
  222. ]
  223. ];
  224. $app = (new apiSingle($config));
  225. $result = $app->sendExternalMessage($msg);
  226. if ($result) {
  227. $record_data[] = [
  228. 'message_id' => $v['id'],
  229. 'employee_id' => $val['id'],
  230. 'sender' => $val['weworksingleUser']['userid'],
  231. 'external_userid' => '',
  232. 'msgid' => $result['msgid'],
  233. 'fail_list' => !empty($result['fail_list']) ? json($result['fail_list']) : ''
  234. ];
  235. }
  236. }
  237. }
  238. }
  239. }
  240. $v->status = 1;
  241. $v->executetime = time();
  242. $v->receive_userid = implode(',', $receive_list);
  243. $v->save();
  244. }
  245. $s_result = (new WeworksingleExternalMessageRecord())->saveAll($record_data);
  246. }
  247. }