微信公眾號 圖文消息修改 新增 圖文素材 的坑


 

微信接口文檔地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738731

1 新增永久素材按照文檔走就可以 沒有什么坑,

articles 中是一個多維數組

{"articles":[{"title":"標題","thumb_media_id":"Tu1YMHMYtpGeoyr3F_Mhp5_wDVNo3qOpsc9eBKGIxm","author":"我是作者","show_cover_pic":"","content":"<p>我是內容<br/></p>","content_source_url":""},{"title":"標題","thumb_media_id":"Tu1YMHMYtpGeoyr3F_Mhp7nAWUMlEjLTTyVCB07GUA","author":"左右","show_cover_pic":"","content":"<p>我是123</p>","content_source_url":""}]}

提交正確

2 修改永久素材中存在問題

 

 這個坑就是,一次只能是修改一篇文章,而且index中是對應的文章的第幾篇,所以這個我使用 的async,異步去進行ajax去提交代碼如下

//保存 修改素材
$scope.savedata = function() {

if (!$scope.wxappid || $scope.articles.length < 0) {
return;
}
if ($scope.wxappid && $scope.media_id) {
$scope.address = {
"media_id": $scope.media_id,
"index": $scope.articles.length > 1 ? $scope.articles.length : 0,
"articles": []
}
var api = 'wxUpdateNews';
}

if (!$scope.media_id) {
$scope.address = { articles: [] };
var api = 'wxAddNews';
}
if ($scope.testData()) {
post();
}

function post() {
var i = 0;
async.mapSeries($scope.articles, function(da, callback) {
var article = {
"title": da.title, //必須
"thumb_media_id": da.thumb_media_id,
"author": da.author,
"digest": da.digest,
"show_cover_pic": da.show_cover_pic,
"content": da.content,
"content_source_url": da.content_source_url,
}
if ($scope.media_id) {
$scope.address.articles = article;
$scope.address.index = i;
$scope.postApi(api, $scope.wxappid, $scope.address).then(function(t) {
i = i + 1;
callback(null, t);
});
} else {
$scope.address.articles.push(article);
callback(null, 1);
}

}, function(e, r) {
if ($scope.address && !$scope.media_id && r) {
$scope.postApi(api, $scope.wxappid, $scope.address).then(function(t) {
if (t) {
helper.myAlert("新增圖文消息成功", "#/article/list");
}
});
}
if ($scope.media_id && r) {
helper.myAlert("修改圖文消息成功", "#/article/list");
}

});
}
}

//檢驗數據
$scope.testData = function() {
for (var i = $scope.articles.length - 1; i >= 0; i--) {
if (!$scope.articles[i].content) return helper.myAlert("圖文內容不能為空!");
if (!$scope.articles[i].thumb_media_id) return helper.myAlert("圖文封面不能為空!");
if (!$scope.articles[i].title) return helper.myAlert("圖文標題不能為空!");
if (!$scope.articles[i].author) return helper.myAlert("圖文作者不能為空!");
}
return true;
}

$scope.postApi = function(api, wxappid, address) {
return dataService[api](wxappid, address).then(function(result) {
return result;
});
}

異步async 進行處理 可以用到批量處理的地方  我使用 的mapSeries 方法是把所有的請求串起來,等結束后,在進行返回,也是直接map不用串起來是進行請求,

 

還有幾個坑是,圖片的上傳,文章中可以使用 

圖片上傳可以是永久的圖文上傳

兩個的區別是,

第一個之后返回url,你在微信中是查不到的,

第二個是media_id,你要是使用鏈接的話,還需要進行查詢展示,所以這地方可以寫成組件,上傳后文件后,在進行展示出來,在選擇;

3 獲取微信公眾號

 1 這有一個坑 就是微信圖片不展示出來 

  在頁面必須加上 

代碼:<head>
<meta name="referrer" content="never">
</head>

 才能進行展示出來,

 2 第二坑是,微信返回的數據格式,是不能使用的,把src中的 地址給去掉了,所以我們要自己進行組裝后,才能使用

 

//組裝數據
//$scope.description h是獲取到的 content 內容

//$scope.description h是獲取到的 content 內容
$scope.AssembleContent = function() {
//把轉義 的網址加上
var str2 = $scope.description.replace(/="cn/g, '="https://mmbiz.qpic.cn');
console.error("dd", $scope.description);
//根據 data-typef分割
$scope.description = str2.split('data-src="');
var sty = {};
console.error("$scope.description",$scope.description);
_.map($scope.description, function(da, isk) {
//從頭開始進行截取 到" 號
var dd = da.substr(0, da.indexOf('"'));
// var dd = da.substr(0, da.indexOf('data-src="'));
console.error("dd", dd);
//判斷 http 是否存在,存在的話,就說明是一個src 鏈接
var d = dd.indexOf('http');
//indexOf 如果存在的話,會返回0
if (d == 0) {
sty[isk] = dd;
// console.error("dd", dd);
}
});
console.error("sty",sty);
var cont = '';
//然后在進行把截取的字符串拼接在一起
_.map($scope.description, function(ddd, kkk) {
if (kkk == 0) {
cont = ddd;
} else {
//因為之前截取的時候是根據 k進行賦值的,k的值不變的,然后判斷有沒有值,
if (sty[kkk]) {
cont = cont + 'src ="' + sty[kkk] + '" data-src="' + ddd;
} else {
cont = cont + 'data-src="' + ddd;
}
}
});
cont = cont.replace(/<a/g, '<a href=""');
$scope.article.content = cont;
}

如果是多篇文章的話,可以進行循環執行;

4 刪除素材 

按照文檔 執行就可以,沒有什么難度

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM