123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- <?php
- namespace app\command;
- use app\model\Company;
- use app\model\ConstructionRecord;
- use app\model\MaterialCase;
- use app\model\WeworksingleCompanySetting;
- use EasyWeChat\Factory;
- use think\console\Command;
- use think\console\input\Argument;
- use think\console\Input;
- use think\console\Output;
- use app\model\Talkskill;
- use think\facade\Cache;
- use wx\Oplatform;
- use OSS\OssClient;
- use app\model\DailyWork as DailyWorkModel;
- use app\model\Employee;
- use app\model\CustomerVisitLog;
- use app\model\Building;
- use app\model\BuildingDevelopCase;
- use app\model\BuildingHousetype;
- use app\model\BuildingProgress;
- use app\model\AgentArticle;
- use app\model\AgentShareLog;
- use app\model\Customer;
- use app\model\CustomerWisdomImg;
- class Download extends Command
- {
- protected function configure()
- {
- $this->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;
- }
- }
|