mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
[更新]更新微信素材资源管理
This commit is contained in:
parent
bbab04b2bc
commit
9d704084c8
@ -47,11 +47,39 @@ class News extends BasicAdmin {
|
||||
return parent::_list($db);
|
||||
}
|
||||
|
||||
/**
|
||||
* 图文选择器
|
||||
* @return string
|
||||
*/
|
||||
public function select() {
|
||||
$this->index();
|
||||
}
|
||||
|
||||
/**
|
||||
* 媒体资源显示
|
||||
* @return array
|
||||
*/
|
||||
public function image() {
|
||||
$_GET['rows'] = 18;
|
||||
$this->assign('field', $this->request->get('field', 'local_url'));
|
||||
return $this->_list(Db::name('WechatNewsMedia')->where('type', 'image'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 图文列表数据处理
|
||||
* @param $data
|
||||
*/
|
||||
protected function _data_filter(&$data) {
|
||||
protected function _index_data_filter(&$data) {
|
||||
foreach ($data as &$vo) {
|
||||
$vo = WechatService::getNewsById($vo['id']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 图文列表数据处理
|
||||
* @param $data
|
||||
*/
|
||||
protected function _select_data_filter(&$data) {
|
||||
foreach ($data as &$vo) {
|
||||
$vo = WechatService::getNewsById($vo['id']);
|
||||
}
|
||||
@ -132,14 +160,6 @@ class News extends BasicAdmin {
|
||||
$this->error("图文删除失败, 请稍候再试!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 图文选择器
|
||||
* @return string
|
||||
*/
|
||||
public function select() {
|
||||
$this->index();
|
||||
}
|
||||
|
||||
/**
|
||||
* 推荐图文
|
||||
* @return array|void
|
||||
@ -200,11 +220,10 @@ class News extends BasicAdmin {
|
||||
* @return boolean
|
||||
*/
|
||||
private function _uploadWechatNews(&$newsinfo) {
|
||||
$self = $this;
|
||||
foreach ($newsinfo['articles'] as &$article) {
|
||||
$article['thumb_media_id'] = WechatService::uploadForeverMedia($article['local_url']);
|
||||
$article['content'] = preg_replace_callback("/<img(.*?)src=['\"](.*?)['\"](.*?)\/?>/i", function ($matches) use ($self) {
|
||||
$src = $self->_filterWechatImage($matches[2]);
|
||||
$article['content'] = preg_replace_callback("/<img(.*?)src=['\"](.*?)['\"](.*?)\/?>/i", function ($matches) {
|
||||
$src = WechatService::uploadImage($matches[2]);
|
||||
return "<img{$matches[1]}src=\"{$src}\"{$matches[3]}/>";
|
||||
}, htmlspecialchars_decode($article['content']));
|
||||
}
|
||||
@ -215,38 +234,10 @@ class News extends BasicAdmin {
|
||||
$result = $wechat->uploadForeverArticles(['articles' => $newsinfo['articles']]);
|
||||
if (isset($result['media_id'])) {
|
||||
$newsinfo['media_id'] = $result['media_id'];
|
||||
return Db::name('WechatNews')->where('id', $newsinfo['id'])->update(['media_id' => $result['media_id']]);
|
||||
return Db::name('WechatNews')->where('id', $newsinfo['id'])->setField('media_id', $result['media_id']);
|
||||
}
|
||||
Log::error("上传永久图文失败, {$wechat->errMsg}[{$wechat->errCode}]");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章内容图片处理
|
||||
* @param string $local_url
|
||||
* @return string|null
|
||||
*/
|
||||
private function _filterWechatImage($local_url = '') {
|
||||
# 检测图片是否已经上传过了
|
||||
$img = Db::name('WechatNewsImage')->where('local_url', $local_url)->find();
|
||||
if (!empty($img) && 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');
|
||||
$mediainfo = $wechat->uploadImg(['media' => "@{$result['file']}"]);
|
||||
if (!empty($mediainfo)) {
|
||||
$data = ['local_url' => $local_url, 'media_url' => $mediainfo['url'], 'md5' => md5($local_url)];
|
||||
Db::name('WechatNewsImage')->insert($data);
|
||||
return $mediainfo['url'];
|
||||
}
|
||||
}
|
||||
Log::error("图片上传失败,请稍后再试!{$wechat->errMsg}[{$wechat->errCode}]");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -64,6 +64,7 @@
|
||||
<div class="col-xs-6">
|
||||
<br>
|
||||
<a data-file="one" data-type="jpg,png,jpeg" data-field="local_url" class="btn btn-sm">上传图片</a>
|
||||
<a data-iframe='{:url("image")}?field=local_url' class="btn btn-sm">选择图片</a>
|
||||
<br><br>
|
||||
<label style="margin-left:0;padding-left:10px">
|
||||
<input data-auto-none type="checkbox" value="1" name="show_cover_pic">
|
||||
@ -270,8 +271,9 @@
|
||||
.news-right .layui-input{border-radius:0;border:1px solid #e5e6e7;}
|
||||
.news-right .layui-input:hover, .news-right .layui-input:focus{border-color:#e5e6e7!important;}
|
||||
.news-right .input-group-addon{background:#fff;border-radius:0;border-color:#e5e6e7;}
|
||||
.news-right .upload-image-box{width:150px;height:120px;background-size:cover;background-position:center center;}
|
||||
.news-right .upload-image-box{width:130px;height:90px;background-size:cover;background-position:center center;border:1px solid rgba(125,125,125,0.1);margin:5px}
|
||||
.news-item .upload-multiple-close{width:30px;height:30px;line-height:30px;text-align:center;background-color:rgba(0,0,0,.5);color:#fff;float:right;}
|
||||
.news-item .upload-multiple-close:hover{text-decoration:none}
|
||||
.edui-default .edui-editor{border-radius:0!important}
|
||||
</style>
|
||||
{/block}
|
28
application/wechat/view/news.image.html
Normal file
28
application/wechat/view/news.image.html
Normal file
@ -0,0 +1,28 @@
|
||||
{extend name='extra@admin/main'}
|
||||
|
||||
{block name="body"}
|
||||
<style>
|
||||
body{min-width:100px!important}
|
||||
.pagination{padding:0 10px}
|
||||
.news-image{cursor:pointer;width:111px;height:120px;background-size:cover;background-position:center center;float:left;margin:10px;border:1px solid rgba(125,125,125,0.3)}
|
||||
</style>
|
||||
<div class="news-container" id='news_box'>
|
||||
{foreach $list as $key=>$vo}
|
||||
<a class="news-image transition" data-src='{$vo.local_url}' style="background-image:url('{$vo.local_url}')"></a>
|
||||
{/foreach}
|
||||
<div style='clear:both'></div>
|
||||
</div>
|
||||
|
||||
{if isset($page)}{$page}{/if}
|
||||
<script>
|
||||
require(['jquery'], function () {
|
||||
$('body').on('click', '[data-open]', function () {
|
||||
window.location.href = this.getAttribute('data-open');
|
||||
}).on('click', '.news-image', function () {
|
||||
var src = this.getAttribute('data-src');
|
||||
top.$('[name="{$field}"]').val(src).trigger('change');
|
||||
parent.layer.close(parent.layer.getFrameIndex(window.name));
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{/block}
|
@ -14,8 +14,8 @@
|
||||
|
||||
namespace service;
|
||||
|
||||
use think\Log;
|
||||
use think\Db;
|
||||
use think\Log;
|
||||
|
||||
/**
|
||||
* 微信数据服务
|
||||
@ -49,6 +49,33 @@ class WechatService {
|
||||
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');
|
||||
$mediainfo = $wechat->uploadImg(['media' => "@{$result['file']}"]);
|
||||
if (!empty($mediainfo)) {
|
||||
$data = ['local_url' => $local_url, 'media_url' => $mediainfo['url'], 'md5' => md5($local_url)];
|
||||
Db::name('WechatNewsImage')->insert($data);
|
||||
return $mediainfo['url'];
|
||||
}
|
||||
}
|
||||
Log::error("图片上传失败,请稍后再试!{$wechat->errMsg}[{$wechat->errCode}]");
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片永久素材
|
||||
* @param string $local_url 文件URL地址
|
||||
@ -60,28 +87,25 @@ class WechatService {
|
||||
public static function uploadForeverMedia($local_url = '', $type = 'image', $is_video = false, $video_info = array()) {
|
||||
# 检测文件上否已经上传过了
|
||||
$wechat = &load_wechat('media');
|
||||
$map = ['md5' => ($md5 = md5($local_url)), 'appid' => $wechat->appid];
|
||||
# 检查文件URL是否已经上传为永久素材
|
||||
if (($result = Db::name('WechatNewsMedia')->where($map)->find()) && !empty($result)) {
|
||||
return $result['media_id'];
|
||||
$map = ['md5' => ($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/' . join('/', str_split($md5, 16)) . '.' . pathinfo($local_url, PATHINFO_EXTENSION);
|
||||
$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'])) {
|
||||
# 上传图片素材
|
||||
$result = $wechat->uploadForeverMedia(array('media' => "@{$upload['file']}"), $type, $is_video, $video_info);
|
||||
# 删除本地文件
|
||||
if (false !== $result) {
|
||||
$data = ['md5' => $md5, 'type' => $type, 'appid' => $wechat->appid, 'media_id' => $result['media_id'], 'local_url' => $local_url,];
|
||||
# 上传图片到微信服务器
|
||||
if (false !== ($result = $wechat->uploadForeverMedia(array('media' => "@{$upload['file']}"), $type, $is_video, $video_info))) {
|
||||
$data = ['md5' => $md5, 'type' => $type, 'appid' => $wechat->appid, 'media_id' => $result['media_id'], 'local_url' => $local_url];
|
||||
isset($result['url']) && $data['media_url'] = $result['url'];
|
||||
if (false !== Db::name('WechatNewsMedia')->insert($data)) {
|
||||
return $data['media_id'];
|
||||
}
|
||||
Db::name('WechatNewsMedia')->insert($data);
|
||||
return $data['media_id'];
|
||||
}
|
||||
}
|
||||
Log::error("素材上传失败,请稍后再试!{$wechat->errMsg}[{$wechat->errCode}]");
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user