CardController.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { getCardList, addCard, addDefaultCard, getCardJsonById, getDefaultCard } from '@/api/card'
  2. import { message } from 'ant-design-vue'
  3. export class CardController {
  4. static cardType = [
  5. { value: 5, title: '推钮卡' },
  6. { value: 21, title: '点触卡' }
  7. ]
  8. static ruleList = [
  9. { title: '单点单播', value: 7, desc: '无err_key, 只加ok_key' }, // 点一下播放一个语音,此时语音放在ok_key里
  10. { title: '无序多点', value: 9, desc: '单点操作,多个答案的无序单选' }, // 点完所有答案播放语音,语音放在ok_key_voice里 按照步骤
  11. { title: '有序单点', value: 10, desc: '单点操作,多个正确答案的有序单选' }, // 点完所有答案播放语音,语音放在ok_key_voice里 按照步骤
  12. // { title: '双点', value: 11, desc: '' },
  13. // { title: '无序多选(有一个固定按钮)', value: 12, desc: '' },
  14. // { title: '有序多选(有一个固定按钮)', value: 13, desc: '' },
  15. { title: '无序多选', value: 14, desc: '' }, // 每组有两个答案,每个答案有一个语音,语音放在ok_key_voice里 按照步骤
  16. { title: '有序多选', value: 15, desc: '' }, // 每组有两个答案,每个答案有一个语音,语音放在ok_key_voice里 按照步骤
  17. { title: 'pk对战', value: 16, desc: '' }, // ok_key下的item同时有两项语音放在ok_key_voice里 按照步骤,错误的err_key数量位置,语音放在err_key_voice里
  18. { title: '有序单点和无序单点混搭', value: 17, desc: '' }, // 17时order代表是有序单点还是无序单点
  19. { title: '新手指引', value: 18, desc: '' }
  20. ]
  21. static cardButtonTitleMap = new Map([
  22. [0, '按钮'],
  23. [1, '刷新'],
  24. [2, '提示']
  25. ])
  26. static cardButtonRowMap = new Map([
  27. [1, 'A'],
  28. [2, 'B'],
  29. [3, 'C'],
  30. [4, 'D'],
  31. [5, 'E'],
  32. [6, 'F']
  33. ])
  34. static breakList = [
  35. { value: 0, title: '能被其他音频打断,不能打断其他音频' },
  36. { value: 1, title: '能被其他音频打断,能打断其他音频' },
  37. { value: 2, title: '不能被其他音频打断,不能打断其他音频' },
  38. { value: 3, title: '不能被其他音频打断,能打断其他音频' }
  39. ]
  40. static breakMap = new Map([
  41. [0, '能被其他音频打断,不能打断其他音频'],
  42. [1, '能被其他音频打断,能打断其他音频'],
  43. [2, '不能被其他音频打断,不能打断其他音频'],
  44. [3, '不能被其他音频打断,能打断其他音频']
  45. ])
  46. static async getCardList (parentId: string) {
  47. const { data } = await getCardList(parentId)
  48. return { data }
  49. }
  50. static async add (data: API.CardJson) {
  51. const _data = data
  52. if (data.header.card_type === 5) {
  53. CardController.completeCard5Json(_data)
  54. }
  55. const { status } = await addCard(data.header.card_type, _data)
  56. status === 200 ? message.success('保存成功') : message.error('保存失败')
  57. return status
  58. }
  59. static async addDefaulJson (data: API.CardJsonDefault) {
  60. const { status } = await addDefaultCard(data)
  61. status === 200 ? message.success('保存成功') : message.error('保存失败')
  62. return status
  63. }
  64. static async getDefaultJson () {
  65. const { data } = await getDefaultCard()
  66. return { data: data }
  67. }
  68. static async cardJsonById (id: string) {
  69. const { data } = await getCardJsonById(id)
  70. // 没有默认数据的,需要让用户选择题卡类型
  71. if (data == null) {
  72. return { data: {}, cardType: '' }
  73. }
  74. const dataJson = JSON.parse(data)
  75. console.log('卡片数据详情:', dataJson)
  76. if (dataJson.header.card_type === 5) {
  77. return CardController.card5JsonById(dataJson)
  78. } else if (dataJson.header.card_type === 21) {
  79. return CardController.card21JsonById(dataJson)
  80. } else {
  81. throw new Error('不支持的卡片类型')
  82. }
  83. }
  84. /**
  85. *
  86. * @description 根据id获取卡片json
  87. * @param id
  88. * @returns
  89. *
  90. * touch_key 和卡片的操作联系在一起,在这里手动处理参数不够的问题
  91. * 给touch_key增加id, name, 是否已经选择也在这里处理
  92. * selected字段代表是否选择, 存在music_name是 selected是true 否则为false
  93. *
  94. */
  95. static async card5JsonById (_dataJson: API.CardJson) {
  96. const dataJson = JSON.parse(JSON.stringify(_dataJson)) as API.CardJson
  97. (dataJson as API.CardJson).touch_key = (dataJson as API.CardJson).touch_key.map((item, index) => {
  98. if ('music_name' in item && item.music_name) {
  99. return {
  100. ...item,
  101. ...CardController.createRectByIndex(index),
  102. id: index,
  103. selected: true
  104. }
  105. } else {
  106. return {
  107. is_break: item.is_break,
  108. ...CardController.createRectByIndex(index),
  109. music_name: '',
  110. id: index,
  111. selected: false
  112. }
  113. }
  114. })
  115. return { data: dataJson, cardType: dataJson.header.card_type }
  116. }
  117. static async card21JsonById (_dataJson: API.CardJson21) {
  118. const dataJson = JSON.parse(JSON.stringify(_dataJson)) as API.CardJson21
  119. console.log('卡片是21时返回的参数', dataJson)
  120. dataJson.game_list.forEach((game: any) => {
  121. if (!game.touch_key || !Array.isArray(game.touch_key)) {
  122. game.touch_key = [
  123. [{ value: 0, is_break: 1, music_name: '' }],
  124. [{ value: 0, is_break: 1, music_name: '' }],
  125. [{ value: 0, is_break: 1, music_name: '' }]
  126. ]
  127. }
  128. game.touch_key.forEach((touch_keys: API.CardJson21TouchKey) => {
  129. if (
  130. touch_keys[0].music_name === undefined ||
  131. touch_keys[0].music_name === null ||
  132. touch_keys[0].music_name === '' ||
  133. !touch_keys[0].music_name
  134. ) {
  135. touch_keys[0].music_name = ''
  136. }
  137. })
  138. })
  139. return { data: dataJson, cardType: dataJson.header.card_type }
  140. }
  141. static createRectByIndex (index: number) {
  142. if (index < 3) {
  143. return {
  144. name: CardController.cardButtonTitleMap.get(index)!,
  145. col: index,
  146. row: 0
  147. }
  148. } else {
  149. const row = Math.floor((index - 2) / 6) + ((index - 2) % 6 === 0 ? 0 : 1)
  150. const prefix = CardController.cardButtonRowMap.get(row)!
  151. const suffix = (index - 2) % 6 === 0 ? 6 : (index - 2) % 6
  152. return {
  153. name: prefix + suffix,
  154. row: row,
  155. col: suffix - 1
  156. }
  157. }
  158. }
  159. static createRect () {
  160. const obj = [] as any[]
  161. for (let row = 1; row < 7; row++) {
  162. for (let col = 1; col < 7; col++) {
  163. const prefix = CardController.cardButtonRowMap.get(row)!
  164. const suffix = col
  165. obj.push({
  166. id: col + (row - 1) * 6,
  167. name: prefix + suffix,
  168. row: row,
  169. col: suffix - 1,
  170. music_name: '',
  171. is_break: 1,
  172. value: '',
  173. select: true
  174. })
  175. }
  176. }
  177. return obj
  178. }
  179. // cardtype = 5
  180. static generateCard5Json (): API.CardJson {
  181. return {
  182. header: {
  183. card_type: 5,
  184. title: {
  185. category: 0,
  186. id: 0,
  187. sub_id: 0,
  188. page: 0,
  189. music_name: '',
  190. is_break: 0
  191. },
  192. card_insert: { music_name: '', is_break: 1 },
  193. card_remove: { music_name: '', is_break: 1 },
  194. ack_ok: { music_name: '', is_break: 1 },
  195. ack_mdf: { music_name: '', is_break: 1 },
  196. ack_err: { music_name: '', is_break: 1 },
  197. remind_ack: { music_name: '', is_break: 1 },
  198. remind_button: { music_name: '', is_break: 1 },
  199. remind_not_ack: { music_name: '', is_break: 1 },
  200. wait_30s: { music_name: '', is_break: 1 },
  201. wait_90s: { music_name: '', is_break: 1 }
  202. },
  203. slide_knob: {
  204. score1: '',
  205. score2: '',
  206. score3: '',
  207. score4: '',
  208. score5: '',
  209. score6: '',
  210. purple: [{ music_name: '', is_break: 1, key: 'purple' }],
  211. red: [{ music_name: '', is_break: 1, key: 'red' }],
  212. green: [{ music_name: '', is_break: 1, key: 'green' }],
  213. tangerine: [{ music_name: '', is_break: 1, key: 'tangerine' }],
  214. yellow: [{ music_name: '', is_break: 1, key: 'yellow' }],
  215. blue: [{ music_name: '', is_break: 1, key: 'blue' }]
  216. },
  217. touch_key: new Array(39).fill(0).map((item, index) => {
  218. return {
  219. id: index,
  220. music_name: '',
  221. is_break: 1,
  222. ...CardController.createRectByIndex(index),
  223. selected: false
  224. }
  225. })
  226. }
  227. }
  228. static generateCard21Json (): API.CardJson21 {
  229. return {
  230. header: {
  231. card_type: 21,
  232. title: {
  233. category: 0,
  234. id: 0,
  235. sub_id: 0,
  236. page: 0,
  237. music_name: '',
  238. is_break: 1
  239. },
  240. card_insert: { music_name: '', is_break: 1 },
  241. card_remove: { music_name: '', is_break: 1 },
  242. finish: { music_name: '', is_break: 1 }
  243. },
  244. game_list: []
  245. }
  246. }
  247. // 用户提交json时,为保证数据的正确统一性,没填写的字段进行补全
  248. static completeCard5Json (dataJson: API.CardJson) {
  249. const card5Json = CardController.generateCard5Json()
  250. Object.keys(card5Json.header).forEach((key) => {
  251. if (dataJson.header[key] === undefined || dataJson.header[key] === null || dataJson.header[key] === '') {
  252. dataJson.header[key] = card5Json.header[key]
  253. }
  254. })
  255. // 给slide_knob赋值
  256. if (dataJson.slide_knob === null || dataJson.slide_knob === undefined) {
  257. dataJson.slide_knob = card5Json.slide_knob
  258. } else {
  259. Object.keys(card5Json.slide_knob!).forEach((key) => {
  260. // 给score赋值
  261. if (typeof card5Json.slide_knob![key] === 'string' && !dataJson.slide_knob![key]) {
  262. dataJson.slide_knob![key] = card5Json.slide_knob![key]
  263. } else {
  264. // 给颜色数组赋值
  265. if (dataJson.slide_knob![key] === undefined || dataJson.slide_knob![key] === null || dataJson.slide_knob![key].length === 0) {
  266. dataJson.slide_knob![key] = card5Json.slide_knob![key]
  267. }
  268. }
  269. })
  270. }
  271. // 给touch_key赋值
  272. if (dataJson.touch_key === null || dataJson.touch_key === undefined) {
  273. dataJson.touch_key = card5Json.touch_key
  274. } else {
  275. Object.keys(card5Json.touch_key!).forEach((key) => {
  276. if (dataJson.touch_key![key] === undefined || dataJson.touch_key![key] === null || dataJson.touch_key![key] === '') {
  277. dataJson.touch_key![key] = card5Json.touch_key![key]
  278. }
  279. })
  280. }
  281. }
  282. }