setName('download') ->addArgument('type', Argument::OPTIONAL, "type") ->addArgument('from', Argument::OPTIONAL, "from") ->addArgument('root_id', Argument::OPTIONAL, "root_id") ->addArgument('data_id', Argument::OPTIONAL, "data_id") ->setDescription('download wx img to oss'); } protected function execute(Input $input, Output $output) { $type = $input->getArgument('type'); $from = $input->getArgument('from'); $root_id = $input->getArgument('root_id'); $data_id = $input->getArgument('data_id'); if ($type == 'talkskill') { $sql = "media_id is not null and media_id <> '' and (imgs='' or imgs is null)"; $media_id = Talkskill::where($sql)->column('id,media_id'); foreach ($media_id as $k => $v) { $arr = explode(',', $v['media_id']); $imgs = []; foreach ($arr as $k2 => $v2) { if (!empty($from) && $from == 'wework'){ $imgs[] = $this->getImgWework($v2, 'Talkskill', $root_id); } else { $imgs[] = $this->getImg($v2, 'Talkskill'); } } Talkskill::where('id',$v['id'])->update(['imgs'=> implode(',', $imgs)]); } } elseif ($type == 'dailyword') { $sql = "media_id is not null and media_id <> '' and (enclosure_file='' or enclosure_file is null)"; $media_id = DailyWorkModel::where($sql)->column('id,media_id'); foreach ($media_id as $k => $v) { $arr = explode(',', $v['media_id']); $imgs = []; foreach ($arr as $k2 => $v2) { $imgs[] = $this->getImg($v2, 'Dailywork'); } DailyWorkModel::where('id',$v['id'])->update(['enclosure_file'=> implode(',', $imgs)]); } } elseif ($type == 'profile') { $sql = "media_id is not null and media_id <> '' and (qrcode='' or qrcode is null)"; $media_id = Employee::where($sql)->column('id,media_id'); foreach ($media_id as $k => $v) { $arr = explode(',', $v['media_id']); $imgs = []; foreach ($arr as $k2 => $v2) { $imgs[] = $this->getImg($v2, 'Employee'); } Employee::where('id',$v['id'])->update(['qrcode'=> implode(',', $imgs)]); } } elseif ($type == 'customer'){ // $sql = "media_id is not null and media_id <> '' and (img='' or img is null)"; if($data_id) $condition = ['id'=>$data_id]; else $condition = [['media_id', 'NOTNULL', ''], ['media_id', '<>', '']]; $media_id = CustomerVisitLog::where($condition)->where(function($query){ $query->whereOr([['img', '=', ''], ['img', 'NULL', '']]); })->column('id,media_id'); foreach ($media_id as $k => $v) { if(empty($v['media_id'])) continue; $arr = explode(',', $v['media_id']); $imgs = []; foreach ($arr as $k2 => $v2) { if (!empty($from) && $from == 'wework'){ $imgs[] = $this->getImgWework($v2, 'Customer', $root_id); } else { $imgs[] = $this->getImg($v2, 'Customer'); } } CustomerVisitLog::where('id',$v['id'])->update(['img'=> implode(',', $imgs)]); } } elseif ($type == 'customer1'){ // $sql = "media_id1 is not null and media_id1 <> '' and (img1='' or img1 is null)"; if($data_id) $condition = ['id'=>$data_id]; else $condition = [['media_id1', 'NOTNULL', ''], ['media_id1', '<>', '']]; $media_id = CustomerVisitLog::where($condition)->where(function($query){ $query->whereOr([['img1', '=', ''], ['img1', 'NULL', '']]); })->column('id,media_id1 media_id'); foreach ($media_id as $k => $v) { if(empty($v['media_id'])) continue; $arr = explode(',', $v['media_id']); $imgs = []; foreach ($arr as $k2 => $v2) { if (!empty($from) && $from == 'wework'){ $imgs[] = $this->getImgWework($v2, 'Customer', $root_id); } else { $imgs[] = $this->getImg($v2, 'Customer'); } } CustomerVisitLog::where('id', $v['id'])->update(['img1'=> implode(',', $imgs)]); } } elseif ($type == 'delivery') { $sql = "delivery_media_id is not null and delivery_media_id <> '' and (delivery_img='' or delivery_img is null)"; $media_id = CustomerVisitLog::where($sql)->column('id,delivery_media_id media_id'); foreach ($media_id as $k => $v) { $arr = explode(',', $v['media_id']); $imgs = []; foreach ($arr as $k2 => $v2) { if (!empty($from) && $from == 'wework'){ $imgs[] = $this->getImgWework($v2, 'Customer', $root_id); } else { $imgs[] = $this->getImg($v2, 'Customer'); } } CustomerVisitLog::where('id', $v['id'])->update(['delivery_img'=> implode(',', $imgs)]); } } elseif ($type == 'sign') { $sql = "sign_media_id is not null and sign_media_id <> '' and (sign_img='' or sign_img is null)"; $media_id = CustomerVisitLog::where($sql)->column('id,sign_media_id media_id'); foreach ($media_id as $k => $v) { $arr = explode(',', $v['media_id']); $imgs = []; foreach ($arr as $k2 => $v2) { if (!empty($from) && $from == 'wework'){ $imgs[] = $this->getImgWework($v2, 'Customer', $root_id); } else { $imgs[] = $this->getImg($v2, 'Customer'); } } CustomerVisitLog::where('id', $v['id'])->update(['sign_img'=> implode(',', $imgs)]); } } elseif ($type == 'material_case'){ $list = MaterialCase::where('img_download_status', '=', 1)->column('id,img_content_data'); foreach ($list as $k => $v){ $img_content = []; $save_content = []; $cover_img = ''; $content = json_decode($v['img_content_data'], true); foreach ($content as $kk => $vv){ if (!empty($vv['serverId'])){ $img = $this->getImg($vv['serverId'], 'Material'); } else { $img = $vv['img'] ?? ''; } $img_content[] = [ 'img'=> $img, 'text'=> $vv['text'] ?? '' ]; // 封面默认为第一张图 if ($kk == 0){ $cover_img = $img; } } $save_content['cover_img'] = $cover_img; $save_content['img_content'] = json_encode($img_content); $save_content['img_content_data'] = ''; $save_content['img_download_status'] = 0; MaterialCase::where('id', $v['id'])->update($save_content); } } elseif ($type== 'building'){ $list = Building::where([['down_status', '=', 1]])->column('id,media_id'); foreach ($list as $k => $v){ $img_content = []; $save_content = []; $cover = ''; $content = json_decode($v['media_id'], true); foreach ($content as $kk => $vv){ if (!empty($vv['serverId'])){ $img = $this->getImg($vv['serverId'], 'Building'); } else { $img = $vv['img'] ?? ''; } $cover.=$img.','; } $save_content['cover'] = trim($cover,','); $save_content['down_status'] = 0; Building::where('id', $v['id'])->update($save_content); } } elseif ($type == 'buildinghousetype'){ $list = BuildingHousetype::where([['down_status', '=', 1]])->column('id,media_id,house_img_media_id'); foreach ($list as $k => $v){ $img_content = []; $save_content = []; $house_img = ''; $cover = ''; if (!empty($v['media_id'])) { $content = json_decode($v['media_id'], true); foreach ($content as $kk => $vv){ if (!empty($vv['serverId'])){ $img = $this->getImg($vv['serverId'], 'Building'); } else { $img = $vv['img'] ?? ''; } $cover.=$img.','; } } if (!empty($v['house_img_media_id'])) { $house_img_content = json_decode($v['house_img_media_id'], true); foreach ($house_img_content as $kk => $vv){ if (!empty($vv['serverId'])){ $img = $this->getImg($vv['serverId'], 'Building'); } else { $img = $vv['img'] ?? ''; } $house_img .= $img . ','; } } $save_content['img_content'] = trim($cover,','); $save_content['house_img'] = trim($house_img,','); $save_content['down_status'] = 0; $save_content['media_id'] = ''; $save_content['house_img_media_id'] = ''; BuildingHousetype::where('id', $v['id'])->update($save_content); } } elseif ($type == 'buildingdevcase'){ $list = BuildingDevelopCase::where([['down_status', '=', 1],['from','=',1]])->column('id,media_id'); foreach ($list as $k => $v){ $img_content = []; $save_content = []; $cover = ''; $content = json_decode($v['media_id'], true); foreach ($content as $kk => $vv){ if (!empty($vv['serverId'])){ $img = $this->getImg($vv['serverId'], 'Building'); } else { $img = $vv['img'] ?? ''; } $img_content[] = [ 'img'=> $img, 'text'=> $vv['text'] ?? '' ]; // 封面默认为第一张图 if ($kk == 0){ $cover_img = $img; } } $save_content['img_content'] = json_encode($img_content); $save_content['down_status'] = 0; $save_content['cover'] = $cover_img; BuildingDevelopCase::where('id', $v['id'])->update($save_content); } } elseif ($type == 'buildingprogress'){ $list = BuildingProgress::where([['down_status', '=', 1]])->column('id,media_id'); foreach ($list as $k => $v){ $img_content = []; $save_content = []; $cover = ''; $content = json_decode($v['media_id'], true); foreach ($content as $kk => $vv){ if (!empty($vv['serverId'])){ $img = $this->getImg($vv['serverId'], 'Building'); } else { $img = $vv['img'] ?? ''; } $cover.=$img.','; } $save_content['down_status'] = 0; $save_content['img'] = trim($cover,','); BuildingProgress::where('id', $v['id'])->update($save_content); } } elseif ($type== 'agent_article_file'){ $list = AgentArticle::where([['down_status', '=', 1]])->column('id,file_media_id'); foreach ($list as $k => $v){ $save_content = []; $files = ''; $content = json_decode($v['file_media_id'], true); foreach ($content as $kk => $vv){ if (!empty($vv['serverId'])){ $img = $this->getImg($vv['serverId'], 'Agent'); } elseif(!empty($vv['img'])) { $img = $vv['img'] ?? ''; } else { $img = $vv['video'] ?? ''; } $files .= $img.','; } $save_content['file'] = trim($files,','); $save_content['down_status'] = 0; AgentArticle::where('id', $v['id'])->update($save_content); } } elseif ($type== 'agent_share_log'){ $list = AgentShareLog::where([['down_status', '=', 1]])->column('id,file_media_id'); foreach ($list as $k => $v){ $save_content = []; $files = ''; $content = json_decode($v['file_media_id'], true); foreach ($content as $kk => $vv){ if (!empty($vv['serverId'])){ $img = $this->getImg($vv['serverId'], 'Agent'); } else { $img = $vv['img'] ?? ''; } $files .= $img.','; } $save_content['img'] = trim($files,','); $save_content['down_status'] = 0; AgentShareLog::where('id', $v['id'])->update($save_content); } } elseif ($type == 'construction_record') { $list = ConstructionRecord::where([['down_status', '=', 1],['media_id','<>','']])->column('id,media_id,img'); foreach ($list as $k => $v) { $img = []; $arr = explode(',',$v['media_id']); foreach ($arr as $v2) { $img[] = $this->getImg($v2, 'Construction'); } $img = $v['img'] ? $v['img'].','.implode(',',$img) : implode(',',$img); ConstructionRecord::where('id', $v['id'])->update(['img'=>$img,'down_status'=>0,'media_id'=>'']); } } elseif ($type == 'crm_ext_field') { $list = Customer::where([['ext_down_status', '=', 1]])->column('id,ext'); foreach ($list as $k => $item){ $extdata = json_decode($item['ext'], true); foreach ($extdata as $keys=>$v){ //只有type=6是图片类型 if(isset($v['type']) && $v['type'] == 6) { $save_content = []; $files = ''; $content = json_decode($v['value'], true); foreach ($content as $kk => $vv){ if (!empty($vv['serverId'])){ $img = $this->getImg($vv['serverId'], 'Construction'); } else { $img = $vv['img'] ?? ''; } $files .= $img.','; } $extdata[$keys]['value'] = trim($files,','); unset($extdata[$keys]['type']); } } $extdata = json_encode($extdata); Customer::where('id', $item['id'])->update(['ext_down_status' => 0, 'ext' => $extdata]); } } elseif ($type == 'save_portrait_field') { $list = CustomerVisitLog::where([['down_portrait_field_status', '=', 1]])->column('id,save_portrait_field'); foreach ($list as $k => $item){ $extdata = json_decode($item['save_portrait_field'], true); foreach ($extdata as $keys=>$v){ //只有type=6是图片类型 if(isset($v['type']) && $v['type'] == 6 && !empty($v['value'])) { $save_content = []; $files = ''; $content = json_decode($v['value'], true); foreach ($content as $kk => $vv){ if (!empty($vv['serverId'])){ $img = $this->getImg($vv['serverId'], 'Construction'); } else { $img = $vv['img'] ?? ''; } $files .= $img.','; } $extdata[$keys]['value'] = trim($files,','); $extdata[$keys]['valname'] = trim($files,','); } } $extdata = json_encode($extdata); CustomerVisitLog::where('id', $item['id'])->update(['down_portrait_field_status' => 0, 'save_portrait_field' => $extdata]); } }elseif($type == 'wisdom'){ $media_id = CustomerWisdomImg::where([['img','=',''],['media_id','<>','']])->column('id,media_id,img'); foreach ($media_id as $k => $v) { $img = $this->getImg($v['media_id'], 'wisdom'); CustomerWisdomImg::where('id', $v['id'])->update(['img'=> $img]); } } $output->writeln('OK'); } public function getImg($media_id, $model='Talkskill'){ $appid = config('app.official_appid'); $secret = config('app.official_secret'); $wx_url = 'https://api.weixin.qq.com/cgi-bin/media/get?access_token='; $accesstoken = Cache::get('zqxg_offi_access_token'); if (!$accesstoken) { $accesstoken = (new Oplatform())->AccessToken($appid,$secret); $accesstoken = $accesstoken['access_token']; Cache::set('zqxg_offi_access_token',$accesstoken,7100); } $url = $wx_url.$accesstoken.'&media_id='.$media_id; $a = file_get_contents($url); $name = uniqid().'.jpg'; $path = './upload/'.$name; file_put_contents($path,$a); $file = $model.'/'.$name;//远程保存地址 $a = $this->ossUpload($file,$path); unlink($path);//删除本地文件 return $file; } /** * 企业微信获取图片 * @param $media_id * @param string $model * @param $root_id */ public function getImgWework($media_id, $model='Talkskill', $root_id){ $company_id = Company::where('root_id', '=', $root_id)->value('id'); $company_setting = WeworksingleCompanySetting::where([['company_id', '=', $company_id], ['corp_id', '<>', '']])->findOrEmpty(); $single_config['corp_id'] = $company_setting['corp_id']; $single_config['agent_id'] = $company_setting['agent_id']; $single_config['secret'] = $company_setting['agent_secret']; $app = Factory::work($single_config); $a = $app->media->get($media_id); $name = uniqid().'.jpg'; $path = './upload/'.$name; file_put_contents($path,$a); $file = $model.'/'.$name;//远程保存地址 $a = $this->ossUpload($file,$path); unlink($path);//删除本地文件 return $file; } /* * oss文件上传 */ public function ossUpload($path, $file) { $accessKeyId = config('app.ali_oss_access_key_id'); $accessKeySecret = config('app.ali_oss_access_key_secret'); $endpoint = config('app.ali_oss_end_point'); $bucket = config('app.ali_oss_bucket'); $oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint); try { $oss->uploadFile($bucket, $path, $file); } catch (OssException $e) { return false; } return true; } }