| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers;
- use app\models\Admin;
- use Comodojo\Zip\Zip;
- use Curl\Curl;
- class UpdateController extends BaseController
- {
- private $api_root = 'http://v7.we10.cn/update.php';
- public function actionIndex()
- {
- $is_platform = \Yii::$app->cache->get('is_platform'); //
- $admin = get_admin();
- if ($is_platform === false && (int)$admin->store_id !== 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '您当前的系统不支持在线更新!',
- ]);
- }
- $version = 'version.json';
- $app_root = str_replace('\\', '/', \Yii::$app->basePath) . '/';
- if (!file_exists($app_root . $version)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '版本文件获取失败!'
- ]);
- }
- $json_string = file_get_contents($app_root . $version);
- $data = json_decode($json_string, true);
- $ver = $data['version'];
- // TODO: test ver
- // $ver = 8.3;
- $updatehost = $this->api_root;
- // 检查客户下一个需要更新的版本
- $lastver = $this->http_get(($updatehost . '?a=check&v=') . $ver);
- $cache_data = cache()->get('cyy_check_auth');
- if ($cache_data === false) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '您当前的系统不支持在线更新!'
- ]);
- }
- if ($cache_data['code'] == 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $cache_data['msg']
- ]);
- }
- if ($cache_data['data']['is_can'] == 0) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $cache_data['data']['expires_message'],
- ]);
- }
- // $key = $cache_data['data']['key'];
- if (empty($lastver)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '云服务器连接失败,请检查您的服务器与云服务器的连接是否正常',
- ]);
- }
- $hosturl = \Yii::$app->getHost();
- // TODO: test hosturl
- // $hosturl = 'qijian.cyyvip.com';
- // 验证授权时间
- if (time() > $cache_data['data']['expire_time']) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $cache_data['data']['expires_message'],
- ]);
- }
- // 检查版本更新内容
- $vern = $ver + 0.1;
- $chosturl = $updatehost . '?a=chanage&v=' . $vern . '&u=' . $hosturl;
- $cinfo = $this->http_get($chosturl);
- $chosturl_time = $updatehost . '?a=chanagetime&v=' . $vern . '&u=' . $hosturl;
- $update_datetime = $this->http_get($chosturl_time);
- $update_datetime = date("Y-m-d H:i:s", $update_datetime);
- //显示最新版本
- $lastverz = $updatehost . '?a=checkl&v=' . $ver . '&u=' . $hosturl;
- $lastverz = $this->http_get($lastverz);
- //获取更新记录
- $logurl = $updatehost . '?a=chanagelog&v=' . $ver . '&u=' . $hosturl;
- $log_info = $this->http_get($logurl);
- $log_info = json_decode($log_info, true);
- $time = array_column($log_info, 'time');
- array_multisort($time, SORT_DESC, $log_info);
- if ($lastver !== $ver) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => array(
- 'next_version' => array(
- 'version' => $lastver,
- 'update_datetime' => $update_datetime,
- 'desc' => $cinfo
- ),
- 'current_version' => $ver,
- 'version_list' => $log_info,
- )
- ]);
- } else {
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'current_version' => $ver,
- 'version_list' => $log_info,
- ]
- ]);
- }
- }
- /**
- * 自动更新操作
- * @return \yii\web\Response
- */
- public function actionUpdate()
- {
- $is_platform = false; //
- $admin = get_admin();
- if (\Yii::$app->isSaas()) {
- if ($admin->type == Admin::ADMIN_TYPE_SUPPLIER ) {
- //平台权限
- $is_platform = true;
- } else {
- if ($admin->username != 'admin') {
- $is_platform = true;
- } else {
- $is_platform = true;
- }
- }
- }
- if ($is_platform === false && (int)$admin->store_id !== 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '您当前的系统不支持在线更新!',
- ]);
- }
- header("Content-type: text/html; charset=utf-8");
- $version = 'version.json';
- $app_root = str_replace('\\', '/', \Yii::$app->basePath) . '/';
- $json_string = file_get_contents($app_root . $version);
- $data = json_decode($json_string, true);
- $ver = $data['version'];
- // TODO: test ver
- // $ver = 8.3;
- $cache_data = cache()->get('cyy_check_auth');
- if ($cache_data['code'] == 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $cache_data['msg'],
- ]);
- }
- if ($cache_data['data']['is_can'] == 0) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $cache_data['data']['expires_message'],
- ]);
- }
- $key = $cache_data['data']['key'];
- $hostUrl = \Yii::$app->getHost();
- // TODO: test hostUrl
- // $hostUrl = 'qijian.cyyvip.com';
- $updateHost = $this->api_root;
- // TODO: test key
- // $key = 'f25eaeaf5223b9ce';
- // $key = $cache_data['data']['key'];
- $curl = new Curl();
- $curl->get($updateHost, [
- 'a' => "update",
- 'v' => $ver,
- 'u' => $hostUrl,
- 'key' => $key,
- ]);
- $res = json_decode($curl->response, true);
- if (!$res)
- return $this->asJson([
- 'code' => 1,
- 'msg' => '更新失败,与云服务器连接失败',
- ]);
- if ($res['code'] != 0)
- return $this->asJson(
- [
- 'code' => 1,
- 'msg' => strip_tags($res['msg'])
- ]
- );
- $temp_dir = \Yii::$app->basePath . "/temp/update/version/{$ver}";
- $res = json_decode($curl->response, true);
- $this->mkdir($temp_dir);
- $src_file = "{$temp_dir}/src.zip";
- $db_file = "{$temp_dir}/db.sql";
- $pathUrl = $updateHost . '?a=down&f=' . $res['data']['src_file'];
- $get_res_src = $this->get_file($pathUrl, "src.zip", $temp_dir);
- if (!$get_res_src) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '缺失更新文件,请等待',
- ]);
- }
- $pathUrl = $updateHost . '?a=down&f=' . $res['data']['db_file'];
- $get_res_db = $this->get_file($pathUrl, "db.sql", $temp_dir);
- if (!$get_res_db) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '缺失数据库更新文件,请等待',
- ]);
- }
- $t = \Yii::$app->db->beginTransaction();
- try {
- $sql = file_get_contents($db_file);
- if ($sql) {
- \Yii::$app->db->createCommand($sql)->execute();
- }
- $zip = Zip::open($src_file);
- $zip->extract(\Yii::$app->basePath);
- $zip->close();
- unlink($src_file);
- unlink($db_file);
- $version = 'version.json';
- $version_info = array();
- $version_info['version'] = $res['data']['target_version'];
- $json_string = json_encode($version_info);
- $app_root = str_replace('\\', '/', \Yii::$app->basePath) . '/';
- file_put_contents($app_root . $version, $json_string);
- $curl->get($updateHost, [
- 'a' => "callback_update",
- 'v' => $ver,
- 'u' => $hostUrl,
- 'key' => $key,
- ]);
- $t->commit();
- if (\RUN_MODE == 'wokerman') {
- cache()->set('wokerman_update', 1);
- }
- try {
- if (class_exists('\app\utils\UpdateAfter') && method_exists(\app\utils\UpdateAfter::class, 'handle')) {
- \app\utils\UpdateAfter::handle();
- }
- } catch (\Throwable $ex) {
- debug_log($ex->getMessage());
- }
-
-
- return $this->asJson([
- 'code' => 0,
- 'msg' => '版本更新成功,已更新至v' . $res['data']['target_version'],
- ]);
- } catch (\Exception $e) {
- unlink($src_file);
- unlink($db_file);
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '更新失败:' . $e->getMessage() .$e->getLine(),
- ]);
- }
- }
- /**
- * 获取文件
- * @param $url
- * @param $name
- * @param string $folder
- * @return bool
- */
- private function get_file($url, $name, $folder = './')
- {
- set_time_limit((24 * 60) * 60);
- $destination_folder = $folder . '/';
- $newfname = $destination_folder . $name;
- $file = fopen($url, 'rb');
- if ($file) {
- $newf = fopen($newfname, 'wb');
- if ($newf) {
- while (!feof($file)) {
- fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
- }
- }
- } else {
- return false;
- }
- if ($file) {
- fclose($file);
- }
- if ($newf) {
- fclose($newf);
- }
- return true;
- }
- private function mkdir($dir)
- {
- if (!is_dir($dir)) {
- if (!$this->mkdir(dirname($dir))) {
- return false;
- }
- if (!mkdir($dir)) {
- return false;
- }
- }
- return true;
- }
- public static function http_get($url)
- {
- $oCurl = curl_init();
- if (stripos($url, "https://") !== FALSE) {
- curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
- }
- curl_setopt($oCurl, CURLOPT_URL, $url);
- curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
- $sContent = curl_exec($oCurl);
- $aStatus = curl_getinfo($oCurl);
- curl_close($oCurl);
- if (intval($aStatus["http_code"]) == 200) {
- return $sContent;
- } else {
- return false;
- }
- }
- }
|