Skip to content

Commit e52dd18

Browse files
committed
feat:后台安装模块时支持选择安装版本
1 parent ebbf14c commit e52dd18

15 files changed

Lines changed: 423 additions & 199 deletions

File tree

app/admin/controller/Module.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ public function state(): void
4242
public function install(): void
4343
{
4444
AdminLog::instance()->setTitle(__('Install module'));
45-
$uid = $this->request->param("uid/s", '');
45+
$uid = $this->request->param("uid/s", '');
46+
$update = $this->request->param("update/b", false);
4647
if (!$uid) {
4748
$this->error(__('Parameter error'));
4849
}
4950
$res = [];
5051
try {
51-
$res = Manage::instance($uid)->install();
52+
$res = Manage::instance($uid)->install($update);
5253
} catch (Exception $e) {
5354
$this->error(__($e->getMessage()), $e->getData(), $e->getCode());
5455
} catch (Throwable $e) {
@@ -113,24 +114,6 @@ public function uninstall(): void
113114
$this->success();
114115
}
115116

116-
public function update(): void
117-
{
118-
AdminLog::instance()->setTitle(__('Update module'));
119-
$uid = $this->request->param("uid/s", '');
120-
$token = $this->request->param("token/s", '');
121-
if (!$token || !$uid) {
122-
$this->error(__('Parameter error'));
123-
}
124-
try {
125-
Manage::instance($uid)->update();
126-
} catch (Exception $e) {
127-
$this->error(__($e->getMessage()), $e->getData(), $e->getCode());
128-
} catch (Throwable $e) {
129-
$this->error(__($e->getMessage()));
130-
}
131-
$this->success();
132-
}
133-
134117
public function upload(): void
135118
{
136119
AdminLog::instance()->setTitle(__('Upload install module'));

app/admin/library/module/Manage.php

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,6 @@ public function upload(string $token, string $file): array
173173
throw new Exception('Basic configuration of the Module is incomplete');
174174
}
175175

176-
// 安装预检 - 系统版本号要求、已安装模块的互斥和依赖检测
177-
try {
178-
Server::installPreCheck([
179-
'uid' => $info['uid'],
180-
'sysVersion' => Config::get('buildadmin.version'),
181-
'nuxtVersion' => Server::getNuxtVersion(),
182-
'moduleVersion' => $info['version'],
183-
'ba-user-token' => $token,
184-
'installed' => Server::getInstalledIds($this->installDir),
185-
'server' => 1,
186-
]);
187-
} catch (Throwable $e) {
188-
Filesystem::delDir($copyToDir);
189-
throw $e;
190-
}
191-
192176
$this->uid = $info['uid'];
193177
$this->modulesDir = $this->installDir . $info['uid'] . DIRECTORY_SEPARATOR;
194178

@@ -202,7 +186,13 @@ public function upload(string $token, string $file): array
202186
}
203187
$nextVersion = implode('.', $versions);
204188
$upgrade = Version::compare($nextVersion, $info['version']);
205-
if (!$upgrade) {
189+
if ($upgrade) {
190+
// 检查模块是否已禁用
191+
if (!in_array($oldInfo['state'], [self::UNINSTALLED, self::WAIT_INSTALL, self::DISABLE])) {
192+
Filesystem::delDir($copyToDir);
193+
throw new Exception('Please disable the module before updating');
194+
}
195+
} else {
206196
Filesystem::delDir($copyToDir);
207197
// 模块已经存在
208198
throw new Exception('Module already exists');
@@ -216,9 +206,26 @@ public function upload(string $token, string $file): array
216206
}
217207
}
218208

