publish_add_detail_mixin.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import {
  2. validator,
  3. mpPlatform
  4. } from '@/js_sdk/validator/opendb-app-list.js';
  5. const formatFilePickerValue = (url) => (url ? {
  6. "name": "",
  7. "extname": "",
  8. "url": url,
  9. } : {})
  10. function getValidator(fields) {
  11. let result = {}
  12. for (let key in validator) {
  13. if (fields.includes(key)) {
  14. result[key] = validator[key]
  15. }
  16. }
  17. return result
  18. }
  19. const schemes = ["mimarket", "samsungapps", "appmarket", "oppomarket", "vivomarket"]
  20. const schemeBrand = ["xiaomi", "samsung", "huawei", "oppo", "vivo"]
  21. export default {
  22. data() {
  23. let formData = {
  24. "appid": "",
  25. "name": "",
  26. "icon_url": "",
  27. "introduction": "",
  28. "alias": "",
  29. "description": "",
  30. "screenshot": [],
  31. "store_list": [],
  32. "app_android": {},
  33. "app_ios": {},
  34. "mp_weixin": {},
  35. "mp_alipay": {},
  36. "mp_baidu": {},
  37. "mp_toutiao": {},
  38. "mp_qq": {},
  39. "mp_lark": {},
  40. "mp_kuaishou": {},
  41. "mp_dingtalk": {},
  42. "mp_jd": {},
  43. "h5": {},
  44. "quickapp": {}
  45. }
  46. const data = {
  47. formData,
  48. rules: Object.freeze(getValidator(Object.keys(formData))),
  49. mpPlatform: Object.freeze(mpPlatform),
  50. screenshotList: [],
  51. middleware_img: {},
  52. middleware_checkbox: {},
  53. appPackageInfo: {},
  54. appPlatformKeys: Object.freeze(['app_ios', 'app_android']),
  55. appPlatformValues: Object.freeze({
  56. app_android: 'Android',
  57. app_ios: 'iOS'
  58. }),
  59. keepItems: Object.freeze([]),
  60. isEdit: false,
  61. deletedStore: []
  62. }
  63. const mpKeys = Object.keys(mpPlatform);
  64. data.mpPlatformKeys = Object.freeze(mpKeys);
  65. [].concat(mpKeys, ['icon_url', 'quickapp']).forEach(key => data.middleware_img[key] = {});
  66. data.platFormKeys = Object.freeze([].concat(mpKeys, data.appPlatformKeys))
  67. data.platFormKeys.forEach(key => data.middleware_checkbox[key] = false)
  68. return data
  69. },
  70. methods: {
  71. requestCloudFunction(functionName, params = {}) {
  72. return this.$request(functionName, params, {
  73. functionName: 'uni-upgrade-center'
  74. })
  75. },
  76. hasValue(value) {
  77. if (typeof value !== 'object') return !!value
  78. if (value instanceof Array) return !!value.length
  79. return !!(value && Object.keys(value).length)
  80. },
  81. initFormData(obj) {
  82. if (!obj || !Object.keys(obj).length) return;
  83. // TODO delete
  84. for (let key in obj) {
  85. const value = obj[key]
  86. switch (key) {
  87. case 'icon_url':
  88. this.middleware_img[key] = formatFilePickerValue(value)
  89. break;
  90. case 'screenshot':
  91. this.screenshotList = value.map(item => formatFilePickerValue(item))
  92. break;
  93. default:
  94. if ((key.indexOf('mp') !== -1 || key.indexOf('app') !== -1) && this.hasValue(value)) {
  95. this.setPlatformChcekbox(key, true)
  96. if (value.qrcode_url)
  97. this.middleware_img[key] = formatFilePickerValue(value.qrcode_url)
  98. }
  99. break;
  100. }
  101. this.setFormData(key, value)
  102. }
  103. },
  104. setFormData(key, value) {
  105. const keys = key.indexOf('.') !== -1 ? key.split('.') : [key];
  106. const lens = keys.length - 1
  107. let tempObj = this.formData
  108. keys.forEach((key, index) => {
  109. const obj = tempObj[key]
  110. if (typeof obj === 'object' && index < lens) {
  111. tempObj = obj
  112. } else {
  113. tempObj[key] = value
  114. }
  115. })
  116. },
  117. getFormData(key) {
  118. const keys = key.indexOf('.') !== -1 ? key.split('.') : [key];
  119. const lens = keys.length - 1
  120. let tempObj = this.formData
  121. for (let i = 0; i < keys.length; i++) {
  122. const key = keys[i]
  123. tempObj = tempObj[key]
  124. if (tempObj == null) {
  125. return false
  126. }
  127. }
  128. return tempObj
  129. },
  130. formatFormData() {
  131. this.setFormData('screenshot', this.screenshotList.map(item => item.fileID || item.url))
  132. for (let i = 0; i < this.formData.store_list.length; i++) {
  133. const item = this.formData.store_list[i]
  134. if (item.scheme.trim().length === 0) {
  135. this.formData.store_list.splice(i, 1)
  136. i--
  137. continue;
  138. }
  139. const index = schemes.indexOf((item.scheme.match(/(.*):\/\//) || [])[1])
  140. if (index !== -1) {
  141. if (item.id !== schemeBrand[index]) {
  142. this.deletedStore.push(item.id)
  143. }
  144. item.id = schemeBrand[index]
  145. }
  146. item.priority = parseFloat(item.priority)
  147. }
  148. this.keepItems = this.platFormKeys
  149. .filter(key =>
  150. this.getPlatformChcekbox(key) &&
  151. (this.formData[key].url || this.formData[key].qrcode_url)
  152. )
  153. .concat(['icon_url', 'screenshot', 'create_date', 'store_list'])
  154. if (this.formData.h5 && this.formData.h5.url)
  155. this.keepItems.push('h5');
  156. },
  157. // 根据 appid 自动填充
  158. autoFill() {
  159. const appid = this.getFormData('appid')
  160. if (!appid) {
  161. return
  162. }
  163. uni.showLoading({
  164. mask: true
  165. })
  166. this.requestCloudFunction('getAppInfo', {
  167. appid
  168. })
  169. .then(res => {
  170. if (res.success) {
  171. this.setFormData('description', res.description)
  172. this.setFormData('name', res.name)
  173. return
  174. }
  175. }).catch(e => {
  176. console.error(e)
  177. }).finally(() => {
  178. uni.hideLoading()
  179. })
  180. },
  181. autoFillApp() {
  182. const appid = this.getFormData('appid')
  183. if (!appid) {
  184. return
  185. }
  186. this.appPlatformKeys.forEach(key => {
  187. this.fetchAppInfo(appid, this.appPlatformValues[key]).then(res => {
  188. if (res && res.success) {
  189. this.setPlatformChcekbox(key, true)
  190. this.setFormData(key, {
  191. name: res.name,
  192. url: res.url
  193. })
  194. return;
  195. }
  196. })
  197. })
  198. },
  199. fetchAppInfo(appid, platform) {
  200. uni.showLoading({
  201. mask: true
  202. })
  203. return this.requestCloudFunction('getAppVersionInfo', {
  204. appid,
  205. platform
  206. }).then(res => {
  207. return res
  208. }).catch(e => {
  209. console.error(e)
  210. }).finally(() => {
  211. uni.hideLoading()
  212. })
  213. },
  214. iconUrlSuccess(res, key) {
  215. uni.showToast({
  216. icon: 'success',
  217. title: '上传成功',
  218. duration: 500
  219. })
  220. this.setFormData(key, res.tempFilePaths[0])
  221. },
  222. async iconUrlDelete(res, key) {
  223. let deleteRes = await this.requestCloudFunction('deleteFile', {
  224. fileList: [res.tempFile.fileID || res.tempFile.url]
  225. })
  226. deleteRes.fileList ?
  227. deleteRes = deleteRes.fileList[0] :
  228. deleteRes = deleteRes[0];
  229. if (deleteRes.success || deleteRes.code === "SUCCESS") {
  230. uni.showToast({
  231. icon: 'success',
  232. title: '删除成功',
  233. duration: 800
  234. })
  235. if (!key) return;
  236. this.setFormData(key, '')
  237. this.$refs.form.clearValidate(key)
  238. }
  239. },
  240. getPlatformChcekbox(mp_name) {
  241. return this.middleware_checkbox[mp_name]
  242. },
  243. setPlatformChcekbox(mp_name, value = false) {
  244. this.middleware_checkbox[mp_name] = value
  245. },
  246. selectFile() {
  247. if (this.hasPackage) {
  248. uni.showToast({
  249. icon: 'none',
  250. title: '只可上传一个文件,请删除已上传后重试',
  251. duration: 1000
  252. });
  253. }
  254. }
  255. },
  256. computed: {
  257. hasPackage() {
  258. return this.appPackageInfo && !!Object.keys(this.appPackageInfo).length
  259. },
  260. }
  261. }