PluginForm.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Plugins;
  9. use Comodojo\Zip\Zip;
  10. use yii\base\Model;
  11. use yii\composer\Plugin;
  12. class PluginForm extends Model
  13. {
  14. public $store_id;
  15. public $id;
  16. public $status;
  17. public $key;
  18. public $version;
  19. public $main_version;
  20. public $is_can_update;
  21. public $is_can_install;
  22. public $pageNo;
  23. public $pageSize;
  24. private static $url = 'http://www.baidu.com';
  25. private static $download_url = 'http://baidu.com';
  26. private static $update_url = 'http://baidu.com';
  27. public function getList() {
  28. // 从授权服务器获取当前插件列表
  29. // $cache_key = 'system_plugin_list_'.$this->pageNo.'_'.$this->pageSize;
  30. // if (cache()->get($cache_key)) {
  31. // return [
  32. // 'code' => 0,
  33. // 'msg' => 'success',
  34. // 'data' => cache()->get($cache_key)
  35. // ];
  36. // }
  37. // 请求
  38. // $data = http_get(self::$url);
  39. $data = [
  40. 'code' => 0,
  41. 'msg' => 'success',
  42. 'data' => [
  43. [
  44. 'name' => '拼团',
  45. 'key' => 'group',
  46. 'version' => '1.0',
  47. 'main_version' => '5.0',
  48. 'is_can_install' => 1,
  49. 'is_can_update' => 1,
  50. 'status' => 0,
  51. 'expire_time' => 1611684125,
  52. 'updated_at' => 1611674125,
  53. ],
  54. [
  55. 'name' => '砍价',
  56. 'key' => 'bargain',
  57. 'version' => '0.9',
  58. 'main_version' => '4.0',
  59. 'is_can_install' => 0,
  60. 'is_can_update' => 1,
  61. 'status' => 1,
  62. 'expire_time' => 1611684125,
  63. 'updated_at' => 1611674125,
  64. ],
  65. [
  66. 'name' => '秒杀',
  67. 'key' => 'ms',
  68. 'version' => '1.0',
  69. 'main_version' => '6.0',
  70. 'is_can_install' => 0,
  71. 'is_can_update' => 0,
  72. 'status' => 1,
  73. 'expire_time' => 1611684125,
  74. 'updated_at' => 1611674125,
  75. ],
  76. ]
  77. ];
  78. $cloud_data = $data['data'];
  79. foreach ($cloud_data as &$val) {
  80. $plugin = Plugins::findOne(['key' => $val['key'], 'store_id' => $this->store_id]);
  81. if ($plugin) {
  82. $plugin->status = $val['status'];
  83. $plugin->is_can_update = $val['is_can_update'];
  84. $plugin->version = $val['version'];
  85. $plugin->main_version = $val['main_version'];
  86. $plugin->is_can_install = $val['is_can_install'];
  87. $plugin->expire_time = $val['expire_time'];
  88. $plugin->save();
  89. $val['id'] = $plugin->id;
  90. $val['status'] = $plugin->status;
  91. $val['is_can_update'] = $plugin->is_can_update;
  92. $val['is_can_install'] = $plugin->is_can_install;
  93. $val['is_have'] = Plugins::TYPE_INSTALL_TRUE;
  94. $val['from'] = Plugins::TYPE_FROM_LOCAL;
  95. } else {
  96. $val['id'] = 'cloud';
  97. $val['from'] = Plugins::TYPE_FROM_CLOUD;
  98. $val['is_have'] = Plugins::TYPE_INSTALL_FALSE;
  99. }
  100. }
  101. $plugin_list = array_slice($cloud_data, ($this->pageNo - 1) * $this->pageSize, $this->pageSize);
  102. $plugin_data = [
  103. 'totalCount' => count($cloud_data),
  104. 'data' => $plugin_list,
  105. 'pageNo' => $this->pageNo,
  106. 'pageSize' => $this->pageSize
  107. ];
  108. // cache()->set($cache_key, $plugin_data);
  109. return [
  110. 'code' => 0,
  111. 'msg' => 'success',
  112. 'data' => $plugin_data
  113. ];
  114. }
  115. /**
  116. * 插件状态更改
  117. * @return array
  118. */
  119. public function changeStatus() {
  120. if (!in_array($this->status, [Plugins::STATUS_OPEN, Plugins::STATUS_CLOSE]) || !$this->id) {
  121. return [
  122. 'code' => 1,
  123. 'msg' => '参数有误'
  124. ];
  125. }
  126. $plugin = Plugins::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
  127. if (!$plugin) {
  128. return [
  129. 'code' => 1,
  130. 'msg' => '插件不存在'
  131. ];
  132. }
  133. $plugin->status = $this->status;
  134. if ($plugin->save()) {
  135. return [
  136. 'code' => 0,
  137. 'msg' => '状态更新成功'
  138. ];
  139. }
  140. return [
  141. 'code' => 1,
  142. 'msg' => '状态更新失败'
  143. ];
  144. }
  145. /**
  146. * 下载安装
  147. * @return array
  148. * @throws \Exception
  149. */
  150. public function install() {
  151. // TODO: cyy_check_auth 校验
  152. $plugin = Plugins::findOne(['store_id' => $this->store_id, 'key' => $this->key]);
  153. if ($plugin) {
  154. return [
  155. 'code' => 1,
  156. 'msg' => '插件已存在!',
  157. ];
  158. }
  159. if ($this->is_can_install == 0) {
  160. return [
  161. 'code' => 1,
  162. 'msg' => '暂不支持在线安装!',
  163. ];
  164. }
  165. if (cyy_version() < $this->main_version) {
  166. return [
  167. 'code' => 1,
  168. 'msg' => '主系统版本过低,请先更新主版本'
  169. ];
  170. }
  171. // 下载
  172. $contents = http_get(self::$download_url, ['key' => $this->key, 'version' => $this->version,
  173. 'main_version' => $this->main_version]);
  174. if (!$contents->getBody()) {
  175. return [
  176. 'code' => 1,
  177. 'msg' => '获取插件失败'
  178. ];
  179. }
  180. // 创建临时目录
  181. $temp_dir = "@runtime/temp/plugin/install/{$this->version}";
  182. $this->mkdir($temp_dir);
  183. $plugin_file = "{$temp_dir}/{$this->version}.zip";
  184. file_put_contents($plugin_file, $contents);
  185. $zip = Zip::open($plugin_file);
  186. $zip->extract("@plugins/{$this->key}/");
  187. $zip->close();
  188. // TODO: cyy_check_auth 目前先做测试
  189. // 记录
  190. $plugin = new Plugins();
  191. $plugin->store_id = $this->store_id;
  192. // TODO: 验证过期时间
  193. $plugin->expire_time = time();
  194. $plugin->version = $this->version;
  195. $plugin->main_version = $this->main_version;
  196. $plugin->is_can_install = 1;
  197. $plugin->is_can_update = 1;
  198. $plugin->status = Plugins::STATUS_OPEN;
  199. if ($plugin->save()) {
  200. return [
  201. 'code' => 0,
  202. 'msg' => '安装成功'
  203. ];
  204. }
  205. return [
  206. 'code' => 1,
  207. 'msg' => '安装失败'
  208. ];
  209. }
  210. /**
  211. * 更新
  212. * @return array
  213. * @throws \Exception
  214. */
  215. public function update() {
  216. // TODO: cyy_check_auth 校验
  217. if ($this->is_can_update == 0) {
  218. return [
  219. 'code' => 1,
  220. 'msg' => '暂不支持更新!',
  221. ];
  222. }
  223. if (cyy_version() < $this->main_version) {
  224. return [
  225. 'code' => 1,
  226. 'msg' => '主系统版本过低,请先更新主版本'
  227. ];
  228. }
  229. // 更新
  230. $contents = http_get(self::$update_url, ['key' => $this->key, 'version' => $this->version,
  231. 'main_version' => $this->main_version]);
  232. if (!$contents->getBody()) {
  233. return [
  234. 'code' => 1,
  235. 'msg' => '获取插件失败'
  236. ];
  237. }
  238. // 创建临时目录
  239. $temp_dir = "@runtime/temp/plugin/update/{$this->version}";
  240. $this->mkdir($temp_dir);
  241. $plugin_file = "{$temp_dir}/{$this->version}.zip";
  242. file_put_contents($plugin_file, $contents);
  243. $zip = Zip::open($plugin_file);
  244. $zip->extract("@plugins/{$this->key}/");
  245. $zip->close();
  246. // 更新操作
  247. $plugin = Plugins::findOne(['store_id' => $this->store_id, 'key' => $this->key]);
  248. $plugin->version = $this->version;
  249. $plugin->main_version = $this->main_version;
  250. $plugin->updated_at = time();
  251. $plugin->expire_time = time();
  252. $plugin->is_can_install = 1;
  253. $plugin->is_can_update = 1;
  254. $plugin->status = Plugins::STATUS_OPEN;
  255. if ($plugin->save()) {
  256. return [
  257. 'code' => 0,
  258. 'msg' => '更新成功'
  259. ];
  260. }
  261. return [
  262. 'code' => 1,
  263. 'msg' => '更新失败'
  264. ];
  265. }
  266. /**
  267. * @param $dir
  268. * @return bool
  269. */
  270. private function mkDir($dir) {
  271. if (!is_dir($dir)) {
  272. if (!$this->mkdir(dirname($dir))) {
  273. return false;
  274. }
  275. if (!mkdir($dir)) {
  276. return false;
  277. }
  278. }
  279. return true;
  280. }
  281. }