UpgradeController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\RegisterField;
  5. use app\model\Address;
  6. use app\model\Area;
  7. use app\model\Order;
  8. use app\model\Goods;
  9. use app\model\GoodsToCategory;
  10. use app\model\Config;
  11. use app\model\Lang;
  12. class UpgradeController extends Base
  13. {
  14. public function index()
  15. {
  16. if (!file_exists($this->getRoot() . '/data/install.lock')) {
  17. $this->mkdataconfig([
  18. 'server' => config('database.connections.mysql.hostname'),
  19. 'username' => config('database.connections.mysql.username'),
  20. 'password' => config('database.connections.mysql.password'),
  21. 'port' => config('database.connections.mysql.hostport'),
  22. 'name' => config('database.connections.mysql.database'),
  23. 'prefix' => str_replace(config('database.app_name') . "_", "", config('database.connections.mysql.prefix'))
  24. ]);
  25. $this->mkLockFile();
  26. }
  27. $url = 'http://console.samcms.com/public/index.php/index/authorization';
  28. $postparam = Author()::authorizationInfo();
  29. $postparam['biaoshi'] = config('database.app_name');
  30. $result = vit_http_request($url, $postparam);
  31. $result = is_null(json_decode($result)) ? [] : json_decode($result, true);
  32. if (($result['data']['secretarray'])) {
  33. $mo = 'win' . 'ger';
  34. $configstr = serialize($result['data']['secretarray']);
  35. $config = Config::where(['weid' => 0, 'module' => $mo])->find();
  36. if (!empty($config)) {
  37. $config = $config->toArray();
  38. Config::update(['settings' => $configstr], ['id' => $config['id']]);
  39. } else {
  40. $data["weid"] = 0;
  41. $data["module"] = $mo;
  42. $data["status"] = 1;
  43. $data["settings"] = $configstr;
  44. Config::create($data);
  45. }
  46. $configaut = Config::where(['weid' => 0, 'module' => 'aut'])->find();
  47. if (empty($configaut)) {
  48. $data["weid"] = 0;
  49. $data["module"] = 'aut';
  50. $data["status"] = 1;
  51. $data["settings"] = serialize(['aut' => 1]);
  52. Config::create($data);
  53. }
  54. }
  55. if (($result['data']['backendphp'])) {
  56. //$this->upvendor();
  57. $file_url = $result['data']['backendphp']; //更新包的下载地址
  58. } else {
  59. throw new ValidateException('更新包不存在!');
  60. }
  61. $filename = basename($file_url); //更新包文件名称
  62. // 检查和创建文件夹
  63. $dir = $this->getRoot() . "/upgrade/";
  64. if (!file_exists($dir)) {
  65. mkdir($dir, 0777, true);
  66. }
  67. // 下载更新包到本地并赋值文件路径变量
  68. $path = file_exists($dir . $filename) ? $dir . $filename : $this->download_file($file_url, $dir, $filename);
  69. // 如果下载没成功就返回报错
  70. if (!file_exists($dir . $filename)) {
  71. throw new ValidateException("文件下载失败!");
  72. }
  73. // PHP解压的扩展类
  74. if (!class_exists("\ZipArchive")) {
  75. throw new ValidateException("请安装ZipArchive扩展!");
  76. }
  77. // 实例化ZipArchive
  78. $zip = new \ZipArchive();
  79. //打开压缩包
  80. if ($zip->open($path) === true) {
  81. // 复制根目录
  82. $toPath = $this->getRoot() . "/";
  83. try {
  84. // 解压文件到toPath路径下,用于覆盖差异文件
  85. $zipres = $zip->extractTo($dir);
  86. // 必须销毁变量,否则会报错
  87. unset($zip);
  88. // 删除更新包
  89. unlink($path);
  90. if (file_exists($dir . 'admin/')) {
  91. $this->deleteDir($toPath . 'admin/');
  92. }
  93. if (file_exists($dir . 'h5/')) {
  94. $this->deleteDir($toPath . 'h5/');
  95. }
  96. $this->xCopy($dir, $toPath, 1);
  97. $this->deleteDir($dir);
  98. //更新数据库
  99. $files = dirname(dirname(dirname(__DIR__))) . '/upgrade.php';
  100. include $files;
  101. RegisterField::datainitial('member');
  102. RegisterField::datainitial('agent');
  103. RegisterField::datainitial('technical');
  104. RegisterField::datainitial('store');
  105. Lang::datainitial();
  106. $Addressdata = Address::where('city_name', '=', '')->where('city_id', '>', 0)->select()->toArray();
  107. if (!empty($Addressdata)) {
  108. foreach ($Addressdata as $avo) {
  109. if (empty($avo['city_name'])) {
  110. Address::where('id', $avo['id'])->update([
  111. 'province_name' => Area::get_area_name($avo['province_id']),
  112. 'city_name' => Area::get_area_name($avo['city_id']),
  113. 'district_name' => Area::get_area_name($avo['district_id'])
  114. ]);
  115. }
  116. }
  117. }
  118. $Orderdata = Order::where('shipping_city_name', '=', '')->where('shipping_city_id', '>', 0)->select()->toArray();
  119. if (!empty($Orderdata)) {
  120. foreach ($Orderdata as $ovo) {
  121. if (empty($ovo['shipping_city_name'])) {
  122. Order::where('id', $ovo['id'])->update([
  123. 'shipping_province_name' => Area::get_area_name($ovo['shipping_province_id']),
  124. 'shipping_city_name' => Area::get_area_name($ovo['shipping_city_id']),
  125. 'shipping_district_name' => Area::get_area_name($ovo['shipping_district_id'])
  126. ]);
  127. }
  128. }
  129. }
  130. $Goodsdata = Goods::where('cat_id', 0)->select()->toArray();
  131. if (!empty($Goodsdata)) {
  132. foreach ($Goodsdata as $gvo) {
  133. $gtc = GoodsToCategory::where('goods_id', $gvo['id'])->find();
  134. if ($gtc) {
  135. Goods::where('id', $gvo['id'])->update(['cat_id' => $gtc->category_id]);
  136. }
  137. }
  138. }
  139. @unlink($files);
  140. // 更新完成
  141. return $this->json(['data' => '版本更新完成!!']);
  142. } catch (\Exception $e) {
  143. throw new ValidateException($e->getMessage() . "或没有[" . $dir . "]目录的写入权限");
  144. }
  145. } else {
  146. // 压缩包打开失败,删除文件并且返回报错
  147. unlink($path);
  148. throw new ValidateException('更新包解压失败,请重试');
  149. }
  150. }
  151. public function upvendor()
  152. {
  153. $file_url = 'https://w7pic.samcms.com/2023716/vendor.zip'; //vendor更新包的下载地址
  154. $filename = basename($file_url); //更新包文件名称
  155. // 检查和创建文件夹
  156. $dir = $this->getRoot() . "/upgradevendor/";
  157. if (!file_exists($dir)) {
  158. mkdir($dir, 0777, true);
  159. }
  160. // 下载更新包到本地并赋值文件路径变量
  161. $path = file_exists($dir . $filename) ? $dir . $filename : $this->download_file($file_url, $dir, $filename);
  162. // 如果下载没成功就返回报错
  163. if (!file_exists($dir . $filename)) {
  164. throw new ValidateException("文件下载失败!");
  165. }
  166. // PHP解压的扩展类
  167. if (!class_exists("\ZipArchive")) {
  168. throw new ValidateException("请安装ZipArchive扩展!");
  169. }
  170. // 实例化ZipArchive
  171. $zip = new \ZipArchive();
  172. //打开压缩包
  173. if ($zip->open($path) === true) {
  174. // 复制根目录
  175. $toPath = $this->getRoot() . "/";
  176. try {
  177. // 解压文件到toPath路径下,用于覆盖差异文件
  178. $zipres = $zip->extractTo($dir);
  179. // 必须销毁变量,否则会报错
  180. unset($zip);
  181. // 删除更新包
  182. unlink($path);
  183. if (file_exists($dir . 'vendor/')) {
  184. $this->deleteDir($toPath . 'vendor/');
  185. }
  186. $this->xCopy($dir, $toPath, 1);
  187. $this->deleteDir($dir);
  188. } catch (\Exception $e) {
  189. throw new ValidateException($e->getMessage() . "或没有[" . $dir . "]目录的写入权限");
  190. }
  191. } else {
  192. // 压缩包打开失败,删除文件并且返回报错
  193. unlink($path);
  194. throw new ValidateException('更新包解压失败,请重试');
  195. }
  196. }
  197. /*
  198. * 文件下载方法
  199. * $url 文件下载地址
  200. * $dir 存储的文件夹
  201. * $filename 文件名字
  202. */
  203. public function download_file($url, $dir, $filename = '')
  204. {
  205. if (empty($url)) {
  206. return false;
  207. }
  208. $ext = strrchr($url, '.');
  209. $dir = realpath($dir);
  210. //目录+文件
  211. $filename = (empty($filename) ? '/' . time() . '' . $ext : '/' . $filename);
  212. $filename = $dir . $filename;
  213. //开始捕捉
  214. ob_start();
  215. try {
  216. readfile($url);
  217. } catch (\Exception $e) {
  218. throw new ValidateException('文件下载失败,请联系开发人员!');
  219. }
  220. $img = ob_get_contents();
  221. ob_end_clean();
  222. $size = strlen($img);
  223. $fp2 = fopen($filename, "a");
  224. fwrite($fp2, $img);
  225. fclose($fp2);
  226. return $filename;
  227. }
  228. // xCopy("feiy","feiy2",1):拷贝feiy下的文件到 feiy2,包括子目录
  229. // xCopy("feiy","feiy2",0):拷贝feiy下的文件到 feiy2,不包括子目录
  230. //参数说明:
  231. // $source:源目录名
  232. // $destination:目的目录名
  233. // $child:复制时,是不是包含的子目录
  234. public function xCopy($source, $destination, $child = 1)
  235. {
  236. if (!is_dir($source)) {
  237. echo ("Error:the $source is not a direction!");
  238. return 0;
  239. }
  240. if (!is_dir($destination)) {
  241. mkdir($destination, 0777);
  242. }
  243. $handle = dir($source);
  244. while ($entry = $handle->read()) {
  245. if (($entry != ".") && ($entry != "..")) {
  246. if (is_dir($source . "/" . $entry)) {
  247. if ($child)
  248. $this->xCopy($source . "/" . $entry, $destination . "/" . $entry, $child);
  249. } else {
  250. copy($source . "/" . $entry, $destination . "/" . $entry);
  251. }
  252. }
  253. }
  254. //return 1;
  255. }
  256. function deleteDir($dir)
  257. {
  258. if (!$handle = @opendir($dir)) {
  259. return false;
  260. }
  261. while (false !== ($file = readdir($handle))) {
  262. if ($file !== "." && $file !== "..") { //排除当前目录与父级目录
  263. $file = $dir . '/' . $file;
  264. if (is_dir($file)) {
  265. $this->deleteDir($file);
  266. } else {
  267. @unlink($file);
  268. }
  269. }
  270. }
  271. @rmdir($dir);
  272. }
  273. public function mkLockFile()
  274. {
  275. return touch($this->getRoot() . '/data/install.lock');
  276. }
  277. public function mkdataconfig($db)
  278. {
  279. $config = include $this->getRoot() . '/data/example.php';
  280. $config = str_replace(array(
  281. '{db-server}', '{db-username}', '{db-password}', '{db-port}', '{db-name}', '{db-tablepre}'
  282. ), array(
  283. $db['server'], $db['username'], $db['password'], $db['port'], $db['name'], $db['prefix']
  284. ), $config);
  285. return file_put_contents($this->getRoot() . '/data/config.php', $config);
  286. }
  287. }