DingService.php 33 KB

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