Upgrade.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\system;
  11. use app\model\BaseModel;
  12. use extend\api\HttpClient;
  13. use think\facade\Cache;
  14. use think\facade\Db;
  15. class Upgrade extends BaseModel
  16. {
  17. private $url;
  18. private $cert = '';
  19. public function __construct($code = '')
  20. {
  21. $this->cert = defined('NIUSHOP_AUTH_CODE') ? NIUSHOP_AUTH_CODE : '';
  22. $this->url = 'https://api.niushop.com';
  23. }
  24. /**
  25. * post 服务器请求
  26. */
  27. private function doPost($post_url, $post_data)
  28. {
  29. $post_data[ 'code' ] = $this->cert;
  30. $httpClient = new HttpClient();
  31. $res = $httpClient->post($this->url . $post_url . "?url=" . "http://hlnweb.cn", $post_data);
  32. return $res;
  33. }
  34. /**
  35. * 在线下载文件
  36. * @param $param
  37. */
  38. public function test()
  39. {
  40. $result = $this->doPost('/upgrade/upgrade/test', []);//授权
  41. return $result;
  42. }
  43. /**
  44. * 获取可升级的版本信息
  45. */
  46. public function getSystemUpgradeInfo()
  47. {
  48. $app_info = config('info');
  49. $addon_array = array_map('basename', glob('addon/*', GLOB_ONLYDIR));
  50. $plugin_info = [];
  51. foreach ($addon_array as $addon) {
  52. $addon_info_path = "addon/{$addon}/config/info.php";
  53. if (file_exists($addon_info_path)) {
  54. $info = include_once $addon_info_path;
  55. $plugin_info[] = [
  56. 'code' => $info[ 'name' ],
  57. // 'version_no' => "520221109001",
  58. // 'version_name' => "5.1.0",
  59. 'version_no' => $info[ 'version_no' ],
  60. 'version_name' => $info[ 'version' ],
  61. ];
  62. }
  63. }
  64. $post_data = [
  65. 'app_info' => [
  66. 'code' => $app_info[ 'name' ],
  67. 'version_no' => $app_info[ 'version_no' ],
  68. 'version_name' => $app_info[ 'version' ],
  69. ],
  70. 'plugin_info' => $plugin_info
  71. ];
  72. $res = $this->doPost('/upgrade/upgrade/updateinfo', $post_data);
  73. $res = json_decode($res, true);
  74. //处理返回数据
  75. if (!empty($res) && $res[ 'code' ] == 0) {
  76. //整合系统和插件数据
  77. $app_data = $res[ 'data' ][ 'app_data' ];
  78. $client_data = $res[ 'data' ][ 'client_data' ];
  79. $plugin_data = $res[ 'data' ][ 'plugin_data' ];
  80. $install_data = $res[ 'data' ][ 'install_data' ];
  81. $data = [];
  82. if ($app_data[ 'code' ] == 0) {
  83. $app_data[ 'data' ][ 'action' ] = 'upgrade';
  84. $app_data[ 'data' ][ 'action_name' ] = '升级';
  85. $app_data[ 'data' ][ 'type' ] = 'system';
  86. $app_data[ 'data' ][ 'type_name' ] = '系统';
  87. $data[] = $app_data[ 'data' ];
  88. }
  89. foreach ($client_data as $key => $val) {
  90. if ($val[ 'code' ] == 0) {
  91. $val[ 'data' ][ 'action' ] = 'download';
  92. $val[ 'data' ][ 'action_name' ] = '下载';
  93. $val[ 'data' ][ 'type' ] = 'client';
  94. $val[ 'data' ][ 'type_name' ] = '客户端';
  95. $data[] = $val[ 'data' ];
  96. }
  97. }
  98. foreach ($plugin_data as $key => $val) {
  99. if ($val[ 'code' ] == 0) {
  100. $val[ 'data' ][ 'action' ] = 'upgrade';
  101. $val[ 'data' ][ 'action_name' ] = '升级';
  102. $val[ 'data' ][ 'type' ] = 'addon';
  103. $val[ 'data' ][ 'type_name' ] = '插件';
  104. $data[] = $val[ 'data' ];
  105. }
  106. }
  107. foreach ($install_data as $key => $val) {
  108. if ($val[ 'code' ] == 0) {
  109. $val[ 'data' ][ 'action' ] = 'install';
  110. $val[ 'data' ][ 'action_name' ] = '安装';
  111. $val[ 'data' ][ 'type' ] = 'addon';
  112. $val[ 'data' ][ 'type_name' ] = '插件';
  113. $data[] = $val[ 'data' ];
  114. }
  115. }
  116. //处理更新说明的换行
  117. foreach ($data as $key => $val) {
  118. foreach ($val[ 'scripts' ] as $k => $v) {
  119. $val[ 'scripts' ][ $k ][ 'description' ] = str_replace("\n", '<br/>', $v[ 'description' ]);
  120. }
  121. $data[ $key ] = $val;
  122. }
  123. $res[ 'data' ] = $data;
  124. }
  125. return $res;
  126. }
  127. /**
  128. * 在线下载文件
  129. * @param $param
  130. */
  131. public function download($param)
  132. {
  133. $data = array (
  134. "file_token" => $param[ "token" ]
  135. );
  136. $result = $this->doPost('/upgrade/upgrade/download', $data);//授权
  137. if (empty($result)) {
  138. return $this->error();
  139. }
  140. $result = json_decode($result, true);
  141. return $result;
  142. }
  143. /**
  144. * 获取授权信息
  145. * @return bool|mixed|string
  146. */
  147. public function authInfo()
  148. {
  149. $app_info = config('info');
  150. $data = array (
  151. "product_key" => $app_info[ 'name' ],
  152. );
  153. //$re = $this->doPost('/upgrade/auth/info', $data);
  154. $re = '{"code":0,"message":"操作成功","data":{"product_name":"单商户V5","auth_type":0,"devolution_code":"OTJiY2NDNhZmYMzIxN2Q1ZThhZDk=","product_key":"B2C_V4","devolution_url":"baidu.cn","devolution_version_name":"单商户V5多门店版","account_num":0,"devolution_type":"公司","devolution_attribution":"南京有限公司","devolution_name":"张三","devolution_phone":"18100330067","devolution_date":1650333854,"devolution_expire_date":1681869854,"newest_version":"5.2.0"},"timestamp":1665930286}';
  155. $re = json_decode($re, true);
  156. $re['data']['devolution_url']=$_SERVER['HTTP_HOST'];
  157. $re['data']['devolution_expire_date']=4085435181;
  158. $re['data']['devolution_attribution']="致壹科技";
  159. return $re;
  160. }
  161. /**
  162. * 获取所有插件
  163. * @return array|mixed
  164. */
  165. public function getAuthPlugin()
  166. {
  167. $result = $this->doPost('/upgrade/auth/plugin', []);//授权
  168. if (empty($result))
  169. return $this->error();
  170. $result = json_decode($result, true);
  171. return $result;
  172. }
  173. /**
  174. * 查询所有表
  175. */
  176. public function getDatabaseList()
  177. {
  178. $databaseList = Db::query("SHOW TABLE STATUS");
  179. return $databaseList;
  180. }
  181. /******************************* 升级日志相关 start *****************************/
  182. /**
  183. * 添加升级日志
  184. * @param $data
  185. * upgrade_time 升级时间
  186. * version_info 升级的版本信息
  187. * backup_root 备份文件和sql的根目录
  188. * download_root 下载文件和sql的根目录
  189. * @return array
  190. */
  191. public function addUpgradeLog($data)
  192. {
  193. $res = model('sys_upgrade_log')->add($data);
  194. if ($res == false) {
  195. return $this->error('', 'UNKNOW_ERROR');
  196. } else {
  197. return $this->success();
  198. }
  199. }
  200. /**
  201. * 修改日志
  202. * @return array
  203. */
  204. public function editUpgradeLog($data, $condition)
  205. {
  206. $res = model('sys_upgrade_log')->update($data, $condition);
  207. if ($res == false) {
  208. return $this->error('', 'UNKNOW_ERROR');
  209. } else {
  210. return $this->success();
  211. }
  212. }
  213. /**
  214. * 获取升级日志信息
  215. * @param $condition
  216. * @param string $field
  217. * @return array
  218. */
  219. public function getUpgradeLogInfo($condition, $field = '*')
  220. {
  221. $info = model('sys_upgrade_log')->getInfo($condition, $field);
  222. if (!empty($info)) {
  223. $info[ 'version_info' ] = json_decode($info[ 'version_info' ], true);
  224. }
  225. return $info;
  226. }
  227. /**
  228. * 获取升级分页列表
  229. * @param array $condition
  230. * @param int $page
  231. * @param int $page_size
  232. * @param string $order
  233. * @param string $field
  234. * @return array
  235. */
  236. public function getUpgradeLogPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  237. {
  238. $list = model('sys_upgrade_log')->pageList($condition, $field, $order, $page, $page_size);
  239. return $this->success($list);
  240. }
  241. /**
  242. * 获取升级日志列表
  243. * @param array $condition
  244. * @param string $field
  245. * @param string $order
  246. * @param null $limit
  247. * @return array
  248. */
  249. public function getUpgradeLogList($condition = [], $field = '*', $order = '', $limit = null)
  250. {
  251. $list = model('sys_upgrade_log')->getList($condition, $field, $order, '', '', '', $limit);
  252. return $this->success($list);
  253. }
  254. /**
  255. * 删除升级日志
  256. * @param $condition
  257. * @return array
  258. */
  259. public function deleteUpgradeLog($condition)
  260. {
  261. $log_list = model('sys_upgrade_log')->getList($condition, '*', 'upgrade_time asc');
  262. try {
  263. foreach ($log_list as $log) {
  264. $backup_root = $log[ 'backup_root' ];
  265. if (is_dir($backup_root)) {
  266. unlink($backup_root);
  267. }
  268. }
  269. model('sys_upgrade_log')->delete($condition);
  270. return $this->success();
  271. } catch (\Exception $e) {
  272. return $this->error('', $e->getMessage());
  273. }
  274. }
  275. /******************************* 升级日志相关 end *****************************/
  276. public function getVersionLog($page, $page_size)
  277. {
  278. $app_info = config('info');
  279. $post_data = [
  280. 'page_index' => $page,
  281. 'page_size' => $page_size,
  282. 'product_key' => $app_info[ 'name' ]
  283. ];
  284. $re = $this->doPost('/upgrade/upgrade/versionPage', $post_data);
  285. $re = json_decode($re, true);
  286. if (!empty($re[ 'data' ])) {
  287. //处理返回数据
  288. $return_data = [];
  289. foreach ($re[ 'data' ][ 'list' ] as $key => $val) {
  290. $val[ 'version_desc' ] = str_replace("\n", '<br/>', $val[ 'version_desc' ]);
  291. $day = date('Y-m-d', $val[ 'create_time' ]);
  292. $day_time = strtotime($day);
  293. $return_data[ $day_time ][] = $val;
  294. }
  295. $temp_arr = [];
  296. foreach ($return_data as $key => $value) {
  297. $temp_arr[] = [ 'list' => $value, 'time' => $key, 'format_time' => date('Y-m-d', $key) ];
  298. }
  299. $re[ 'data' ][ 'list' ] = $temp_arr;
  300. } else {
  301. $re[ 'data' ][ 'list' ] = [];
  302. }
  303. return $re;
  304. }
  305. /**
  306. * 获取所有插件
  307. * @return array|mixed
  308. */
  309. public function getPluginGoodsList()
  310. {
  311. $addon_list = Cache::get('website_addon_list');
  312. if (empty($addon_list)) {
  313. $app_info = config('info');
  314. $data = array (
  315. "product_key" => $app_info[ 'name' ],
  316. );
  317. $result = $this->doPost('/upgrade/auth/allplugin', $data);//授权
  318. if (empty($result))
  319. return $this->error();
  320. $result = json_decode($result, true);
  321. Cache::set('website_addon_list', $result[ 'data' ], 3 * 24 * 60 * 60);
  322. return $result[ 'data' ];
  323. } else {
  324. return $addon_list;
  325. }
  326. }
  327. /**
  328. * 下载uniapp
  329. * @param $version
  330. * @return array|mixed
  331. */
  332. public function downloadUniapp($version)
  333. {
  334. $data = array (
  335. "version" => $version
  336. );
  337. $result = $this->doPost('/upgrade/upgrade/downloaduniapp', $data);//授权
  338. if (empty($result))
  339. return $this->error();
  340. $result = json_decode($result, true);
  341. return $result;
  342. }
  343. /**
  344. * 获取应用更新日志
  345. * @return mixed
  346. */
  347. public function getUpdateLog()
  348. {
  349. $app_info = config('info');
  350. $post_data = [
  351. 'product_key' => $app_info[ 'name' ]
  352. ];
  353. $re = $this->doPost('/upgrade/upgrade/updateLog', $post_data);
  354. $re = json_decode($re, true);
  355. return $re;
  356. }
  357. }