| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Plugins;
- use Comodojo\Zip\Zip;
- use yii\base\Model;
- use yii\composer\Plugin;
- class PluginForm extends Model
- {
- public $store_id;
- public $id;
- public $status;
- public $key;
- public $version;
- public $main_version;
- public $is_can_update;
- public $is_can_install;
- public $pageNo;
- public $pageSize;
- private static $url = 'http://www.baidu.com';
- private static $download_url = 'http://baidu.com';
- private static $update_url = 'http://baidu.com';
- public function getList() {
- // 从授权服务器获取当前插件列表
- // $cache_key = 'system_plugin_list_'.$this->pageNo.'_'.$this->pageSize;
- // if (cache()->get($cache_key)) {
- // return [
- // 'code' => 0,
- // 'msg' => 'success',
- // 'data' => cache()->get($cache_key)
- // ];
- // }
- // 请求
- // $data = http_get(self::$url);
- $data = [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- [
- 'name' => '拼团',
- 'key' => 'group',
- 'version' => '1.0',
- 'main_version' => '5.0',
- 'is_can_install' => 1,
- 'is_can_update' => 1,
- 'status' => 0,
- 'expire_time' => 1611684125,
- 'updated_at' => 1611674125,
- ],
- [
- 'name' => '砍价',
- 'key' => 'bargain',
- 'version' => '0.9',
- 'main_version' => '4.0',
- 'is_can_install' => 0,
- 'is_can_update' => 1,
- 'status' => 1,
- 'expire_time' => 1611684125,
- 'updated_at' => 1611674125,
- ],
- [
- 'name' => '秒杀',
- 'key' => 'ms',
- 'version' => '1.0',
- 'main_version' => '6.0',
- 'is_can_install' => 0,
- 'is_can_update' => 0,
- 'status' => 1,
- 'expire_time' => 1611684125,
- 'updated_at' => 1611674125,
- ],
- ]
- ];
- $cloud_data = $data['data'];
- foreach ($cloud_data as &$val) {
- $plugin = Plugins::findOne(['key' => $val['key'], 'store_id' => $this->store_id]);
- if ($plugin) {
- $plugin->status = $val['status'];
- $plugin->is_can_update = $val['is_can_update'];
- $plugin->version = $val['version'];
- $plugin->main_version = $val['main_version'];
- $plugin->is_can_install = $val['is_can_install'];
- $plugin->expire_time = $val['expire_time'];
- $plugin->save();
- $val['id'] = $plugin->id;
- $val['status'] = $plugin->status;
- $val['is_can_update'] = $plugin->is_can_update;
- $val['is_can_install'] = $plugin->is_can_install;
- $val['is_have'] = Plugins::TYPE_INSTALL_TRUE;
- $val['from'] = Plugins::TYPE_FROM_LOCAL;
- } else {
- $val['id'] = 'cloud';
- $val['from'] = Plugins::TYPE_FROM_CLOUD;
- $val['is_have'] = Plugins::TYPE_INSTALL_FALSE;
- }
- }
- $plugin_list = array_slice($cloud_data, ($this->pageNo - 1) * $this->pageSize, $this->pageSize);
- $plugin_data = [
- 'totalCount' => count($cloud_data),
- 'data' => $plugin_list,
- 'pageNo' => $this->pageNo,
- 'pageSize' => $this->pageSize
- ];
- // cache()->set($cache_key, $plugin_data);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $plugin_data
- ];
- }
- /**
- * 插件状态更改
- * @return array
- */
- public function changeStatus() {
- if (!in_array($this->status, [Plugins::STATUS_OPEN, Plugins::STATUS_CLOSE]) || !$this->id) {
- return [
- 'code' => 1,
- 'msg' => '参数有误'
- ];
- }
- $plugin = Plugins::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
- if (!$plugin) {
- return [
- 'code' => 1,
- 'msg' => '插件不存在'
- ];
- }
- $plugin->status = $this->status;
- if ($plugin->save()) {
- return [
- 'code' => 0,
- 'msg' => '状态更新成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '状态更新失败'
- ];
- }
- /**
- * 下载安装
- * @return array
- * @throws \Exception
- */
- public function install() {
- // TODO: cyy_check_auth 校验
- $plugin = Plugins::findOne(['store_id' => $this->store_id, 'key' => $this->key]);
- if ($plugin) {
- return [
- 'code' => 1,
- 'msg' => '插件已存在!',
- ];
- }
- if ($this->is_can_install == 0) {
- return [
- 'code' => 1,
- 'msg' => '暂不支持在线安装!',
- ];
- }
- if (cyy_version() < $this->main_version) {
- return [
- 'code' => 1,
- 'msg' => '主系统版本过低,请先更新主版本'
- ];
- }
- // 下载
- $contents = http_get(self::$download_url, ['key' => $this->key, 'version' => $this->version,
- 'main_version' => $this->main_version]);
- if (!$contents->getBody()) {
- return [
- 'code' => 1,
- 'msg' => '获取插件失败'
- ];
- }
- // 创建临时目录
- $temp_dir = "@runtime/temp/plugin/install/{$this->version}";
- $this->mkdir($temp_dir);
- $plugin_file = "{$temp_dir}/{$this->version}.zip";
- file_put_contents($plugin_file, $contents);
- $zip = Zip::open($plugin_file);
- $zip->extract("@plugins/{$this->key}/");
- $zip->close();
- // TODO: cyy_check_auth 目前先做测试
- // 记录
- $plugin = new Plugins();
- $plugin->store_id = $this->store_id;
- // TODO: 验证过期时间
- $plugin->expire_time = time();
- $plugin->version = $this->version;
- $plugin->main_version = $this->main_version;
- $plugin->is_can_install = 1;
- $plugin->is_can_update = 1;
- $plugin->status = Plugins::STATUS_OPEN;
- if ($plugin->save()) {
- return [
- 'code' => 0,
- 'msg' => '安装成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '安装失败'
- ];
- }
- /**
- * 更新
- * @return array
- * @throws \Exception
- */
- public function update() {
- // TODO: cyy_check_auth 校验
- if ($this->is_can_update == 0) {
- return [
- 'code' => 1,
- 'msg' => '暂不支持更新!',
- ];
- }
- if (cyy_version() < $this->main_version) {
- return [
- 'code' => 1,
- 'msg' => '主系统版本过低,请先更新主版本'
- ];
- }
- // 更新
- $contents = http_get(self::$update_url, ['key' => $this->key, 'version' => $this->version,
- 'main_version' => $this->main_version]);
- if (!$contents->getBody()) {
- return [
- 'code' => 1,
- 'msg' => '获取插件失败'
- ];
- }
- // 创建临时目录
- $temp_dir = "@runtime/temp/plugin/update/{$this->version}";
- $this->mkdir($temp_dir);
- $plugin_file = "{$temp_dir}/{$this->version}.zip";
- file_put_contents($plugin_file, $contents);
- $zip = Zip::open($plugin_file);
- $zip->extract("@plugins/{$this->key}/");
- $zip->close();
- // 更新操作
- $plugin = Plugins::findOne(['store_id' => $this->store_id, 'key' => $this->key]);
- $plugin->version = $this->version;
- $plugin->main_version = $this->main_version;
- $plugin->updated_at = time();
- $plugin->expire_time = time();
- $plugin->is_can_install = 1;
- $plugin->is_can_update = 1;
- $plugin->status = Plugins::STATUS_OPEN;
- if ($plugin->save()) {
- return [
- 'code' => 0,
- 'msg' => '更新成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '更新失败'
- ];
- }
- /**
- * @param $dir
- * @return bool
- */
- private function mkDir($dir) {
- if (!is_dir($dir)) {
- if (!$this->mkdir(dirname($dir))) {
- return false;
- }
- if (!mkdir($dir)) {
- return false;
- }
- }
- return true;
- }
- }
|