WechatQrcodeServices.php 12 KB

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