作者 ZhengBing He

ding

  1 +<?php
  2 +
  3 +namespace App\Console\Commands;
  4 +
  5 +use App\Models\WorkOrder\WorkOrderLog;
  6 +use App\Services\DingTalkService;
  7 +use Illuminate\Console\Command;
  8 +use Illuminate\Support\Facades\Http;
  9 +
  10 +class WorkOrderDing extends Command
  11 +{
  12 + /**
  13 + * The name and signature of the console command.
  14 + *
  15 + * @var string
  16 + */
  17 + protected $signature = 'workorder:ding';
  18 +
  19 + /**
  20 + * The console command description.
  21 + *
  22 + * @var string
  23 + */
  24 + protected $description = '售后工单钉钉通知';
  25 +
  26 + /**
  27 + * Create a new command instance.
  28 + *
  29 + * @return void
  30 + */
  31 + public function __construct()
  32 + {
  33 + parent::__construct();
  34 + }
  35 +
  36 + /**
  37 + * Execute the console command.
  38 + *
  39 + * @return int
  40 + */
  41 + public function handle()
  42 + {
  43 + while (true) {
  44 + try {
  45 + $log = WorkOrderLog::where('ding', 0)->first();
  46 + if (!$log) {
  47 + sleep(3);
  48 + continue;
  49 + }
  50 + $mobile = $log->manager->mobile;
  51 + $response = Http::withBasicAuth(
  52 + env('DINGDING_BASIC_USER'),
  53 + env('DINGDING_BASIC_PASS')
  54 + )->get('https://oa.cmer.com/api/dingding/user/' . $mobile);
  55 + if ($response->status() == 200) {
  56 + $userid = $response->json()['data']['userid'];
  57 + $text = "**您有新的售后工单**<br>";
  58 + $text .= "工单ID:{$log->work_order_id}<br>";
  59 + $text .= "工单类型:{$log->workOrder->product}<br>";
  60 + $text .= "项目:{$log->workOrder->project->title}<br>";
  61 + $ding = new DingTalkService();
  62 + $resp = $ding->danliao(json_encode([
  63 + 'text' => $text,
  64 + 'title' => '售后工单通知',
  65 + ]), [$userid]);
  66 + $log->ding = 1;
  67 + }else
  68 + $log->ding = 2;
  69 + $log->save();
  70 + }catch (\Exception $exception){
  71 + echo date('Y-m-d H:i:s')." ".$exception->getMessage()."\n";
  72 + break;
  73 + }
  74 + }
  75 + }
  76 +}
  1 +<?php
  2 +
  3 +namespace App\Services;
  4 +
  5 +use App\DingDepartment;
  6 +use App\DingUser;
  7 +
  8 +/**
  9 + * 文档 https://apihub.cmer.com/docs/cmerdingtalk.html
  10 + */
  11 +class DingTalkService
  12 +{
  13 + protected $appKey;
  14 + protected $appSecret;
  15 + protected $robotCode;
  16 +
  17 + protected $headers = [];
  18 + protected $bashUrl = 'https://api.cmer.com';
  19 +
  20 + public function __construct()
  21 + {
  22 + $this->appKey = env('DING_TALK_APP_KEY');
  23 + $this->appSecret = env('DING_TALK_APP_SECRET');
  24 + $this->robotCode = env('DING_TALK_APP_KEY');
  25 + $this->headers = [
  26 + "Content-Type" => "application/json",
  27 + "apikey" => "UkzZljFv83Z2qBi5YR1o3f2otAVWtug6",
  28 + "X-CmerApi-Host" => "cmerdingtalk.p.cmer.com",
  29 + ];
  30 + }
  31 +
  32 + /** 发起请求 */
  33 + public function send_request(string $method, string $url, array $payload = [], array $params = [])
  34 + {
  35 + $client = new \GuzzleHttp\Client();
  36 + $options = [
  37 + 'headers' => $this->headers,
  38 + 'json' => $payload
  39 + ];
  40 + if (!empty($params))
  41 + $options['query'] = $params;
  42 +
  43 + $response = $client->request($method, $url, $options);
  44 + return $response;
  45 + }
  46 +
  47 + /** 批量发送私聊消息 */
  48 + public function danliao(string $text, array $user_ids)
  49 + {
  50 + $endpoint = '/v1/danliao';
  51 + $payload = [
  52 + "appKey" => $this->appKey,
  53 + "appSecret" => $this->appSecret,
  54 + "robotCode" => $this->robotCode,
  55 + "msg_param" => $text,
  56 + "user_ids" => $user_ids
  57 + ];
  58 + return $this->send_request('POST', $this->bashUrl . $endpoint, $payload);
  59 + }
  60 +
  61 + /** 批量发送Ding */
  62 + public function danliao_ding(string $text, array $user_ids)
  63 + {
  64 + $endpoint = '/v1/danliao_ding';
  65 + $payload = [
  66 + "appKey" => $this->appKey,
  67 + "appSecret" => $this->appSecret,
  68 + "robotCode" => $this->robotCode,
  69 + "content" => $text,
  70 + "receiver_user_id_list" => $user_ids
  71 + ];
  72 + return $this->send_request('POST', $this->bashUrl . $endpoint, $payload);
  73 + }
  74 +
  75 +}