123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace app\admin\command;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\Request;
- class FillDescription extends Command
- {
- protected function configure()
- {
- $this->setName('FillDescription')->setDescription('填充图文推荐库的简介内容');
- }
- protected function execute(Input $input, Output $output)
- {
- Request::instance()->module('admin');
- //更新智能推送库
- $this->updateSmartPush($output);
- //更新自动回复资源库
- $this->updateResponse($output);
- }
- //更新自动回复资源库
- protected function updateResponse(Output $output){
- if($response_list = model('WechatResponse')->where('type','news')->column('id,content')){
- foreach($response_list as $id => $content){
- //解析json
- if(!$source = json_decode($content,true)){
- $output->warning("id:{$id} Failed to parse data content, skipped");
- continue;
- }
- foreach($source as $key => &$val){
- try{
- //获取url
- if(!$url = $val['url'] ?? ''){
- $output->warning("id:{$id} URL acquisition failed, skipped");
- continue;
- }
- //解析book_id
- if(!$book_id = $this->getUrlParams('book_id',$url)){
- $output->warning("id:{$id} Failed to get book ID, skipped");
- continue;
- }
- //获取书籍简介
- if(!$up_description = model('Book')->where('id',$book_id)->value('description')){
- $output->warning("id:{$id} book_id:{$book_id} Books are empty of information");
- continue;
- }
- //已设置过简介
- if(isset($val['description']) && trim($val['description'])){
- $output->warning("id:{$id} book_id:{$book_id} Brief introduction is not skipped");
- continue;
- }
- $val['description'] = $up_description;
- $up_content = ['content'=>json_encode($source,JSON_UNESCAPED_UNICODE)];
- model('WechatResponse')->where('id',$id)->update($up_content);
- $output->info("id:{$id} book_id:{$book_id} Brief Introduction to Successful Update ".($val['description'] ?? ''));
- }catch (\Exception $e){
- $output->warning("id:{$id} WeChat Response Exception:".$e->getMessage());
- }
- }
- }
- }else{
- $output->warning("wechat_response table is null");
- }
- $output->newLine(1);
- $output->question("**************微信自动回复资源库修改完成**************");
- $output->newLine(1);
- }
- private function getUrlParams($name,$url){
- try{
- if($url_array = parse_url($url)){
- parse_str($url_array['query'], $query_arr);
- if(isset($query_arr[$name])){
- return $query_arr[$name];
- }else{
- return false;
- }
- }else{
- return false;
- }
- }catch (\Exception $e){
- return false;
- }catch (\Throwable $e){
- return false;
- }
- }
- //更新智能推送库
- /**
- * @param Output $output
- */
- protected function updateSmartPush(Output $output){
- $output->newLine(2);
- if($smart_list = model('SmartRecommand')->column('id,book_id,description')){
- foreach($smart_list as $id => $val){
- $book_id = $val['book_id'];
- $description = $val['description'];
- //检查简介是否为空
- if(trim($description)){
- $output->warning("id:{$id} book_id:{$book_id} Brief introduction is not skipped");
- continue;
- }
- //检查book_id
- if(!intval($book_id)){
- $output->warning("id:{$id} book_id:{$book_id} Book_id error skipped");
- continue;
- }
- //获取书籍简介信息
- if(!$up_description = model('Book')->where('id',$book_id)->value('description')){
- $output->warning("id:{$id} book_id:{$book_id} Books are empty of information");
- continue;
- }
- //更新
- model('SmartRecommand')->where('id',$id)->update(['description'=>$up_description]);
- $output->info("id:{$id} book_id:{$book_id} Brief Introduction to Successful Update ".$up_description);
- }
- }else{
- $output->warning("smart_recommand table is null");
- }
- $output->newLine(1);
- $output->question("**************智能推送库修改完成**************");
- $output->newLine(1);
- }
- }
|