DingService.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\ProcessDataJob;
  4. use App\Model\DDEmployee;
  5. use App\Model\Record;
  6. use App\Model\U8State;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Support\Facades\Redis;
  10. class DingService extends Service
  11. {
  12. public function getAccessToken($type)
  13. {
  14. $key = DDEmployee::type_redis[$type];
  15. if(empty($key)) return [false, 'token名称未设置'];
  16. $token = Redis::get($key);
  17. if(! empty($token)) return [true, ['access_token' => $token]];
  18. $config = config("dingtalk.{$type}");
  19. if(empty($config)) return [false, '钉钉配置信息不存在'];
  20. $appKey = $config['app_key'] ?? null;
  21. $appSecret = $config['app_secret'] ?? null;
  22. if (!$appKey) return [false, 'AppKey 配置不完整'];
  23. if (!$appSecret) return [false, 'AppSecret 配置不完整'];
  24. $url = "https://api.dingtalk.com/v1.0/oauth2/accessToken";
  25. $resp = $this->curlOpen1($url, [
  26. 'request' => 'post',
  27. 'header' => ['Content-Type: application/json'],
  28. 'json' => [
  29. "appKey" => $appKey,
  30. "appSecret" => $appSecret
  31. ]
  32. ]);
  33. $res = json_decode($resp, true);
  34. $accessToken = $res['accessToken'] ?? "";
  35. $expires_in = $res['expires_in'] ?? 0;
  36. if(empty($accessToken)) return [false, 'AccessToken获取失败'];
  37. Redis::setex($key, $expires_in, $accessToken);
  38. return [true, ['access_token' => $accessToken]];
  39. }
  40. /**
  41. * 根据前端传来的免登 code 获取用户信息
  42. * @param string $code 前端 dd.getAuthCode 获取的 code
  43. * @return array [bool, data] bool 表示成功与否,data 成功返回用户信息,失败返回错误信息
  44. */
  45. public function getUserByCode($data, $LoginType)
  46. {
  47. if(empty($LoginType)) return [false, 'login_type类型不能为空'];
  48. if(! isset(DDEmployee::type[$LoginType])) return [false, 'login_type类型不存在或错误'];
  49. $code = $data['code'] ?? "";
  50. if (empty($code)) return [false, '钉钉授权code不能为空'];
  51. // 1. 获取 access_token
  52. [$success, $tokenData] = $this->getAccessToken($LoginType);
  53. if (! $success) return [false, $tokenData]; // tokenData 是错误信息
  54. $accessToken = $tokenData['access_token'];
  55. // 2. 用 code 换取用户信息(v2 接口)
  56. $url = "https://oapi.dingtalk.com/topapi/v2/user/getuserinfo?access_token={$accessToken}";
  57. $resp = $this->curlOpen1($url, [
  58. 'request' => 'post',
  59. 'header' => [
  60. "Content-Type: application/json",
  61. ],
  62. 'json' => [
  63. "code" => $code
  64. ]
  65. ]);
  66. $res = json_decode($resp, true);
  67. if (!isset($res['errcode'])) {
  68. return [false, '接口返回异常: ' . $resp];
  69. }
  70. if ($res['errcode'] !== 0) {
  71. return [false, '获取用户信息失败: ' . $res['errmsg']];
  72. }
  73. if(! empty($res['result'])){
  74. $result = $res['result'];
  75. DDEmployee::updateOrCreate(
  76. ['userid' => $result['userid'], 'login_type' => $LoginType],
  77. ['name' => $result['name'], 'userid' => $result['userid'], 'login_type' => $LoginType, 'u_userid' => $result['unionid']]
  78. );
  79. }
  80. return [true, $res];
  81. }
  82. private function getManDetail($user, $accessToken){
  83. // 3. 根据 userid 获取详细用户信息(包括部门)
  84. $urlDetail = "https://oapi.dingtalk.com/topapi/v2/user/get?access_token={$accessToken}";
  85. $respDetail = $this->curlOpen1($urlDetail, [
  86. 'request' => 'post',
  87. 'header' => ["Content-Type: application/json"],
  88. 'json' => ["userid" => $user['userid']]
  89. ]);
  90. $detail = json_decode($respDetail, true);
  91. if (!isset($detail['errcode'])) return [false, '获取用户详情接口异常: ' . $respDetail];
  92. if ($detail['errcode'] !== 0) return [false, '获取用户详情失败: ' . $detail['errmsg']];
  93. if (empty($detail['result'])) return [false, '获取用户详情失败,结果为空'];
  94. // 返回完整用户信息
  95. return [true, $detail['result']];
  96. }
  97. public function createProcessInstance($data, $user)
  98. {
  99. if(empty($data['type'])) return [false, '单据类型不能为空'];
  100. $type = $data['type'];
  101. if(empty($data['order_number'])) return [false,'单号不能为空'];
  102. [$success, $msg] = $this->checkCreateProcessInstance($data, $user);
  103. if(! $success) return [false, $msg];
  104. //获取模板id
  105. $code = $this->getModelCode($type, $user);
  106. //获取模板数据
  107. [$success, $formData] = $this->getFormData($data, $user);
  108. if(! $success) return [false, $formData];
  109. // 1. 获取 access_token
  110. [$success, $tokenData] = $this->getAccessToken($user['login_type']);
  111. if (!$success) return [false, $tokenData];
  112. $accessToken = $tokenData['access_token'];
  113. $userId = $user['userid'];
  114. [$success, $userDetail] = $this->getManDetail($user, $accessToken);
  115. if(!$success) return [false, $userDetail];
  116. //创建审批
  117. [$success, $msg] = $this->createFlow($accessToken, $code, $userId, $userDetail, $formData);
  118. if(! $success) return [false, $msg];
  119. //记录信息
  120. $this->recordDatabase($data, $user, $msg);
  121. return [true, ''];
  122. }
  123. private function recordDatabase($data, $user, $process_instance_id){
  124. $type = $data['type'];
  125. Record::insert([
  126. 'type' => $type,
  127. 'login_type' => $user['login_type'],
  128. 'database' => $user['zt_database'],
  129. 'userid' => $user['userid'],
  130. 'order_number'=> $data['order_number'],
  131. 'crt_time' => time(),
  132. 'process_instance_id' => $process_instance_id,
  133. 'state' => Record::state_zero
  134. ]);
  135. //保证
  136. U8State::updateOrCreate(
  137. ['order_number' => $data['order_number'], 'login_type' => $user['login_type'], 'type' => $type],
  138. ['state' => U8State::state_zero, 'userid' => $user['userid'],]
  139. );
  140. return [true, ''];
  141. }
  142. private function checkCreateProcessInstance($data, $user){
  143. list($status,$msg) = $this->limitingSendRequestBackgExpire($data['order_number'].$data['type'].$user['login_type']);
  144. if(! $status) return [false,$msg];
  145. $type = $data['type'];
  146. $login_type = $user['login_type'];
  147. // // 1. 获取该单号最新的一条记录
  148. // $lastRecord = Record::where('del_time', 0)
  149. // ->where('type', $type)
  150. // ->where('login_type', $login_type)
  151. // ->where('order_number', $data['order_number'])
  152. // ->latest('id') // 按 ID 倒序,取最新
  153. // ->first(['state']); // 只取状态字段
  154. //
  155. // // 2. 逻辑判断
  156. // if ($lastRecord) {
  157. // $text_o = Record::type_name[$type];
  158. // // 如果状态是 0 (待审核) 或 1 (审核通过),拦截
  159. // if (in_array($lastRecord->state, [Record::state_zero, Record::state_one])) {
  160. // $text = Record::state_name[$lastRecord->state];
  161. // return [false, $text_o . ' (' . $data['order_number'] . ') ' . $text . ',操作失败'];
  162. // }
  163. // }
  164. // 1. 获取该单号最新的一条记录
  165. $lastRecord = U8State::where('del_time', 0)
  166. ->where('type', $type)
  167. ->where('login_type', $login_type)
  168. ->where('order_number', $data['order_number'])
  169. ->first(['state']); // 只取状态字段
  170. // 2. 逻辑判断
  171. if ($lastRecord) {
  172. $text_o = U8State::type_name[$type];
  173. // 如果状态是 0 (待审核) 或 1 (审核通过),拦截
  174. if (in_array($lastRecord->state, [U8State::state_zero, U8State::state_one])) {
  175. $text = U8State::state_name[$lastRecord->state];
  176. return [false, $text_o . ' (' . $data['order_number'] . ') 处于' . $text . ',操作失败'];
  177. }
  178. }
  179. return [true, ''];
  180. }
  181. private function createFlow($accessToken, $code, $userId, $userDetail, $formData){
  182. // 2. 请求 URL
  183. $url = "https://oapi.dingtalk.com/topapi/processinstance/create?access_token={$accessToken}";
  184. // 3. 请求体
  185. $payload = [
  186. "process_code" => $code, // 审批模板编码
  187. "originator_user_id" => $userId, // 发起人 userId
  188. "dept_id" => $userDetail['dept_id_list'][0], // 发起人部门 ID
  189. "form_component_values" => $formData, // 表单数据
  190. ];
  191. // 4. 发送请求
  192. $resp = $this->curlOpen1($url, [
  193. 'request' => 'post',
  194. 'header' => [
  195. "Content-Type: application/json",
  196. ],
  197. 'json' => $payload
  198. ]);
  199. $res = json_decode($resp, true);
  200. if (!isset($res['errcode'])) {
  201. return [false, "接口返回异常: " . $resp];
  202. }
  203. if ($res['errcode'] !== 0) {
  204. return [false, "创建审批实例失败: " . $res['errmsg']];
  205. }
  206. return [true, $res['process_instance_id']];
  207. }
  208. //待审核
  209. public function getTodoProcessList1($data, $user)
  210. {
  211. // 1. 获取 AccessToken
  212. [$success, $tokenData] = $this->getAccessToken($user['login_type']);
  213. if (!$success) return [false, $tokenData];
  214. $accessToken = $tokenData['access_token'];
  215. // 2. 请求 URL (使用 TOP 接口)
  216. $url = "https://oapi.dingtalk.com/topapi/processinstance/listids?access_token={$accessToken}";
  217. // 3. 准备参数
  218. // 注意:TOP 接口的分页叫 cursor (偏移量),从 0 开始
  219. if(empty($data['page_index'])) {
  220. $cursor = 0;
  221. }else{
  222. $cursor = ($data['page_index'] - 1) * $data['page_size'];
  223. }
  224. $payload = [
  225. 'userid' => $user['userid'],
  226. 'cursor' => $cursor,
  227. 'size' => (int)($data['page_size'] ?? 20),
  228. 'process_code' => 'PROC-B31B268E-6B7E-417D-A103-8765D8AE9090'
  229. ];
  230. // 4. 发送请求
  231. $resp = $this->curlOpen1($url, [
  232. 'request' => 'post',
  233. 'json' => $payload
  234. ]);
  235. $res = json_decode($resp, true);
  236. dd($res);
  237. // 5. 错误处理 (TOP 接口 errcode 为 0 表示成功)
  238. if (($res['errcode'] ?? -1) !== 0) {
  239. return [false, "获取待审批列表失败: " . ($res['errmsg'] ?? '未知错误')];
  240. }
  241. $result = $res['result'] ?? [];
  242. $instanceIds = $result['list'] ?? [];
  243. $nextCursor = $result['next_cursor'] ?? null;
  244. $hasMore = !is_null($nextCursor);
  245. if (empty($instanceIds)) {
  246. return [true, ['list' => [], 'has_more' => false]];
  247. }
  248. // 6. 关联本地业务记录
  249. // 因为 TOP 接口只给实例 ID,不给 TaskID,如果你本地需要 TaskID,
  250. // 通常需要再通过单据详情接口获取,或者本地生成。
  251. $records = Record::where('del_time', 0)
  252. ->whereIn('process_instance_id', $instanceIds)
  253. ->where('login_type', $user['login_type'])
  254. ->select('type', 'order_number', 'process_instance_id')
  255. ->get();
  256. $finalList = [];
  257. foreach ($records as $item) {
  258. $finalList[] = [
  259. 'type' => $item->type,
  260. 'type_title' => U8State::type_name[$item->type] ?? '未知',
  261. 'order_number' => $item->order_number,
  262. 'process_instance_id' => $item->process_instance_id,
  263. 'task_id' => '' // TOP 列表接口不直接返回 taskId
  264. ];
  265. }
  266. return [true, [
  267. 'list' => $finalList,
  268. 'has_more' => $hasMore
  269. ]];
  270. }
  271. public function getTodoProcessList($data, $user)
  272. {
  273. [$success, $tokenData] = $this->getAccessToken($user['login_type']);
  274. if (!$success) return [false, $tokenData];
  275. $accessToken = $tokenData['access_token'];
  276. $pageNumber = $data['page_index'] ?? 1;
  277. $pageSize = $data['page_size'] ?? 20;
  278. // 重点:加上 viewId=all
  279. $url = "https://api.dingtalk.com/v1.0/workflow/processCentres/todoTasks?userId={$user['userid']}&pageNumber=1&pageSize=20";
  280. $url = "https://api.dingtalk.com/v1.0/workflow/processes/todoTasks/numbers?userId={$user['userid']}";
  281. $resp = $this->curlOpen1($url, [
  282. 'request' => 'get',
  283. 'header' => [
  284. "Content-Type: application/json",
  285. "x-acs-dingtalk-access-token: {$accessToken}"
  286. ]
  287. ]);
  288. $res = json_decode($resp, true);
  289. if (isset($res['code']) && $res['code'] !== 'success') {
  290. return [false, "钉钉接口报错: " . ($res['message'] ?? '未知错误')];
  291. }
  292. $result = $res['result'] ?? [];
  293. $tasks = $result['list'] ?? [];
  294. $hasMore = $result['hasMore'] ?? false;
  295. if (empty($tasks)) {
  296. return [true, ['list' => [], 'has_more' => $hasMore]];
  297. }
  298. // 提取实例ID
  299. $instanceIds = array_column($tasks, 'processInstanceId');
  300. // 建立 实例ID -> 任务ID 的映射
  301. $taskMap = array_column($tasks, 'taskId', 'processInstanceId');
  302. // 关联本地业务表记录
  303. $records = Record::where('del_time', 0)
  304. ->whereIn('process_instance_id', $instanceIds)
  305. ->where('login_type', $user['login_type'])
  306. ->select('type', 'order_number', 'process_instance_id')
  307. ->get();
  308. $finalList = [];
  309. foreach ($records as $item) {
  310. $finalList[] = [
  311. 'type' => $item->type,
  312. 'type_title' => U8State::type_name[$item->type],
  313. 'order_number' => $item->order_number,
  314. 'process_instance_id' => $item->process_instance_id,
  315. 'task_id' => $taskMap[$item->process_instance_id] ?? ''
  316. ];
  317. }
  318. return [true, [
  319. 'list' => $finalList,
  320. 'has_more' => $hasMore
  321. ]];
  322. }
  323. //审核
  324. public function executeProcess($data, $user)
  325. {
  326. if(empty($data['result']) || ! in_array($data['result'], ['agree','refuse'])) return [false, 'result错误或不存在'];
  327. if(empty($data['task_id'])) return [false, 'task_id错误或不存在'];
  328. if(empty($data['process_instance_id'])) return [false, 'process_instance_id错误或不存在'];
  329. // 1. 获取 AccessToken
  330. [$success, $tokenData] = $this->getAccessToken($user['login_type']);
  331. if (!$success) return [false, $tokenData];
  332. $accessToken = $tokenData['access_token'];
  333. // 2. 接口 URL (新版 v1.0 路径)
  334. $url = "https://api.dingtalk.com/v1.0/workflow/processInstances/execute";
  335. // 3. 构建请求体
  336. // 注意:taskId 必须从待办任务接口中获取,不能随意填写
  337. $payload = [
  338. "processInstanceId" => $data['process_instance_id'], // 审批实例 ID
  339. "actionerUserId" => $user['userid'], // 操作人钉钉 ID
  340. "result" => $data['result'],
  341. "remark" => $data['remark'] ?? '同意。', // 审批意见
  342. "taskId" => $data['task_id'],
  343. ];
  344. // 如果有图片或附件,则加入 file 字段
  345. if (!empty($data['photos']) || !empty($data['attachments'])) {
  346. $payload['file'] = [
  347. "photos" => $data['photos'] ?? [],
  348. "attachments" => $data['attachments'] ?? []
  349. ];
  350. }
  351. // 4. 发送请求
  352. $resp = $this->curlOpen1($url, [
  353. 'request' => 'post',
  354. 'header' => [
  355. "Content-Type: application/json",
  356. "x-acs-dingtalk-access-token: {$accessToken}"
  357. ],
  358. 'json' => $payload
  359. ]);
  360. $res = json_decode($resp, true);
  361. // 5. 结果处理
  362. // 新版接口成功时通常返回空对象或 {"result": true}
  363. if (isset($res['code']) && $res['code'] !== 'success') {
  364. return [false, "审批操作失败: " . ($res['message'] ?? '未知错误')];
  365. }
  366. try {
  367. DB::beginTransaction();
  368. // 成功后续业务逻辑
  369. if(isset($res['success']) && $res['success'] == true) $this->updateDatabase($data, $user);
  370. DB::commit();
  371. }catch (\Throwable $exception){
  372. DB::rollBack();
  373. return [false, $exception->getMessage()];
  374. }
  375. return [true, ""];
  376. }
  377. private function updateDatabase($data, $user){
  378. $record = Record::where('login_type', $user['login_type'])
  379. ->where('process_instance_id')
  380. ->first();
  381. if(empty($record)) return [false, 'record记录不存在'];
  382. $record = $record->toArray();
  383. if($data['result'] == 'agree'){
  384. //通过
  385. Record::where('login_type', $user['login_type'])
  386. ->where('process_instance_id')
  387. ->update(['del_time' => 2]);
  388. ProcessDataJob::dispatch($record)->onQueue(Record::$job);
  389. }else{
  390. //驳回
  391. Record::where('login_type', $user['login_type'])
  392. ->where('process_instance_id')
  393. ->update(['del_time' => 1]);
  394. U8State::updateOrCreate(
  395. ['order_number' => $record['order_number'], 'login_type' => $user['login_type'], 'type' => $record['type']],
  396. ['state' => U8State::state_two]
  397. );
  398. }
  399. return [true, ''];
  400. }
  401. public function executeApproval($data, $user)
  402. {
  403. [$success, $tokenData] = $this->getAccessToken($user['login_type']);
  404. if (!$success) return [false, $tokenData];
  405. $accessToken = $tokenData['access_token'];
  406. $processInstanceId = $data['process_instance_id'] ?? 0;
  407. $action = $data['action'] ?? 'agree';
  408. $remark = $data['remark'] ?? '同意';
  409. // 1. 请求 URL
  410. $url = "https://oapi.dingtalk.com/topapi/process/instance/execute?access_token={$accessToken}";
  411. // 2. 构造请求体
  412. $payload = [
  413. "process_instance_id" => $processInstanceId,
  414. "remark" => $remark,
  415. "result" => $action, // 'agree' 或 'refuse'
  416. "task_id" => $this->getTaskIdByInstanceId($accessToken, $processInstanceId, $user['userid']), // 获取当前人的任务ID
  417. ];
  418. // 3. 发送请求
  419. $resp = $this->curlOpen1($url, [
  420. 'request' => 'post',
  421. 'header' => ["Content-Type: application/json"],
  422. 'json' => $payload
  423. ]);
  424. $res = json_decode($resp, true);
  425. if (!isset($res['errcode']) || $res['errcode'] !== 0) {
  426. return [false, "审批操作失败: " . ($res['errmsg'] ?? '未知错误')];
  427. }
  428. return [true, ""];
  429. }
  430. private function getTaskIdByInstanceId($accessToken, $instanceId, $userId)
  431. {
  432. $url = "https://oapi.dingtalk.com/topapi/processinstance/get?access_token={$accessToken}";
  433. $resp = $this->curlOpen1($url, [
  434. 'request' => 'post',
  435. 'header' => ["Content-Type: application/json"],
  436. 'json' => ["process_instance_id" => $instanceId]
  437. ]);
  438. $res = json_decode($resp, true);
  439. $tasks = $res['process_instance']['tasks'] ?? [];
  440. foreach ($tasks as $task) {
  441. // 状态为 RUNNING (进行中) 且 处理人是当前用户
  442. if ($task['task_status'] === 'RUNNING' && $task['userid'] === $userId) {
  443. return $task['task_id'];
  444. }
  445. }
  446. return null;
  447. }
  448. private function getModelCode($type, $user){
  449. $login_type = $user['login_type'];
  450. if($login_type == DDEmployee::type_one){
  451. //浙江
  452. if($type == 1){
  453. // 采购单
  454. $code = "PROC-B31B268E-6B7E-417D-A103-8765D8AE9090";
  455. }elseif ($type == 2){
  456. // 请购单
  457. $code = "PROC-C2F6D31A-979F-4038-B2A7-F2295A71702B";
  458. }elseif ($type == 3){
  459. // 采购入库单
  460. $code = "PROC-6DFEB04C-0E53-4430-B6F8-ED887E78EA9E";
  461. }elseif ($type == 4){
  462. // 存货
  463. $code = "PROC-9934D26D-D25D-408B-8B0E-FBD88EAB510D";
  464. }elseif ($type == 5){
  465. // 供应商
  466. $code = "PROC-30C00FA9-BD09-4139-92AD-220579109E07";
  467. }
  468. }elseif ($login_type == DDEmployee::type_two){
  469. //杭州
  470. if($type == 1){
  471. // 采购单
  472. $code = "PROC-DC4D5B5E-EF49-4230-A0A8-E6CCA55336DC";
  473. }elseif ($type == 2){
  474. // 请购单
  475. $code = "PROC-E44BE6B5-4530-42E1-A1A1-707708E45841";
  476. }elseif ($type == 3){
  477. // 采购入库单
  478. $code = "PROC-E166769C-4FEA-4D49-BD3C-F9183BD366F2";
  479. }elseif ($type == 4){
  480. // 存货
  481. $code = "PROC-229F1263-4180-4B50-A92B-749E699AB16E";
  482. }elseif ($type == 5){
  483. // 供应商
  484. $code = "PROC-D7304810-1767-4FA3-A62C-E791B5801715";
  485. }
  486. }
  487. return $code ?? "";
  488. }
  489. private function getFormData($data, $user){
  490. //cs
  491. // $formData = [ [ "name" => "订单日期", "value" => "2025-09-23" ], [ "name" => "订单编号", "value" => "PO20250923001" ], [ "name" => "业务类型", "value" => "标准采购" ], [ "name" => "供应商", "value" => "XX供应商有限公司" ], [ "name" => "制单人", "value" => "陈庆鹏" ], [ "name" => "表体", "value" => json_encode([ [ [ "name" => "存货名称", "value" => "打印机" ], [ "name" => "数量", "value" => "2" ], [ "name" => "主计量单位", "value" => "台" ], [ "name" => "原币价税合计", "value" => "3000" ] ], [ [ "name" => "存货名称", "value" => "显示器" ], [ "name" => "数量", "value" => "5" ], [ "name" => "主计量单位", "value" => "个" ], [ "name" => "原币价税合计", "value" => "5000" ] ] ], JSON_UNESCAPED_UNICODE) ] ];
  492. // return [true, $formData];
  493. //cs
  494. $service = new U8ServerService($user);
  495. $error = $service->getError();
  496. if(! empty($error)) return [false, $error];
  497. [$success, $order] = $service->getOrderDetails($data, $user);
  498. if(! $success) return [false, $order];
  499. $type = $data['type'];
  500. if($type == 1){
  501. // 采购单
  502. $formData = $this->typeOne($order);
  503. }elseif ($type == 2){
  504. // 采购请购单
  505. $formData = $this->typeTwo($order);
  506. }elseif($type == 3){
  507. // 采购入库单
  508. $formData = $this->typeThree($order);
  509. }elseif($type == 4){
  510. // 存货
  511. $formData = $this->typeFour($order);
  512. }elseif($type == 5){
  513. // 供应商
  514. $formData = $this->typeFive($order);
  515. }
  516. if(empty($formData)) return [false, '审批参数不能为空'];
  517. return [true, $formData];
  518. }
  519. private function typeOne($userOrder){
  520. if (empty($userOrder)) return [];
  521. $formData = [
  522. [
  523. "name" => "订单日期",
  524. "value" => date('Y-m-d', strtotime($userOrder['order_date'] ?? ''))
  525. ],
  526. [
  527. "name" => "订单编号",
  528. "value" => $userOrder['order_number'] ?? ''
  529. ],
  530. [
  531. "name" => "业务类型",
  532. "value" => $userOrder['business_type'] ?? ''
  533. ],
  534. [
  535. "name" => "供应商",
  536. "value" => $userOrder['supplier_title'] ?? ''
  537. ],
  538. [
  539. "name" => "制单人",
  540. "value" => $userOrder['crt_name'] ?? ''
  541. ],
  542. [
  543. "name" => "表体", // 对应 TableField 的 label
  544. "value" => json_encode(
  545. array_map(function($item){
  546. return [
  547. [
  548. "name" => "存货名称",
  549. "value" => $item['product_title'] ?? ''
  550. ],
  551. [
  552. "name" => "数量",
  553. "value" => $item['quantity'] ?? 0
  554. ],
  555. [
  556. "name" => "主计量", // 修改这里,对应模板字段
  557. "value" => $item['unit_title'] ?? ''
  558. ],
  559. [
  560. "name" => "原币价税合计",
  561. "value" => $item['amount'] ?? 0
  562. ]
  563. ];
  564. }, $userOrder['detail'] ?? []),
  565. JSON_UNESCAPED_UNICODE
  566. )
  567. ]
  568. ];
  569. return $formData;
  570. }
  571. private function typeTwo($userOrder){
  572. if (empty($userOrder)) return [];
  573. $formData = [
  574. [
  575. "name" => "单据号",
  576. "value" => $userOrder['order_number'] ?? ''
  577. ],
  578. [
  579. "name" => "日期",
  580. "value" => date('Y-m-d', strtotime($userOrder['order_date'] ?? ''))
  581. ],
  582. [
  583. "name" => "业务类型",
  584. "value" => $userOrder['business_type'] ?? ''
  585. ],
  586. [
  587. "name" => "请购人",
  588. "value" => $userOrder['purchase_name'] ?? ''
  589. ],
  590. [
  591. "name" => "制单人",
  592. "value" => $userOrder['crt_name'] ?? ''
  593. ],
  594. [
  595. "name" => "表体", // 对应 TableField 的 label
  596. "value" => json_encode(
  597. array_map(function($item){
  598. return [
  599. [
  600. "name" => "存货名称",
  601. "value" => $item['product_title'] ?? ''
  602. ],
  603. [
  604. "name" => "数量",
  605. "value" => $item['quantity'] ?? 0
  606. ],
  607. [
  608. "name" => "主计量", // 修改这里,对应模板字段
  609. "value" => $item['unit_title'] ?? ''
  610. ],
  611. [
  612. "name" => "要求到货日期",
  613. "value" => $item['need_arrived_date'] ?? ''
  614. ]
  615. ];
  616. }, $userOrder['detail'] ?? []),
  617. JSON_UNESCAPED_UNICODE
  618. )
  619. ]
  620. ];
  621. return $formData;
  622. }
  623. private function typeThree($userOrder){
  624. if (empty($userOrder)) return [];
  625. $formData = [
  626. [
  627. "name" => "入库单号",
  628. "value" => $userOrder['order_number'] ?? ''
  629. ],
  630. [
  631. "name" => "入库日期",
  632. "value" => date('Y-m-d', strtotime($userOrder['order_date'] ?? ''))
  633. ],
  634. [
  635. "name" => "仓库",
  636. "value" => $userOrder['warehouse_name'] ?? ''
  637. ],
  638. [
  639. "name" => "入库类别",
  640. "value" => $userOrder['rd_style_name'] ?? ''
  641. ],
  642. [
  643. "name" => "供货单位",
  644. "value" => $userOrder['supplier_title'] ?? ''
  645. ],
  646. [
  647. "name" => "制单人",
  648. "value" => $userOrder['crt_name'] ?? ''
  649. ],
  650. [
  651. "name" => "表体", // 对应 TableField 的 label
  652. "value" => json_encode(
  653. array_map(function($item){
  654. return [
  655. [
  656. "name" => "存货名称",
  657. "value" => $item['product_title'] ?? ''
  658. ],
  659. [
  660. "name" => "数量",
  661. "value" => $item['quantity'] ?? 0,
  662. ],
  663. [
  664. "name" => "主计量", // 修改这里,对应模板字段
  665. "value" => $item['unit_title'] ?? ''
  666. ],
  667. [
  668. "name" => "原币价税合计",
  669. "value" => $item['tax_amount'] ?? 0
  670. ]
  671. ];
  672. }, $userOrder['detail'] ?? []),
  673. JSON_UNESCAPED_UNICODE
  674. )
  675. ]
  676. ];
  677. return $formData;
  678. }
  679. private function typeFour($userOrder){
  680. if (empty($userOrder)) return [];
  681. $formData = [
  682. [
  683. "name" => "存货编码",
  684. "value" => $userOrder['code'] ?? ''
  685. ],
  686. [
  687. "name" => "存货名称",
  688. "value" => $userOrder['title'] ?? ''
  689. ],
  690. [
  691. "name" => "规格型号",
  692. "value" => $userOrder['size'] ?? ''
  693. ],
  694. [
  695. "name" => "计量单位组名",
  696. "value" => $userOrder['unit_group_code_title'] ?? ''
  697. ],
  698. [
  699. "name" => "主计量单位名",
  700. "value" => $userOrder['unit_code_title'] ?? ''
  701. ],
  702. [
  703. "name" => "生产企业",
  704. "value" => $userOrder['vendor_code_title'] ?? ''
  705. ],
  706. [
  707. "name" => "制单人",
  708. "value" => $userOrder['crt_name'] ?? ''
  709. ],
  710. ];
  711. return $formData;
  712. }
  713. private function typeFive($userOrder){
  714. if (empty($userOrder)) return [];
  715. $formData = [
  716. [
  717. "name" => "供应商编码",
  718. "value" => $userOrder['code'] ?? ''
  719. ],
  720. [
  721. "name" => "供应商名称",
  722. "value" => $userOrder['title'] ?? ''
  723. ],
  724. [
  725. "name" => "供应商简称",
  726. "value" => $userOrder['easy_title'] ?? ''
  727. ],
  728. [
  729. "name" => "制单人",
  730. "value" => $userOrder['crt_name'] ?? ''
  731. ],
  732. ];
  733. return $formData;
  734. }
  735. public function getTemplateFields($data)
  736. {
  737. $processCode = $data['code'] ?? "";
  738. if (empty($processCode)) {
  739. return [false, '模板编号 process_code 不能为空'];
  740. }
  741. [$ok, $tokenData] = $this->getAccessToken();
  742. if (! $ok) return [false, $tokenData];
  743. $accessToken = $tokenData['access_token'];
  744. // 注意这里是 GET,并且 processCode 是 query 参数
  745. $url = "https://api.dingtalk.com/v1.0/workflow/forms/schemas/processCodes?processCode={$processCode}";
  746. $resp = $this->curlOpen1($url, [
  747. 'request' => 'get',
  748. 'header' => [
  749. "Content-Type: application/json",
  750. "x-acs-dingtalk-access-token: {$accessToken}"
  751. ],
  752. ]);
  753. $res = json_decode($resp, true);
  754. if (isset($res['schemas'])) {
  755. return [true, $res['schemas']];
  756. } else {
  757. return [false, $res];
  758. }
  759. }
  760. protected function curlOpen1($url, $config = [])
  761. {
  762. $default = [
  763. 'post' => false,
  764. 'request' => 'get',
  765. 'header' => [],
  766. 'json' => null,
  767. 'timeout' => 30
  768. ];
  769. $arr = array_merge($default, $config);
  770. $ch = curl_init();
  771. curl_setopt($ch, CURLOPT_URL, $url);
  772. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  773. curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
  774. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  775. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  776. if (!empty($arr['header'])) {
  777. curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']);
  778. }
  779. if ($arr['post'] || $arr['request'] !== 'get') {
  780. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($arr['request']));
  781. if ($arr['json'] !== null) {
  782. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arr['json'], JSON_UNESCAPED_UNICODE));
  783. }
  784. }
  785. $result = curl_exec($ch);
  786. if ($result === false) {
  787. $err = curl_error($ch);
  788. curl_close($ch);
  789. return json_encode(['error' => $err]);
  790. }
  791. curl_close($ch);
  792. return $result;
  793. }
  794. }