王飞
2025-03-07 c5e49c10499435802a6551b98772f95660c6f85a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<template>
    <view class="uni-container">
        <view class="uni-header">
            <view class="uni-group">
                <view class="uni-title">包类型</view>
                <view class="uni-sub-title">{{type_valuetotext[formData.type]}}</view>
            </view>
        </view>
        <uni-forms ref="form" :value="formData" validateTrigger="bind" :labelWidth="labelWidth">
            <uni-forms-item name="appid" label="AppID" required>
                <uni-easyinput :disabled="true" v-model="formData.appid" trim="both" />
            </uni-forms-item>
            <uni-forms-item name="name" label="应用名称">
                <uni-easyinput :disabled="true" v-model="formData.name" trim="both" />
            </uni-forms-item>
            <uni-forms-item name="title" label="更新标题">
                <uni-easyinput placeholder="更新标题" v-model="formData.title" />
            </uni-forms-item>
            <uni-forms-item name="contents" label="更新内容" required>
                <textarea auto-height style="box-sizing: content-box;" :maxlength="-1"
                    @input="binddata('contents', $event.detail.value)" class="uni-textarea-border"
                    :value="formData.contents" @update:value="val => formData.contents = val"></textarea>
            </uni-forms-item>
            <uni-forms-item name="platform" label="平台" required>
                <uni-data-checkbox :multiple="isWGT" v-model="formData.platform" :localdata="platformLocaldata" />
            </uni-forms-item>
            <uni-forms-item name="version" label="版本号" required>
                <uni-easyinput v-model="formData.version" placeholder="当前包版本号,必须大于当前线上发行版本号" />
            </uni-forms-item>
            <uni-forms-item v-if="isWGT" key="min_uni_version" name="min_uni_version" label="原生App最低版本"
                :required="isWGT">
                <uni-easyinput placeholder="原生App最低版本" v-model="formData.min_uni_version" />
                <show-info :content="minUniVersionContent"></show-info>
            </uni-forms-item>
 
            <uni-forms-item label="存储选择">
                <view class="flex">
                    <radio-group @change="e => uniFilePickerProvider = e.detail.value" style="width: 100%;">
                        <view class="flex" style="flex-wrap: nowrap;">
                    上传至:
                            <label>
                                <radio value="unicloud" :checked="uniFilePickerProvider === 'unicloud'"/><text>内置存储</text>
                            </label>
                            <label style="margin-left: 20rpx;">
                                <radio value="extStorage" :checked="uniFilePickerProvider === 'extStorage'"/><text>扩展存储</text>
                            </label>
                        </view>
                    </radio-group>
                    <text class="uni-sub-title" style="margin-top: 10px;font-size: 12px;color: #666;width: 100%;">内置存储是服务空间开通后自带的云存储,不支持自定义域名,不支持阶梯计费</text>
                    <text class="uni-sub-title" style="margin-top: 10px;font-size: 12px;color: #666;">扩展存储支持自定义域名、阶梯计费,越用越便宜、功能更强大</text>
                    <text class="uni-sub-title" style="margin-top: 10px;font-size: 12px;color: #2979ff;cursor: pointer;text-decoration: underline; margin-left: 10px;" @click="toUrl('https://doc.dcloud.net.cn/uniCloud/ext-storage/service.html')">扩展存储开通文档</text>
                </view>
            </uni-forms-item>
 
            <uni-forms-item label="自定义域名" v-if="uniFilePickerProvider === 'extStorage'">
                <view class="flex" style="flex-direction: column;align-items:flex-start;">
                    <uni-easyinput placeholder="请输入扩展存储自定义域名" v-model="domain" :maxlength="-1" style="width: 550px;" />
                    <text class="uni-sub-title" style="margin-top: 10px;font-size: 12px;color: #666;">输入扩展存储绑定的域名,在服务空间-云存储-扩展存储页面可查看,如:cdn.example.com</text>
                </view>
            </uni-forms-item>
 
            <uni-forms-item v-if="!isiOS" :label="'上传'+fileExtname[0]+'包'">
                <uni-file-picker v-model="appFileList" :file-extname="fileExtname" :disabled="hasPackage"
                    returnType="object" file-mediatype="all" limit="1" @success="packageUploadSuccess" :provider="uniFilePickerProvider"
                    @delete="packageDelete">
                    <view class="flex">
                        <button type="primary" size="mini" @click="selectFile" style="margin: 0px;">选择文件</button>
                    </view>
                    <view class="flex">
                        <text style="margin-top: 10px;font-size: 12px;color: #666;">上传{{fileExtname[0]}}到当前服务空间的云存储中,上传成功后,会自动使用云存储地址填充下载链接</text>
                        <text style="margin-top: 10px;font-size: 12px;color: #666;">上传文件后同步到各地cdn缓存节点有延迟。请适当等候再提交新版信息入库,触发客户端更新提示。</text>
                    </view>
                </uni-file-picker>
                <text v-if="hasPackage" style="padding-left: 20px;color: #a8a8a8;">{{Number(appFileList.size / 1024 / 1024).toFixed(2)}}M</text>
            </uni-forms-item>
 
            <uni-forms-item key="url" name="url" :label="isiOS ? 'AppStore' : '下载链接'" required>
                <view class="flex" style="flex-direction: column;align-items:flex-start;flex: 1;">
                    <view class="flex" style="width: 100%;">
                        <uni-easyinput placeholder="链接" v-model="formData.url" :maxlength="-1" />
                        <text style="margin-left: 10px;color: #2979ff;cursor: pointer;text-decoration: underline;" v-if="formData.url" @click="toUrl(formData.url)">测试下载</text>
                    </view>
                    <text style="margin-top: 10px;font-size: 12px;color: #666;" v-if="formData.url">建议点击【测试下载】能正常下载后,再进行发布</text>
                </view>
            </uni-forms-item>
 
            <uni-forms-item v-if="!isiOS && !isWGT && formData.store_list.length" label="Android应用市场" labelWidth="125px"
                key="store_list" name="store_list">
                <view style="flex: 1;">
                    <view v-for="(item) in formData.store_list" :key="item.id">
                        <uni-card style="margin: 0px 0px 20px 0px;">
                            <view style="display: flex;">
                                <checkbox-group style="user-select: none;"
                                    @change="({detail:{value}}) => {item.enable = !!value.length}">
                                    <label class="title_padding">
                                        <checkbox value="scheme" :checked="item.enable" />
                                        <text>是否启用</text>
                                    </label>
                                </checkbox-group>
 
                            </view>
                            <uni-forms-item label="商店名称">
                                <uni-easyinput disabled v-model="item.name" trim="both"></uni-easyinput>
                            </uni-forms-item>
                            <uni-forms-item label="Scheme">
                                <uni-easyinput disabled v-model="item.scheme" trim="both"></uni-easyinput>
                            </uni-forms-item>
                            <uni-forms-item label="优先级">
                                <uni-easyinput v-model="item.priority" type="number"></uni-easyinput>
                                <show-info :top="-100" :left="-180" :content="priorityContent"></show-info>
                            </uni-forms-item>
                        </uni-card>
                    </view>
                </view>
            </uni-forms-item>
            <uni-forms-item v-if="isWGT" key="is_silently" name="is_silently" label="静默更新">
                <switch @change="binddata('is_silently', $event.detail.value)" :checked="formData.is_silently" />
                <show-info :top="-80" :content="silentlyContent"></show-info>
            </uni-forms-item>
            <uni-forms-item v-if="!isiOS" key="is_mandatory" name="is_mandatory" label="强制更新">
                <switch @change="binddata('is_mandatory', $event.detail.value)" :checked="formData.is_mandatory" />
                <show-info :content="mandatoryContent"></show-info>
            </uni-forms-item>
            <uni-forms-item name="stable_publish" label="上线发行">
                <switch @change="binddata('stable_publish', $event.detail.value)" :checked="formData.stable_publish" />
                <show-info :top="-40" :content="stablePublishContent2"></show-info>
            </uni-forms-item>
            <uni-forms-item v-show="false" name="type" label="安装包类型">
                <uni-data-checkbox v-model="formData.type" :localdata="formOptions.type_localdata" />
            </uni-forms-item>
            <view class="uni-button-group">
                <button type="primary" class="uni-button" style="width: 100px;" @click="submit">发布</button>
                <button type="warn" class="uni-button" style="width: 100px;margin-left: 15px;" @click="back">取消</button>
            </view>
        </uni-forms>
    </view>