209+
// 安装预检 - 系统版本号要求、已安装模块的互斥和依赖检测
210+
try {
211+
Server::installPreCheck([
212+
'uid' => $info['uid'],
213+
'version' => $info['version'],
214+
'sysVersion' => Config::get('buildadmin.version'),
215+
'nuxtVersion' => Server::getNuxtVersion(),
216+
'moduleVersion' => $info['version'],
217+
'ba-user-token' => $token,
218+
'installed' => Server::getInstalledIds($this->installDir),
219+
'server' => 1,
220+
]);
221+
} catch (Throwable $e) {
222+
Filesystem::delDir($copyToDir);
223+
throw $e;
224+
}
225+
219226
$newInfo = ['state' => self::WAIT_INSTALL];
220227
if ($upgrade) {
221-
$newInfo['update'] = 1;
228+
$info['update'] = 1;
222229

223230
// 清理旧版本代码
224231
Filesystem::delDir($this->modulesDir);
@@ -236,54 +243,52 @@ public function upload(string $token, string $file): array
236243
return $info;
237244
}
238245

239-
/**
240-
* 更新
241-
* @throws Throwable
242-
*/
243-
public function update(): void
244-
{
245-
$state = $this->getInstallState();
246-
if ($state != self::DISABLE) {
247-
throw new Exception('Please disable the module before updating');
248-
}
249-
250-
$this->download();
251-
252-
// 标记需要执行更新脚本,并在安装请求执行(当前请求未自动加载到新文件不方便执行)
253-
$info = $this->getInfo();
254-
$info['update'] = 1;
255-
$this->setInfo([], $info);
256-
}
257-
258246
/**
259247
* 安装模块
260248
* @return array 模块基本信息
261249
* @throws Throwable
262250
*/
263-
public function install(): array
251+
public function install(bool $update): array
264252
{
265253
$state = $this->getInstallState();
266-
if ($state == self::INSTALLED || $state == self::DIRECTORY_OCCUPIED || $state == self::DISABLE) {
267-
throw new Exception('Module already exists');
268-
}
269254

270-
if ($state == self::UNINSTALLED) {
271-
$this->download();
255+
if ($update) {
256+
if (!in_array($state, [self::UNINSTALLED, self::WAIT_INSTALL, self::DISABLE])) {
257+
throw new Exception('Please disable the module before updating');
258+
}
259+
260+
/**
261+
* self::WAIT_INSTALL=待安装
262+
* 即本地上传文件进行升级的安装流程,文件上传成功后将被标记为待安装,免去此处的下载
263+
*/
264+
if ($state == self::UNINSTALLED || $state != self::WAIT_INSTALL) {
265+
$this->download();
266+
}
267+
} else {
268+
if ($state == self::INSTALLED || $state == self::DIRECTORY_OCCUPIED || $state == self::DISABLE) {
269+
throw new Exception('Module already exists');
270+
}
271+
272+
if ($state == self::UNINSTALLED) {
273+
$this->download();
274+
}
272275
}
273276

274277
// 导入sql
275278
Server::importSql($this->modulesDir);
276279

277280
// 如果是更新,先执行更新脚本
278281
$info = $this->getInfo();
279-
if (isset($info['update']) && $info['update']) {
282+
if ($update) {
283+
$info['update'] = 1;
280284
Server::execEvent($this->uid, 'update');
281-
unset($info['update']);
282-
$this->setInfo([], $info);
283285
}
284286

285-
// 执行安装脚本
286-
Server::execEvent($this->uid, 'install');
287+
// 执行安装脚本 - 排除冲突处理时会重复提交至此的请求
288+
$extend = request()->post('extend/a', []);
289+
if (!isset($extend['conflictHandle'])) {
290+
Server::execEvent($this->uid, 'install');
291+
}
287292

288293
// 启用插件
289294
$this->enable('install');
@@ -572,9 +577,6 @@ public function disable(): array
572577
]);
573578

574579
if ($update) {
575-
$token = request()->post("token/s", '');
576-
$order = request()->post("order/d", 0);
577-
$this->update($token, $order);
578580
throw new Exception('update', -3, [
579581
'uid' => $this->uid,
580582
]);

web/src/api/backend/module.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ export function payCheck(sn: string) {
8989
)
9090
}
9191

92+
/**
93+
* 获取模块的可安装版本列表
94+
*/
95+
export function preDownload(data: anyObj) {
96+
const baAccount = useBaAccount()
97+
const siteConfig = useSiteConfig()
98+
return createAxios(
99+
{
100+
url: siteConfig.apiUrl + storeUrl + 'preDownload',
101+
method: 'POST',
102+
data,
103+
},
104+
{
105+
anotherToken: baAccount.getToken('auth'),
106+
}
107+
)
108+
}
109+
92110
export function getInstallState(uid: string) {
93111
return createAxios({
94112
url: moduleControllerUrl + 'state',
@@ -99,19 +117,19 @@ export function getInstallState(uid: string) {
99117
})
100118
}
101119

102-
export function postInstallModule(uid: string, orderId: number, extend: anyObj = {}) {
120+
export function postInstallModule(uid: string, orderId: number, version: string, update: boolean, extend: anyObj = {}) {
103121
const baAccount = useBaAccount()
104122
return createAxios(
105123
{
106124
url: moduleControllerUrl + 'install',
107-
method: 'post',
108-
params: {
109-
uid: uid,
110-
orderId: orderId,
111-
token: baAccount.getToken('auth'),
112-
},
125+
method: 'POST',
113126
data: {
114-
extend: extend,
127+
uid,
128+
update,
129+
version,
130+
orderId,
131+
token: baAccount.getToken('auth'),
132+
extend,
115133
},
116134
timeout: 3000 * 10,
117135
},
@@ -121,23 +139,6 @@ export function postInstallModule(uid: string, orderId: number, extend: anyObj =
121139
)
122140
}
123141

124-
export function postUpdate(uid: string, orderId: number, extend: anyObj = {}) {
125-
const baAccount = useBaAccount()
126-
return createAxios({
127-
url: moduleControllerUrl + 'update',
128-
method: 'POST',
129-
params: {
130-
uid,
131-
orderId: orderId,
132-
token: baAccount.getToken('auth'),
133-
},
134-
data: {
135-
extend: extend,
136-
},
137-
timeout: 3000 * 10,
138-
})
139-
}
140-
141142
export function postUninstall(uid: string) {
142143
return createAxios(
143144
{

web/src/lang/backend/en/module.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export default {
22
'stateTitle init': 'Module installer initialization...',
33
'stateTitle download': 'Downloading module...',
44
'stateTitle install': 'Installing module...',
5+
'stateTitle getInstallableVersion': 'Get installable version...',
56
'env require': 'Composer',
67
'env require-dev': 'Composer-dev',
78
'env dependencies': 'NPM',
@@ -89,8 +90,7 @@ export default {
8990
'You need to disable this module before updating Do you want to disable it now?':
9091
'You need to disable this module before updating. Do you want to disable it now?',
9192
'Disable and update': 'Disable and update',
92-
'No module purchase order was found within the expiration date':
93-
'No module purchase order was found within the expiration date. Do you want to purchase the current module now?',
93+
'No module purchase order was found': 'No module purchase order was found. Do you want to purchase the current module now?',
9494
// installConflict
9595
'new file': 'new file',
9696
'Existing files': 'Existing files',
@@ -135,6 +135,7 @@ export default {
135135
'Installation cancelled because the directory required by the module is occupied!',
136136
'Installation complete': 'Installation complete',
137137
'A conflict is found Please handle it manually': 'A conflict is found. Please handle it manually',
138+
'Select Version': 'Select install version',
138139
'Wait for dependent installation': 'Wait for dependent installation',
139140
'The operation succeeds Please clear the system cache and refresh the browser ~':
140141
'The operation succeeds. Please clear the system cache and refresh the browser ~',
@@ -147,4 +148,15 @@ export default {
147148
'Before successfully restarting the service, you can find the button to manually restart the service from the button group on the right side of the top bar.',
148149
'Manual restart': 'Manual restart',
149150
'Restart Now': 'Restart Now',
151+
// 选择安装版本
152+
'Available system version': 'Available system version',
153+
Description: 'Description',
154+
Version: 'Version',
155+
'Current installed version': 'Current installed version',
156+
'Insufficient system version': 'Insufficient system version',
157+
'Click to install': 'Click to install',
158+
'Versions released beyond the authorization period': 'Versions released beyond the authorization period',
159+
Renewal: 'Renewal',
160+
'Order expiration time':
161+
'The expiration time of the current order authorization is {expiration_time}, and the release time of this version is {create_time}',
150162
}

web/src/lang/backend/zh-cn/module.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export default {
22
'stateTitle init': '模块安装器初始化...',
33
'stateTitle download': '正在下载模块...',
44
'stateTitle install': '正在安装模块...',
5+
'stateTitle getInstallableVersion': '正在获取模块版本列表...',
56
'env require': '后端依赖(composer)',
67
'env require-dev': '后端开发环境依赖(composer)',
78
'env dependencies': '前端依赖(NPM)',
@@ -85,7 +86,7 @@ export default {
8586
'There are no more works': '没有更多作品了',
8687
'You need to disable this module before updating Do you want to disable it now?': '更新前需要先禁用该模块,立即禁用?',
8788
'Disable and update': '禁用并更新',
88-
'No module purchase order was found within the expiration date': '没有找到在有效期以内的模块购买订单,是否立即购买当前模块?',
89+
'No module purchase order was found': '没有找到有效的模块购买订单,是否立即购买当前模块?',
8990
// installConflict
9091
'new file': '新文件',
9192
'Existing files': '已有文件',
@@ -127,6 +128,7 @@ export default {
127128
'Installation cancelled because the directory required by the module is occupied!': '安装取消,因为模块所需目录被占用!',
128129
'Installation complete': '安装完成',
129130
'A conflict is found Please handle it manually': '发现冲突,请手动处理',
131+
'Select Version': '选择安装版本',
130132
'Wait for dependent installation': '等待依赖安装',
131133
'The operation succeeds Please clear the system cache and refresh the browser ~': '操作成功,请清理系统缓存并刷新浏览器~',
132134
'Deal with conflict': '处理冲突',
@@ -137,4 +139,14 @@ export default {
137139
'Restart Vite hot server tips': '在完成服务重启之前,您还可以随时从顶栏右侧的按钮组中找到手动重启服务的按钮。',
138140
'Manual restart': '手动重启',
139141
'Restart Now': '立即重启',
142+
// 选择安装版本
143+
'Available system version': '可用系统版本',
144+
Description: '描述',
145+
Version: '版本',
146+
'Current installed version': '当前安装版本',
147+
'Insufficient system version': '系统版本不足',
148+
'Click to install': '点击安装',
149+
'Versions released beyond the authorization period': '授权期限以外发布的版本',
150+
Renewal: '续费',
151+
'Order expiration time': '当前订单授权过期时间为 {expiration_time},此版本发布时间为 {create_time}',
140152
}

web/src/views/backend/module/components/buy.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<el-button
5656
v-else
5757
:loading="state.loading.common"
58-
@click="onInstall(state.buy.info.uid, state.buy.info.id)"
58+
@click="onPreInstallModule(state.buy.info.uid, state.buy.info.id, true)"
5959
v-blur
6060
type="warning"
6161
>
@@ -70,11 +70,11 @@
7070
</template>
7171

7272
<script setup lang="ts">
73+
import { isEmpty } from 'lodash-es'
74+
import { useI18n } from 'vue-i18n'
75+
import { currency, onPay, onPreInstallModule, specificUserName } from '../index'
7376
import { state } from '../store'
74-
import { onPay, currency, onInstall, specificUserName } from '../index'
7577
import { useBaAccount } from '/@/stores/baAccount'
76-
import { useI18n } from 'vue-i18n'
77-
import { isEmpty } from 'lodash-es'
7878
7979
const { t } = useI18n()
8080
const baAccount = useBaAccount()

0 commit comments

Comments
 (0)