123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- // +----------------------------------------------------------------------
- // | Think.Admin
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: http://think.ctolog.com
- // +----------------------------------------------------------------------
- // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | github开源项目:https://github.com/zoujingli/Think.Admin
- // +----------------------------------------------------------------------
- namespace service;
- use think\Db;
- use think\Log;
- /**
- * 微信数据服务
- * Class WechatService
- * @package service
- * @author Anyon <zoujingli@qq.com>
- * @date 2017/03/22 15:32
- */
- class WechatService
- {
- /**
- * 通过图文ID读取图文信息
- * @param int $id 本地图文ID
- * @param array $where 额外的查询条件
- * @return array
- */
- public static function getNewsById($id, $where = [])
- {
- $data = Db::name('WechatNews')->where('id', $id)->where($where)->find();
- $article_ids = explode(',', $data['article_id']);
- $articles = Db::name('WechatNewsArticle')->where('id', 'in', $article_ids)->select();
- $data['articles'] = [];
- foreach ($article_ids as $article_id) {
- foreach ($articles as $article) {
- if (intval($article['id']) === intval($article_id)) {
- unset($article['create_by'], $article['create_at']);
- $data['articles'][] = $article;
- }
- }
- }
- unset($articles);
- return $data;
- }
- /**
- * 上传图片到微信服务器
- * @param string $local_url
- * @return string|null
- */
- public static function uploadImage($local_url)
- {
- # 检测文件上否已经上传过了
- if (($img = Db::name('WechatNewsImage')->where('md5', md5($local_url))->find()) && isset($img['media_url'])) {
- return $img['media_url'];
- }
- # 下载临时文件到本地
- $filename = 'wechat/image/' . join('/', str_split(md5($local_url), 16)) . '.' . strtolower(pathinfo($local_url, 4));
- $result = FileService::local($filename, file_get_contents($local_url));
- # 上传图片到微信服务器
- if ($result && isset($result['file'])) {
- $wechat = &load_wechat('media');
- $info = $wechat->uploadImg(['media' => "@{$result['file']}"]);
- if (!empty($info)) {
- $data = ['local_url' => $local_url, 'media_url' => $info['url'], 'md5' => md5($local_url)];
- Db::name('WechatNewsImage')->insert($data);
- return $info['url'];
- }
- Log::error("图片上传失败,请稍后再试!{$wechat->errMsg}[{$wechat->errCode}]");
- }
- return null;
- }
- /**
- * 上传图片永久素材
- * @param string $local_url 文件URL地址
- * @param string $type 文件类型
- * @param bool $is_video 是否为视频文件
- * @param array $video_info 视频信息
- * @return string|null
- */
- public static function uploadForeverMedia($local_url = '', $type = 'image', $is_video = false, $video_info = [])
- {
- # 检测文件上否已经上传过了
- $wechat = &load_wechat('media');
- # 检查文件URL是否已经上传为永久素材
- $map = ['md5' => md5($local_url), 'appid' => $wechat->appid];
- if (($img = Db::name('WechatNewsMedia')->where($map)->find()) && isset($img['media_id'])) {
- return $img['media_id'];
- }
- # 下载临时文件到本地
- $filename = 'wechat/image/' . join('/', str_split(md5($local_url), 16)) . '.' . strtolower(pathinfo($local_url, 4));
- $upload = FileService::local($filename, file_get_contents($local_url));
- if (!empty($upload) && isset($upload['file']) && file_exists($upload['file'])) {
- # 上传图片到微信服务器
- if (false !== ($result = $wechat->uploadForeverMedia(['media' => "@{$upload['file']}"], $type, $is_video, $video_info))) {
- $data = ['md5' => $map['md5'], 'type' => $type, 'appid' => $wechat->appid, 'media_id' => $result['media_id'], 'local_url' => $local_url];
- isset($result['url']) && $data['media_url'] = $result['url'];
- Db::name('WechatNewsMedia')->insert($data);
- return $data['media_id'];
- }
- }
- Log::error("素材上传失败, 请稍后再试! {$wechat->errMsg}[{$wechat->errCode}]");
- return null;
- }
- /**
- * 从微信服务器获取所有标签
- * @return bool
- */
- public static function syncFansTags()
- {
- $wechat = &load_wechat("User");
- if (($result = $wechat->getTags()) !== false) {
- $tags = $result['tags'];
- foreach ($tags as &$tag) {
- $tag['appid'] = $wechat->appid;
- }
- Db::name('WechatFansTags')->where('appid', $wechat->appid)->delete();
- foreach (array_chunk($tags, 100) as $list) {
- Db::name('WechatFansTags')->insertAll($list);
- }
- }
- return true;
- }
- /**
- * 同步粉丝的标签
- * @param string $openid
- * @return bool
- */
- public static function syncFansTagsByOpenid($openid)
- {
- $wechat = &load_wechat('User');
- $tagsid = $wechat->getUserTags($openid);
- if ($tagsid === false || !is_array($tagsid)) {
- return false;
- }
- $data = ['appid' => $wechat->appid, 'openid' => $openid, 'tagid_list' => join(',', $tagsid)];
- return DataService::save('wechat_fans', $data, 'openid', ['appid' => $wechat->appid]);
- }
- /**
- * 保存/更新粉丝信息
- * @param array $user
- * @param string $appid
- * @return bool
- */
- public static function setFansInfo($user, $appid = '')
- {
- if (!empty($user['subscribe_time'])) {
- $user['subscribe_at'] = date('Y-m-d H:i:s', $user['subscribe_time']);
- }
- if (!empty($user['tagid_list']) && is_array($user['tagid_list'])) {
- $user['tagid_list'] = join(',', $user['tagid_list']);
- }
- $user['appid'] = $appid;
- $user['nickname'] = ToolsService::emojiEncode($user['nickname']);
- return DataService::save('WechatFans', $user, 'openid');
- }
- /**
- * 读取粉丝信息
- * @param string $openid 微信用户openid
- * @param string $appid 公众号appid
- * @return array|false
- */
- public static function getFansInfo($openid, $appid = null)
- {
- $map = ['openid' => $openid];
- is_string($appid) && $map['appid'] = $appid;
- if (($fans = Db::name('WechatFans')->where($map)->find()) && isset($fans['nickname'])) {
- $fans['nickname'] = ToolsService::emojiDecode($fans['nickname']);
- }
- return $fans;
- }
- /**
- * 同步获取粉丝列表
- * @param string $next_openid
- * @return bool
- */
- public static function syncAllFans($next_openid = '')
- {
- $wechat = &load_wechat('User');
- if (false === ($result = $wechat->getUserList($next_openid)) || empty($result['data']['openid'])) {
- Log::error("获取粉丝列表失败, {$wechat->errMsg} [{$wechat->errCode}]");
- return false;
- }
- foreach (array_chunk($result['data']['openid'], 100) as $openids) {
- if (false === ($info = $wechat->getUserBatchInfo($openids)) || !is_array($info)) {
- Log::error("获取用户信息失败, {$wechat->errMsg} [{$wechat->errCode}]");
- return false;
- }
- foreach ($info as $user) {
- if (false === self::setFansInfo($user, $wechat->appid)) {
- Log::error('更新粉丝信息更新失败!');
- return false;
- }
- if ($result['next_openid'] === $user['openid']) {
- unset($result['next_openid']);
- }
- }
- }
- return empty($result['next_openid']) ? true : self::syncAllFans($result['next_openid']);
- }
- /**
- * 同步获取黑名单信息
- * @param string $next_openid
- * @return bool
- */
- public static function syncBlackFans($next_openid = '')
- {
- $wechat = &load_wechat('User');
- $result = $wechat->getBacklist($next_openid);
- if ($result === false || (empty($result['data']['openid']))) {
- if (empty($result['total'])) {
- return true;
- }
- Log::error("获取粉丝黑名单列表失败,{$wechat->errMsg} [{$wechat->errCode}]");
- return false;
- }
- foreach ($result['data']['openid'] as $openid) {
- if (false === ($user = $wechat->getUserInfo($openid))) {
- Log::error("获取用户[{$openid}]信息失败,$wechat->errMsg");
- return false;
- }
- $user['is_back'] = '1';
- if (false === self::setFansInfo($user)) {
- Log::error('更新粉丝信息更新失败!');
- return false;
- }
- if ($result['next_openid'] === $openid) {
- unset($result['next_openid']);
- }
- }
- return empty($result['next_openid']) ? true : self::syncBlackFans($result['next_openid']);
- }
- }
|