index.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * 知识库项目编辑
  3. */
  4. import { Text, View } from "@tarojs/components";
  5. import PageCustom from "@/components/page-custom/index";
  6. import NavBarNormal from "@/components/NavBarNormal/index";
  7. import BottomBar from "@/components/BottomBar";
  8. import IconQ from "@/components/icon/IconQ";
  9. import IconA from "@/components/icon/IconA";
  10. import IconPlusColor14 from "@/components/icon/IconPlusColor14";
  11. import IconALink from "@/components/icon/IconALink";
  12. import IconDeleteGray16 from "@/components/icon/IconDeleteGray16";
  13. import { useEffect, useRef, useState } from "react";
  14. import IconAImage from "@/components/icon/IconAImage";
  15. import WemetaTextarea from "@/components/wemeta-textarea";
  16. import WemetaInput from "@/components/wemeta-input";
  17. import UploaderGrid from '@/components/UploaderGrid'
  18. import type { TMediaType } from "@/types/index";
  19. import { uploadFile } from "@/utils/http";
  20. import { EUploadFileScene } from "@/consts/enum";
  21. import Taro, { useRouter } from "@tarojs/taro";
  22. import { isSuccess } from "@/utils";
  23. import { editVisitorDislikeAnswer, getVisitorMessagesByMsgId } from "@/service/visitor";
  24. import { TVisitorChat } from "@/types/visitor";
  25. import { EContentType, TMessageBodyContent } from "@/types/bot";
  26. import MarkDislike from "@/components/marks/MarkDislike";
  27. import MarkLike from "@/components/marks/MarkLike";
  28. type TFormdata = {content:string, correctionAnswer: string, links: string[], mediaList: TMediaType[]}
  29. export default function Index() {
  30. const router = useRouter()
  31. const { msgId, visitorId, agentId } = router.params as {msgId: string, visitorId: string, agentId: string}
  32. const [visitorQ, setQVisitor] = useState<TVisitorChat|null>(null)
  33. const [visitor, setVisitor] = useState<TVisitorChat|null>(null)
  34. const [isDislike, setIsDislike] = useState(false)
  35. const [formData, setFormData] = useState<TFormdata>(
  36. {
  37. content: '',
  38. correctionAnswer: '',
  39. links: [],
  40. mediaList: [] //{fileType: 'image',url: 'https://cdn.wehome.cn/cmn/png/53/META-H8UKWHWU-2JNUAG2BARJF55VHU9QS3-YBQGHDAM-IW.png'}
  41. }
  42. );
  43. const handleInput = (value: string) => {
  44. setFormData({
  45. ...formData,
  46. content: value,
  47. });
  48. };
  49. const handleValueAInput = (value: string) => {
  50. setFormData({
  51. ...formData,
  52. correctionAnswer: value,
  53. });
  54. };
  55. // 添加链接
  56. const addLink = () => {
  57. setFormData({
  58. ...formData,
  59. links: [...formData.links, ""],
  60. });
  61. };
  62. // 删除链接
  63. const removeLink = (e, index) => {
  64. e.stopPropagation();
  65. const newLinks = [...formData.links];
  66. newLinks.splice(index, 1);
  67. setFormData({
  68. ...formData,
  69. links: newLinks,
  70. });
  71. };
  72. // 更新单个链接
  73. const handleLinkChange = (index:number, value: string) => {
  74. const newLinks = [...formData.links];
  75. newLinks[index] = value;
  76. setFormData({
  77. ...formData,
  78. links: newLinks,
  79. });
  80. };
  81. const handleDeleteMedia = (index:number) => {
  82. setFormData(prev => ({
  83. ...prev,
  84. mediaList: prev.mediaList.filter((_, i) => i !== index)
  85. }));
  86. };
  87. const handleSubmit = async () => {
  88. if(!msgId || !visitorId || !visitor){
  89. return
  90. }
  91. const dataToSubmit = {
  92. questions : visitorQ?.content ? [visitorQ?.content] : [],
  93. visitorId: visitorId,
  94. answer: formData.correctionAnswer,
  95. content: formData.content,
  96. agentId: agentId,
  97. msgId: msgId,
  98. correctionId: visitor.correctionId,
  99. links: formData.links.filter(link => link.trim() !== ''),
  100. pics: formData.mediaList.map( item => item.url),
  101. }
  102. console.log(dataToSubmit)
  103. const {status} = await editVisitorDislikeAnswer(dataToSubmit)
  104. if(isSuccess(status)){
  105. Taro.showToast({title: '保存成功', icon: 'success'})
  106. setTimeout(() => {
  107. Taro.navigateBack()
  108. }, 300)
  109. }
  110. }
  111. const getDetail = async () => {
  112. const {status, data} = await getVisitorMessagesByMsgId({
  113. msgId,
  114. visitorId,
  115. size: 2,
  116. })
  117. if(isSuccess(status), data[0]){
  118. const _visitor = data[0]
  119. setVisitor(_visitor)
  120. if(data[1]){
  121. setQVisitor(data[1])
  122. }
  123. setIsDislike(_visitor.isDislike)
  124. let content = _visitor.content
  125. let links = _visitor.correctionLinks ?? []
  126. let pics = _visitor.correctionPics ?? []
  127. // 特殊处理消息内容
  128. if(_visitor.contentType === EContentType.AiseekQA){
  129. try{
  130. const json = JSON.parse(_visitor.content) as TMessageBodyContent
  131. content = json.answer.text
  132. links = json.answer.payload.links
  133. pics = json.answer.payload.pics
  134. }catch(e){}
  135. }
  136. setFormData({
  137. content: content,
  138. correctionAnswer: _visitor.correctionAnswer,
  139. links: links,
  140. mediaList: pics?.map( pic => {
  141. return {fileType: 'image', url: pic}
  142. }) ?? [],
  143. })
  144. }
  145. }
  146. const handleChooseMedia = ()=> {
  147. Taro.chooseMedia({
  148. count: 10,
  149. mediaType: ["image"],
  150. sourceType: ["album", "camera"],
  151. async success(r) {
  152. // const tempFiles = r.tempFiles;
  153. for (const tempFile of r.tempFiles){
  154. const result = await uploadFile(tempFile.tempFilePath, EUploadFileScene.OTHER)
  155. if(result?.publicUrl){
  156. setFormData(prev => {
  157. return {...prev, mediaList: [{fileType:'image', url: result.publicUrl}, ...prev.mediaList]}
  158. })
  159. }
  160. }
  161. }
  162. })
  163. }
  164. useEffect(()=> {
  165. if(visitorId && msgId){
  166. getDetail()
  167. }
  168. }, [visitorId, msgId])
  169. return (
  170. <PageCustom>
  171. <NavBarNormal scrollFadeIn backText={isDislike ? '纠错记录' : '编辑问答'}></NavBarNormal>
  172. <View className="w-full pb-120">
  173. <View className="p-16">
  174. {isDislike && <View className="text-gray-45 text-12 text-center py-8">修改后的问答将存入知识库,持续训练提升您的智能体</View>}
  175. <View className="flex flex-col gap-16">
  176. <View className="flex flex-col">
  177. <View className="flex items-start gap-8 mb-6">
  178. <View className="flex-center h-28">
  179. <IconQ />
  180. </View>
  181. <View className="flex-1 text-14 leading-28">提问</View>
  182. </View>
  183. <View className="p-16 bg-white rounded-12">
  184. {visitorQ?.content}
  185. </View>
  186. </View>
  187. <View className="flex flex-col">
  188. <View className="flex items-start gap-8 mb-6">
  189. <View className="flex-center h-28">
  190. <IconA />
  191. </View>
  192. <View className="flex-1 text-14 leading-28">原回答</View>
  193. </View>
  194. <View className="text-[#777E95] rounded-12 overflow-hidden relative p-16 bg-white">
  195. <View className="absolute top-0 right-0 z-10">
  196. {visitor?.isDislike ? <MarkDislike/>:<></>}
  197. {visitor?.isLike ? <MarkLike />:<></>}
  198. </View>
  199. {formData.content}
  200. </View>
  201. </View>
  202. {/* 回答 */}
  203. <View className="flex flex-col">
  204. <View className="flex items-start gap-8 mb-6">
  205. <View className="flex-center h-28">
  206. <IconA />
  207. </View>
  208. <View className="flex-1 text-14 leading-28">纠错回答</View>
  209. </View>
  210. <View>
  211. <WemetaTextarea
  212. value={formData.correctionAnswer}
  213. onInput={handleValueAInput}
  214. placeholder="描述正确的回答"
  215. />
  216. </View>
  217. </View>
  218. {/* 链接 */}
  219. <View className="flex flex-col">
  220. <View className="flex items-start gap-8 mb-6">
  221. <View className="flex-center h-28">
  222. <IconALink />
  223. </View>
  224. <View className="flex-1 text-14 leading-28">链接</View>
  225. <View className="flex-center px-8 gap-4" onClick={addLink}>
  226. <IconPlusColor14 />
  227. <View className="text-primary">新增</View>
  228. </View>
  229. </View>
  230. <View className="flex flex-col gap-8">
  231. {formData.links.map((link, index) => {
  232. return (
  233. <WemetaInput
  234. value={link}
  235. onInput={(value) => handleLinkChange(index, value)}
  236. suffix={() => (
  237. <View onClick={(e) => removeLink(e, index)}>
  238. <IconDeleteGray16 />
  239. </View>
  240. )}
  241. placeholder="请输入查看链接,支持长按粘贴..."
  242. />
  243. );
  244. })}
  245. </View>
  246. </View>
  247. {/* 图片 */}
  248. <View className="flex flex-col">
  249. <View className="flex items-start gap-8 mb-6">
  250. <View className="flex-center h-28">
  251. <IconAImage />
  252. </View>
  253. <View className="flex-1 text-14 leading-28">图片</View>
  254. </View>
  255. <UploaderGrid onNewUpload={handleChooseMedia} list = {formData.mediaList} onChange={()=> {}} onDelete={handleDeleteMedia} />
  256. </View>
  257. </View>
  258. </View>
  259. <BottomBar>
  260. <View className="button-rounded button-primary flex-1" onClick={handleSubmit}>保存</View>
  261. </BottomBar>
  262. </View>
  263. </PageCustom>
  264. );
  265. }