UpgradeController.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. if (!empty($avo['province_id'])) {
  111. $province_name = Area::get_area_name($avo['province_id']);
  112. if (!empty($province_name)) {
  113. Address::where('id', $avo['id'])->update([
  114. 'province_name' => $province_name,
  115. ]);
  116. }
  117. }
  118. if (!empty($avo['city_id'])) {
  119. $city_name = Area::get_area_name($avo['city_id']);
  120. if (!empty($city_name)) {
  121. Address::where('id', $avo['id'])->update([
  122. 'city_name' => $city_name,
  123. ]);
  124. }
  125. }
  126. if (!empty($avo['district_id'])) {
  127. $district_name = Area::get_area_name($avo['district_id']);
  128. if (!empty($district_name)) {
  129. Address::where('id', $avo['id'])->update([
  130. 'district_name' => $district_name,
  131. ]);
  132. }
  133. }
  134. }
  135. }
  136. }
  137. /*太老的版本升级需要行以下代码
  138. $Orderdata = Order::where('shipping_city_name', '=', '')->where('shipping_city_id', '>', 0)->select()->toArray();
  139. if (!empty($Orderdata)) {
  140. foreach ($Orderdata as $ovo) {
  141. if (empty($ovo['shipping_city_name'])) {
  142. Order::where('id', $ovo['id'])->update([
  143. 'shipping_province_name' => Area::get_area_name($ovo['shipping_province_id']),
  144. 'shipping_city_name' => Area::get_area_name($ovo['shipping_city_id']),
  145. 'shipping_district_name' => Area::get_area_name($ovo['shipping_district_id'])
  146. ]);
  147. }
  148. }
  149. }*/
  150. $Goodsdata = Goods::where('cat_id', 0)->select()->toArray();
  151. if (!empty($Goodsdata)) {
  152. foreach ($Goodsdata as $gvo) {
  153. $gtc = GoodsToCategory::where('goods_id', $gvo['id'])->find();
  154. if ($gtc) {
  155. Goods::where('id', $gvo['id'])->update(['cat_id' => $gtc->category_id]);
  156. }
  157. }
  158. }
  159. @unlink($files);
  160. // 更新完成
  161. return $this->json(['data' => '版本更新完成!!']);
  162. } catch (\Exception $e) {
  163. throw new ValidateException($e->getMessage() . "或没有[" . $dir . "]目录的写入权限");
  164. }
  165. } else {
  166. // 压缩包打开失败,删除文件并且返回报错
  167. unlink($path);
  168. throw new ValidateException('更新包解压失败,请重试');
  169. }
  170. }
  171. public function upvendor()
  172. {
  173. $file_url = 'https://w7pic2.samcms.com/2023716/vendor.zip'; //vendor更新包的下载地址
  174. $filename = basename($file_url); //更新包文件名称
  175. // 检查和创建文件夹
  176. $dir = $this->getRoot() . "/upgradevendor/";
  177. if (!file_exists($dir)) {
  178. mkdir($dir, 0777, true);
  179. }
  180. // 下载更新包到本地并赋值文件路径变量
  181. $path = file_exists($dir . $filename) ? $dir . $filename : $this->download_file($file_url, $dir, $filename);
  182. // 如果下载没成功就返回报错
  183. if (!file_exists($dir . $filename)) {
  184. throw new ValidateException("文件下载失败!");
  185. }
  186. // PHP解压的扩展类
  187. if (!class_exists("\ZipArchive")) {
  188. throw new ValidateException("请安装ZipArchive扩展!");
  189. }
  190. // 实例化ZipArchive
  191. $zip = new \ZipArchive();
  192. //打开压缩包
  193. if ($zip->open($path) === true) {
  194. // 复制根目录
  195. $toPath = $this->getRoot() . "/";
  196. try {
  197. // 解压文件到toPath路径下,用于覆盖差异文件
  198. $zipres = $zip->extractTo($dir);
  199. // 必须销毁变量,否则会报错
  200. unset($zip);
  201. // 删除更新包
  202. unlink($path);
  203. if (file_exists($dir . 'vendor/')) {
  204. $this->deleteDir($toPath . 'vendor/');
  205. }
  206. $this->xCopy($dir, $toPath, 1);
  207. $this->deleteDir($dir);
  208. } catch (\Exception $e) {
  209. throw new ValidateException($e->getMessage() . "或没有[" . $dir . "]目录的写入权限");
  210. }
  211. } else {
  212. // 压缩包打开失败,删除文件并且返回报错
  213. unlink($path);
  214. throw new ValidateException('更新包解压失败,请重试');
  215. }
  216. }
  217. /*
  218. * 文件下载方法
  219. * $url 文件下载地址
  220. * $dir 存储的文件夹
  221. * $filename 文件名字
  222. */
  223. public function download_file($url, $dir, $filename = '')
  224. {
  225. if (empty($url)) {
  226. return false;
  227. }
  228. $ext = strrchr($url, '.');
  229. $dir = realpath($dir);
  230. //目录+文件
  231. $filename = (empty($filename) ? '/' . time() . '' . $ext : '/' . $filename);
  232. $filename = $dir . $filename;
  233. //开始捕捉
  234. ob_start();
  235. try {
  236. readfile($url);
  237. } catch (\Exception $e) {
  238. throw new ValidateException('文件下载失败,请联系开发人员!');
  239. }
  240. $img = ob_get_contents();
  241. ob_end_clean();
  242. $size = strlen($img);
  243. $fp2 = fopen($filename, "a");
  244. fwrite($fp2, $img);
  245. fclose($fp2);
  246. return $filename;
  247. }
  248. // xCopy("feiy","feiy2",1):拷贝feiy下的文件到 feiy2,包括子目录
  249. // xCopy("feiy","feiy2",0):拷贝feiy下的文件到 feiy2,不包括子目录
  250. //参数说明:
  251. // $source:源目录名
  252. // $destination:目的目录名
  253. // $child:复制时,是不是包含的子目录
  254. public function xCopy($source, $destination, $child = 1)
  255. {
  256. if (!is_dir($source)) {
  257. echo ("Error:the $source is not a direction!");
  258. return 0;
  259. }
  260. if (!is_dir($destination)) {
  261. mkdir($destination, 0777);
  262. }
  263. $handle = dir($source);
  264. while ($entry = $handle->read()) {
  265. if (($entry != ".") && ($entry != "..")) {
  266. if (is_dir($source . "/" . $entry)) {
  267. if ($child)
  268. $this->xCopy($source . "/" . $entry, $destination . "/" . $entry, $child);
  269. } else {
  270. copy($source . "/" . $entry, $destination . "/" . $entry);
  271. }
  272. }
  273. }
  274. //return 1;
  275. }
  276. function deleteDir($dir)
  277. {
  278. if (!$handle = @opendir($dir)) {
  279. return false;
  280. }
  281. while (false !== ($file = readdir($handle))) {
  282. if ($file !== "." && $file !== "..") { //排除当前目录与父级目录
  283. $file = $dir . '/' . $file;
  284. if (is_dir($file)) {
  285. $this->deleteDir($file);
  286. } else {
  287. @unlink($file);
  288. }
  289. }
  290. }
  291. @rmdir($dir);
  292. }
  293. public function mkLockFile()
  294. {
  295. return touch($this->getRoot() . '/data/install.lock');
  296. }
  297. public function mkdataconfig($db)
  298. {
  299. $config = include $this->getRoot() . '/data/example.php';
  300. $config = str_replace(array(
  301. '{db-server}', '{db-username}', '{db-password}', '{db-port}', '{db-name}', '{db-tablepre}'
  302. ), array(
  303. $db['server'], $db['username'], $db['password'], $db['port'], $db['name'], $db['prefix']
  304. ), $config);
  305. return file_put_contents($this->getRoot() . '/data/config.php', $config);
  306. }
  307. }