index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { useState } from "react";
  2. import { View } from "@tarojs/components";
  3. import PageCustom from "@/components/page-custom/index";
  4. import NavBarNormal from "@/components/NavBarNormal/index";
  5. import KnowledgePicker from '@/components/KnowledgePicker'
  6. import Taro, { useUnload } from "@tarojs/taro";
  7. import { uploadImage } from "@/utils/http";
  8. import { useComponentStore } from "@/store/componentStore";
  9. import type { TMediaType } from "@/types/index";
  10. import { EComponentType, EKnowlegeTypes } from "@/consts/enum";
  11. import UploaderGrid from '@/components/UploaderGrid'
  12. import BottomBar from "@/components/BottomBar";
  13. import { TKnowledgeItem } from "@/types/knowledge";
  14. interface Props {
  15. editMode?: boolean;
  16. }
  17. export default function Index({ editMode }: Props) {
  18. let currentComponent = useComponentStore((state) => state.currentComponent);
  19. const { saveComponent } = useComponentStore();
  20. const loading = useComponentStore((state) => state.loading);
  21. const [mediaValue, setMediaValue] = useState<TMediaType[]>(
  22. currentComponent?.data?.media ?? []
  23. );
  24. const handleSubmit = async () => {
  25. if(!currentComponent?.data?.id){
  26. return
  27. }
  28. if (loading) {
  29. return;
  30. }
  31. const c = {
  32. data: {
  33. media: mediaValue,
  34. layout: currentComponent.data.layout ?? 'mini',
  35. id: currentComponent.data.id,
  36. index: currentComponent.data.index,
  37. },
  38. enabled: currentComponent?.enabled ?? true,
  39. type: EComponentType.media,
  40. };
  41. await saveComponent(c);
  42. Taro.navigateBack();
  43. };
  44. let isUploading = false;
  45. const handleChooseMedia = () => {
  46. Taro.chooseMedia({
  47. count: 9,
  48. mediaType: ["image", "video"],
  49. sourceType: ["album", "camera"],
  50. async success(res) {
  51. isUploading = true;
  52. // tempFilePath可以作为img标签的src属性显示图片
  53. const tempFilePaths = res.tempFiles;
  54. const arr: TMediaType[] = [];
  55. for (let t of tempFilePaths) {
  56. Taro.showLoading();
  57. const res = await uploadImage(t.tempFilePath);
  58. Taro.hideLoading();
  59. if (res?.publicUrl) {
  60. arr.push({
  61. duration: t.duration,
  62. height: t.height,
  63. width: t.width,
  64. size: t.size,
  65. url: res.publicUrl,
  66. fileType: t.fileType,
  67. });
  68. console.log(arr);
  69. }else{
  70. Taro.showModal({
  71. title: '上传异常',
  72. content: '文件上传失败',
  73. showCancel: false,
  74. })
  75. }
  76. }
  77. setMediaValue([...mediaValue, ...arr]);
  78. isUploading = false;
  79. },
  80. fail(err) {
  81. console.log(err);
  82. isUploading = false;
  83. },
  84. });
  85. };
  86. const handleDeleteMedia = (index: number, current: TMediaType) => {
  87. const results = mediaValue.filter((item) => item.url !== current.url);
  88. setMediaValue([...results]);
  89. };
  90. const [showPopup, setShowPopup] = useState(false);
  91. const showKnowlegePopup = ()=> {
  92. setShowPopup(true)
  93. }
  94. const onPicked = (picked?: TKnowledgeItem[]) => {
  95. if(picked){
  96. const r = picked.map( item => {
  97. return {
  98. size: item.fileSize,
  99. url: item.picUrl,
  100. fileType: item.type,
  101. }
  102. })
  103. if(r.length){
  104. setMediaValue([...mediaValue, ...r])
  105. }
  106. }
  107. }
  108. return (
  109. <PageCustom isflex={false} bgColor="global-light-green-bg">
  110. <NavBarNormal
  111. >
  112. 图片/视频
  113. </NavBarNormal>
  114. <View className="flex flex-col w-full px-16 pt-12 gap-12">
  115. <View className="text-primary" onClick={showKnowlegePopup}>知识库导入</View>
  116. <UploaderGrid onNewUpload={handleChooseMedia} list = {mediaValue} onChange={()=> {}} onDelete={handleDeleteMedia} />
  117. </View>
  118. <KnowledgePicker types={[EKnowlegeTypes.image]} multi onPicked={onPicked} setShow={setShowPopup} show={showPopup}></KnowledgePicker>
  119. <BottomBar>
  120. <View className="button-rounded button-primary flex-1" onClick={handleSubmit}>保存</View>
  121. </BottomBar>
  122. </PageCustom>
  123. );
  124. }