add.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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;"
  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 v-if="!isiOS" label="上传apk包">
  36. <uni-file-picker v-model="appFileList" :file-extname="fileExtname" :disabled="hasPackage"
  37. returnType="object" file-mediatype="all" limit="1" @success="packageUploadSuccess"
  38. @delete="packageDelete">
  39. <view class="flex">
  40. <button type="primary" size="mini" @click="selectFile" style="margin: 0;">选择文件</button>
  41. <text
  42. style="padding: 10px;font-size: 12px;color: #666;">上传apk到当前服务空间的云存储中,上传成功后,会自动使用云存储地址填充下载链接</text>
  43. </view>
  44. </uni-file-picker>
  45. <text v-if="hasPackage"
  46. style="padding-left: 20px;color: #a8a8a8;">{{Number(appFileList.size / 1024 / 1024).toFixed(2)}}M</text>
  47. </uni-forms-item>
  48. <uni-forms-item key="url" name="url" :label="isiOS ? 'AppStore' : '下载链接'" required>
  49. <uni-easyinput placeholder="链接" v-model="formData.url" :maxlength="-1" />
  50. <!-- <show-info :top="-80" :content="uploadFileContent"></show-info> -->
  51. </uni-forms-item>
  52. <uni-forms-item v-if="!isiOS && !isWGT && formData.store_list.length" label="Android应用市场" labelWidth="125px"
  53. key="store_list" name="store_list">
  54. <view style="flex: 1;">
  55. <view v-for="(item) in formData.store_list" :key="item.id">
  56. <uni-card style="margin: 0px 0px 20px 0px;">
  57. <view style="display: flex;">
  58. <checkbox-group style="user-select: none;"
  59. @change="({detail:{value}}) => {item.enable = !!value.length}">
  60. <label class="title_padding">
  61. <checkbox value="scheme" :checked="item.enable" />
  62. <text>是否启用</text>
  63. </label>
  64. </checkbox-group>
  65. <!-- <view style="padding-left: 10px;">
  66. <button type="warn" size="mini"
  67. @click="formData.store_list.splice(index,1)">删除</button>
  68. </view> -->
  69. </view>
  70. <uni-forms-item label="商店名称">
  71. <uni-easyinput disabled v-model="item.name" trim="both"></uni-easyinput>
  72. </uni-forms-item>
  73. <uni-forms-item label="Scheme">
  74. <uni-easyinput disabled v-model="item.scheme" trim="both"></uni-easyinput>
  75. </uni-forms-item>
  76. <uni-forms-item label="优先级">
  77. <uni-easyinput v-model="item.priority" type="number"></uni-easyinput>
  78. <show-info :top="-100" :left="-180" :content="priorityContent"></show-info>
  79. </uni-forms-item>
  80. </uni-card>
  81. </view>
  82. </view>
  83. </uni-forms-item>
  84. <uni-forms-item v-if="isWGT" key="is_silently" name="is_silently" label="静默更新">
  85. <switch @change="binddata('is_silently', $event.detail.value)" :checked="formData.is_silently" />
  86. <show-info :top="-80" :content="silentlyContent"></show-info>
  87. </uni-forms-item>
  88. <uni-forms-item v-if="!isiOS" key="is_mandatory" name="is_mandatory" label="强制更新">
  89. <switch @change="binddata('is_mandatory', $event.detail.value)" :checked="formData.is_mandatory" />
  90. <show-info :content="mandatoryContent"></show-info>
  91. </uni-forms-item>
  92. <uni-forms-item name="stable_publish" label="上线发行">
  93. <switch @change="binddata('stable_publish', $event.detail.value)" :checked="formData.stable_publish" />
  94. <show-info :top="-40" :content="stablePublishContent2"></show-info>
  95. </uni-forms-item>
  96. <uni-forms-item v-show="false" name="type" label="安装包类型">
  97. <uni-data-checkbox v-model="formData.type" :localdata="formOptions.type_localdata" />
  98. </uni-forms-item>
  99. <view class="uni-button-group">
  100. <button type="primary" class="uni-button" style="width: 100px;" @click="submit">发布</button>
  101. <button type="warn" class="uni-button" style="width: 100px;margin-left: 15px;" @click="back">取消</button>
  102. </view>
  103. </uni-forms>
  104. </view>
  105. </template>
  106. <script>
  107. import {
  108. validator,
  109. enumConverter
  110. } from '@/js_sdk/validator/opendb-app-versions.js';
  111. import addAndDetail, {
  112. fields
  113. } from '../mixin/version_add_detail_mixin.js';
  114. import {
  115. appVersionListDbName
  116. } from '../utils.js';
  117. const db = uniCloud.database();
  118. const dbCmd = db.command;
  119. const dbCollectionName = appVersionListDbName;
  120. const platform_iOS = 'iOS';
  121. const platform_Android = 'Android';
  122. /**
  123. * 对比版本号,如需要,请自行修改判断规则
  124. * 支持比对 ("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") 之类的
  125. * @param {Object} v1
  126. * @param {Object} v2
  127. * v1 > v2 return 1
  128. * v1 < v2 return -1
  129. * v1 == v2 return 0
  130. */
  131. function compare(v1 = '0', v2 = '0') {
  132. v1 = String(v1).split('.')
  133. v2 = String(v2).split('.')
  134. const minVersionLens = Math.min(v1.length, v2.length);
  135. let result = 0;
  136. for (let i = 0; i < minVersionLens; i++) {
  137. const curV1 = Number(v1[i])
  138. const curV2 = Number(v2[i])
  139. if (curV1 > curV2) {
  140. result = 1
  141. break;
  142. } else if (curV1 < curV2) {
  143. result = -1
  144. break;
  145. }
  146. }
  147. if (result === 0 && (v1.length !== v2.length)) {
  148. const v1BiggerThenv2 = v1.length > v2.length;
  149. const maxLensVersion = v1BiggerThenv2 ? v1 : v2;
  150. for (let i = minVersionLens; i < maxLensVersion.length; i++) {
  151. const curVersion = Number(maxLensVersion[i])
  152. if (curVersion > 0) {
  153. v1BiggerThenv2 ? result = 1 : result = -1
  154. break;
  155. }
  156. }
  157. }
  158. return result;
  159. }
  160. export default {
  161. mixins: [addAndDetail],
  162. data() {
  163. return {
  164. latestVersion: '0.0.0',
  165. lastVersionId: ''
  166. }
  167. },
  168. async onLoad({
  169. appid,
  170. name,
  171. type
  172. }) {
  173. if (appid && type && name) {
  174. const store_list = await this.getStoreList(appid)
  175. this.formData = {
  176. ...this.formData,
  177. ...{
  178. appid,
  179. name,
  180. type,
  181. store_list
  182. }
  183. }
  184. this.latestStableData = await this.getDetail(appid, type)
  185. // 如果有数据,否则为发布第一版,默认为Android
  186. if (!this.isWGT && this.latestStableData.length) {
  187. this.setFormData(platform_Android)
  188. }
  189. // 如果是wgt ,则需要将 min_uni_version 设为必填
  190. if (this.isWGT) {
  191. this.rules.min_uni_version.rules.push({
  192. "required": true
  193. })
  194. }
  195. }
  196. },
  197. watch: {
  198. isiOS(val) {
  199. if (!val && this.hasPackage) {
  200. this.formData.url = this.appFileList.url
  201. return;
  202. }
  203. this.formData.url = ''
  204. },
  205. "formData.platform"(val) {
  206. this.setFormData(val)
  207. }
  208. },
  209. methods: {
  210. setFormData(os) {
  211. uni.showLoading({
  212. mask: true
  213. })
  214. // 每次需初始化 版本 与 id ,因为可能是新增第一版
  215. this.latestVersion = '0.0.0';
  216. this.lastVersionId = ''
  217. const data = this.getData(this.latestStableData, os)[0]
  218. if (data) {
  219. const {
  220. _id,
  221. version,
  222. name,
  223. platform,
  224. min_uni_version,
  225. url
  226. } = data
  227. this.lastVersionId = _id
  228. this.latestVersion = version;
  229. this.formData.name = name
  230. // 如果不是wgt,则需要删除 min_uni_version 字段
  231. if (!this.isWGT) {
  232. delete this.formData.min_uni_version;
  233. this.formData.platform = platform[0]
  234. // iOS需要带出上一版本的AppStore链接
  235. if (this.isiOS) {
  236. this.formData.url = url;
  237. }
  238. } else {
  239. this.formData.min_uni_version = min_uni_version
  240. // this.formData.platform = [os]
  241. }
  242. } else if (this.isWGT) {
  243. this.formData.min_uni_version = ''
  244. }
  245. uni.hideLoading()
  246. },
  247. /**
  248. * 触发表单提交
  249. */
  250. submit() {
  251. uni.showLoading({
  252. mask: true
  253. })
  254. this.$refs.form.validate(['store_list']).then((res) => {
  255. if (compare(this.latestVersion, res.version) >= 0) {
  256. uni.showModal({
  257. content: `版本号必须大于当前已上线版本(${this.latestVersion})`,
  258. showCancel: false
  259. })
  260. throw new Error('版本号必须大于已上线版本(${this.latestVersion})');
  261. return;
  262. }
  263. // 如果不是 wgt 更新,则需将 platform 字段还原为 array
  264. if (!this.isWGT) {
  265. res.platform = [res.platform]
  266. }
  267. if (this.isiOS || this.isWGT) delete res.store_list;
  268. if (res.store_list) {
  269. res.store_list.forEach(item => {
  270. item.priority = parseFloat(item.priority)
  271. })
  272. }
  273. this.submitForm(res)
  274. }).catch((errors) => {
  275. uni.hideLoading()
  276. })
  277. },
  278. async submitForm(value) {
  279. value = this.createCenterRecord(value)
  280. const collectionDB = db.collection(dbCollectionName)
  281. // uni-stat 会创建这些字段 appid
  282. let recordCreateByUniStat = []
  283. if (!this.isWGT) {
  284. recordCreateByUniStat = await this.getDetail(value.appid, value.type, this.createStatQuery(value))
  285. }
  286. let dbOperate
  287. if (!recordCreateByUniStat.length) {
  288. dbOperate = collectionDB.add(value)
  289. } else {
  290. value.create_date = Date.now()
  291. dbOperate = collectionDB.doc(recordCreateByUniStat[0]._id).update(value)
  292. }
  293. // 使用 clientDB 提交数据
  294. dbOperate.then(async (res) => {
  295. // 如果新增版本为上线发行,且之前有该平台的上线发行,则自动将上一版设为下线
  296. if (value.stable_publish && this.lastVersionId) {
  297. await collectionDB.doc(this.lastVersionId).update({
  298. stable_publish: false
  299. })
  300. }
  301. uni.showToast({
  302. title: '新增成功'
  303. })
  304. this.getOpenerEventChannel().emit('refreshData')
  305. setTimeout(() => uni.navigateBack(), 500)
  306. }).catch((err) => {
  307. uni.showModal({
  308. content: err.message || '请求服务失败',
  309. showCancel: false
  310. })
  311. }).finally(() => {
  312. uni.hideLoading()
  313. })
  314. },
  315. /**
  316. * 获取表单数据
  317. * @param {Object} id
  318. */
  319. getDetail(appid, type, args = {}) {
  320. uni.showLoading({
  321. mask: true
  322. })
  323. return db.collection(dbCollectionName)
  324. .where(
  325. Object.assign({
  326. appid,
  327. type,
  328. stable_publish: true
  329. }, args)
  330. )
  331. .field(fields)
  332. .get()
  333. .then((res) => res.result.data)
  334. .catch((err) => {
  335. uni.showModal({
  336. content: err.message || '请求服务失败',
  337. showCancel: false
  338. })
  339. }).finally(() => {
  340. uni.hideLoading()
  341. })
  342. },
  343. getStoreList(appid) {
  344. return db.collection('opendb-app-list')
  345. .where({
  346. appid
  347. })
  348. .get()
  349. .then(res => {
  350. const data = res.result.data[0]
  351. return data.store_list || []
  352. })
  353. },
  354. getData(data = [], platform) {
  355. if (typeof platform === 'string') {
  356. return data.filter(item => item.platform.includes(platform))
  357. } else {
  358. return data.filter(item => item.platform.toString() === platform.toString())
  359. }
  360. },
  361. back() {
  362. uni.showModal({
  363. title: '取消发布',
  364. content: this.hasPackage ? '将会删除已上传的包' : undefined,
  365. success: res => {
  366. if (res.confirm) {
  367. // 若已上传包但取消发布,则自动将包删除
  368. if (this.hasPackage) {
  369. let fileList = [];
  370. fileList.push(this.appFileList.url)
  371. this.$request('deleteFile', {
  372. fileList
  373. }, {
  374. functionName: 'upgrade-center'
  375. })
  376. }
  377. uni.navigateBack()
  378. }
  379. }
  380. });
  381. }
  382. }
  383. }
  384. </script>
  385. <style lang="scss">
  386. ::v-deep .uni-forms-item__content {
  387. display: flex;
  388. align-items: center;
  389. }
  390. .uni-button-group {
  391. & button {
  392. margin-left: 15px;
  393. }
  394. & button:first-child {
  395. margin-left: 0px;
  396. }
  397. }
  398. .title_padding {
  399. padding-bottom: 15px;
  400. display: block;
  401. }
  402. ::v-deep .uni-file-picker__files {
  403. max-width: 100%;
  404. }
  405. </style>