Download.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. namespace app\command;
  3. use app\model\Company;
  4. use app\model\ConstructionRecord;
  5. use app\model\MaterialCase;
  6. use app\model\WeworksingleCompanySetting;
  7. use EasyWeChat\Factory;
  8. use think\console\Command;
  9. use think\console\input\Argument;
  10. use think\console\Input;
  11. use think\console\Output;
  12. use app\model\Talkskill;
  13. use think\facade\Cache;
  14. use wx\Oplatform;
  15. use OSS\OssClient;
  16. use app\model\DailyWork as DailyWorkModel;
  17. use app\model\Employee;
  18. use app\model\CustomerVisitLog;
  19. use app\model\Building;
  20. use app\model\BuildingDevelopCase;
  21. use app\model\BuildingHousetype;
  22. use app\model\BuildingProgress;
  23. use app\model\AgentArticle;
  24. use app\model\AgentShareLog;
  25. use app\model\Customer;
  26. use app\model\CustomerWisdomImg;
  27. class Download extends Command
  28. {
  29. protected function configure()
  30. {
  31. $this->setName('download')
  32. ->addArgument('type', Argument::OPTIONAL, "type")
  33. ->addArgument('from', Argument::OPTIONAL, "from")
  34. ->addArgument('root_id', Argument::OPTIONAL, "root_id")
  35. ->addArgument('data_id', Argument::OPTIONAL, "data_id")
  36. ->setDescription('download wx img to oss');
  37. }
  38. protected function execute(Input $input, Output $output)
  39. {
  40. $type = $input->getArgument('type');
  41. $from = $input->getArgument('from');
  42. $root_id = $input->getArgument('root_id');
  43. $data_id = $input->getArgument('data_id');
  44. if ($type == 'talkskill') {
  45. $sql = "media_id is not null and media_id <> '' and (imgs='' or imgs is null)";
  46. $media_id = Talkskill::where($sql)->column('id,media_id');
  47. foreach ($media_id as $k => $v) {
  48. $arr = explode(',', $v['media_id']);
  49. $imgs = [];
  50. foreach ($arr as $k2 => $v2) {
  51. if (!empty($from) && $from == 'wework'){
  52. $imgs[] = $this->getImgWework($v2, 'Talkskill', $root_id);
  53. } else {
  54. $imgs[] = $this->getImg($v2, 'Talkskill');
  55. }
  56. }
  57. Talkskill::where('id',$v['id'])->update(['imgs'=> implode(',', $imgs)]);
  58. }
  59. } elseif ($type == 'dailyword') {
  60. $sql = "media_id is not null and media_id <> '' and (enclosure_file='' or enclosure_file is null)";
  61. $media_id = DailyWorkModel::where($sql)->column('id,media_id');
  62. foreach ($media_id as $k => $v) {
  63. $arr = explode(',', $v['media_id']);
  64. $imgs = [];
  65. foreach ($arr as $k2 => $v2) {
  66. $imgs[] = $this->getImg($v2, 'Dailywork');
  67. }
  68. DailyWorkModel::where('id',$v['id'])->update(['enclosure_file'=> implode(',', $imgs)]);
  69. }
  70. } elseif ($type == 'profile') {
  71. $sql = "media_id is not null and media_id <> '' and (qrcode='' or qrcode is null)";
  72. $media_id = Employee::where($sql)->column('id,media_id');
  73. foreach ($media_id as $k => $v) {
  74. $arr = explode(',', $v['media_id']);
  75. $imgs = [];
  76. foreach ($arr as $k2 => $v2) {
  77. $imgs[] = $this->getImg($v2, 'Employee');
  78. }
  79. Employee::where('id',$v['id'])->update(['qrcode'=> implode(',', $imgs)]);
  80. }
  81. } elseif ($type == 'customer'){
  82. // $sql = "media_id is not null and media_id <> '' and (img='' or img is null)";
  83. if($data_id) $condition = ['id'=>$data_id];
  84. else $condition = [['media_id', 'NOTNULL', ''], ['media_id', '<>', '']];
  85. $media_id = CustomerVisitLog::where($condition)->where(function($query){
  86. $query->whereOr([['img', '=', ''], ['img', 'NULL', '']]);
  87. })->column('id,media_id');
  88. foreach ($media_id as $k => $v) {
  89. if(empty($v['media_id'])) continue;
  90. $arr = explode(',', $v['media_id']);
  91. $imgs = [];
  92. foreach ($arr as $k2 => $v2) {
  93. if (!empty($from) && $from == 'wework'){
  94. $imgs[] = $this->getImgWework($v2, 'Customer', $root_id);
  95. } else {
  96. $imgs[] = $this->getImg($v2, 'Customer');
  97. }
  98. }
  99. CustomerVisitLog::where('id',$v['id'])->update(['img'=> implode(',', $imgs)]);
  100. }
  101. } elseif ($type == 'customer1'){
  102. // $sql = "media_id1 is not null and media_id1 <> '' and (img1='' or img1 is null)";
  103. if($data_id) $condition = ['id'=>$data_id];
  104. else $condition = [['media_id1', 'NOTNULL', ''], ['media_id1', '<>', '']];
  105. $media_id = CustomerVisitLog::where($condition)->where(function($query){
  106. $query->whereOr([['img1', '=', ''], ['img1', 'NULL', '']]);
  107. })->column('id,media_id1 media_id');
  108. foreach ($media_id as $k => $v) {
  109. if(empty($v['media_id'])) continue;
  110. $arr = explode(',', $v['media_id']);
  111. $imgs = [];
  112. foreach ($arr as $k2 => $v2) {
  113. if (!empty($from) && $from == 'wework'){
  114. $imgs[] = $this->getImgWework($v2, 'Customer', $root_id);
  115. } else {
  116. $imgs[] = $this->getImg($v2, 'Customer');
  117. }
  118. }
  119. CustomerVisitLog::where('id', $v['id'])->update(['img1'=> implode(',', $imgs)]);
  120. }
  121. } elseif ($type == 'delivery') {
  122. $sql = "delivery_media_id is not null and delivery_media_id <> '' and (delivery_img='' or delivery_img is null)";
  123. $media_id = CustomerVisitLog::where($sql)->column('id,delivery_media_id media_id');
  124. foreach ($media_id as $k => $v) {
  125. $arr = explode(',', $v['media_id']);
  126. $imgs = [];
  127. foreach ($arr as $k2 => $v2) {
  128. if (!empty($from) && $from == 'wework'){
  129. $imgs[] = $this->getImgWework($v2, 'Customer', $root_id);
  130. } else {
  131. $imgs[] = $this->getImg($v2, 'Customer');
  132. }
  133. }
  134. CustomerVisitLog::where('id', $v['id'])->update(['delivery_img'=> implode(',', $imgs)]);
  135. }
  136. } elseif ($type == 'sign') {
  137. $sql = "sign_media_id is not null and sign_media_id <> '' and (sign_img='' or sign_img is null)";
  138. $media_id = CustomerVisitLog::where($sql)->column('id,sign_media_id media_id');
  139. foreach ($media_id as $k => $v) {
  140. $arr = explode(',', $v['media_id']);
  141. $imgs = [];
  142. foreach ($arr as $k2 => $v2) {
  143. if (!empty($from) && $from == 'wework'){
  144. $imgs[] = $this->getImgWework($v2, 'Customer', $root_id);
  145. } else {
  146. $imgs[] = $this->getImg($v2, 'Customer');
  147. }
  148. }
  149. CustomerVisitLog::where('id', $v['id'])->update(['sign_img'=> implode(',', $imgs)]);
  150. }
  151. } elseif ($type == 'material_case'){
  152. $list = MaterialCase::where('img_download_status', '=', 1)->column('id,img_content_data');
  153. foreach ($list as $k => $v){
  154. $img_content = [];
  155. $save_content = [];
  156. $cover_img = '';
  157. $content = json_decode($v['img_content_data'], true);
  158. foreach ($content as $kk => $vv){
  159. if (!empty($vv['serverId'])){
  160. $img = $this->getImg($vv['serverId'], 'Material');
  161. } else {
  162. $img = $vv['img'] ?? '';
  163. }
  164. $img_content[] = [
  165. 'img'=> $img,
  166. 'text'=> $vv['text'] ?? ''
  167. ];
  168. // 封面默认为第一张图
  169. if ($kk == 0){
  170. $cover_img = $img;
  171. }
  172. }
  173. $save_content['cover_img'] = $cover_img;
  174. $save_content['img_content'] = json_encode($img_content);
  175. $save_content['img_content_data'] = '';
  176. $save_content['img_download_status'] = 0;
  177. MaterialCase::where('id', $v['id'])->update($save_content);
  178. }
  179. } elseif ($type== 'building'){
  180. $list = Building::where([['down_status', '=', 1]])->column('id,media_id');
  181. foreach ($list as $k => $v){
  182. $img_content = [];
  183. $save_content = [];
  184. $cover = '';
  185. $content = json_decode($v['media_id'], true);
  186. foreach ($content as $kk => $vv){
  187. if (!empty($vv['serverId'])){
  188. $img = $this->getImg($vv['serverId'], 'Building');
  189. } else {
  190. $img = $vv['img'] ?? '';
  191. }
  192. $cover.=$img.',';
  193. }
  194. $save_content['cover'] = trim($cover,',');
  195. $save_content['down_status'] = 0;
  196. Building::where('id', $v['id'])->update($save_content);
  197. }
  198. } elseif ($type == 'buildinghousetype'){
  199. $list = BuildingHousetype::where([['down_status', '=', 1]])->column('id,media_id,house_img_media_id');
  200. foreach ($list as $k => $v){
  201. $img_content = [];
  202. $save_content = [];
  203. $house_img = '';
  204. $cover = '';
  205. if (!empty($v['media_id'])) {
  206. $content = json_decode($v['media_id'], true);
  207. foreach ($content as $kk => $vv){
  208. if (!empty($vv['serverId'])){
  209. $img = $this->getImg($vv['serverId'], 'Building');
  210. } else {
  211. $img = $vv['img'] ?? '';
  212. }
  213. $cover.=$img.',';
  214. }
  215. }
  216. if (!empty($v['house_img_media_id'])) {
  217. $house_img_content = json_decode($v['house_img_media_id'], true);
  218. foreach ($house_img_content as $kk => $vv){
  219. if (!empty($vv['serverId'])){
  220. $img = $this->getImg($vv['serverId'], 'Building');
  221. } else {
  222. $img = $vv['img'] ?? '';
  223. }
  224. $house_img .= $img . ',';
  225. }
  226. }
  227. $save_content['img_content'] = trim($cover,',');
  228. $save_content['house_img'] = trim($house_img,',');
  229. $save_content['down_status'] = 0;
  230. $save_content['media_id'] = '';
  231. $save_content['house_img_media_id'] = '';
  232. BuildingHousetype::where('id', $v['id'])->update($save_content);
  233. }
  234. } elseif ($type == 'buildingdevcase'){
  235. $list = BuildingDevelopCase::where([['down_status', '=', 1],['from','=',1]])->column('id,media_id');
  236. foreach ($list as $k => $v){
  237. $img_content = [];
  238. $save_content = [];
  239. $cover = '';
  240. $content = json_decode($v['media_id'], true);
  241. foreach ($content as $kk => $vv){
  242. if (!empty($vv['serverId'])){
  243. $img = $this->getImg($vv['serverId'], 'Building');
  244. } else {
  245. $img = $vv['img'] ?? '';
  246. }
  247. $img_content[] = [
  248. 'img'=> $img,
  249. 'text'=> $vv['text'] ?? ''
  250. ];
  251. // 封面默认为第一张图
  252. if ($kk == 0){
  253. $cover_img = $img;
  254. }
  255. }
  256. $save_content['img_content'] = json_encode($img_content);
  257. $save_content['down_status'] = 0;
  258. $save_content['cover'] = $cover_img;
  259. BuildingDevelopCase::where('id', $v['id'])->update($save_content);
  260. }
  261. } elseif ($type == 'buildingprogress'){
  262. $list = BuildingProgress::where([['down_status', '=', 1]])->column('id,media_id');
  263. foreach ($list as $k => $v){
  264. $img_content = [];
  265. $save_content = [];
  266. $cover = '';
  267. $content = json_decode($v['media_id'], true);
  268. foreach ($content as $kk => $vv){
  269. if (!empty($vv['serverId'])){
  270. $img = $this->getImg($vv['serverId'], 'Building');
  271. } else {
  272. $img = $vv['img'] ?? '';
  273. }
  274. $cover.=$img.',';
  275. }
  276. $save_content['down_status'] = 0;
  277. $save_content['img'] = trim($cover,',');
  278. BuildingProgress::where('id', $v['id'])->update($save_content);
  279. }
  280. } elseif ($type== 'agent_article_file'){
  281. $list = AgentArticle::where([['down_status', '=', 1]])->column('id,file_media_id');
  282. foreach ($list as $k => $v){
  283. $save_content = [];
  284. $files = '';
  285. $content = json_decode($v['file_media_id'], true);
  286. foreach ($content as $kk => $vv){
  287. if (!empty($vv['serverId'])){
  288. $img = $this->getImg($vv['serverId'], 'Agent');
  289. } elseif(!empty($vv['img'])) {
  290. $img = $vv['img'] ?? '';
  291. } else {
  292. $img = $vv['video'] ?? '';
  293. }
  294. $files .= $img.',';
  295. }
  296. $save_content['file'] = trim($files,',');
  297. $save_content['down_status'] = 0;
  298. AgentArticle::where('id', $v['id'])->update($save_content);
  299. }
  300. } elseif ($type== 'agent_share_log'){
  301. $list = AgentShareLog::where([['down_status', '=', 1]])->column('id,file_media_id');
  302. foreach ($list as $k => $v){
  303. $save_content = [];
  304. $files = '';
  305. $content = json_decode($v['file_media_id'], true);
  306. foreach ($content as $kk => $vv){
  307. if (!empty($vv['serverId'])){
  308. $img = $this->getImg($vv['serverId'], 'Agent');
  309. } else {
  310. $img = $vv['img'] ?? '';
  311. }
  312. $files .= $img.',';
  313. }
  314. $save_content['img'] = trim($files,',');
  315. $save_content['down_status'] = 0;
  316. AgentShareLog::where('id', $v['id'])->update($save_content);
  317. }
  318. } elseif ($type == 'construction_record') {
  319. $list = ConstructionRecord::where([['down_status', '=', 1],['media_id','<>','']])->column('id,media_id,img');
  320. foreach ($list as $k => $v) {
  321. $img = [];
  322. $arr = explode(',',$v['media_id']);
  323. foreach ($arr as $v2) {
  324. $img[] = $this->getImg($v2, 'Construction');
  325. }
  326. $img = $v['img'] ? $v['img'].','.implode(',',$img) : implode(',',$img);
  327. ConstructionRecord::where('id', $v['id'])->update(['img'=>$img,'down_status'=>0,'media_id'=>'']);
  328. }
  329. } elseif ($type == 'crm_ext_field') {
  330. $list = Customer::where([['ext_down_status', '=', 1]])->column('id,ext');
  331. foreach ($list as $k => $item){
  332. $extdata = json_decode($item['ext'], true);
  333. foreach ($extdata as $keys=>$v){
  334. //只有type=6是图片类型
  335. if(isset($v['type']) && $v['type'] == 6) {
  336. $save_content = [];
  337. $files = '';
  338. $content = json_decode($v['value'], true);
  339. foreach ($content as $kk => $vv){
  340. if (!empty($vv['serverId'])){
  341. $img = $this->getImg($vv['serverId'], 'Construction');
  342. } else {
  343. $img = $vv['img'] ?? '';
  344. }
  345. $files .= $img.',';
  346. }
  347. $extdata[$keys]['value'] = trim($files,',');
  348. unset($extdata[$keys]['type']);
  349. }
  350. }
  351. $extdata = json_encode($extdata);
  352. Customer::where('id', $item['id'])->update(['ext_down_status' => 0, 'ext' => $extdata]);
  353. }
  354. } elseif ($type == 'save_portrait_field') {
  355. $list = CustomerVisitLog::where([['down_portrait_field_status', '=', 1]])->column('id,save_portrait_field');
  356. foreach ($list as $k => $item){
  357. $extdata = json_decode($item['save_portrait_field'], true);
  358. foreach ($extdata as $keys=>$v){
  359. //只有type=6是图片类型
  360. if(isset($v['type']) && $v['type'] == 6 && !empty($v['value'])) {
  361. $save_content = [];
  362. $files = '';
  363. $content = json_decode($v['value'], true);
  364. foreach ($content as $kk => $vv){
  365. if (!empty($vv['serverId'])){
  366. $img = $this->getImg($vv['serverId'], 'Construction');
  367. } else {
  368. $img = $vv['img'] ?? '';
  369. }
  370. $files .= $img.',';
  371. }
  372. $extdata[$keys]['value'] = trim($files,',');
  373. $extdata[$keys]['valname'] = trim($files,',');
  374. }
  375. }
  376. $extdata = json_encode($extdata);
  377. CustomerVisitLog::where('id', $item['id'])->update(['down_portrait_field_status' => 0, 'save_portrait_field' => $extdata]);
  378. }
  379. }elseif($type == 'wisdom'){
  380. $media_id = CustomerWisdomImg::where([['img','=',''],['media_id','<>','']])->column('id,media_id,img');
  381. foreach ($media_id as $k => $v) {
  382. $img = $this->getImg($v['media_id'], 'wisdom');
  383. CustomerWisdomImg::where('id', $v['id'])->update(['img'=> $img]);
  384. }
  385. }
  386. $output->writeln('OK');
  387. }
  388. public function getImg($media_id, $model='Talkskill'){
  389. $appid = config('app.official_appid');
  390. $secret = config('app.official_secret');
  391. $wx_url = 'https://api.weixin.qq.com/cgi-bin/media/get?access_token=';
  392. $accesstoken = Cache::get('zqxg_offi_access_token');
  393. if (!$accesstoken) {
  394. $accesstoken = (new Oplatform())->AccessToken($appid,$secret);
  395. $accesstoken = $accesstoken['access_token'];
  396. Cache::set('zqxg_offi_access_token',$accesstoken,7100);
  397. }
  398. $url = $wx_url.$accesstoken.'&media_id='.$media_id;
  399. $a = file_get_contents($url);
  400. $name = uniqid().'.jpg';
  401. $path = './upload/'.$name;
  402. file_put_contents($path,$a);
  403. $file = $model.'/'.$name;//远程保存地址
  404. $a = $this->ossUpload($file,$path);
  405. unlink($path);//删除本地文件
  406. return $file;
  407. }
  408. /**
  409. * 企业微信获取图片
  410. * @param $media_id
  411. * @param string $model
  412. * @param $root_id
  413. */
  414. public function getImgWework($media_id, $model='Talkskill', $root_id){
  415. $company_id = Company::where('root_id', '=', $root_id)->value('id');
  416. $company_setting = WeworksingleCompanySetting::where([['company_id', '=', $company_id], ['corp_id', '<>', '']])->findOrEmpty();
  417. $single_config['corp_id'] = $company_setting['corp_id'];
  418. $single_config['agent_id'] = $company_setting['agent_id'];
  419. $single_config['secret'] = $company_setting['agent_secret'];
  420. $app = Factory::work($single_config);
  421. $a = $app->media->get($media_id);
  422. $name = uniqid().'.jpg';
  423. $path = './upload/'.$name;
  424. file_put_contents($path,$a);
  425. $file = $model.'/'.$name;//远程保存地址
  426. $a = $this->ossUpload($file,$path);
  427. unlink($path);//删除本地文件
  428. return $file;
  429. }
  430. /*
  431. * oss文件上传
  432. */
  433. public function ossUpload($path, $file)
  434. {
  435. $accessKeyId = config('app.ali_oss_access_key_id');
  436. $accessKeySecret = config('app.ali_oss_access_key_secret');
  437. $endpoint = config('app.ali_oss_end_point');
  438. $bucket = config('app.ali_oss_bucket');
  439. $oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  440. try {
  441. $oss->uploadFile($bucket, $path, $file);
  442. } catch (OssException $e) {
  443. return false;
  444. }
  445. return true;
  446. }
  447. }