ChannelActiveCodeCreateJobs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. use App\Models\OpenWork\External\Tag;
  17. /**
  18. * SCRM 创建渠道活码
  19. * @author 唐远望
  20. * @version 1.0
  21. * @date 2025-08-15
  22. */
  23. class ChannelActiveCodeCreateJobs implements ShouldQueue
  24. {
  25. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  26. protected $message_data;
  27. protected $Records;
  28. /**
  29. * Create a new job instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct(array $message_data)
  34. {
  35. $this->message_data = $message_data;
  36. }
  37. /**
  38. * Execute the job.
  39. *
  40. * @return void
  41. */
  42. public function handle()
  43. {
  44. try {
  45. $companyId = $this->message_data['company_id'];
  46. (new DbService())->getConnectionNameByCompanyId($companyId);
  47. // 创建任务记录
  48. $this->Records = Records::create([
  49. 'job_id' => $companyId . '_ChannelActiveCodeCreateJobs',
  50. 'name' => static::class,
  51. 'payload' => json_encode($this->message_data),
  52. 'status' => 'processing',
  53. 'started_at' => now()
  54. ]);
  55. $result = $this->add($this->message_data);
  56. if (isset($result['code']) && $result['code'] == 'error') {
  57. Log::info('job_error', '渠道活码创建任务失败', ['error' => $result]);
  58. }
  59. //删除任务记录
  60. $this->Records->delete();
  61. } catch (\Exception $e) {
  62. // 失败处理...
  63. if ($this->Records) {
  64. $this->Records->delete();
  65. }
  66. Log::info('job_error', '渠道活码创建任务失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  67. }
  68. }
  69. public function failed(\Throwable $exception)
  70. {
  71. Log::info('job_error', '渠道活码创建任务完全失败', ['data' => $this->message_data, 'error' => $exception]);
  72. $this->Records->delete();
  73. }
  74. /**
  75. * 新增配置 /openwork/contactway_qrcode/add
  76. *
  77. *
  78. */
  79. public function add($message_data)
  80. {
  81. $Model = new QrcodeModel();
  82. $qrcodeUserModel = new QrcodeUser();
  83. $qrcodeWelcomeModel = new QrcodeWelcome();
  84. // 接收参数
  85. $companyId = $message_data['company_id'];
  86. $data['corpid'] = $message_data['corpid'];
  87. $data['remark'] = $message_data['remark'];
  88. $data['logo'] = empty($message_data['logo']) ? '' : $message_data['logo'];
  89. $data['group_id'] = $message_data['group_id']; // 分组ID
  90. $data['tags'] = $message_data['tags'];
  91. $data['show_file_list'] = $message_data['show_file_list']; // 回显
  92. $data['tags'] = $data['tags'] ? explode(',', $data['tags']) : [];
  93. $data['is_exclusive'] = $message_data['is_exclusive'];
  94. // 组装联系我数据
  95. $type = $message_data['type']; // 联系方式类型,1-单人, 2-多人
  96. $scene = $message_data['scene']; // 场景,1-在小程序中联系,2-通过二维码联系
  97. $data['type'] = $type; // 联系方式类型,1-单人, 2-多人 写入本地数据表
  98. $config['user'] = $message_data['user'];
  99. $config['user'] = $config['user'] ? explode(',', $config['user']) : [];
  100. $config['skip_verify'] = $message_data['skip_verify'] ? true : false; // 外部客户添加时是否无需验证
  101. $config['is_exclusive'] = $message_data['is_exclusive'] ? true : false; // 是否开启同一外部企业客户只能添加同一个员工
  102. $config['remark'] = $data['remark'];
  103. // 如果用户超过100个
  104. if (count($config['user']) > 100) return ['code' => 'error', 'msg' => '每个联系方式最多配置100个使用成员'];
  105. // 标签超过100个
  106. if (count($data['tags']) > 100) return ['code' => 'error', 'msg' => '标签超过100个,请减少标签数量'];
  107. // 转字符串
  108. $data['tags'] = implode(',', $data['tags']);
  109. //标签id如果不在标签表则报错提示
  110. if (!empty($data['tags'])) {
  111. $tempTagIds = explode(',', $data['tags']);
  112. foreach ($tempTagIds as $tempTagId) {
  113. if (! (new Tag())->query()->where('tag_id', $tempTagId)->exists()) {
  114. return ['code' => 'error', 'msg' => '标签id=' . $tempTagId . '不存在,请检查!'];
  115. }
  116. }
  117. }
  118. //$data['user'] = implode(',',$config['user']); //这个数据放到openwork_contactway_qrcode_user表中处理 2025-03-31 modify by zengwei
  119. //渠道活码欢迎语
  120. $attachments = $message_data['attachments']; //多附件json数组参数,最多9个
  121. if (!empty($attachments)) {
  122. //$attachments = json_decode($attachments,true);
  123. //如果attachments 不为空则需要判断attachments的内容和数量(不超过9个)
  124. $attachmentsCount = count($attachments);
  125. if ($attachmentsCount > 9) {
  126. return ['code' => 'error', 'msg' => '欢迎附件超过了9个,请减少数量'];
  127. }
  128. $returnMsg = $this->verifyAttachmentsData($attachments);
  129. if (!empty($returnMsg)) {
  130. return ['code' => 'error', 'msg' => $returnMsg];
  131. }
  132. }
  133. // 开始事务
  134. DB::connection('company')->beginTransaction();
  135. // 尝试执行
  136. try {
  137. // 获取实例
  138. $data['id'] = $Model->add($data);
  139. // 获取失败
  140. if (!$data['id']) return ['code' => 'error', 'msg' => '创建失败'];
  141. //增加一个Qrcode和user的一对多的表
  142. if (!empty($config['user'])) {
  143. foreach ($config['user'] as $item) {
  144. $qrcodeUserData = [
  145. 'qrcode_id' => $data['id'],
  146. 'user_id' => $item,
  147. ];
  148. $insertQrcodeUserData[] = $qrcodeUserData;
  149. }
  150. if (!empty($insertQrcodeUserData)) {
  151. $qrcodeUserModel->batchCreate($insertQrcodeUserData);
  152. }
  153. }
  154. //欢迎语的数据组装
  155. $welcomeData['content'] = $message_data['content'];
  156. $welcomeData['corpid'] = $message_data['corpid'];
  157. $welcomeData['qrcodeid'] = $data['id'];
  158. //dd($welcomeData['content']);
  159. $welcomeData['attachments'] = json_encode($attachments, JSON_UNESCAPED_UNICODE);
  160. $qrcodeWelcomeModel->add($welcomeData);
  161. // 组合附加参数
  162. //$config['state'] = $message_data['state','t1='.$data['id']);
  163. $config['state'] = 't1&' . $data['id']; // 最多30个字符
  164. // 实例
  165. $work = OpenWork::getWork($data['corpid']);
  166. // 获取token
  167. $result = $work->contact_way->create($type, $scene, $config);
  168. // 接口返回失败的话
  169. if (!$result) {
  170. // 回滚事务
  171. DB::connection('company')->rollBack();
  172. // 提醒
  173. return ['code' => 'error', 'msg' => '企微接口返回失败'];
  174. }
  175. // 接口返回失败的话
  176. if ($result['errcode']) {
  177. // 回滚事务
  178. DB::connection('company')->rollBack();
  179. // 提醒
  180. return ['code' => 'error', 'msg' => OpenWork::getErrmsg($result['errcode'])];
  181. }
  182. // 二维码信息
  183. $data['qr_code'] = $result['qr_code'];
  184. // 返回结果
  185. $data['config_id'] = $result['config_id'];
  186. // 修改数据
  187. $result = $Model->edit($data['id'], $data);
  188. // 接口返回失败的话
  189. if (!$result) {
  190. // 回滚事务
  191. DB::connection('company')->rollBack();
  192. // 提醒
  193. return ['code' => 'error', 'msg' => '创建失败', 'data' => $data];
  194. }
  195. // 提交事务
  196. DB::connection('company')->commit();
  197. // 获取二维码保存到本地
  198. if ($data['qr_code']) $Model->editQrcodeLogo($data['qr_code'], $companyId, $data['logo']);
  199. // 表单令牌
  200. return ['code' => 'success', 'msg' => '创建成功', 'data' => $data];
  201. } catch (\Throwable $th) {
  202. // 回滚事务
  203. DB::connection('company')->rollBack();
  204. // 提醒
  205. return ['code' => 'error', 'msg' => '创建异常,请重试', 'data' => ['error' => $th->getMessage()]];
  206. }
  207. }
  208. public function verifyAttachmentsData($attachments)
  209. {
  210. $msgTypes = ['image', 'link', 'miniprogram', 'video', 'file']; //msgType数组
  211. foreach ($attachments as $attachment) {
  212. if (isset($attachment['msgtype'])) {
  213. if (!in_array($attachment['msgtype'], $msgTypes)) {
  214. return '消息类型不正确,请检查!';
  215. }
  216. if ($attachment['msgtype'] == 'image') {
  217. if (empty($attachment['image']['media_id']) && empty($attachment['image']['pic_url'])) {
  218. return '图片类型的附件,media_id和pic_url参数的值不能同时为空!';
  219. }
  220. } elseif ($attachment['msgtype'] == 'link') {
  221. if (empty($attachment['link']['title']) || empty($attachment['link']['url'])) {
  222. return '图文消息类型的附件,title和url参数的值不能为空!';
  223. }
  224. } elseif ($attachment['msgtype'] == 'miniprogram') {
  225. if (
  226. empty($attachment['miniprogram']['title']) || empty($attachment['miniprogram']['pic_media_id']) ||
  227. empty($attachment['miniprogram']['appid']) || empty($attachment['miniprogram']['page'])
  228. ) {
  229. return '小程序类型的附件,title、pic_media_id、appid和page参数的值不能为空!';
  230. }
  231. } elseif ($attachment['msgtype'] == 'video') {
  232. if (empty($attachment['video']['media_id'])) {
  233. return '视频类型的附件,media_id参数的值不能为空!';
  234. }
  235. } elseif ($attachment['msgtype'] == 'file') {
  236. if (empty($attachment['file']['media_id'])) {
  237. return '文件类型的附件,media_id参数的值不能为空!';
  238. }
  239. }
  240. }
  241. }
  242. }
  243. }