add.vue 16 KB

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