ContactWayQrCode.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\traits\service;
  12. use app\dao\BaseDao;
  13. use app\services\work\WorkMediaServices;
  14. use crmeb\services\wechat\ErrorMessage;
  15. use crmeb\services\wechat\WechatResponse;
  16. use crmeb\services\wechat\Work;
  17. use think\exception\ValidateException;
  18. /**
  19. * 客户联系
  20. * Trait ContactWayQrCode
  21. * @package crmeb\traits\service
  22. * @property BaseDao $dao
  23. */
  24. trait ContactWayQrCode
  25. {
  26. /**
  27. * 检测欢迎语字段
  28. * @param array $welcomeWords
  29. * @param int $type
  30. */
  31. public function checkWelcome(array $welcomeWords, int $type)
  32. {
  33. if (1 === $type) {
  34. return;
  35. }
  36. if (empty($welcomeWords['text']['content']) && empty($welcomeWords['attachments'])) {
  37. throw new ValidateException('请填写欢迎语');
  38. }
  39. if (!empty($welcomeWords['text']['content']) && strlen($welcomeWords['text']['content']) > 3000) {
  40. throw new ValidateException('内容不能超过4000字');
  41. }
  42. foreach ($welcomeWords['attachments'] as $item) {
  43. switch ($item['msgtype']) {
  44. case 'image':
  45. if (empty($item['image']['pic_url'])) {
  46. throw new ValidateException('请上传欢迎语图片');
  47. }
  48. break;
  49. case 'link':
  50. if (empty($item['link']['title'])) {
  51. throw new ValidateException('请填写连接标题');
  52. }
  53. if (empty($item['link']['url'])) {
  54. throw new ValidateException('请填写连接地址');
  55. }
  56. break;
  57. case 'miniprogram':
  58. if (empty($item['miniprogram']['title'])) {
  59. throw new ValidateException('请填写小程序消息标题');
  60. }
  61. if (empty($item['miniprogram']['appid'])) {
  62. throw new ValidateException('请填写小程序Appid');
  63. }
  64. if (empty($item['miniprogram']['page'])) {
  65. throw new ValidateException('请填写小程序页面路径');
  66. }
  67. if (empty($item['miniprogram']['pic_url'])) {
  68. throw new ValidateException('请选择小程序消息封面图');
  69. }
  70. break;
  71. case 'video':
  72. if (empty($item['video']['url'])) {
  73. throw new ValidateException('请上传视频文件');
  74. }
  75. break;
  76. case 'file':
  77. if (empty($item['file']['url'])) {
  78. throw new ValidateException('请上传文件');
  79. }
  80. break;
  81. }
  82. }
  83. }
  84. /**
  85. * 执行创建或者修改【联系我】成员情况
  86. * @param int $channleId
  87. * @param array $userIds
  88. * @param bool $skipVerify
  89. * @param string|null $wxConfigId
  90. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  91. * @throws \GuzzleHttp\Exception\GuzzleException
  92. */
  93. public function handleQrCode(int $channleId, array $userIds, bool $skipVerify = true, string $wxConfigId = null)
  94. {
  95. if (!$wxConfigId) {
  96. $qrCodeRes = Work::createQrCode($channleId, $userIds, $skipVerify);
  97. } else {
  98. $qrCodeRes = Work::updateQrCode($channleId, $userIds, $wxConfigId, $skipVerify);
  99. }
  100. if ($qrCodeRes['errcode'] !== 0) {
  101. throw new ValidateException(ErrorMessage::getWorkMessage($qrCodeRes['errcode'], $qrCodeRes['errmsg'] ?? '生成企业渠道码失败'));
  102. }
  103. if (!$wxConfigId) {
  104. $this->dao->update($channleId, [
  105. 'qrcode_url' => $qrCodeRes['qr_code'],
  106. 'config_id' => $qrCodeRes['config_id']
  107. ]);
  108. }
  109. }
  110. /**
  111. * 创建企业微信群发
  112. * @param array $externalUserid
  113. * @param array $attachments
  114. * @param string $chatType
  115. * @param string|null $sender
  116. * @return WechatResponse
  117. */
  118. public function sendMsgTemplate(array $externalUserid, array $attachments, string $chatType = 'single', string $sender = null)
  119. {
  120. $msg = [
  121. 'chat_type' => $chatType,
  122. 'external_userid' => $externalUserid,
  123. ];
  124. if ('group' == $chatType) {
  125. if (!$sender) {
  126. throw new ValidateException('群发消息成员userid为必须填写');
  127. }
  128. }
  129. if ($sender) {
  130. $msg['sender'] = $sender;
  131. }
  132. if (empty($msg['external_userid'])) {
  133. unset($msg['external_userid']);
  134. }
  135. //转换欢迎语当中的图片为素材库中
  136. /** @var WorkMediaServices $mediaService */
  137. $mediaService = app()->make(WorkMediaServices::class);
  138. $attachments = $mediaService->resolvingWelcome($attachments);
  139. $msg = array_merge($msg, $attachments);
  140. return Work::addMsgTemplate($msg);
  141. }
  142. /**
  143. * 创建发送朋友圈
  144. * @param array $attachments
  145. * @param array $userIds
  146. * @param array $tag
  147. * @return WechatResponse
  148. */
  149. public function addMomentTask(array $attachments, array $userIds = [], array $tag = [])
  150. {
  151. //转换欢迎语当中的图片为素材库中
  152. /** @var WorkMediaServices $mediaService */
  153. $mediaService = app()->make(WorkMediaServices::class);
  154. $data = $mediaService->resolvingWelcome($attachments, 1);
  155. if ($userIds) {
  156. $data['visible_range']['sender_list']['user_list'] = $userIds;
  157. }
  158. if ($tag) {
  159. $data['visible_range']['external_contact_list']['tag_list'] = $tag;
  160. }
  161. return Work::addMomentTask($data);
  162. }
  163. }