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; } }