index.tsx 9.1 KB

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