</template>
 
<script>
    import {
        validator,
        enumConverter
    } from '@/js_sdk/validator/opendb-app-versions.js';
    import addAndDetail, {
        fields
    } from '../mixin/version_add_detail_mixin.js';
    import {
        appVersionListDbName
    } from '../utils.js';
 
    const db = uniCloud.database();
    const dbCmd = db.command;
    const dbCollectionName = appVersionListDbName;
 
    const platform_iOS = 'iOS';
    const platform_Android = 'Android';
 
    /**
     * 对比版本号,如需要,请自行修改判断规则
     * 支持比对    ("3.0.0.0.0.1.0.1", "3.0.0.0.0.1")    ("3.0.0.1", "3.0")    ("3.1.1", "3.1.1.1") 之类的
     * @param {Object} v1
     * @param {Object} v2
     * v1 > v2 return 1
     * v1 < v2 return -1
     * v1 == v2 return 0
     */
    function compare(v1 = '0', v2 = '0') {
        v1 = String(v1).split('.')
        v2 = String(v2).split('.')
        const minVersionLens = Math.min(v1.length, v2.length);
 
        let result = 0;
        for (let i = 0; i < minVersionLens; i++) {
            const curV1 = Number(v1[i])
            const curV2 = Number(v2[i])
 
            if (curV1 > curV2) {
                result = 1
                break;
            } else if (curV1 < curV2) {
                result = -1
                break;
            }
        }
 
        if (result === 0 && (v1.length !== v2.length)) {
            const v1BiggerThenv2 = v1.length > v2.length;
            const maxLensVersion = v1BiggerThenv2 ? v1 : v2;
            for (let i = minVersionLens; i < maxLensVersion.length; i++) {
                const curVersion = Number(maxLensVersion[i])
                if (curVersion > 0) {
                    v1BiggerThenv2 ? result = 1 : result = -1
                    break;
                }
            }
        }
 
        return result;
    }
 
    export default {
        mixins: [addAndDetail],
        data() {
            return {
                latestVersion: '0.0.0',
                lastVersionId: '',
                uniFilePickerProvider: 'unicloud',
                domain: ""
            }
        },
        async onLoad({
            appid,
            name,
            type
        }) {
            let { domain, provider } = this.getCloudStorageConfig();
            if (domain) this.domain = domain;
            if (provider) this.uniFilePickerProvider = provider;
 
            if (appid && type && name) {
                const store_list = await this.getStoreList(appid)
                this.formData = {
                    ...this.formData,
                    ...{
                        appid,
                        name,
                        type,
                        store_list,
                    }
                }
 
                this.latestStableData = await this.getDetail(appid, type)
                // 如果有数据,否则为发布第一版,默认为Android
                if (!this.isWGT && this.latestStableData.length) {
                    this.setFormData(platform_Android)
                }
                // 如果是wgt ,则需要将 min_uni_version 设为必填
                if (this.isWGT) {
                    this.rules.min_uni_version.rules.push({
                        "required": true
                    })
                }
            }
        },
        onUnload() {
            // 临时处理,后面会再优化
            this.setCloudStorage({
                provider: null
            });
        },
        watch: {
            isiOS(val) {
                if (!val && this.hasPackage) {
                    this.formData.url = this.appFileList.url
                    return;
                }
                this.formData.url = ''
            },
            "formData.platform"(val) {
                this.setFormData(val)
            },
            "domain"(val) {
                this.setCloudStorage({
                    domain: val
                });
                if (this.formData.url) {
                    // 替换 this.formData.url 内的域名
                    if (!val) val = "请输入自定义域名"
                    this.formData.url = this.formData.url.replace(/^(https?:\/\/)[^\/]+/, `$1${val}`);
                }
            },
            uniFilePickerProvider:{
                immediate: true,
                handler(val){
                    this.setCloudStorage({
                        provider: val
                    });
                }
            }
        },
        methods: {
            setFormData(os) {
                uni.showLoading({
                    mask: true
                })
                // 每次需初始化 版本 与 id ,因为可能是新增第一版
                this.latestVersion = '0.0.0';
                this.lastVersionId = ''
 
                const data = this.getData(this.latestStableData, os)[0]
 
                if (data) {
                    const {
                        _id,
                        version,
                        name,
                        platform,
                        min_uni_version,
                        url
                    } = data
 
                    this.lastVersionId = _id
                    this.latestVersion = version;
 
                    this.formData.name = name
 
                    // 如果不是wgt,则需要删除 min_uni_version 字段
                    if (!this.isWGT) {
                        delete this.formData.min_uni_version;
                        this.formData.platform = platform[0]
 
                        // iOS需要带出上一版本的AppStore链接
                        if (this.isiOS) {
                            this.formData.url = url;
                        }
                    } else {
                        this.formData.min_uni_version = min_uni_version
                        // this.formData.platform = [os]
                    }
                } else if (this.isWGT) {
                    this.formData.min_uni_version = ''
                }
                uni.hideLoading()
            },
            /**
             * 触发表单提交
             */
            submit() {
                uni.showLoading({
                    mask: true
                })
                this.$refs.form.validate(['store_list']).then((res) => {
                    if (compare(this.latestVersion, res.version) >= 0) {
                        uni.showModal({
                            content: `版本号必须大于当前已上线版本(${this.latestVersion})`,
                            showCancel: false
                        })
                        throw new Error('版本号必须大于已上线版本(${this.latestVersion})');
                    }
                    // 如果不是 wgt 更新,则需将 platform 字段还原为 array
                    if (!this.isWGT) {
                        res.platform = [res.platform]
                    }
                    if (this.isiOS || this.isWGT) delete res.store_list;
                    if (res.store_list) {
                        res.store_list.forEach(item => {
                            item.priority = parseFloat(item.priority)
                        })
                    }
                    this.submitForm(res)
                }).catch((errors) => {
                    uni.hideLoading()
                })
            },
            async submitForm(value) {
                value = this.createCenterRecord(value)
                const collectionDB = db.collection(dbCollectionName)
                // uni-stat 会创建这些字段 appid
                let recordCreateByUniStat = []
                if (!this.isWGT) {
                    recordCreateByUniStat = await this.getDetail(value.appid, value.type, this.createStatQuery(value))
                }
 
                let dbOperate
                if (!recordCreateByUniStat.length) {
                    dbOperate = collectionDB.add(value)
                } else {
                    value.create_date = Date.now()
                    dbOperate = collectionDB.doc(recordCreateByUniStat[0]._id).update(value)
                }
 
                // 使用 clientDB 提交数据
                dbOperate.then(async (res) => {
                    // 如果新增版本为上线发行,且之前有该平台的上线发行,则自动将上一版设为下线
                    if (value.stable_publish && this.lastVersionId) {
                        await collectionDB.doc(this.lastVersionId).update({
                            stable_publish: false
                        })
                    }
                    uni.showToast({
                        title: '新增成功'
                    })
                    this.getOpenerEventChannel().emit('refreshData')
                    setTimeout(() => uni.navigateBack(), 500)
                }).catch((err) => {
                    uni.showModal({
                        content: err.message || '请求服务失败',
                        showCancel: false
                    })
                }).finally(() => {
                    uni.hideLoading()
                })
 
                this.setCloudStorageConfig({
                    provider: this.uniFilePickerProvider,
                    domain: this.domain,
                });
            },
            /**
             * 获取表单数据
             * @param {Object} id
             */
            getDetail(appid, type, args = {}) {
                uni.showLoading({
                    mask: true
                })
                return db.collection(dbCollectionName)
                    .where(
                        Object.assign({
                            appid,
                            type,
                            stable_publish: true
                        }, args)
                    )
                    .field(fields)
                    .get()
                    .then((res) => res.result.data)
                    .catch((err) => {
                        uni.showModal({
                            content: err.message || '请求服务失败',
                            showCancel: false
                        })
                    }).finally(() => {
                        uni.hideLoading()
                    })
            },
            getData(data = [], platform) {
                if (typeof platform === 'string') {
                    return data.filter(item => item.platform.includes(platform))
                } else {
                    return data.filter(item => item.platform.toString() === platform.toString())
                }
            },
            back() {
                uni.showModal({
                    title: '取消发布',
                    content: this.hasPackage ? '将会删除已上传的包' : undefined,
                    success: res => {
                        if (res.confirm) {
                            // 若已上传包但取消发布,则自动将包删除
                            if (this.hasPackage) {
                                this.deleteFile([this.appFileList.url])
                            }
 
                            uni.navigateBack()
                        }
                    }
                });
            }
        }
    }
</script>
 
<style lang="scss">
    ::v-deep .uni-forms-item__content {
        display: flex;
        align-items: center;
    }
 
    .uni-button-group {
        & button {
            margin-left: 15px;
        }
 
        & button:first-child {
            margin-left: 0px;
        }
    }
 
    .title_padding {
        padding-bottom: 15px;
        display: block;
    }
 
    ::v-deep .uni-file-picker__files {
        max-width: 100%;
    }
</style>