WechatQrcodeServices.php 11 KB

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