UserJobs.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. namespace App\Jobs\OpenWork\Contact;
  3. use App\Servers\DB\DbService;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Models\OpenWork\Contact\User as Usermodel;
  10. use App\Models\OpenWork\Contact\UserDepart as UserDepartmodel;
  11. use Illuminate\Support\Facades\DB;
  12. use App\Facades\Servers\Logs\Log;
  13. use App\Facades\Servers\Wechat\OpenWork;
  14. use App\Models\OpenWork\Contact\UserActiveLog;
  15. use App\Models\OpenWork\job\Records;
  16. use App\Servers\OpenWork\External\ContactService;
  17. /**
  18. * 部门成员队列
  19. * @author 唐远望
  20. * @date 2025-04-09
  21. * @version 1.0
  22. */
  23. class UserJobs implements ShouldQueue
  24. {
  25. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  26. protected $userData;
  27. protected $Records;
  28. /**
  29. * Create a new job instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct(array $userData)
  34. {
  35. $this->userData = $userData;
  36. }
  37. public function getCorpId()
  38. {
  39. return $this->userData['authCorpId'] ?? null;
  40. }
  41. /**
  42. * Execute the job.
  43. *
  44. * @return void
  45. */
  46. public function handle()
  47. {
  48. $corpId = $this->userData['authCorpId'];
  49. (new DbService())->getConnectionNameByCorpId($corpId);
  50. try {
  51. $userData = $this->userData;
  52. // 创建任务记录
  53. $this->Records = Records::create([
  54. 'job_id' => $this->userData['authCorpId'] . '_UserJobs',
  55. 'name' => static::class,
  56. 'payload' => json_encode($this->userData),
  57. 'status' => 'processing',
  58. 'started_at' => now()
  59. ]);
  60. //处理明文用户ID
  61. $userData['userid'] = $this->handle_user_info($userData['authCorpId'], $userData['userid']);
  62. //查询用户是否存在
  63. $result = $this->get_one($userData['authCorpId'], $userData['userid']);
  64. if (!$result) {
  65. //执行添加用户
  66. $res = $this->add_user($userData);
  67. //更新用户授权状态
  68. // $this->update_wxwork_user_status($userData['authCorpId'], $userData['userid']);
  69. } else {
  70. //执行更新用户
  71. $wheres = [
  72. 'corpid' => $userData['authCorpId'],
  73. 'userid' => $userData['userid']
  74. ];
  75. //先初始开通信息,在通过激活记录更新
  76. $userData['is_open_interoperability'] ='0';
  77. $userData['is_open_foundation'] ='0';
  78. $userData['is_external_contact_permissions'] ='0';
  79. //更新用户信息
  80. $department = isset($userData['department']) ? $userData['department'] : [];
  81. $this->update_user($wheres, $userData, $department);
  82. //更新用户授权状态
  83. // $this->update_wxwork_user_status($userData['authCorpId'], $userData['userid']);
  84. }
  85. //更新激活历史记录
  86. $this->activate_information($userData['authCorpId'], $userData['userid']);
  87. //删除任务记录
  88. $this->Records->delete();
  89. } catch (\Exception $e) {
  90. // 更新任务状态为失败
  91. $this->Records->update([
  92. 'status' => 'failed',
  93. 'exception' => $e->getMessage(),
  94. 'started_at' => now()
  95. ]);
  96. Log::info('job_error', '部门成员队列失败-异常原因', ['error' => $e->getMessage()]);
  97. }
  98. }
  99. /**
  100. * 查询用户
  101. */
  102. public function get_one($authCorpId, $userid)
  103. {
  104. // 获取实例
  105. $data = Usermodel::where([['corpid', '=', $authCorpId], ['userid', '=', $userid]])->first(['name', 'userid', 'corpid']);
  106. // 返回结果
  107. if ($data) {
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }
  113. /**
  114. * 查询成员授权状态
  115. * @author 唐远望
  116. * @date 2025-04-13
  117. * @version 1.0
  118. * @param string $authCorpId 授权企业ID
  119. * @param string $userid 用户ID
  120. * @return bool
  121. */
  122. public function update_wxwork_user_status($authCorpId, $userid)
  123. {
  124. // 获取实例
  125. $app = OpenWork::getWork($authCorpId);
  126. //获取成员授权状态
  127. $result = $app->user->checkMemberAuth($userid);
  128. if (!$result) return json_send(['code' => 'error', 'msg' => '获取失败']);
  129. // 错误提示
  130. if ($result['errcode']) {
  131. Log::info('job_error', '获取成员授权状态失败', ['error' => $result]);
  132. return false;
  133. }
  134. // 获取实例
  135. $user_info = Usermodel::where([['corpid', '=', $authCorpId], ['userid', '=', $userid]])->first();
  136. // 返回结果
  137. if ($user_info) {
  138. $user_info->is_member_auth = $result['is_member_auth'];
  139. $user_info->save();
  140. }
  141. }
  142. /**
  143. * 添加用户
  144. * @author 唐远望
  145. * @date 2025-04-09
  146. * @version 1.0
  147. * @param array $userData 用户信息
  148. * @return bool
  149. */
  150. public function add_user($userData)
  151. {
  152. $nowdate = time();
  153. // 获取实例
  154. $user_info = [
  155. 'corpid' => $userData['authCorpId'],
  156. 'userid' => $userData['userid'],
  157. 'status' => $userData['status'], //激活状态: 1=已激活,2=已禁用,4=未激活,5=退出企业
  158. 'avatar' => $userData['avatar'],
  159. 'gender' => $userData['gender'],
  160. 'main_department' => isset($userData['main_department']) ? $userData['main_department'] : 0,
  161. 'insert_time' => $nowdate,
  162. 'update_time' => $nowdate,
  163. ];
  164. DB::connection('company')->beginTransaction();
  165. try {
  166. //添加部门关系信息
  167. $department_list = isset($userData['department_list']) ? $userData['department_list'] : [];
  168. if (count($department_list) > 0) {
  169. foreach ($department_list as $key => $value) {
  170. $user_depart_info = [
  171. 'corpid' => $userData['authCorpId'],
  172. 'userid' => $userData['userid'],
  173. 'department_id' => $value
  174. ];
  175. $user_depart_info_log = UserDepartmodel::where([['corpid', '=', $userData['authCorpId']], ['userid', '=', $userData['userid']], ['department_id', '=', $value]])->first();
  176. if (!$user_depart_info_log) {
  177. UserDepartmodel::insert($user_depart_info);
  178. }
  179. }
  180. }
  181. //添加用户
  182. $user_id = Usermodel::insertGetId($user_info);
  183. DB::connection('company')->commit();
  184. // 成功处理...
  185. return true;
  186. } catch (\Exception $e) {
  187. DB::connection('company')->rollBack();
  188. // 错误处理...
  189. return false;
  190. }
  191. }
  192. /**
  193. * 处理明文用户信息
  194. * @author 唐远望
  195. * @date 2025-05-13
  196. * @version 1.0
  197. * @param array $userData 用户信息
  198. */
  199. public function handle_user_info($corpid, $user_id)
  200. {
  201. $user_config_list = (new ContactService())->get_external_contact_batchget_user_list_by_data($corpid, [$user_id]);
  202. if (!empty($user_config_list)) {
  203. return $user_config_list[0];
  204. } else {
  205. return $user_id;
  206. }
  207. }
  208. /**
  209. * 更新用户信息
  210. * @author 唐远望
  211. * @date 2025-04-09
  212. * @version 1.0
  213. * @param array $userData 用户信息
  214. * @return bool
  215. */
  216. public function update_user($wheres, $userData, $department_list)
  217. {
  218. $nowdate = time();
  219. // 获取实例
  220. $user_info = [
  221. 'status' => $userData['status'], //激活状态: 1=已激活,2=已禁用,4=未激活,5=退出企业
  222. 'update_time' => $nowdate,
  223. 'avatar' => $userData['avatar'],
  224. 'gender' => $userData['gender'],
  225. 'main_department' => isset($userData['main_department']) ? $userData['main_department'] : 0,
  226. 'is_open_interoperability'=>isset($userData['is_open_interoperability']) ? $userData['is_open_interoperability'] : 0,
  227. 'is_open_foundation' =>isset($userData['is_open_foundation']) ? $userData['is_open_foundation'] : 0,
  228. 'is_external_contact_permissions'=> isset($userData['is_external_contact_permissions']) ? $userData['is_external_contact_permissions'] : 0,
  229. ];
  230. DB::connection('company')->beginTransaction();
  231. try {
  232. //添加部门关系信息
  233. if (count($department_list) > 0) {
  234. foreach ($department_list as $key => $value) {
  235. //聚合查询部门关系信息
  236. $user_depart_info_all_log = UserDepartmodel::where([['corpid', '=', $userData['authCorpId']], ['userid', '=', $userData['userid']]])->pluck('department_id')
  237. ->implode(',');
  238. $user_depart_info_all_log_arr = explode(',', $user_depart_info_all_log);
  239. //如果部门数据多余服务端
  240. if ($user_depart_info_all_log && $user_depart_info_all_log_arr > $department_list) {
  241. // 筛选出 $user_depart_info_all_log 中有但 $department_list 中没有的元素
  242. $diff = array_diff($user_depart_info_all_log_arr, $department_list);
  243. sort($diff); //重置下标
  244. UserDepartmodel::whereIn('department_id', $diff)->where([['corpid', '=', $userData['authCorpId']], ['userid', '=', $userData['userid']]])->delete();
  245. } else if ($user_depart_info_all_log && $user_depart_info_all_log_arr < $department_list) {
  246. // 筛选出 $department_list 中有但 $user_depart_info_all_log 中没有的元素
  247. $diff = array_diff($department_list, $user_depart_info_all_log_arr);
  248. sort($diff); //重置下标
  249. $user_depart_info_li = [];
  250. foreach ($diff as $k => $v) {
  251. $user_depart_info_li[$k] = [
  252. 'corpid' => $userData['authCorpId'],
  253. 'userid' => $userData['userid'],
  254. 'department_id' => $value
  255. ];
  256. }
  257. UserDepartmodel::insert($user_depart_info_li);
  258. } else if (!$user_depart_info_all_log) {
  259. // 新增关系
  260. $user_depart_info_li = [];
  261. foreach ($department_list as $k => $v) {
  262. $user_depart_info_li[$k] = [
  263. 'corpid' => $userData['authCorpId'],
  264. 'userid' => $userData['userid'],
  265. 'department_id' => $value
  266. ];
  267. }
  268. UserDepartmodel::insert($user_depart_info_li);
  269. }
  270. }
  271. }
  272. //添加用户
  273. Usermodel::where($wheres)->update($user_info);
  274. //DB::commit();
  275. DB::connection('company')->commit();
  276. // 成功处理...
  277. return true;
  278. // 成功处理...
  279. } catch (\Exception $e) {
  280. DB::connection('company')->rollBack();
  281. // 错误处理...
  282. Log::info('job_error', 'message', ['error' => $e->getMessage()]);
  283. return false;
  284. }
  285. }
  286. /**
  287. * 获取成员的激活详情
  288. * @author 唐远望
  289. * @version 1.0
  290. * @date 2025-04-22
  291. * @param string $authCorpId 授权企业ID
  292. * @param string $userid 用户ID
  293. * @return bool
  294. */
  295. public function activate_information($authCorpId, $userid)
  296. {
  297. // 获取实例
  298. $work = OpenWork::getApp();
  299. //获取成员激活详情
  300. $result = $work->license_account->getActiveAccountInfo($authCorpId, $userid);
  301. if (!$result) return json_send(['code' => 'error', 'msg' => '获取失败']);
  302. // 错误提示
  303. if ($result['errcode']) {
  304. Log::info('job_error', '获取成员激活详情失败', ['error' => $result]);
  305. return true;
  306. }
  307. //存在激活记录
  308. $active_info_list = $result['active_info_list'];
  309. if (count($active_info_list) < 1) {
  310. //如果本地存在激活记录则删除
  311. $active_info_log = UserActiveLog::where([['corpid', '=', $authCorpId], ['userid', '=', $userid]])->first();
  312. if ($active_info_log) UserActiveLog::where([['corpid', '=', $authCorpId], ['userid', '=', $userid]])->delete();
  313. return true;
  314. }
  315. DB::connection('company')->beginTransaction();
  316. try {
  317. $is_open_interoperability = 0;
  318. $is_open_foundation = 0;
  319. foreach ($active_info_list as $key => $value) {
  320. $active_info = [
  321. 'corpid' => $authCorpId,
  322. 'userid' => $userid,
  323. 'type' => $value['type'],
  324. 'active_code' => $value['active_code'],
  325. 'account_active_time' => $value['active_time'],
  326. 'account_expire_time' => $value['expire_time'],
  327. 'insert_time' => time(),
  328. ];
  329. $active_info_log = UserActiveLog::where([['corpid', '=', $authCorpId], ['userid', '=', $userid], ['active_code', '=', $value['active_code']]])->first();
  330. if (!$active_info_log) {
  331. UserActiveLog::insert($active_info);
  332. } else {
  333. UserActiveLog::where([['corpid', '=', $authCorpId], ['userid', '=', $userid], ['active_code', '=', $value['active_code']]])->update([
  334. 'account_active_time' => $value['active_time'],
  335. 'account_expire_time' => $value['expire_time']
  336. ]);
  337. }
  338. //基础账户开通校验
  339. if ($value['type'] == '1' && $value['expire_time'] > time()) {
  340. $is_open_foundation = 1;
  341. }
  342. //互通账户开通校验
  343. if ($value['type'] == '2' && $value['expire_time'] > time()) {
  344. $is_open_interoperability = 1;
  345. }
  346. }
  347. $this->update_user_account_status($authCorpId, $userid, $is_open_interoperability, $is_open_foundation);
  348. DB::connection('company')->commit();
  349. // 成功处理...
  350. } catch (\Exception $e) {
  351. DB::connection('company')->rollBack();
  352. // 错误处理...
  353. Log::info('job_error', '获取成员激活详情失败', [$e->getMessage()]);
  354. }
  355. return true;
  356. }
  357. /**
  358. * 更新用户账号开通状态
  359. * @author 唐远望
  360. * @version 1.0
  361. * @date 2025-04-22
  362. * @param string $authCorpId 授权企业ID
  363. * @param string $userid 用户ID
  364. * @param int $is_open_interoperability 互通开通状态
  365. * @param int $is_open_foundation 基础开通状态
  366. * @return bool
  367. */
  368. public function update_user_account_status($authCorpId, $userid, $is_open_interoperability = 0, $is_open_foundation = 0)
  369. {
  370. DB::connection('company')->beginTransaction();
  371. try {
  372. $user_info = Usermodel::where([['corpid', '=', $authCorpId], ['userid', '=', $userid]])->first();
  373. if ($user_info) {
  374. $user_info->is_open_interoperability = $is_open_interoperability;
  375. $user_info->is_open_foundation = $is_open_foundation;
  376. $user_info->save();
  377. }
  378. DB::connection('company')->commit();
  379. // 成功处理...
  380. } catch (\Exception $e) {
  381. DB::connection('company')->rollBack();
  382. // 错误处理...
  383. }
  384. return true;
  385. }
  386. public function failed(\Throwable $exception)
  387. {
  388. Log::info('job_error', 'UserJobs彻底失败', ['data' => $this->userData, 'error' => $exception]);
  389. // 失败处理...
  390. if ($this->Records) {
  391. $this->Records->delete();
  392. }
  393. }
  394. }