WechatQrcodeServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace app\services\wechat;
  3. use app\dao\wechat\WechatQrcodeDao;
  4. use app\services\BaseServices;
  5. use app\services\other\QrcodeServices;
  6. use app\services\system\attachment\SystemAttachmentServices;
  7. use app\services\user\UserLabelRelationServices;
  8. use app\services\user\UserLabelServices;
  9. use app\services\user\UserServices;
  10. use crmeb\exceptions\AdminException;
  11. use crmeb\services\UploadService;
  12. use crmeb\services\WechatService;
  13. class WechatQrcodeServices extends BaseServices
  14. {
  15. /**
  16. * WechatQrcodeServices constructor.
  17. * @param WechatQrcodeDao $dao
  18. */
  19. public function __construct(WechatQrcodeDao $dao)
  20. {
  21. $this->dao = $dao;
  22. }
  23. /**
  24. * 获取渠道码列表
  25. * @param $where
  26. * @return array
  27. */
  28. public function qrcodeList($where)
  29. {
  30. /** @var UserLabelServices $userLabel */
  31. $userLabel = app()->make(UserLabelServices::class);
  32. [$page, $limit] = $this->getPageValue();
  33. $list = $this->dao->getList($where, $page, $limit);
  34. foreach ($list as &$item) {
  35. $item['y_follow'] = $item['y_follow'] ?? 0;
  36. $item['stop'] = $item['end_time'] ? $item['end_time'] > time() ? 1 : -1 : 0;
  37. $item['label_name'] = $userLabel->getColumn([['id', 'in', $item['label_id']]], 'label_name');
  38. $item['end_time'] = date('Y-m-d H:i:s', $item['end_time']);
  39. }
  40. $count = $this->dao->count($where);
  41. return compact('list', 'count');
  42. }
  43. /**
  44. * 获取详情
  45. * @param $id
  46. * @return array
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function qrcodeInfo($id)
  52. {
  53. $info = $this->dao->get($id);
  54. if ($info) {
  55. $info = $info->toArray();
  56. } else {
  57. throw new AdminException('数据不存在');
  58. }
  59. /** @var UserServices $userService */
  60. $userService = app()->make(UserServices::class);
  61. $info['label_id'] = explode(',', $info['label_id']);
  62. foreach ($info['label_id'] as &$item) {
  63. $item = (int)$item;
  64. }
  65. /** @var UserLabelServices $userLabelServices */
  66. $userLabelServices = app()->make(UserLabelServices::class);
  67. $info['label_id'] = $userLabelServices->getLabelList(['ids' => $info['label_id']], ['id', 'label_name']);
  68. $info['time'] = $info['continue_time'];
  69. $info['content'] = json_decode($info['content'], true);
  70. $info['data'] = json_decode($info['data'], true);
  71. $info['avatar'] = $userService->value(['uid' => $info['uid']], 'avatar');
  72. return $info;
  73. }
  74. /**
  75. * 保存渠道码数据
  76. * @param $id
  77. * @param $data
  78. * @return bool
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function saveQrcode($id, $data)
  84. {
  85. $data['label_id'] = implode(',', $data['label_id']);
  86. $data['add_time'] = time();
  87. $data['continue_time'] = $data['time'];
  88. $data['end_time'] = $data['time'] ? $data['add_time'] + ($data['time'] * 86400) : 0;
  89. /** @var WechatReplyServices $replyServices */
  90. $replyServices = app()->make(WechatReplyServices::class);
  91. $type = $data['type'];
  92. if ($data['type'] == 'url') $type = 'text';
  93. $content = $data['content'];
  94. if ($data['type'] == 'news') $content = $data['content']['list'] ?? [];
  95. $method = 'tidy' . ucfirst($type);
  96. $data['data'] = $replyServices->{$method}($content, 0);
  97. $data['content'] = json_encode($data['content']);
  98. $data['data'] = json_encode($data['data']);
  99. if ($id) {
  100. $info = $this->dao->get($id);
  101. if (!$info) throw new AdminException('数据不存在');
  102. if ($info['image'] == '') $data['image'] = $this->getChannelCode($id);
  103. $info = $this->dao->update($id, $data);
  104. if (!$info) throw new AdminException('保存失败');
  105. } else {
  106. $info = $this->dao->save($data);
  107. $image = $this->getChannelCode($info['id']);
  108. $info = $this->dao->update($info['id'], ['image' => $image]);
  109. if (!$info) throw new AdminException('保存失败');
  110. }
  111. return true;
  112. }
  113. /**
  114. * 生成渠道码
  115. * @param int $id
  116. * @return mixed|string
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function getChannelCode($id = 0)
  122. {
  123. /** @var SystemAttachmentServices $systemAttachment */
  124. $systemAttachment = app()->make(SystemAttachmentServices::class);
  125. $name = 'wechatqrcode_' . $id . '.jpg';
  126. $siteUrl = sys_config('site_url', '');
  127. $imageInfo = $systemAttachment->getInfo(['name' => $name]);
  128. if (!$imageInfo) {
  129. /** @var QrcodeServices $qrCode */
  130. $qrCode = app()->make(QrcodeServices::class);
  131. //公众号
  132. $resCode = $qrCode->getForeverQrcode('wechatqrcode', $id);
  133. if ($resCode) {
  134. $res = ['res' => $resCode, 'id' => $resCode['id']];
  135. } else {
  136. $res = false;
  137. }
  138. if (!$res) throw new AdminException('二维码生成失败');
  139. $imageInfo = $this->downloadImage($resCode['url'], $name);
  140. $systemAttachment->attachmentAdd($name, $imageInfo['size'], $imageInfo['type'], $imageInfo['att_dir'], $imageInfo['att_dir'], 1, $imageInfo['image_type'], time(), 2);
  141. }
  142. return strpos($imageInfo['att_dir'], 'http') === false ? $siteUrl . $imageInfo['att_dir'] : $imageInfo['att_dir'];
  143. }
  144. /**
  145. * 下载图片
  146. * @param string $url
  147. * @param string $name
  148. * @param int $type
  149. * @param int $timeout
  150. * @param int $w
  151. * @param int $h
  152. * @return string
  153. */
  154. public function downloadImage($url = '', $name = '', $type = 0, $timeout = 30, $w = 0, $h = 0)
  155. {
  156. if (!strlen(trim($url))) return '';
  157. //TODO 获取远程文件所采用的方法
  158. if ($type) {
  159. $ch = curl_init();
  160. curl_setopt($ch, CURLOPT_URL, $url);
  161. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  162. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  163. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //TODO 跳过证书检查
  164. if (stripos($url, "https://") !== FALSE) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //TODO 从证书中检查SSL加密算法是否存在
  165. curl_setopt($ch, CURLOPT_HTTPHEADER, array('user-agent:' . $_SERVER['HTTP_USER_AGENT']));
  166. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//TODO 是否采集301、302之后的页面
  167. $content = curl_exec($ch);
  168. curl_close($ch);
  169. } else {
  170. try {
  171. ob_start();
  172. readfile($url);
  173. $content = ob_get_contents();
  174. ob_end_clean();
  175. } catch (\Exception $e) {
  176. return $e->getMessage();
  177. }
  178. }
  179. $size = strlen(trim($content));
  180. if (!$content || $size <= 2) return '图片流获取失败';
  181. $upload_type = sys_config('upload_type', 1);
  182. $upload = UploadService::init();
  183. if ($upload->to('attach/spread/agent')->setAuthThumb(false)->stream($content, $name) === false) {
  184. return $upload->getError();
  185. }
  186. $imageInfo = $upload->getUploadInfo();
  187. $data['att_dir'] = $imageInfo['dir'];
  188. $data['name'] = $imageInfo['name'];
  189. $data['size'] = $imageInfo['size'];
  190. $data['type'] = $imageInfo['type'];
  191. $data['image_type'] = $upload_type;
  192. $data['is_exists'] = false;
  193. return $data;
  194. }
  195. /**
  196. * 扫码完成后方法
  197. * @param $qrcodeInfo
  198. * @param $userInfo
  199. * @param $spreadInfo
  200. * @param int $isFollow
  201. * @return mixed
  202. */
  203. public function wechatQrcodeRecord($qrcodeInfo, $userInfo, $spreadInfo, $isFollow = 0)
  204. {
  205. $response = $this->transaction(function () use ($qrcodeInfo, $userInfo, $spreadInfo, $isFollow) {
  206. //绑定用户标签
  207. /** @var UserLabelRelationServices $labelServices */
  208. $labelServices = app()->make(UserLabelRelationServices::class);
  209. foreach ($qrcodeInfo['label_id'] as $item) {
  210. $labelArr[] = [
  211. 'uid' => $userInfo['uid'],
  212. 'label_id' => $item['id'] ?? $item
  213. ];
  214. }
  215. $labelServices->saveAll($labelArr);
  216. //增加二维码扫码数量
  217. $this->dao->upFollowAndScan($qrcodeInfo['id'], $isFollow);
  218. //写入扫码记录
  219. /** @var WechatQrcodeRecordServices $recordServices */
  220. $recordServices = app()->make(WechatQrcodeRecordServices::class);
  221. $data['qid'] = $qrcodeInfo['id'];
  222. $data['uid'] = $userInfo['uid'];
  223. $data['is_follow'] = $isFollow;
  224. $data['add_time'] = time();
  225. $recordServices->save($data);
  226. //回复信息内容
  227. return $this->replyDataByMessage($qrcodeInfo['type'], $qrcodeInfo['data']);
  228. });
  229. return $response;
  230. }
  231. /**
  232. * 发送扫码之后的信息
  233. * @param $type
  234. * @param $data
  235. * @return array|\EasyWeChat\Message\Image|\EasyWeChat\Message\News|\EasyWeChat\Message\Text|\EasyWeChat\Message\Voice
  236. */
  237. public function replyDataByMessage($type, $data)
  238. {
  239. if ($type == 'text') {
  240. return WechatService::textMessage($data['content']);
  241. } else if ($type == 'image') {
  242. return WechatService::imageMessage($data['media_id']);
  243. } else if ($type == 'news') {
  244. $title = $data['title'] ?? '';
  245. $image = $data['image'] ?? '';
  246. $description = $data['synopsis'] ?? '';
  247. $url = $data['url'] ?? '';
  248. return WechatService::newsMessage($title, $description, $url, $image);
  249. } else if ($type == 'url') {
  250. $title = $data['content'];
  251. $image = sys_config('h5_avatar');
  252. $description = $data['content'];
  253. $url = $data['content'];
  254. return WechatService::newsMessage($title, $description, $url, $image);
  255. } else if ($type == 'voice') {
  256. return WechatService::voiceMessage($data['media_id']);
  257. }
  258. }
  259. }