ChannelActiveCodeEditJobs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace App\Jobs\OpenWork\Contactway;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use App\Servers\DB\DbService;
  9. use App\Models\OpenWork\job\Records;
  10. use App\Facades\Servers\Logs\Log;
  11. use App\Models\OpenWork\Contactway\Qrcode as QrcodeModel;
  12. use App\Models\OpenWork\Contactway\QrcodeUser;
  13. use App\Models\OpenWork\Contactway\QrcodeWelcome;
  14. use App\Facades\Servers\Wechat\OpenWork;
  15. use Illuminate\Support\Facades\DB;
  16. /**
  17. * SCRM 编辑渠道活码
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2025-08-15
  21. */
  22. class ChannelActiveCodeEditJobs implements ShouldQueue
  23. {
  24. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  25. protected $message_data;
  26. protected $Records;
  27. /**
  28. * Create a new job instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(array $message_data)
  33. {
  34. $this->message_data = $message_data;
  35. }
  36. /**
  37. * Execute the job.
  38. *
  39. * @return void
  40. */
  41. public function handle()
  42. {
  43. try {
  44. $companyId = $this->message_data['company_id'];
  45. (new DbService())->getConnectionNameByCompanyId($companyId);
  46. // 创建任务记录
  47. $this->Records = Records::create([
  48. 'job_id' => $companyId . '_ChannelActiveCodeEditJobs',
  49. 'name' => static::class,
  50. 'payload' => json_encode($this->message_data),
  51. 'status' => 'processing',
  52. 'started_at' => now()
  53. ]);
  54. $result = $this->edit($this->message_data);
  55. if (isset($result['code']) && $result['code'] == 'error') {
  56. Log::info('job_error', '渠道活码编辑任务失败', ['data' => $this->message_data, 'error' =>$result]);
  57. }
  58. //删除任务记录
  59. $this->Records->delete();
  60. } catch (\Exception $e) {
  61. // 失败处理...
  62. if ($this->Records) {
  63. $this->Records->delete();
  64. }
  65. Log::info('job_error', '渠道活码编辑任务失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  66. }
  67. }
  68. public function failed(\Throwable $exception)
  69. {
  70. Log::info('job_error', '渠道活码编辑任务完全失败', ['data' => $this->message_data, 'error' => $exception]);
  71. // 失败处理...
  72. if ($this->Records) {
  73. $this->Records->delete();
  74. }
  75. }
  76. /**
  77. * 编辑配置 /openwork/contactway_qrcode/edit
  78. *
  79. *
  80. */
  81. public function edit($message_data)
  82. {
  83. $Model = new QrcodeModel();
  84. $qrcodeUserModel = new QrcodeUser();
  85. $qrcodeWelcomeModel = new QrcodeWelcome();
  86. // 接收参数
  87. $id = $message_data['id'];
  88. $companyId = $message_data['company_id'];
  89. // 获取实例
  90. $oldData = $Model->query()->find($id);
  91. // 获取失败
  92. if (!$oldData) return ['code' => 'error', 'msg' => '查无数据'];
  93. // 修改
  94. $data['remark'] = $message_data['remark'];
  95. $data['logo'] = empty($message_data['logo']) ? '' : $message_data['logo'];
  96. //活码分组,有传就修改,没传不修改
  97. if (array_key_exists('group_id', $message_data)) {
  98. $data['group_id'] = $message_data['group_id']; // 分组ID
  99. }
  100. $old_tags = explode(',', $oldData['tags']);
  101. $new_tags ='';
  102. //标签,有传就修改,没传不修改
  103. // if (array_key_exists('tags', $message_data)) {
  104. // $data['tags'] = $message_data['tags'];
  105. // $data['tags'] = $data['tags'] ? explode(',', $data['tags']) : [];
  106. // // 转字符串
  107. // $data['tags'] = implode(',', $data['tags']);
  108. // }
  109. if (array_key_exists('del_tags', $message_data) && array_key_exists('add_tags', $message_data)) {
  110. if ($message_data['del_tags'] != '' && $message_data['add_tags'] != '') {
  111. //如果删除标签在$old_tags旧标签中,则移除
  112. $del_tags = explode(',', $message_data['del_tags']);
  113. $tags_array = array_intersect($old_tags, $del_tags); //交集
  114. $old_tags = array_diff($old_tags, $tags_array); //差集
  115. //如果新增标签已经在$old_tags标签中,则不再添加
  116. $add_tags = explode(',', $message_data['add_tags']);
  117. //如果删除的标签在新增标签中,则移除
  118. $tags_array = array_intersect($add_tags, $del_tags); //交集
  119. $add_tags = array_diff($add_tags, $tags_array); //差集
  120. $tags_array = array_merge($add_tags, $old_tags); //合并
  121. $tags_array = array_filter($tags_array); //去除空值
  122. $new_tags = implode(',', array_unique($tags_array)); //去重
  123. }
  124. } else if (array_key_exists('add_tags', $message_data)) { //新增标签业务处理
  125. if ($message_data['add_tags'] != '') {
  126. //如果新增标签已经在$old_tags旧标签中,则不再添加
  127. $add_tags = explode(',', $message_data['add_tags']);
  128. $tags_array = array_merge($add_tags, $old_tags); //合并
  129. $tags_array = array_filter($tags_array); //去除空值
  130. $new_tags = implode(',', array_unique($tags_array)); //去重
  131. }
  132. } else if (array_key_exists('del_tags', $message_data)) { //删除标签业务处理
  133. if ($message_data['del_tags'] != '') {
  134. //如果删除标签在$old_tags旧标签中,则移除
  135. $del_tags = explode(',', $message_data['del_tags']);
  136. $tags_array = array_intersect($old_tags, $del_tags); //交集
  137. $tags_array = array_filter($tags_array); //去除空值
  138. $new_tags = implode(',', array_diff($old_tags, $tags_array)); //差集
  139. }
  140. }
  141. if($new_tags != ''){
  142. $data['tags'] = $new_tags;
  143. }
  144. //接待人员,有传就修改,不传不修改
  145. if (array_key_exists('user', $message_data)) {
  146. $config['user'] = $message_data['user'];
  147. $config['user'] = $config['user'] ? explode(',', $config['user']) : [];
  148. }
  149. if (array_key_exists('show_name', $message_data)) {
  150. $data['show_name'] = $message_data['show_name']; // 回显
  151. }
  152. if (array_key_exists('show_file_list', $message_data)) {
  153. $data['show_file_list'] = $message_data['show_file_list']; // 回显
  154. }
  155. //是否无需验证,有传就修改,不传不修改
  156. if (array_key_exists('skip_verify', $message_data)) {
  157. $config['skip_verify'] = $message_data['skip_verify'] ? true : false; // 外部客户添加时是否无需验证
  158. }
  159. $config['remark'] = $data['remark'];
  160. // 企业联系方式的配置id
  161. $configId = $oldData['config_id'];
  162. // 组合附加参数
  163. //$config['state'] = $message_data['state','t1&'.$oldData['id']);
  164. $config['state'] = 't1&' . $oldData['id'];
  165. // 接收参数
  166. $data['corpid'] = $oldData['corpid'];
  167. // 开始事务
  168. DB::connection('company')->beginTransaction();
  169. // 尝试执行
  170. try {
  171. // 实例
  172. $work = OpenWork::getWork($data['corpid']);
  173. // 获取token
  174. $result = $work->contact_way->update($configId, $config);
  175. // 接口返回失败的话
  176. if (!$result) {
  177. // 回滚事务
  178. DB::connection('company')->rollBack();
  179. // 提醒
  180. return ['code' => 'error', 'msg' => '企微接口返回失败'];
  181. }
  182. // 接口返回失败的话
  183. if ($result['errcode']) {
  184. // 回滚事务
  185. DB::connection('company')->rollBack();
  186. // 提醒
  187. return ['code' => 'error', 'msg' => OpenWork::getErrmsg($result['errcode'])];
  188. }
  189. // 修改数据
  190. $result = $Model->edit($id, $data);
  191. // 接口返回失败的话
  192. if (!$result) {
  193. // 回滚事务
  194. DB::connection('company')->rollBack();
  195. // 提醒
  196. return ['code' => 'error', 'msg' => '编辑失败', 'data' => $data];
  197. }
  198. $userIds = $qrcodeUserModel->query()->where([['qrcode_id', '=', $id]])->pluck('user_id')->toArray();
  199. if (array_key_exists('user', $message_data)) {
  200. $intersectUserIds = array_intersect($config['user'], $userIds);
  201. foreach ($userIds as $userId) {
  202. if (!in_array($userId, $intersectUserIds)) {
  203. $qrcodeUserModel->remove($id, $userId); //删除
  204. }
  205. }
  206. foreach ($config['user'] as $value) {
  207. if (!in_array($value, $intersectUserIds)) {
  208. $insertQrcodeUserData['qrcode_id'] = $id;
  209. $insertQrcodeUserData['user_id'] = $value;
  210. $qrcodeUserModel->add($insertQrcodeUserData);
  211. }
  212. }
  213. }
  214. $welcomeData['corpid'] = $message_data['corpid'];
  215. $welcomeData['qrcodeid'] = $id;
  216. $welcomeData['update_time'] = time();
  217. //附件内容,有传就修改,不传不修改
  218. if (array_key_exists('attachments', $message_data)) {
  219. $attachments = $message_data['attachments']; //多附件json数组参数,最多9个
  220. $welcomeData['attachments'] = json_encode($attachments, JSON_UNESCAPED_UNICODE);
  221. }
  222. //欢迎语内容,有传就修改,不传不修改
  223. if (array_key_exists('content', $message_data)) {
  224. $welcomeData['content'] = $message_data['content']; //欢迎语内容
  225. }
  226. $qrcodeWelcomeModel->query()->where('qrcodeid', $id)->update($welcomeData);
  227. // 提交事务
  228. DB::connection('company')->commit();
  229. // 获取二维码保存到本地
  230. if ($oldData['qr_code'] && $data['logo'] != $oldData['logo']) $Model->editQrcodeLogo($oldData['qr_code'], $companyId, $data['logo']);
  231. // 表单令牌
  232. return ['code' => 'success', 'msg' => '编辑成功', 'data' => $data];
  233. } catch (\Throwable $th) {
  234. // 回滚事务
  235. DB::connection('company')->rollBack();
  236. // 提醒
  237. return ['code' => 'error', 'msg' => '编辑异常,请重试', 'data' => ['error' => $th->getMessage()]];
  238. }
  239. // 返回结果
  240. return ['code' => 'success', 'msg' => '获取', 'data' => $oldData];
  241. }
  242. }