WechatQrcodeServices.php 11 KB

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