| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- namespace backend\modules\admin\controllers;
- use addons\UpgradeGift\common\listeners\UpgradeGiftListener;
- use backend\controllers\AdminController;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\CurlHelper;
- use common\models\addons\AddonsCombo;
- use common\models\backend\SystemVersionLog;
- use common\models\common\Addons;
- use services\common\UpgradeService;
- use Yii;
- use yii\helpers\Json;
- use Exception;
- /**
- * 更新管理
- *
- * @Author zmz
- * @DateTime 2022-10-30 10:11:22
- * @copyright: Copyright (c) 2022 广东七件事集团
- */
- class UpgradeController extends AdminController
- {
- public function actionIndex()
- {
- $retuest = Yii::$app->request;
- if ($retuest->isAjax) {
- if ($retuest->isPost) {
- $post = Yii::$app->request->post();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($post,[
- [['limit'],'default','value' => '15'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [];
- $order = 'id desc';
- $params = array('where' => $where, 'order'=>$order, 'limit' => $post['limit']);
- $status_msg = [
- -1 => '更新失败',
- 0 => '更新中',
- 1 => '更新完成'
- ];
- $list = SystemVersionLog::listPage($params, false, true);
- foreach($list['list'] as $key => $value){
- $value['status_msg'] = $status_msg[$value['status']] ?? '';
- //upgrade_addons_version
- if(!$value['upgrade_addons_version']){
- $value['addons_version'] = '';
- }else{
- $upgrade_addons_version = explode(',',$value['upgrade_addons_version']);
- $tmp_upgrade_addons_version = [];
- foreach($upgrade_addons_version as $v){
- $v = explode(':',$v);
- $tmp_upgrade_addons_version[$v[0]] = $v[1];
- }
- $tmp_addons_version = [];
- $addons_version = explode(',',$value['addons_version']);
- foreach($addons_version as $v){
- $v = explode(':',$v);
- if(array_key_exists($v[0],$tmp_upgrade_addons_version)){
- //存在更新的插件
- $tmp_addons_version[] = implode(':',$v);
- }
- }
- $value['addons_version'] = implode(',',$tmp_addons_version);
- }
- $list['list'][$key] = $value;
- }
- // 检查最近一次更新时间
- $list['alert'] = SystemVersionLog::getUpgradeAlert();
- return $this->success('获取成功', $list);
- }
- }else {
- return $this->render($this->action->id);
- }
- }
- /**
- * 请求中台系统,检测是否有版本更新
- */
- public function actionCheck()
- {
- try{
- $version_list = $this->getNowVersion();
- //请求远程接口
- $result = Yii::$app->services->upgrade->getNewVersion($version_list);
- if(!$result){
- throw new Exception('请求远程接口出现错误');
- }
- Yii::error('actionCheck:'.json_encode($result));
- if($result['code']){
- throw new Exception($result['msg']);
- }
- return $this->success('存在版本更新',$result['data']);
- }catch(Exception $e){
- return $this->error($e->getMessage());
- }
- }
- /**
- * 升级版本
- */
- public function actionUpgrade()
- {
- try {
- $dbConfig = \Yii::$app->getComponents()['db'];
- $dsn = $dbConfig['dsn'];
- preg_match('|port=(.*?);|',$dsn,$match);
- $port = $match[1] ?? '';
- if($port != 3306){
- throw new Exception('鉴于当前系统是分布式部署,请联系技术处理版本更新');
- }
- //add lock
- Yii::$app->services->upgrade->lockUpgrade();
- $version_list = $this->getNowVersion();
- //请求远程接口
- $result = Yii::$app->services->upgrade->getNewVersion($version_list);
- if(!$result || !$result['data']){
- $msg = $result['msg'] ?? '请求远程接口出现错误';
- throw new Exception($msg);
- }
- $data = [
- 'now_version' => $version_list,
- 'upgrade' => $result['data']
- ];
- //写入队列,下载更新
- Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $data, UpgradeService::class, 'upgrade');
- return $this->success('正在更新,请等待5分钟后刷新列表查看...');
- }catch(Exception $e){
- return $this->error($e->getMessage());
- }
- }
- /**
- * 获取当前版本信息
- * @return void
- */
- private function getNowVersion(){
- $addons_path = Yii::getAlias('@addons');
- $addons = [];
- foreach( glob( $addons_path . '/*') as $item ){
- if( is_dir( $item )){
- $addons_name = str_replace($addons_path.'/','',$item);
- $version_file = $item.'/version';
- if(file_exists($version_file)){
- $version = file_get_contents($version_file);
- $addons[] = $addons_name.':'.trim($version);
- }else{
- //默认版本 1.0.0
- $addons[] = $addons_name.':1.0.0';
- }
- }
- }
- //主包版本
- if(file_exists(Yii::getAlias('@webpath').'/version')){
- $mian_version = file_get_contents(Yii::getAlias('@webpath').'/version');
- $mian_version = 'mian:'.trim($mian_version);
- }else{
- $mian_version = 'mian:1.0.0';
- }
- //h5版本
- if(file_exists(Yii::getAlias('@webpath').'/h5/version')){
- $h5_version = file_get_contents(Yii::getAlias('@webpath').'/h5/version');
- $h5_version = 'h5:'.trim($h5_version);
- }else{
- $h5_version = 'h5:1.0.0';
- }
- // 发薪猫版本
- $salary_h5_version = '';
- if (Addons::install('Salary')) {
- if(file_exists(Yii::getAlias('@webpath').'/salary/version')){
- $salary_h5_version = file_get_contents(Yii::getAlias('@webpath').'/salary/version');
- $salary_h5_version = 'salary-h5:'.trim($salary_h5_version) . ',';
- }else{
- $salary_h5_version = 'salary-h5:1.0.0,';
- }
- }
- //$system_info = file_get_contents(Yii::getAlias('@webpath').'/system.info');
- $version_list = $mian_version.','.$h5_version.','.$salary_h5_version.implode(',',$addons);
- return $version_list;
- }
- /**
- * 强制降低h5版本
- *
- * @return void
- */
- public function actionForceDowngradeH5()
- {
- try {
- Yii::$app->services->upgrade->forceDowngradeH5();
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
-
- return $this->success('降级成功');
- }
- }
|