index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import OSticky from '@/components/o-sticky'
  2. import request from '@/helpers/request'
  3. import dayjs from 'dayjs'
  4. import {
  5. Button,
  6. Cell,
  7. CellGroup,
  8. DatePicker,
  9. Field,
  10. Picker,
  11. Popup,
  12. Radio,
  13. RadioGroup,
  14. showSuccessToast,
  15. showToast
  16. } from 'vant'
  17. import { defineComponent, onMounted, reactive } from 'vue'
  18. import styles from './index.module.less'
  19. import iconUpload from '../images/icon-upload.png'
  20. import iconUploadVideo from '../images/icon-upload-video.png'
  21. import iconUploadVideoCover from '../images/icon-upload-video-cover.png'
  22. import OUploadAll from '@/components/o-upload-all'
  23. import OHeader from '@/components/o-header'
  24. import ODialog from '@/components/o-dialog'
  25. import { useRoute, useRouter } from 'vue-router'
  26. import { browser } from '@/helpers/utils'
  27. export default defineComponent({
  28. name: 'story-operation',
  29. setup() {
  30. const router = useRouter()
  31. const route = useRoute()
  32. const forms = reactive({
  33. id: route.query.id || null,
  34. orchestraId: route.query.orchestraId || null,
  35. content: '',
  36. orchestraStatus: false,
  37. orchestraList: [] as any,
  38. selectOrchestra: {} as any,
  39. createTime: new Date() as any,
  40. createTimeStatus: false,
  41. currentDate: [dayjs().format('YYYY'), dayjs().format('MM'), dayjs().format('DD')],
  42. storyType: 'IMAGE',
  43. attachments: [] as any, //群发消息附件
  44. video: [] as any,
  45. videoCover: [] as any,
  46. delStatus: false
  47. })
  48. // 获取乐团列表
  49. const getOrchestras = async () => {
  50. try {
  51. const { data } = await request.post('/api-school/orchestra/page', {
  52. data: {
  53. page: 1,
  54. rows: 100,
  55. status: 'DONE'
  56. }
  57. })
  58. const temps = data.rows || []
  59. const s = [] as any
  60. temps.forEach((item: any) => {
  61. s.push({
  62. text: item.name,
  63. value: item.id
  64. })
  65. })
  66. forms.orchestraList = [...s]
  67. // 判断是否有乐团
  68. if (s.length > 0) {
  69. if (forms.orchestraId) {
  70. const item = s.find((child: any) => child.value === forms.orchestraId)
  71. forms.selectOrchestra = item || s[0]
  72. } else {
  73. forms.selectOrchestra = s[0]
  74. }
  75. }
  76. } catch {
  77. //
  78. }
  79. }
  80. const getDetails = async () => {
  81. try {
  82. if (!forms.id) {
  83. return
  84. }
  85. const { data } = await request.get('/api-school/orchestraStory/detail/' + forms.id)
  86. console.log(data)
  87. forms.content = data.content
  88. forms.createTime = data.createTime
  89. forms.storyType = data.type
  90. forms.orchestraList.forEach((item: any) => {
  91. if (item.value === item.orchestraId) {
  92. forms.selectOrchestra = item
  93. }
  94. })
  95. if (data.type === 'IMAGE') {
  96. data.attachments &&
  97. data.attachments.forEach((item: any) => {
  98. forms.attachments.push(item.url)
  99. })
  100. } else {
  101. const temp = data.attachments ? data.attachments[0] : []
  102. forms.video.push(temp.url)
  103. forms.videoCover.push(temp.coverImage)
  104. }
  105. } catch {
  106. //
  107. }
  108. }
  109. const onSumbit = async () => {
  110. try {
  111. if (!forms.selectOrchestra.value) {
  112. showToast('请选择乐团')
  113. return
  114. }
  115. if (!forms.createTime) {
  116. showToast('请选择事迹日期')
  117. return
  118. }
  119. if (!forms.content) {
  120. showToast('请输入事迹内容')
  121. return
  122. }
  123. if (forms.storyType === 'IMAGE' && forms.attachments.length <= 0) {
  124. showToast('请上传照片')
  125. return
  126. }
  127. if (forms.storyType === 'VIDEO') {
  128. if (forms.video.length <= 0) {
  129. showToast('请上传视频')
  130. return
  131. }
  132. if (forms.videoCover.length <= 0) {
  133. showToast('请上传视频封面')
  134. return
  135. }
  136. }
  137. const attachments: any = []
  138. if (forms.storyType === 'IMAGE') {
  139. forms.attachments.forEach((item: any) => {
  140. const temp = {
  141. attachmentType: forms.storyType,
  142. url: item
  143. }
  144. attachments.push(temp)
  145. })
  146. } else {
  147. attachments.push({
  148. attachmentType: forms.storyType,
  149. url: forms.video[0],
  150. coverImage: forms.videoCover[0]
  151. })
  152. }
  153. console.log({
  154. createTime: dayjs(forms.createTime).format('YYYY-MM-DD HH:mm:ss'),
  155. orchestraId: forms.selectOrchestra.value,
  156. content: forms.content,
  157. type: forms.storyType,
  158. attachments
  159. })
  160. const params = {
  161. createTime: dayjs(forms.createTime).format('YYYY-MM-DD HH:mm:ss'),
  162. orchestraId: forms.selectOrchestra.value,
  163. content: forms.content,
  164. type: forms.storyType,
  165. attachments
  166. }
  167. if (forms.id) {
  168. await request.post('/api-school/orchestraStory/update', {
  169. data: {
  170. ...params,
  171. id: forms.id
  172. }
  173. })
  174. // setTimeout(() => {
  175. // showSuccessToast('修改成功')
  176. // }, 100)
  177. router.back()
  178. } else {
  179. await request.post('/api-school/orchestraStory/save', {
  180. data: params
  181. })
  182. // setTimeout(() => {
  183. // showSuccessToast('添加成功')
  184. // }, 100)
  185. router.back()
  186. }
  187. // setTimeout(() => {
  188. // router.back()
  189. // }, 1100)
  190. } catch {
  191. //
  192. }
  193. }
  194. //
  195. const onConfirm = async () => {
  196. try {
  197. await request.post('/api-school/orchestraStory/remove', {
  198. requestType: 'form',
  199. data: {
  200. id: forms.id
  201. }
  202. })
  203. router.back()
  204. // setTimeout(() => {
  205. // showSuccessToast('删除成功')
  206. // }, 100)
  207. // setTimeout(() => {
  208. // router.back()
  209. // }, 1100)
  210. } catch {
  211. //
  212. }
  213. }
  214. onMounted(async () => {
  215. if (forms.id) {
  216. document.title = '修改事迹'
  217. }
  218. await getOrchestras()
  219. await getDetails()
  220. })
  221. return () => (
  222. <div class={styles.storyOperation}>
  223. <OHeader title={forms.id ? '修改事迹' : '添加事迹'}>
  224. {{
  225. right: () =>
  226. forms.id && (
  227. <span
  228. style={{ color: '#777777' }}
  229. onClick={() => {
  230. forms.delStatus = true
  231. }}
  232. >
  233. 删除
  234. </span>
  235. )
  236. }}
  237. </OHeader>
  238. <CellGroup inset class={styles.cellGroup}>
  239. <Field
  240. inputAlign="right"
  241. label="所属乐团"
  242. modelValue={forms.selectOrchestra.text}
  243. placeholder="请选择所属乐团"
  244. onClick={() => {
  245. forms.orchestraStatus = true
  246. }}
  247. readonly
  248. isLink
  249. />
  250. <Field
  251. inputAlign="right"
  252. label="事迹日期"
  253. modelValue={forms.createTime ? dayjs(forms.createTime).format('YYYY-MM-DD') : ''}
  254. placeholder="请选择事迹日期"
  255. onClick={() => {
  256. forms.createTimeStatus = true
  257. }}
  258. readonly
  259. isLink
  260. class={styles.inputForm}
  261. />
  262. </CellGroup>
  263. <CellGroup inset class={styles.cellGroup}>
  264. <Cell title="事迹内容">
  265. {{
  266. title: () => (
  267. <div class={styles.title}>
  268. <div class={styles.name}>事迹内容</div>
  269. <div class={styles.nums}>{forms.content.length || 0}/200</div>
  270. </div>
  271. ),
  272. label: () => (
  273. <Field
  274. style={{ padding: '0', marginTop: '12px' }}
  275. placeholder="请输入乐团事迹内容"
  276. type="textarea"
  277. rows={3}
  278. v-model={forms.content}
  279. maxlength={200}
  280. />
  281. )
  282. }}
  283. </Cell>
  284. </CellGroup>
  285. <CellGroup inset class={styles.cellGroup}>
  286. <Cell title="事迹资料类型" center>
  287. {{
  288. value: () => (
  289. <RadioGroup
  290. checked-color="#FF8057"
  291. v-model={forms.storyType}
  292. direction="horizontal"
  293. >
  294. <Radio class={styles.radioItem} name={'IMAGE'}>
  295. 图片
  296. </Radio>
  297. <Radio class={styles.radioItem} name={'VIDEO'}>
  298. 视频
  299. </Radio>
  300. </RadioGroup>
  301. )
  302. }}
  303. </Cell>
  304. {forms.storyType === 'IMAGE' && (
  305. <Cell center>
  306. {{
  307. title: () => (
  308. <OUploadAll
  309. style={{ marginBottom: '12px' }}
  310. v-model:modelValue={forms.attachments}
  311. maxCount={9}
  312. uploadIcon={iconUpload}
  313. />
  314. )
  315. }}
  316. </Cell>
  317. )}
  318. {forms.storyType === 'VIDEO' && (
  319. <Cell center titleStyle={{ display: 'flex' }}>
  320. {{
  321. title: () => (
  322. <>
  323. <OUploadAll
  324. style={{ marginBottom: '12px' }}
  325. v-model:modelValue={forms.video}
  326. accept="video/*"
  327. native={browser().isApp}
  328. uploadType="VIDEO"
  329. uploadIcon={iconUploadVideo}
  330. deletable={false}
  331. />
  332. <OUploadAll
  333. deletable={false}
  334. style={{ marginBottom: '12px' }}
  335. v-model:modelValue={forms.videoCover}
  336. uploadIcon={iconUploadVideoCover}
  337. />
  338. </>
  339. )
  340. }}
  341. </Cell>
  342. )}
  343. </CellGroup>
  344. <OSticky position="bottom">
  345. <div class={'btnGroup'}>
  346. <Button round block type="primary" onClick={onSumbit}>
  347. 保存
  348. </Button>
  349. </div>
  350. </OSticky>
  351. <Popup v-model:show={forms.createTimeStatus} position="bottom" round>
  352. <DatePicker
  353. maxDate={new Date()}
  354. v-model={forms.currentDate}
  355. onConfirm={(val: any) => {
  356. const selectedValues = val.selectedValues.join('-')
  357. forms.createTime = dayjs(selectedValues).toDate()
  358. forms.createTimeStatus = false
  359. }}
  360. />
  361. </Popup>
  362. <Popup v-model:show={forms.orchestraStatus} position="bottom" round>
  363. <Picker
  364. columns={forms.orchestraList}
  365. onCancel={() => (forms.orchestraStatus = false)}
  366. onConfirm={(val: any) => {
  367. forms.selectOrchestra = val.selectedOptions[0]
  368. forms.orchestraStatus = false
  369. }}
  370. />
  371. </Popup>
  372. <ODialog
  373. v-model:show={forms.delStatus}
  374. title="删除事迹"
  375. messageAlign="left"
  376. message="删除后学生将无法再看到本条事迹确认要删除吗?"
  377. showCancelButton
  378. onConfirm={onConfirm}
  379. ></ODialog>
  380. </div>
  381. )
  382. }
  383. })