WechatQrcodeServices.php 11 KB

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