微信公众号相关调整
This commit is contained in:
@@ -3,6 +3,7 @@ namespace app\common;
|
||||
use think\Db;
|
||||
use app\common\QrCodeImage;
|
||||
use app\common\Wechat;
|
||||
use think\Cache;
|
||||
class Material
|
||||
{
|
||||
|
||||
@@ -23,6 +24,12 @@ class Material
|
||||
//默认头像
|
||||
protected $sDefaultUserIcon = '/static/img/userImg.f3d9bc3b.jpg';
|
||||
|
||||
protected $sMaterialStepName = 'upload_step_name_';
|
||||
protected $sMaterialError = 'upload_error_';
|
||||
|
||||
protected $sAppID; //= 'wx03cb871b66e34e10';
|
||||
protected $sAppSecret; //= 'f59ccaf00383dcfab489292c4697620a';
|
||||
|
||||
public function __construct(\think\Request $request = null) {
|
||||
|
||||
}
|
||||
@@ -161,7 +168,7 @@ class Material
|
||||
* @param sType select 查询|add 新增
|
||||
* @param $sPath 图片地址
|
||||
*/
|
||||
public function addArticleMaterial($aParam = []){
|
||||
private function addArticleMaterial($aParam = []){
|
||||
|
||||
//文章ID
|
||||
$iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id'];
|
||||
@@ -219,7 +226,7 @@ class Material
|
||||
/**
|
||||
* 添加期刊素材
|
||||
*/
|
||||
public function addJournalMaterial($aParam = []){
|
||||
private function addJournalMaterial($aParam = []){
|
||||
|
||||
//期刊ID
|
||||
$iJournalId = empty($aParam['journal_id']) ? '' : $aParam['journal_id'];
|
||||
@@ -275,7 +282,7 @@ class Material
|
||||
/**
|
||||
* 更新期刊二维码关联微信公众号素材
|
||||
*/
|
||||
public function updateJournalQrcode($aParam = []){
|
||||
private function updateJournalQrcode($aParam = []){
|
||||
|
||||
//期刊ID
|
||||
$iJournalId = empty($aParam['journal_id']) ? '' : $aParam['journal_id'];
|
||||
@@ -312,13 +319,14 @@ class Material
|
||||
if($response === false){
|
||||
return json_encode(['status' => 3,'msg' => 'fail']);
|
||||
}
|
||||
|
||||
return json_encode(['status' => 1,'msg' => 'success','data' => $aUpdate]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文章二维码关联微信公众号素材
|
||||
*/
|
||||
public function updateArticleQrcode($aParam = []){
|
||||
private function updateArticleQrcode($aParam = []){
|
||||
|
||||
$iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id'];
|
||||
if(empty($iArticleId)){
|
||||
@@ -361,7 +369,7 @@ class Material
|
||||
/**
|
||||
* 更新通讯作者关联微信公众号素材
|
||||
*/
|
||||
public function updateArticleAuthor($aParam = []){
|
||||
private function updateArticleAuthor($aParam = []){
|
||||
|
||||
$sEmail = empty($aParam['email']) ? '' : $aParam['email'];
|
||||
//文章图片
|
||||
@@ -406,13 +414,14 @@ class Material
|
||||
}
|
||||
$sSql = $sStartSql.trim($sSql,',').$sEndSql;
|
||||
$result = Db::execute($sSql);
|
||||
|
||||
return json_encode(['status' => 1,'msg' => 'success','data' => $aInsert]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新通讯作者关联微信公众号素材
|
||||
*/
|
||||
public function updateJournalEditorQrcode($aParam = []){
|
||||
private function updateJournalEditorQrcode($aParam = []){
|
||||
|
||||
$iJournalId = empty($aParam['journal_id']) ? '' : $aParam['journal_id'];
|
||||
//文章图片
|
||||
@@ -455,6 +464,237 @@ class Material
|
||||
}
|
||||
$sSql = $sStartSql.trim($sSql,',').$sEndSql;
|
||||
$result = Db::execute($sSql);
|
||||
|
||||
return json_encode(['status' => 1,'msg' => 'success','data' => $aInsert]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入Reids 防止两小时内重复上传
|
||||
*/
|
||||
public function setStepForRedis($iArticleId = 0,$sStepName = 'addArticleMaterial'){
|
||||
//写入redis 避免重复请求
|
||||
$sStepNameForRedis = $this->getStepForRedis($iArticleId);
|
||||
if(empty($sStepNameForRedis) || (!empty($sStepNameForRedis) && $sStepNameForRedis != 'finish')){
|
||||
$sRedisKey = $this->sMaterialStepName.'upload_article_id_'.$iArticleId;
|
||||
return Cache::set($sRedisKey, $sStepName, 600); // 有效期600秒
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Reids 防止两小时内重复上传
|
||||
*/
|
||||
public function getStepForRedis($iArticleId = 0){
|
||||
$sRedisKey = $this->sMaterialStepName.'upload_article_id_'.$iArticleId;
|
||||
return Cache::get($sRedisKey); // 有效期600秒
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入Reids 防止两小时内重复上传
|
||||
*/
|
||||
public function setErrorForRedis($iArticleId = 0,$sStepName = 'addArticleMaterial'){
|
||||
$sRedisKey = $this->sMaterialError.$iArticleId;
|
||||
return Cache::set($sRedisKey, $sStepName, 600); // 有效期600秒
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入Reids 防止两小时内重复上传
|
||||
*/
|
||||
public function getErrorForRedis($iArticleId = 0){
|
||||
$sRedisKey = $this->sMaterialError.$iArticleId;
|
||||
return Cache::set($sRedisKey, $sStepName, 600); // 有效期600秒
|
||||
}
|
||||
/**
|
||||
* 上传素材
|
||||
*/
|
||||
|
||||
public function uploadMaterial($aParam = []){
|
||||
|
||||
//获取参数
|
||||
$aParam = empty($aParam) ? $this->input->post() : $aParam;
|
||||
|
||||
//文章ID
|
||||
$iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id'];
|
||||
|
||||
//必填验证
|
||||
if(empty($iArticleId)){
|
||||
return json_encode(['status' => 2,'msg' => 'article_id is empty']);
|
||||
}
|
||||
//期刊ID
|
||||
$iJournalId = empty($aParam['journal_id']) ? '' : $aParam['journal_id'];
|
||||
if(empty($iJournalId)){
|
||||
return json_encode(['status' => 2,'msg' => 'journal_id is empty']);
|
||||
}
|
||||
|
||||
// 获取微信公众号APPID和APPKEY
|
||||
if(empty($aParam['wechat_app_id']) || empty($aParam['wechat_app_secret'])){
|
||||
return json_encode(['status' => 2,'msg' => 'Please configure the AppID and AppSecret']);
|
||||
}
|
||||
|
||||
//获取缓存标识
|
||||
$sStepName = $this->getStepForRedis($iArticleId);
|
||||
if(!empty($sStepName) && $sStepName == 'finish'){
|
||||
return json_encode(['status' => 1,'msg' => 'The relevant materials for the article have been uploaded. Please try again in ten minutes']);
|
||||
}
|
||||
if(empty($sStepName)){
|
||||
//写入标识
|
||||
$this->setStepForRedis($iArticleId);
|
||||
$sStepName = 'addArticleMaterial';
|
||||
}
|
||||
|
||||
|
||||
//微信公众号密钥
|
||||
$aWechatParam = ['wechat_app_id' => $aParam['wechat_app_id'],'wechat_app_secret' => $aParam['wechat_app_secret']];
|
||||
|
||||
//获取文章参数
|
||||
$aArticle = empty($aParam['article_info']) ? [] : $aParam['article_info'];
|
||||
|
||||
//上传文章素材参数组装
|
||||
$aArticleParam = ['article_id' => $iArticleId,'icon' => empty($aArticle['icon']) ? '' : $aArticle['icon']];
|
||||
$aArticleParam += $aWechatParam;
|
||||
|
||||
//上传文章图片素材
|
||||
if(!empty($aArticleParam['icon']) && $sStepName == 'addArticleMaterial'){
|
||||
$aUploadResult = json_decode($this->addArticleMaterial($aArticleParam),true);
|
||||
$aParam['article_info'] = empty($aUploadResult['data']) ? [] : $aUploadResult['data'];
|
||||
if(empty($aParam['article_info'])){
|
||||
$sMsg = empty($aUploadResult['msg']) ? 'Article material upload failed' : $aUploadResult['msg'];
|
||||
$this->setErrorForRedis($iArticleId,$sMsg);
|
||||
return json_encode(['status' => 2,'msg' => $sMsg]);
|
||||
}
|
||||
}
|
||||
//写入Redis下一个执行方法
|
||||
$sStepName = 'updateArticleQrcode';
|
||||
$this->setStepForRedis($iArticleId,$sStepName);
|
||||
//文章二维码图片上传
|
||||
if($sStepName == 'updateArticleQrcode'){
|
||||
$aUploadResult = json_decode($this->updateArticleQrcode($aArticleParam),true);
|
||||
$aParam['article_qrcode_info'] = empty($aUploadResult['data']) ? [] : $aUploadResult['data'];
|
||||
if(empty($aParam['article_qrcode_info'])){
|
||||
$sMsg = empty($aUploadResult['msg']) ? 'Article QR code image upload failed' : $aUploadResult['msg'];
|
||||
$this->setErrorForRedis($iArticleId,$sMsg);
|
||||
return json_encode(['status' => 2,'msg' => $sMsg]);
|
||||
}
|
||||
}
|
||||
//写入Redis下一个执行方法
|
||||
$sStepName = 'addJournalMaterial';
|
||||
$this->setStepForRedis($iArticleId, $sStepName);
|
||||
//获取期刊信息
|
||||
$aJournal = empty($aParam['journal_info']) ? [] : $aParam['journal_info'];
|
||||
//上传期刊素材相关参数组装
|
||||
$aJournalParam = ['journal_id' => $iJournalId,'icon' => empty($aJournal['icon']) ? '' : $aJournal['icon'],'editor_qrcode' => empty($aJournal['editor_qrcode']) ? '' : $aJournal['editor_qrcode']];
|
||||
$aJournalParam += $aWechatParam;
|
||||
//上传期刊图片
|
||||
if(!empty($aJournalParam['icon']) && $sStepName == 'addJournalMaterial'){
|
||||
$aUploadResult = json_decode($this->addJournalMaterial($aJournalParam),true);
|
||||
$aParam['journal_info'] = empty($aUploadResult['data']) ? [] : $aUploadResult['data'];
|
||||
if(empty($aParam['journal_info'])){
|
||||
|
||||
$sMsg = empty($aUploadResult['msg']) ? 'Journal material image upload failed' : $aUploadResult['msg'];
|
||||
$this->setErrorForRedis($iArticleId,$sMsg);
|
||||
return json_encode(['status' => 2,'msg' => $sMsg]);
|
||||
}
|
||||
}
|
||||
//写入Redis下一个执行方法
|
||||
$sStepName = 'updateJournalQrcode';
|
||||
$this->setStepForRedis($iArticleId,$sStepName);
|
||||
//上传期刊二维码
|
||||
if($sStepName == 'updateJournalQrcode'){
|
||||
$aUploadResult = json_decode($this->updateJournalQrcode($aJournalParam),true);
|
||||
$aParam['journal_qrcode_info'] = empty($aUploadResult['data']) ? [] : $aUploadResult['data'];
|
||||
if(empty($aParam['journal_qrcode_info'])){
|
||||
$sMsg = empty($aUploadResult['msg']) ? 'Journal QR code image upload failed' : $aUploadResult['msg'];
|
||||
$this->setErrorForRedis($iArticleId,$sMsg);
|
||||
return json_encode(['status' => 2,'msg' => $sMsg]);
|
||||
}
|
||||
}
|
||||
//写入Redis下一个执行方法
|
||||
$sStepName = 'updateJournalEditorQrcode';
|
||||
$this->setStepForRedis($iArticleId,$sStepName);
|
||||
//期刊编辑二维码
|
||||
if(!empty($aJournalParam['editor_qrcode']) && $sStepName == 'updateJournalEditorQrcode'){
|
||||
$aUploadResult = json_decode($this->updateJournalEditorQrcode($aJournalParam),true);
|
||||
$aParam['editor_qrcode_info'] = empty($aUploadResult['data']) ? [] : $aUploadResult['data'];
|
||||
if(empty($aParam['editor_qrcode_info'])){
|
||||
$sMsg = empty($aUploadResult['msg']) ? "Journal editor's QR code image upload failed" : $aUploadResult['msg'];
|
||||
$this->setErrorForRedis($iArticleId,$sMsg);
|
||||
return json_encode(['status' => 2,'msg' => $sMsg]);
|
||||
}
|
||||
}
|
||||
//写入下一个执行方法
|
||||
$sStepName = 'updateArticleAuthor';
|
||||
$this->setStepForRedis($iArticleId,$sStepName);
|
||||
//作者
|
||||
$aAuthor = empty($aParam['author_info']) ? [] : $aParam['author_info'];
|
||||
if(!empty($aAuthor) && $sStepName == 'updateArticleAuthor'){
|
||||
$sError = '';
|
||||
foreach ($aAuthor as $key => $value) {
|
||||
if(empty($value['icon'])){
|
||||
continue;
|
||||
}
|
||||
//文章二维码图片上传
|
||||
$aUpload = ['email' => $value['email'],'icon' => $value['icon'],'user_id' => $value['user_id']];
|
||||
$aUpload += $aWechatParam;
|
||||
$aAuthorResult = json_decode($this->updateArticleAuthor($aUpload),true);
|
||||
$aAuthorData = empty($aAuthorResult['data']) ? [] : $aAuthorResult['data'];
|
||||
if(empty($aAuthorData)){
|
||||
$sError .= empty($aAuthorResult['msg']) ? "The corresponding author's avatar upload failed:".$value['email'].';' : $aAuthorResult['msg']."-".$value['email'].';';
|
||||
}
|
||||
$aAuthor[$key]['icon'] = empty($aAuthorData['media_url']) ? $value['icon'] : $aAuthorData['media_url'];
|
||||
}
|
||||
$aParam['author_info'] = $aAuthor;
|
||||
}
|
||||
if(!empty($sError)){
|
||||
$this->setErrorForRedis($iArticleId,$sError);
|
||||
return json_encode(['status' => 2,'msg' => $sError]);
|
||||
}
|
||||
//写入redis 上传执行完成
|
||||
$this->setStepForRedis($iArticleId,'finish');
|
||||
//返回参数
|
||||
return json_encode(['status' => 1,'msg' => 'Upload successful','data' => $aParam]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取素材
|
||||
*/
|
||||
|
||||
public function getMaterial($aParam = []){
|
||||
|
||||
//获取参数
|
||||
$aParam = empty($aParam) ? $this->input->post() : $aParam;
|
||||
|
||||
//文章ID
|
||||
$iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id'];
|
||||
|
||||
//必填验证
|
||||
if(empty($iArticleId)){
|
||||
return json_encode(['status' => 2,'msg' => 'article_id is empty']);
|
||||
}
|
||||
//期刊ID
|
||||
$iJournalId = empty($aParam['journal_id']) ? '' : $aParam['journal_id'];
|
||||
if(empty($iJournalId)){
|
||||
return json_encode(['status' => 2,'msg' => 'journal_id is empty']);
|
||||
}
|
||||
|
||||
//返回数据定义空数组
|
||||
$aData = [];
|
||||
//文章图片素材
|
||||
$aWhere = ['article_id' => $iArticleId,'is_delete' => 2];
|
||||
$aData['ai_article_material'] = Db::name('ai_article_material')->field('media_id,media_url')->where($aWhere)->find();
|
||||
//文章二维码素材
|
||||
$aData['ai_article_qrcode'] = Db::name('ai_article_qrcode')->field('media_id,media_url')->where($aWhere)->find();
|
||||
//期刊素材
|
||||
$aJournalWhere = ['journal_id' => $iJournalId,'is_delete' => 2];
|
||||
$aData['ai_journal_material'] = Db::name('ai_journal_material')->field('media_id,media_url')->where($aJournalWhere)->find();
|
||||
//期刊二维码
|
||||
$aData['ai_journal_qrcode'] = Db::name('ai_journal_qrcode')->field('media_id,media_url')->where($aJournalWhere)->find();
|
||||
//期刊编辑
|
||||
$aData['ai_journal_editor_material'] = Db::name('ai_journal_editor_material')->field('media_id,media_url')->where($aJournalWhere)->find();
|
||||
//文章作者
|
||||
if(!empty($aParam['author_info'])){
|
||||
$aEmail = array_column($aParam['author_info'], 'email');
|
||||
$aWhere = ['email' => ['in',$aEmail],'is_delete' => 2];
|
||||
$aData['ai_author_material'] = Db::name('ai_author_material')->field('user_id,email,media_id,media_url')->where($aWhere)->find();
|
||||
}
|
||||
return json_encode(['status' => 1,'msg' => 'Successfully obtained data','data' => $aData]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,6 @@ class Wechat
|
||||
return FALSE;
|
||||
}
|
||||
$result = json_decode($result, true);
|
||||
|
||||
if (!empty($result['access_token'])) {
|
||||
$sToken = $result['access_token'];
|
||||
Cache::set($this->sWechatAccessToken.'_'.$this->sAppID, $sToken, 7000); // 有效期7200秒
|
||||
@@ -305,19 +304,18 @@ class Wechat
|
||||
return json_encode($aInfo);
|
||||
}
|
||||
|
||||
// //CURL请求
|
||||
// $sUrl = "https://api.weixin.qq.com/cgi-bin/freepublish/submit?access_token={$sToken}";
|
||||
// //参数组装
|
||||
// $sJsonData = json_encode([
|
||||
// 'media_id' => $sMediaId
|
||||
// ], JSON_UNESCAPED_UNICODE);
|
||||
// //发送CURL请求
|
||||
// $aCurlResult = $this->curlWechatApi($sUrl,$sJsonData);
|
||||
// if($aCurlResult['status'] != 1){
|
||||
// return $aCurlResult;
|
||||
// }
|
||||
// $response = empty($aCurlResult['data']) ? [] : $aCurlResult['data'];
|
||||
$response['publish_id'] = '100000001';
|
||||
//CURL请求
|
||||
$sUrl = "https://api.weixin.qq.com/cgi-bin/freepublish/submit?access_token={$sToken}";
|
||||
//参数组装
|
||||
$sJsonData = json_encode([
|
||||
'media_id' => $sMediaId
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
//发送CURL请求
|
||||
$aCurlResult = $this->curlWechatApi($sUrl,$sJsonData);
|
||||
if($aCurlResult['status'] != 1){
|
||||
return $aCurlResult;
|
||||
}
|
||||
$response = empty($aCurlResult['data']) ? [] : $aCurlResult['data'];
|
||||
//判断是否推送成功
|
||||
if(empty($response['publish_id'])){
|
||||
return json_encode(['status' => 9,'msg' => "The publish_id of the published draft was not obtained"]);
|
||||
@@ -345,25 +343,24 @@ $response['publish_id'] = '100000001';
|
||||
return json_encode(['status' => 2,'msg' => 'Please configure the AppID and AppSecret']);
|
||||
}
|
||||
|
||||
// //获取Token
|
||||
// $sToken = $this->getAccessToken();
|
||||
// if (empty($sToken)) {
|
||||
// return json_encode(['status' => 3, 'msg' => 'Failed to obtain access_token']);
|
||||
// }
|
||||
//获取Token
|
||||
$sToken = $this->getAccessToken();
|
||||
if (empty($sToken)) {
|
||||
return json_encode(['status' => 3, 'msg' => 'Failed to obtain access_token']);
|
||||
}
|
||||
|
||||
// //CURL请求
|
||||
// $sUrl = "https://api.weixin.qq.com/cgi-bin/freepublish/get?access_token={$sToken}";
|
||||
// //参数组装
|
||||
// $sJsonData = json_encode([
|
||||
// 'publish_id' => $sPublishId
|
||||
// ], JSON_UNESCAPED_UNICODE);
|
||||
// //发送CURL请求
|
||||
// $aCurlResult = $this->curlWechatApi($sUrl,$sJsonData);
|
||||
// $response = empty($aCurlResult['data']) ? [] : $aCurlResult['data'];
|
||||
// if(empty($response)){
|
||||
// return json_encode(['status' => 4,'msg' => 'Query failed']);
|
||||
// }
|
||||
$response = ['publish_id' => '1000001','publish_status' => 2,'article_id'=> '','article_detail','fail_idx' => [1,2]];
|
||||
//CURL请求
|
||||
$sUrl = "https://api.weixin.qq.com/cgi-bin/freepublish/get?access_token={$sToken}";
|
||||
//参数组装
|
||||
$sJsonData = json_encode([
|
||||
'publish_id' => $sPublishId
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
//发送CURL请求
|
||||
$aCurlResult = $this->curlWechatApi($sUrl,$sJsonData);
|
||||
$response = empty($aCurlResult['data']) ? [] : $aCurlResult['data'];
|
||||
if(empty($response)){
|
||||
return json_encode(['status' => 4,'msg' => 'Query failed']);
|
||||
}
|
||||
return json_encode(['status' => 1,'msg' => 'query was successful','data' => $response]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user