index.vue 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570
  1. <template>
  2. <view class="chat-container">
  3. <scroll-view class="messages" scroll-y :scroll-top="scrollTop" scroll-with-animation>
  4. <block v-for="message in messages" :key="message.id">
  5. <view :class="['message', message.user]">
  6. <view class="avatar-container">
  7. <image :src="message.user === 'user' ? userAvatar : '/static/机器人.png'" class="avatar"
  8. mode="aspectFill" />
  9. <text class="sender-name">{{ message.user === 'user' ? userInfo.nickName : 'AI助理' }}</text>
  10. </view>
  11. <view class="message-content"> <!-- 改为 message-content 类 -->
  12. <!-- 添加图片展示部分 -->
  13. <template v-if="message.images && message.images.length > 0">
  14. <view class="message-images">
  15. <image v-for="(img, index) in message.images" :key="index" :src="img" class="chat-image"
  16. mode="aspectFill" @tap="previewImage(message.images, index)" />
  17. </view>
  18. </template>
  19. <!-- 视频展示部分 -->
  20. <template v-if="message.videos && message.videos.length > 0">
  21. <view class="message-videos">
  22. <view v-for="(video, index) in message.videos" :key="index"
  23. class="video-thumbnail-container">
  24. <!-- 视频封面图 -->
  25. <image :src="video + '?x-oss-process=video/snapshot,t_0,f_jpg'" class="chat-image"
  26. mode="aspectFill" @tap="previewVideoThumbnail(message.videos, index)" />
  27. </view>
  28. </view>
  29. </template>
  30. <!-- 文档展示部分 -->
  31. <template v-if="message.docs && message.docs.length > 0">
  32. <view class="message-docs">
  33. <view v-for="(doc, index) in message.docs" :key="index" class="doc-preview">
  34. <view class="doc-preview-container">
  35. <image src="/static/doc-icon.svg" mode="aspectFit" class="doc-icon" />
  36. <text class="doc-name">{{ doc.name }}</text>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <template v-if="message.message.includes('相关资料来源')">
  42. <view>{{ getMainContent(message.message) }}</view>
  43. <view class="source-section" v-if="minioUrls && minioUrls.length > 0">
  44. <view class="source-title">相关资料来源:</view>
  45. <view class="source-links">
  46. <view v-for="(item, index) in minioUrls" :key="index" class="source-link"
  47. @tap="copyLink(item.url)">
  48. {{ item.object_name }}
  49. </view>
  50. </view>
  51. </view>
  52. <!-- <view v-if="message.user === 'bot' && message.audioData && message.audioData.visible"
  53. class="audio-controls">
  54. <view class="audio-btn" @tap="playAudio(message.audioData.base64, message.id)">
  55. <image :src="message.audioData.isPlaying ? '/static/pause.svg' : '/static/play.svg'"
  56. class="audio-icon" mode="aspectFit" />
  57. </view>
  58. <view class="audio-progress" v-if="message.audioData.progress">
  59. {{ message.audioData.progress }}%
  60. </view>
  61. </view> -->
  62. </template>
  63. <template v-else>
  64. {{ message.message }}
  65. </template>
  66. </view>
  67. </view>
  68. </block>
  69. </scroll-view>
  70. <!-- 添加新建对话按钮 -->
  71. <!-- <view class="new-chat-btn">
  72. <button @tap="startNewChat" class="btn-new">新建对话</button>
  73. </view> -->
  74. <view class="input-container">
  75. <!-- 图片预览区域和上传按钮组合 -->
  76. <scroll-view class="image-upload-area" scroll-x :show-scrollbar="false"
  77. v-if="selectedImages.length > 0 || selectedVideos.length > 0 || selectedDocs.length > 0">
  78. <view class="image-list">
  79. <!-- 图片预览 -->
  80. <view class="image-preview" v-for="(image, index) in selectedImages" :key="'img-'+index">
  81. <image :src="image" mode="aspectFill" class="preview-img" />
  82. <view class="delete-btn" @tap="removeImage(index)">
  83. <text class="delete-icon">×</text>
  84. </view>
  85. </view>
  86. <!-- 视频预览 -->
  87. <view class="image-preview" v-for="(video, index) in uploadedVideos" :key="'vid-'+index">
  88. <view class="video-preview-container">
  89. <image :src="video.url + '?x-oss-process=video/snapshot,t_0,f_jpg'" mode="aspectFill"
  90. class="preview-img" />
  91. <!-- <image src="/static/play-icon.svg" class="play-icon" /> -->
  92. </view>
  93. <view class="delete-btn" @tap="removeVideo(index)">
  94. <text class="delete-icon">×</text>
  95. </view>
  96. </view>
  97. <!-- 文档预览 -->
  98. <view class="doc-preview" v-for="(doc, index) in selectedDocs" :key="'doc-'+index">
  99. <view class="doc-preview-container">
  100. <image src="/static/doc-icon.svg" mode="aspectFit" class="doc-icon" />
  101. <text class="doc-name">{{ doc.name }}</text>
  102. </view>
  103. <view class="delete-btn" @tap="removeDoc(index)">
  104. <text class="delete-icon">×</text>
  105. </view>
  106. </view>
  107. <!-- 继续上传按钮 -->
  108. <view class="upload-btn" @tap="chooseMedia"
  109. v-if="selectedImages.length < maxImages && selectedVideos.length < maxVideos && selectedDocs.length < maxDocs">
  110. <image src="/static/upload.svg" class="upload-icon" mode="aspectFit" />
  111. </view>
  112. </view>
  113. </scroll-view>
  114. <view class="input-wrapper">
  115. <textarea v-model="userInput" @keydown="handleKeydown" placeholder="有问题可以咨询我..." maxlength="100"
  116. class="input-textarea" />
  117. <!-- 添加设置按钮 -->
  118. <view class="set_btn">
  119. <!-- <image src="/static/images/setting.png" @tap="showVoiceSettings" class="settings-icon" /> -->
  120. <view
  121. v-if="!userInput.trim() && !selectedImages.length && !selectedVideos.length && !selectedDocs.length"
  122. class="upload-options">
  123. <image src="/static/upload.svg" @tap="chooseMedia" class="upload-icon" />
  124. </view>
  125. <image v-else src="/static/sendm.svg" @tap="sendMessage" class="upload-img"></image>
  126. </view>
  127. </view>
  128. </view>
  129. <!-- 添加语音设置弹窗 -->
  130. <uni-popup ref="voiceSettingsPopup" type="center" :mask-click="false" mask-background-color="rgba(0, 0, 0, 0.6)"
  131. @change="handlePopupChange">
  132. <view class="settings-popup">
  133. <view class="settings-header">
  134. <text class="settings-title">语音设置</text>
  135. <text class="close-btn" @tap="closeVoiceSettings">×</text>
  136. </view>
  137. <view class="settings-content">
  138. <view class="settings-item">
  139. <text class="item-label">语言</text>
  140. <picker :value="languageOptions.indexOf(voiceSettings.language)" :range="languageOptions"
  141. @change="handleLanguageChange">
  142. <view class="picker-value">{{voiceSettings.language || '请选择'}}</view>
  143. </picker>
  144. </view>
  145. <view class="settings-item">
  146. <text class="item-label">声音名称</text>
  147. <picker :value="currentVoices.findIndex(v => v.name === voiceSettings.voice_name)"
  148. :range="currentVoices" range-key="name" @change="handleVoiceNameChange">
  149. <view class="picker-value">{{voiceSettings.voice_name || '请选择'}}</view>
  150. </picker>
  151. </view>
  152. <view class="settings-item">
  153. <text class="item-label">性别</text>
  154. <view class="picker-value">{{voiceSettings.gender}}</view>
  155. </view>
  156. <view class="settings-item">
  157. <text class="item-label">风格</text>
  158. <picker :value="styleOptions.indexOf(voiceSettings.style)" :range="styleOptions"
  159. @change="handleStyleChange">
  160. <view class="picker-value">{{voiceSettings.style || '请选择'}}</view>
  161. </picker>
  162. </view>
  163. <view class="settings-item">
  164. <text class="item-label">语速 ({{voiceSettings.rate}})</text>
  165. <slider :value="parseInt(voiceSettings.rate)" @change="handleRateChange" min="0" max="100"
  166. show-value />
  167. </view>
  168. <view class="settings-item">
  169. <text class="item-label">音量 ({{voiceSettings.volume}})</text>
  170. <slider :value="parseInt(voiceSettings.volume)" @change="handleVolumeChange" min="0" max="100"
  171. show-value />
  172. </view>
  173. </view>
  174. <button class="save-btn" @tap="saveVoiceSettings">保存设置</button>
  175. </view>
  176. </uni-popup>
  177. </view>
  178. </template>
  179. <script>
  180. import MarkdownIt from "markdown-it";
  181. const md = new MarkdownIt();
  182. export default {
  183. data() {
  184. return {
  185. messages: [{
  186. user: "bot",
  187. messageType: "TEXT",
  188. message: "欢迎使用智能AI助理",
  189. html: "",
  190. time: "",
  191. done: true,
  192. }, ],
  193. userInput: "",
  194. websocket: null,
  195. AiUrl: '',
  196. apiUrl: 'https://ylaiapi.raycos.com.cn', //https://ylaiapi.raycos.com.cn
  197. scrollTop: 0,
  198. tweaks: {},
  199. userAvatar: '/static/用户.png',
  200. userInfo: {},
  201. level: 1,
  202. idArray: [],
  203. minioUrls: [], // 新增:用于存储 Minio URL
  204. generating: false, // 是否正在生成回复
  205. typewriterTimer: null,
  206. currentIndex: 0,
  207. fullMessage: '',
  208. session_id: "",
  209. platform: {
  210. type: '', // 平台类型
  211. name: '', // 平台名称
  212. isWeixin: false
  213. },
  214. selectedImages: [], // 存储已上传图片的信息
  215. uploadedFiles: [], // 存储上传后的文件信息
  216. maxImages: 9,
  217. selectedVideos: [], // 存储已选择的视频
  218. uploadedVideos: [], // 存储已上传的视频信息
  219. maxVideos: 1, // 限制视频上传数量
  220. currentAudioContext: null,
  221. currentPlayingMessage: null,
  222. currentPlayingMessageId: null, // 添加:跟踪当前正在播放的消息ID
  223. selectedDocs: [], // 存储已选择的文档
  224. uploadedDocs: [], // 存储已上传的文档信息
  225. maxDocs: 3, // 限制文档上传数量
  226. voiceSettings: {
  227. language: 'zh-cn',
  228. voice_name: '',
  229. gender: '',
  230. style: '',
  231. rate: '+10%',
  232. volume: '+20%'
  233. },
  234. voicesData: {}, // 存储接口返回的语音数据
  235. languageOptions: [], // 可选择的语言
  236. currentVoices: [], // 当前语言下的声音选项
  237. styleOptions: [], // 当前选择的声音可用的风格
  238. videoThumbnails: {}, // 存储视频封面
  239. /* 文档类型 */
  240. docTypeMap: {
  241. 'pdf': 'PDF文档',
  242. 'doc': 'Word文档',
  243. 'docx': 'Word文档',
  244. 'txt': '文本文件',
  245. 'xls': 'Excel表格',
  246. 'xlsx': 'Excel表格',
  247. 'ppt': 'PPT演示',
  248. 'pptx': 'PPT演示'
  249. },
  250. };
  251. },
  252. created() {
  253. },
  254. onLoad() {
  255. // this.getKbmUrl()
  256. this.loadUserInfo()
  257. this.getPlatformInfo();
  258. },
  259. onShow() {
  260. this.getKbmUrl()
  261. },
  262. watch: {
  263. messages: {
  264. handler() {
  265. this.$nextTick(() => {
  266. this.scrollToBottom();
  267. });
  268. },
  269. deep: true
  270. }
  271. },
  272. methods: {
  273. /* 语速及音量 */
  274. handleRateChange(e) {
  275. const value = e.detail.value;
  276. // 将滑块值转换为百分比格式
  277. this.voiceSettings.rate = value > 0 ? `+${value}%` : `${value}%`;
  278. },
  279. handleVolumeChange(e) {
  280. const value = e.detail.value;
  281. // 将滑块值转换为百分比格式
  282. this.voiceSettings.volume = value > 0 ? `+${value}%` : `${value}%`;
  283. },
  284. // 添加新建对话方法
  285. startNewChat() {
  286. uni.showModal({
  287. title: '确认新建对话',
  288. content: '是否清空当前对话内容开始新的对话?',
  289. success: (res) => {
  290. if (res.confirm) {
  291. // 重置消息列表,只保留欢迎息
  292. this.messages = [{
  293. user: "bot",
  294. messageType: "TEXT",
  295. message: "欢迎使用智能AI助理",
  296. html: "",
  297. time: "",
  298. done: true,
  299. }];
  300. // 清空session_id
  301. this.session_id = "";
  302. // 清空MinioUrls
  303. this.minioUrls = [];
  304. // 重置其他相关状态
  305. this.generating = false;
  306. this.currentIndex = 0;
  307. this.fullMessage = '';
  308. // 显示提示
  309. uni.showToast({
  310. title: '已开始新对话',
  311. icon: 'success',
  312. duration: 1500
  313. });
  314. }
  315. }
  316. });
  317. },
  318. // 获取平台信息
  319. getPlatformInfo() {
  320. try {
  321. const systemInfo = uni.getSystemInfoSync();
  322. // console.log('系统信息:', systemInfo);
  323. // 判断平台类型
  324. // #ifdef MP-WEIXIN
  325. this.platform.type = 'mp-weixin';
  326. this.platform.name = '微信小程序';
  327. this.platform.isWeixin = true;
  328. // #endif
  329. // #ifdef MP-ALIPAY
  330. this.platform.type = 'mp-alipay';
  331. this.platform.name = '支付宝小程序';
  332. // #endif
  333. // #ifdef MP-BAIDU
  334. this.platform.type = 'mp-baidu';
  335. this.platform.name = '百度小程序';
  336. // #endif
  337. // #ifdef MP-TOUTIAO
  338. this.platform.type = 'mp-toutiao';
  339. this.platform.name = '头条小程序';
  340. // #endif
  341. // #ifdef H5
  342. this.platform.type = 'h5';
  343. this.platform.name = 'H5';
  344. // 在H5中判断是否在微信浏览器中
  345. if (navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1) {
  346. this.platform.isWeixin = true;
  347. this.platform.name = 'H5微信浏览器';
  348. }
  349. // #endif
  350. // #ifdef APP-PLUS
  351. this.platform.type = 'app';
  352. this.platform.name = 'App';
  353. // #endif
  354. // console.log('��前平台:', this.platform);
  355. // 获取更详细的平台信息
  356. const platformInfo = {
  357. brand: systemInfo.brand, // 手机品牌
  358. model: systemInfo.model, // 手机型号
  359. system: systemInfo.system, // 操作系统版本
  360. platform: systemInfo.platform, // 客户端平台
  361. SDKVersion: systemInfo.SDKVersion, // 小程序基库版本
  362. version: systemInfo.version, // 微信版本号
  363. };
  364. // console.log('平台详细信息:', platformInfo);
  365. return this.platform;
  366. } catch (error) {
  367. console.error('获取平台信息失败:', error);
  368. return {
  369. type: 'unknown',
  370. name: '未知平台',
  371. isWeixin: false
  372. };
  373. }
  374. },
  375. getMainContent(message) {
  376. const parts = message.split('相关资料来源');
  377. return parts[0].trim();
  378. },
  379. extractUrls(message) {
  380. const urlRegex = /href="([^"]+)"/g;
  381. const urls = [];
  382. let match;
  383. while ((match = urlRegex.exec(message)) !== null) {
  384. urls.push(match[1]);
  385. }
  386. return urls;
  387. },
  388. getFileName(url) {
  389. try {
  390. const decodedUrl = decodeURIComponent(url);
  391. const parts = decodedUrl.split('/');
  392. let fileName = parts[parts.length - 1];
  393. // 如果文件名过长,截断
  394. if (fileName.length > 30) {
  395. fileName = fileName.substring(0, 27) + '...';
  396. }
  397. return fileName;
  398. } catch (e) {
  399. return '文件';
  400. }
  401. },
  402. // 修改 copyLink 方法
  403. copyLink(url) {
  404. if (!url) return;
  405. uni.setClipboardData({
  406. data: url,
  407. success: () => {
  408. uni.showToast({
  409. title: '链接已复制',
  410. icon: 'none',
  411. duration: 1500
  412. });
  413. }
  414. });
  415. },
  416. loadUserInfo() {
  417. const userDataString = uni.getStorageSync('userInfo');
  418. if (userDataString) {
  419. this.userInfo = JSON.parse(userDataString);
  420. this.userAvatar = this.userInfo.avatarUrl || '/static/用户.png'; // Fallback to default if no avatar
  421. }
  422. },
  423. scrollToBottom() {
  424. // 使用一个很大的值来确保滚动到底部
  425. this.scrollTop += 9999;
  426. // 重置 scrollTop 以便下次动生效
  427. // this.$nextTick(() => {
  428. // this.scrollTop = 0;
  429. // });
  430. },
  431. /* 请求链接 */
  432. getKbmUrl() {
  433. let userDataString;
  434. if (uni.getStorageSync('userData') == '') {
  435. // 跳转到个人页面
  436. uni.switchTab({
  437. url: '/pages/my/index',
  438. success: function() {},
  439. fail: function(err) {
  440. console.error('Failed to switch to personal page:', err);
  441. }
  442. });
  443. return;
  444. } else {
  445. userDataString = JSON.parse(uni.getStorageSync('userData'));
  446. }
  447. uni.request({
  448. url: this.apiUrl + '/wechat/getUserInfo',
  449. method: 'POST',
  450. data: {
  451. openid: userDataString.openid
  452. },
  453. header: {
  454. 'content-type': 'application/x-www-form-urlencoded'
  455. },
  456. success: (res) => {
  457. console.log('获取用户信息成功', res.data);
  458. this.AiUrl = this.apiUrl + '/chatbot/multimodal'
  459. this.level = res.data.data.level
  460. },
  461. fail: (err) => {
  462. console.error('获取用户信息失败', err);
  463. uni.showToast({
  464. title: '获取用户信息失败,请重试',
  465. icon: 'none'
  466. });
  467. }
  468. });
  469. /* uni.request({
  470. url: this.apiUrl + '/wechat/getKbmUrl', // 确保这是正确的URL
  471. method: 'POST',
  472. data: {
  473. openid: userDataString.openid
  474. }, //JSON.stringify()
  475. header: {
  476. 'content-type': 'application/x-www-form-urlencoded' // 设置正确的内容类型
  477. },
  478. success: (res) => {
  479. console.log('Login response:', res.data);
  480. if (res.data && res.data.status == 200) {
  481. this.tweaks = res.data.data.tweaks
  482. this.connectWebSocket();
  483. }
  484. },
  485. fail: (err) => {
  486. console.error('请求失败', err);
  487. uni.showToast({
  488. title: '请求失败,请重试',
  489. icon: 'none'
  490. });
  491. }
  492. }) */
  493. },
  494. getUuid() {
  495. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
  496. /[xy]/g,
  497. function(c) {
  498. var r = (Math.random() * 16) | 0,
  499. v = c == "x" ? r : (r & 0x3) | 0x8;
  500. return v.toString(16);
  501. }
  502. );
  503. },
  504. connectWebSocket() {
  505. const wsUrl = this.AiUrl
  506. //'http://ai.raycos.net/api/v1/run/c7ea3a2d-f50c-4936-b96c-3fe7c34fa062?stream=false';
  507. // 使用 XMLHttpRequest 替代 fetch
  508. uni.request({
  509. url: wsUrl,
  510. method: 'POST',
  511. data: JSON.stringify({}), // 如果需要发送数据,可以在这里加
  512. header: {
  513. 'content-type': 'application/json'
  514. },
  515. success: (res) => {
  516. if (res.statusCode === 200) {
  517. const result = res.data;
  518. this.handleWebSocketMessage(result);
  519. } else {
  520. console.error("请求失败:", res.statusCode);
  521. }
  522. },
  523. fail: (err) => {
  524. console.error("网络连接错误:", err);
  525. }
  526. });
  527. // 如果需要发送数据,可以在这里添加
  528. },
  529. // 新增方法来处理消息
  530. handleWebSocketMessage(result) {
  531. // 检查消息是否完结
  532. if (result.done) {
  533. this.messages[this.messages.length - 1].done = true;
  534. return;
  535. }
  536. if (this.messages[this.messages.length - 1].done) {
  537. // 添加新的消息
  538. this.messages.push({
  539. time: Date.now(),
  540. message: result.content,
  541. messageType: "TEXT",
  542. user: "bot",
  543. done: false,
  544. });
  545. } else {
  546. // 更新最后一条消息
  547. let lastMessage = this.messages[this.messages.length - 1];
  548. lastMessage.message += result.content;
  549. this.messages[this.messages.length - 1] = lastMessage;
  550. }
  551. },
  552. // 修改发送消息方法
  553. async sendMessage() {
  554. /* // 确保在发送新消息前停止当前音频
  555. if (this.currentAudioContext) {
  556. this.stopCurrentAudio();
  557. } */
  558. const message = this.userInput.trim();
  559. if (!message && !this.selectedImages.length && !this.selectedVideos.length && !this.selectedDocs
  560. .length) {
  561. return;
  562. }
  563. // 构建用户消息对象
  564. const userMessage = {
  565. id: this.getUuid(),
  566. user: "user",
  567. messageType: "TEXT",
  568. message: message,
  569. time: Date.now(),
  570. done: true,
  571. images: this.uploadedFiles.map(file => file.url),
  572. videos: this.uploadedVideos.map(file => file.url),
  573. docs: this.uploadedDocs.map(file => file.url)
  574. };
  575. this.messages.push(userMessage);
  576. // 清空输入框和媒体文件
  577. this.userInput = "";
  578. this.selectedImages = [];
  579. this.uploadedFiles = [];
  580. this.selectedVideos = [];
  581. this.uploadedVideos = [];
  582. this.selectedDocs = [];
  583. this.uploadedDocs = [];
  584. this.scrollToBottom();
  585. // 添加AI助理的消息
  586. const botMessage = {
  587. id: this.getUuid(),
  588. user: "bot",
  589. messageType: "TEXT",
  590. message: "正在思考中...",
  591. time: Date.now(),
  592. done: false,
  593. };
  594. this.messages.push(botMessage);
  595. try {
  596. const response = await uni.request({
  597. url: this.AiUrl,
  598. method: 'POST',
  599. header: {
  600. 'Content-Type': 'application/json'
  601. },
  602. timeout: 180000, // 设置30秒超时,默认是60秒
  603. data: {
  604. message: message,
  605. image_urls: userMessage.images,
  606. video_urls: userMessage.videos, // 添加视频URL到请求
  607. documents: userMessage.docs, // 添加文档URL到请求
  608. chat_config_id: '17',
  609. user_id: this.userInfo.phoneNumber,
  610. session_id: this.session_id || "",
  611. }
  612. });
  613. // 检查响应状态和数据
  614. if (response.statusCode !== 200 || !response.data || !response.data.data?.answer) {
  615. throw new Error('Invalid response from server');
  616. }
  617. if (this.level === '0' && response.data.data?.milvus_ids) {
  618. await this.getMinioUrls({
  619. ids: response.data.data.milvus_ids
  620. });
  621. }
  622. this.session_id = response.data.data.session_id;
  623. await this.handleResponse(response.data, botMessage);
  624. // 如果有音频,播放音频
  625. if (
  626. response.data.data.audio_info &&
  627. response.data.data.audio_info.audio
  628. ) {
  629. this.playAudio(response.data.data.audio_info.audio);
  630. }
  631. } catch (error) {
  632. console.error("Error sending message:", error);
  633. // 找到最后一条消息(应该是机器人的消息)并更新它
  634. const lastMessage = this.messages[this.messages.length - 1];
  635. if (lastMessage && lastMessage.user === 'bot') {
  636. lastMessage.message = "抱歉,处理消息时出现错误,请重试。";
  637. lastMessage.done = true;
  638. }
  639. uni.showToast({
  640. title: '发送消息失败,请重试',
  641. icon: 'none'
  642. });
  643. }
  644. },
  645. // 修改停止当前音频的方法
  646. stopCurrentAudio() {
  647. try {
  648. if (this.currentAudioContext) {
  649. // 先停止播放
  650. this.currentAudioContext.stop();
  651. // 更新消息的播放状态
  652. if (this.currentPlayingMessageId) {
  653. const message = this.messages.find(m => m.id === this.currentPlayingMessageId);
  654. if (message && message.audioData) {
  655. message.audioData.isPlaying = false;
  656. message.audioData.progress = 0;
  657. }
  658. }
  659. // 销毁音频实例
  660. this.currentAudioContext.destroy();
  661. this.currentAudioContext = null;
  662. this.currentPlayingMessageId = null;
  663. }
  664. } catch (error) {
  665. console.error('停止音频时出错:', error);
  666. // 即使出错也要重置状态
  667. this.currentAudioContext = null;
  668. this.currentPlayingMessageId = null;
  669. }
  670. },
  671. // 修改播放音频方法
  672. playAudio(audioBase64, messageId) {
  673. try {
  674. // 如果有正在播放的音频,先停止它
  675. if (this.currentAudioContext) {
  676. this.stopCurrentAudio();
  677. }
  678. // 创建新的音频上下文
  679. const innerAudioContext = uni.createInnerAudioContext();
  680. // 设置音频源
  681. innerAudioContext.src = audioBase64;
  682. // 更新状态和引用
  683. this.currentAudioContext = innerAudioContext;
  684. this.currentPlayingMessageId = messageId;
  685. // 更新消息的播放状态
  686. const message = this.messages.find(m => m.id === messageId);
  687. if (message && message.audioData) {
  688. message.audioData.isPlaying = true;
  689. message.audioData.progress = 0;
  690. }
  691. // 监听播放错误
  692. innerAudioContext.onError((res) => {
  693. console.error('音频播放错误:', res.errMsg);
  694. uni.showToast({
  695. title: '音频播放失败',
  696. icon: 'none'
  697. });
  698. this.stopCurrentAudio();
  699. });
  700. // 播放结束后的处理
  701. innerAudioContext.onEnded(() => {
  702. this.stopCurrentAudio();
  703. });
  704. // 开始播放
  705. innerAudioContext.play();
  706. } catch (error) {
  707. console.error('播放音频失败:', error);
  708. this.stopCurrentAudio();
  709. }
  710. },
  711. // 修改组件销毁时的处理
  712. beforeDestroy() {
  713. // 确保在组件销毁前停止并清理音频
  714. if (this.currentAudioContext) {
  715. this.stopCurrentAudio();
  716. }
  717. },
  718. // 修改 getMinioUrls 方法
  719. async getMinioUrls(idObject) {
  720. this.minioUrls = []
  721. try {
  722. const response = await uni.request({
  723. url: `${this.apiUrl}/milvus/getMinioURl`,
  724. method: 'POST',
  725. data: idObject,
  726. header: {
  727. 'Content-Type': 'application/json'
  728. }
  729. });
  730. if (response.statusCode === 200 && response.data?.data) {
  731. this.minioUrls = response.data.data;
  732. console.log("Received Minio URLs:", this.minioUrls);
  733. } else {
  734. this.minioUrls = [];
  735. console.error("Invalid response format:", response);
  736. }
  737. } catch (error) {
  738. console.error("Error fetching Minio URLs:", error);
  739. this.minioUrls = [];
  740. uni.showToast({
  741. title: '获取资源链接失败',
  742. icon: 'none'
  743. });
  744. }
  745. },
  746. // 修改 handleResponse 方法
  747. async handleResponse(value, existingMessage) {
  748. try {
  749. if (!value?.data?.answer) {
  750. throw new Error('Invalid response format');
  751. }
  752. const data = value.data.answer;
  753. this.fullMessage = data;
  754. this.currentIndex = 0;
  755. // 先设置基本消息结构
  756. existingMessage.message = '';
  757. existingMessage.done = false;
  758. // 如果有音频数据,创建完整的音频数据结构
  759. if (value.data.audio_info?.audio) {
  760. existingMessage.audioData = {
  761. base64: value.data.audio_info.audio,
  762. isPlaying: false,
  763. progress: 0,
  764. visible: true // 添加可见性标志
  765. };
  766. }
  767. // 开始打字效果
  768. this.startTypewriter(existingMessage);
  769. } catch (error) {
  770. console.error("Error handling response:", error);
  771. existingMessage.message = "抱歉,处理响应时出现错误。";
  772. existingMessage.done = true;
  773. }
  774. },
  775. // 新的打字效果实现
  776. startTypewriter(existingMessage) {
  777. // 清除可能存在的旧计时器
  778. if (this.typewriterTimer) {
  779. clearInterval(this.typewriterTimer);
  780. this.typewriterTimer = null;
  781. }
  782. const doTypewriter = () => {
  783. if (this.currentIndex < this.fullMessage.length) {
  784. // 保存有的音频数据
  785. const audioData = existingMessage.audioData;
  786. // 新消息内容
  787. existingMessage.message = this.fullMessage.slice(0, this.currentIndex + 1);
  788. // 恢复音频数据
  789. if (audioData) {
  790. existingMessage.audioData = audioData;
  791. }
  792. this.currentIndex++;
  793. // 使用 nextTick 确保视图更新
  794. this.$nextTick(() => {
  795. this.scrollToBottom();
  796. });
  797. // 使用 setTimeout 代替 setInterval
  798. this.typewriterTimer = setTimeout(() => {
  799. doTypewriter();
  800. }, 30);
  801. } else {
  802. // 打字效果��成
  803. // 保存原有的音频数据
  804. const audioData = existingMessage.audioData;
  805. if (this.minioUrls && this.minioUrls.length > 0) {
  806. existingMessage.message += '\n\n相关资料来源';
  807. }
  808. // 恢复音频数据
  809. if (audioData) {
  810. existingMessage.audioData = audioData;
  811. }
  812. existingMessage.done = true;
  813. this.typewriterTimer = null;
  814. this.scrollToBottom();
  815. }
  816. };
  817. // 立即开始打字效果
  818. doTypewriter();
  819. },
  820. // 修改 renderMarkdown 方法以保留 HTML 标签
  821. /* renderMarkdown(rawMarkdown) {
  822. if (typeof rawMarkdown !== 'string') {
  823. console.warn('Invalid input for renderMarkdown:', rawMarkdown);
  824. return '';
  825. }
  826. try {
  827. const md = new MarkdownIt({
  828. html: true, // 允许 HTML 标签
  829. breaks: true,
  830. linkify: true
  831. });
  832. // 自定义链接渲染
  833. md.renderer.rules.link_open = (tokens, idx) => {
  834. const token = tokens[idx];
  835. const hrefIndex = token.attrIndex('href');
  836. const href = token.attrs[hrefIndex][1];
  837. return `<text class="link" style="color: #1296db; text-decoration: underline;" data-href="${href}">`;
  838. };
  839. md.renderer.rules.link_close = () => '</text>';
  840. // 自定义段落染
  841. md.renderer.rules.paragraph_open = () =>
  842. '<p style="font-size: 12px; margin: 5px 0 10px; padding: 0; line-height: 22px;">';
  843. return md.render(rawMarkdown);
  844. } catch (error) {
  845. console.error('Markdown rendering error:', error);
  846. return rawMarkdown;
  847. }
  848. }, */
  849. // 修改 sendMessage 方法
  850. /* async sendMessage() {
  851. const wsUrl = this.AiUrl;
  852. let message = this.userInput.trim();
  853. if (message) {
  854. // 添加用户消息
  855. const userMessage = {
  856. id: this.getUuid(),
  857. user: "user",
  858. messageType: "TEXT",
  859. message: message,
  860. time: Date.now(),
  861. done: true,
  862. };
  863. this.messages.push(userMessage);
  864. // 清空输入框并滚动
  865. this.userInput = "";
  866. this.scrollToBottom();
  867. // 添加AI助理的消息
  868. const botMessage = {
  869. id: this.getUuid(),
  870. user: "bot",
  871. messageType: "TEXT",
  872. message: "正在思考中...", // 添加加载提示
  873. time: Date.now(),
  874. done: false,
  875. };
  876. this.messages.push(botMessage);
  877. try {
  878. const response = await uni.request({
  879. url: wsUrl,
  880. method: 'POST',
  881. header: {
  882. 'Content-Type': 'application/json'
  883. },
  884. data: {
  885. message: message,
  886. chat_config_id: '17',
  887. user_id: this.userInfo.phoneNumber,
  888. session_id: this.session_id || "",
  889. }
  890. });
  891. if (response.statusCode === 200 && response.data) {
  892. // 如果需要获取 MinioUrls,先获取
  893. if (this.level === '0' && response.data.data?.milvus_ids) {
  894. await this.getMinioUrls({
  895. ids: response.data.data.milvus_ids
  896. });
  897. }
  898. // 然后处理完整响应
  899. await this.handleResponse(response.data, botMessage);
  900. }
  901. } catch (error) {
  902. console.error("Error sending message:", error);
  903. botMessage.message = "抱歉,发送消息失败,请重试。";
  904. botMessage.done = true;
  905. uni.showToast({
  906. title: '发送消息失败,请重试',
  907. icon: 'none'
  908. });
  909. }
  910. }
  911. }, */
  912. // 修改 renderMarkdown 方法
  913. /* renderMarkdown(rawMarkdown) {
  914. if (typeof rawMarkdown !== 'string') {
  915. console.warn('Invalid input for renderMarkdown:', rawMarkdown);
  916. return '';
  917. }
  918. try {
  919. const md = new MarkdownIt({
  920. html: true,
  921. breaks: true,
  922. linkify: true
  923. });
  924. // 自定义链接渲染
  925. md.renderer.rules.link_open = (tokens, idx) => {
  926. const token = tokens[idx];
  927. const hrefIndex = token.attrIndex('href');
  928. const href = token.attrs[hrefIndex][1];
  929. return `<text class="link" data-href="${href}">`;
  930. };
  931. md.renderer.rules.link_close = () => '</text>';
  932. return md.render(rawMarkdown);
  933. } catch (error) {
  934. console.error('Markdown rendering error:', error);
  935. return rawMarkdown;
  936. }
  937. }, */
  938. // 添加链接点击处理方法
  939. // 修改 handleLinkClick 方法
  940. // 修改 renderMarkdown 方法
  941. renderMarkdown(rawMarkdown) {
  942. if (typeof rawMarkdown !== 'string') {
  943. console.warn('Invalid input for renderMarkdown:', rawMarkdown);
  944. return '';
  945. }
  946. try {
  947. const md = new MarkdownIt({
  948. html: true,
  949. breaks: true,
  950. linkify: true
  951. });
  952. // 自定义链接渲染
  953. md.renderer.rules.link_open = (tokens, idx) => {
  954. const token = tokens[idx];
  955. const hrefIndex = token.attrIndex('href');
  956. const href = token.attrs[hrefIndex][1];
  957. // 使用自定义的类名和数据属性
  958. return `<view class="custom-link" data-url="${href}">`;
  959. };
  960. md.renderer.rules.link_close = () => '</view>';
  961. const html = md.render(rawMarkdown);
  962. return html;
  963. } catch (error) {
  964. console.error('Markdown rendering error:', error);
  965. return rawMarkdown;
  966. }
  967. },
  968. // 修改 handleLinkClick 方法
  969. handleLinkClick(e) {
  970. // 查找最近的带有 custom-link 类的父元素
  971. let target = e.target;
  972. while (target && !target.classList?.contains('custom-link')) {
  973. target = target.parentElement;
  974. }
  975. if (target && target.dataset.url) {
  976. const url = target.dataset.url;
  977. console.log('Clicked URL:', url);
  978. // 复制链接到剪贴板
  979. uni.setClipboardData({
  980. data: url,
  981. success: () => {
  982. uni.showToast({
  983. title: '链接已复制',
  984. icon: 'none'
  985. });
  986. }
  987. });
  988. }
  989. },
  990. /* renderMarkdown(rawMarkdown) {
  991. if (typeof rawMarkdown !== 'string') {
  992. console.warn('Invalid input for renderMarkdown:', rawMarkdown);
  993. return '';
  994. }
  995. return md.render(rawMarkdown);
  996. }, */
  997. handleKeydown(event) {
  998. // Check if 'Enter' is pressed without the 'Alt' key
  999. if (event.key === "Enter" && !(event.shiftKey || event.altKey)) {
  1000. event.preventDefault(); // Prevent the default action to avoid line break in textarea
  1001. this.sendMessage();
  1002. } else if (event.key === "Enter" && event.altKey) {
  1003. // Allow 'Alt + Enter' to insert a newline
  1004. const cursorPosition = event.target.selectionStart;
  1005. const textBeforeCursor = this.userInput.slice(0, cursorPosition);
  1006. const textAfterCursor = this.userInput.slice(cursorPosition);
  1007. // Insert the newline character at the cursor position
  1008. this.userInput = textBeforeCursor + "\n" + textAfterCursor;
  1009. // Move the cursor to the right after the inserted newline
  1010. this.$nextTick(() => {
  1011. event.target.selectionStart = cursorPosition + 1;
  1012. event.target.selectionEnd = cursorPosition + 1;
  1013. });
  1014. }
  1015. },
  1016. beforeDestroy() {
  1017. if (this.websocket) {
  1018. this.websocket.close();
  1019. }
  1020. },
  1021. // 选择图片
  1022. chooseImage() {
  1023. const remainingSlots = this.maxImages - this.selectedImages.length;
  1024. if (remainingSlots <= 0) {
  1025. uni.showToast({
  1026. title: `最多只能上传${this.maxImages}张图片`,
  1027. icon: 'none'
  1028. });
  1029. return;
  1030. }
  1031. uni.chooseImage({
  1032. count: remainingSlots,
  1033. success: async (res) => {
  1034. // 即处理每个选择的图片
  1035. for (const tempFilePath of res.tempFilePaths) {
  1036. await this.uploadSingleImage(tempFilePath);
  1037. }
  1038. },
  1039. fail: (err) => {
  1040. console.error('选择图片失败:', err);
  1041. uni.showToast({
  1042. title: '选择图片失败',
  1043. icon: 'none'
  1044. });
  1045. }
  1046. });
  1047. },
  1048. // 新增单个图片上传方法
  1049. async uploadSingleImage(tempFilePath) {
  1050. try {
  1051. // 显示上传进度提示
  1052. uni.showLoading({
  1053. title: '正在上传...',
  1054. mask: true
  1055. });
  1056. const uploadResult = await uni.uploadFile({
  1057. url: `${this.apiUrl}/upload/file`, // 修改为你的上传接口
  1058. filePath: tempFilePath,
  1059. name: 'file',
  1060. header: {
  1061. 'Content-Type': 'multipart/form-data'
  1062. }
  1063. });
  1064. if (uploadResult.statusCode === 200) {
  1065. const response = JSON.parse(uploadResult.data);
  1066. const fileUrl = response.data.fileUrl; // 根据实际返回结构调整
  1067. // 添加已上传文件组
  1068. this.uploadedFiles.push({
  1069. url: fileUrl
  1070. });
  1071. // 更新预览图片数组
  1072. this.selectedImages.push(tempFilePath);
  1073. uni.hideLoading();
  1074. uni.showToast({
  1075. title: '上传成功',
  1076. icon: 'success'
  1077. });
  1078. } else {
  1079. throw new Error('Upload failed');
  1080. }
  1081. } catch (error) {
  1082. console.error('图片上传失败:', error);
  1083. uni.hideLoading();
  1084. uni.showToast({
  1085. title: '��传失败',
  1086. icon: 'none'
  1087. });
  1088. }
  1089. },
  1090. // 修改移除图片方法
  1091. removeImage(index) {
  1092. // 同时移除预览图和已上传的文件信息
  1093. this.selectedImages.splice(index, 1);
  1094. this.uploadedFiles.splice(index, 1);
  1095. },
  1096. // 修改选择视频方法
  1097. chooseVideo() {
  1098. if (this.selectedVideos.length >= this.maxVideos) {
  1099. uni.showToast({
  1100. title: `最多只能上传${this.maxVideos}个视频`,
  1101. icon: 'none'
  1102. });
  1103. return;
  1104. }
  1105. uni.chooseVideo({
  1106. count: 1,
  1107. sourceType: ['album', 'camera'],
  1108. maxDuration: 60, // 限制视频时长最大60秒
  1109. success: async (res) => {
  1110. // 检查视频大小(res.size 单位为字节)
  1111. const sizeInMB = res.size / (1024 * 1024);
  1112. if (sizeInMB > 10) {
  1113. uni.showToast({
  1114. title: '视频大小不能超过10MB',
  1115. icon: 'none'
  1116. });
  1117. return;
  1118. }
  1119. await this.uploadSingleVideo(res.tempFilePath, res.size);
  1120. },
  1121. fail: (err) => {
  1122. console.error('选择视频失败:', err);
  1123. uni.showToast({
  1124. title: '选择视频失败',
  1125. icon: 'none'
  1126. });
  1127. }
  1128. });
  1129. },
  1130. // 修改上传单个视频方法
  1131. async uploadSingleVideo(tempFilePath, fileSize) {
  1132. try {
  1133. // 再次检查文件大小
  1134. if (fileSize && fileSize / (1024 * 1024) > 10) {
  1135. throw new Error('视频大小不能超过10MB');
  1136. }
  1137. uni.showLoading({
  1138. title: '正在上传视频...',
  1139. mask: true
  1140. });
  1141. const uploadResult = await uni.uploadFile({
  1142. url: `${this.apiUrl}/upload/file`,
  1143. filePath: tempFilePath,
  1144. name: 'file',
  1145. header: {
  1146. 'Content-Type': 'multipart/form-data'
  1147. },
  1148. formData: {
  1149. // 可以添加额外的表单数据
  1150. fileType: 'video',
  1151. maxSize: 10 * 1024 * 1024 // 10MB in bytes
  1152. }
  1153. });
  1154. if (uploadResult.statusCode === 200) {
  1155. const response = JSON.parse(uploadResult.data);
  1156. // 检查服务器响应
  1157. if (response.code === 413 || response.message?.includes('大小超过限制')) {
  1158. throw new Error('视频大小超过限制');
  1159. }
  1160. const fileUrl = response.data.fileUrl;
  1161. // 添加到已上传视频数组
  1162. this.uploadedVideos.push({
  1163. url: fileUrl,
  1164. size: fileSize
  1165. });
  1166. // 更新预览视频数组
  1167. this.selectedVideos.push(tempFilePath);
  1168. uni.hideLoading();
  1169. uni.showToast({
  1170. title: '上传成功',
  1171. icon: 'success'
  1172. });
  1173. } else {
  1174. throw new Error('Upload failed');
  1175. }
  1176. } catch (error) {
  1177. console.error('视频上传失败:', error);
  1178. uni.hideLoading();
  1179. uni.showToast({
  1180. title: error.message || '上传失败',
  1181. icon: 'none',
  1182. duration: 2000
  1183. });
  1184. }
  1185. },
  1186. // 添加获取文件大小的辅助方法
  1187. formatFileSize(bytes) {
  1188. if (bytes === 0) return '0 B';
  1189. const k = 1024;
  1190. const sizes = ['B', 'KB', 'MB', 'GB'];
  1191. const i = Math.floor(Math.log(bytes) / Math.log(k));
  1192. return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + sizes[i];
  1193. },
  1194. // 修改移除视频方法
  1195. removeVideo(index) {
  1196. this.selectedVideos.splice(index, 1);
  1197. this.uploadedVideos.splice(index, 1);
  1198. },
  1199. // 新增统一的媒体选择方法
  1200. chooseMedia() {
  1201. const options = [];
  1202. if (this.selectedImages.length < this.maxImages) {
  1203. options.push('选择图片');
  1204. }
  1205. if (this.selectedVideos.length < this.maxVideos) {
  1206. options.push('选择视频');
  1207. }
  1208. /* if (this.selectedDocs.length < this.maxDocs) {
  1209. options.push('选择文档');
  1210. } */
  1211. if (options.length === 0) {
  1212. uni.showToast({
  1213. title: '已达上传上限',
  1214. icon: 'none'
  1215. });
  1216. return;
  1217. }
  1218. uni.showActionSheet({
  1219. itemList: options,
  1220. success: (res) => {
  1221. const selected = options[res.tapIndex];
  1222. if (selected === '选择图片') {
  1223. this.chooseImage();
  1224. } else if (selected === '选择视频') {
  1225. this.chooseVideo();
  1226. } /* else if (selected === '选择文档') {
  1227. this.chooseDoc();
  1228. } */
  1229. }
  1230. });
  1231. },
  1232. // 切换音频播放状态
  1233. toggleAudio(message) {
  1234. if (!message.audioData) {
  1235. uni.showToast({
  1236. title: '没有可用的音频',
  1237. icon: 'none'
  1238. });
  1239. return;
  1240. }
  1241. if (message.audioData.isPlaying) {
  1242. this.stopAudio(message);
  1243. } else {
  1244. this.playAudio(message.audioData.base64);
  1245. }
  1246. },
  1247. // 播放音频
  1248. playAudio(audioBase64) {
  1249. try {
  1250. // 先停止当前正在播放的音频
  1251. this.stopCurrentAudio();
  1252. // 创建新的音频上下文
  1253. const innerAudioContext = uni.createInnerAudioContext();
  1254. this.currentAudioContext = innerAudioContext;
  1255. // 设置音频源
  1256. innerAudioContext.src = audioBase64;
  1257. // 监听播放错误
  1258. innerAudioContext.onError((res) => {
  1259. console.error('音频播放错误:', res.errMsg);
  1260. uni.showToast({
  1261. title: '音频播放失败',
  1262. icon: 'none'
  1263. });
  1264. this.stopCurrentAudio();
  1265. });
  1266. // 播放结束后的处理
  1267. innerAudioContext.onEnded(() => {
  1268. // 只调用 stop,不销毁实例
  1269. if (this.currentAudioContext) {
  1270. this.currentAudioContext.stop();
  1271. }
  1272. });
  1273. // 开始播放
  1274. innerAudioContext.play();
  1275. } catch (error) {
  1276. console.error('播放音频失败:', error);
  1277. this.stopCurrentAudio();
  1278. }
  1279. },
  1280. // 停止音频
  1281. stopAudio(message) {
  1282. if (this.currentAudioContext) {
  1283. this.currentAudioContext.stop();
  1284. this.currentAudioContext.destroy();
  1285. this.currentAudioContext = null;
  1286. }
  1287. message.audioData.isPlaying = false;
  1288. message.audioData.progress = 0;
  1289. },
  1290. // 组件销毁时清理音频
  1291. beforeDestroy() {
  1292. this.stopCurrentAudio();
  1293. },
  1294. // 添加选择文档方法
  1295. chooseDoc() {
  1296. if (this.selectedDocs.length >= this.maxDocs) {
  1297. uni.showToast({
  1298. title: `最多只能上传${this.maxDocs}个文档`,
  1299. icon: 'none'
  1300. });
  1301. return;
  1302. }
  1303. this.chooseFromChat();
  1304. /* // 根据平台显示不同的选项
  1305. const options = ['从聊天记录选择', '从文件管理器选择'];
  1306. uni.showActionSheet({
  1307. itemList: options,
  1308. success: (res) => {
  1309. if (res.tapIndex === 0) {
  1310. // 从聊天记录选择
  1311. } else {
  1312. // 从文件管理器选择
  1313. this.chooseFromFileSystem();
  1314. }
  1315. }
  1316. }); */
  1317. },
  1318. // 添加从聊天记录选择文档的方法
  1319. chooseFromChat() {
  1320. // #ifdef MP-WEIXIN
  1321. uni.chooseMessageFile({
  1322. count: 1,
  1323. type: 'file',
  1324. extension: ['doc', 'docx', 'pdf', 'txt'],
  1325. success: (res) => {
  1326. const tempFile = res.tempFiles[0];
  1327. this.uploadSingleDoc(tempFile);
  1328. }
  1329. });
  1330. // #endif
  1331. // #ifdef APP-PLUS
  1332. plus.gallery.pick((path) => {
  1333. this.uploadSingleDoc({
  1334. path: path,
  1335. name: path.substring(path.lastIndexOf('/') + 1)
  1336. });
  1337. }, (error) => {
  1338. uni.showToast({
  1339. title: '选择文件失败',
  1340. icon: 'none'
  1341. });
  1342. }, {
  1343. filter: 'document'
  1344. });
  1345. // #endif
  1346. },
  1347. // 修改 chooseFromFileSystem 方法
  1348. async chooseFromFileSystem() {
  1349. // #ifdef MP-WEIXIN
  1350. // 微信小程序使用 wx.chooseMessageFile API
  1351. uni.chooseMessageFile({
  1352. count: 1, // 可选择文件的数量
  1353. type: 'file', // 选择文件类型
  1354. extension: ['doc', 'docx', 'pdf', 'txt'], // 允许的文件类型
  1355. success: (res) => {
  1356. if (res.tempFiles && res.tempFiles.length > 0) {
  1357. const file = res.tempFiles[0];
  1358. this.uploadSingleDoc({
  1359. path: file.path,
  1360. name: file.name
  1361. });
  1362. }
  1363. },
  1364. fail: (err) => {
  1365. console.error('选择文件失败:', err);
  1366. uni.showToast({
  1367. title: '选择文件失败',
  1368. icon: 'none'
  1369. });
  1370. }
  1371. });
  1372. // #endif
  1373. // #ifdef APP-PLUS
  1374. // 保持原有的 APP 端代码
  1375. try {
  1376. if (uni.getSystemInfoSync().platform === 'android') {
  1377. // ... 原有的 Android 代码 ...
  1378. }
  1379. } catch (error) {
  1380. console.error('File picker error:', error);
  1381. uni.showToast({
  1382. title: '选择文件失败',
  1383. icon: 'none',
  1384. duration: 2000
  1385. });
  1386. }
  1387. // #endif
  1388. },
  1389. // 修改 uploadSingleDoc 方法以处理不同格式的文件路径
  1390. async uploadSingleDoc(file) {
  1391. try {
  1392. uni.showLoading({
  1393. title: '正在上传文档...',
  1394. mask: true
  1395. });
  1396. let uploadFile;
  1397. // #ifdef APP-PLUS
  1398. if (typeof file === 'object' && file.path) {
  1399. // 处理 Android/iOS 返回的文件对象
  1400. uploadFile = {
  1401. path: file.path,
  1402. name: file.name
  1403. };
  1404. }
  1405. // #endif
  1406. // #ifdef MP-WEIXIN
  1407. if (file.path) {
  1408. uploadFile = {
  1409. path: file.path,
  1410. name: file.name
  1411. };
  1412. }
  1413. // #endif
  1414. // #ifdef H5
  1415. if (file instanceof File) {
  1416. uploadFile = file;
  1417. }
  1418. // #endif
  1419. if (!uploadFile) {
  1420. throw new Error('Invalid file format');
  1421. }
  1422. const uploadResult = await uni.uploadFile({
  1423. url: `${this.apiUrl}/upload/file`,
  1424. filePath: uploadFile.path || uploadFile,
  1425. name: 'file',
  1426. header: {
  1427. 'Content-Type': 'multipart/form-data'
  1428. }
  1429. });
  1430. if (uploadResult.statusCode === 200) {
  1431. const response = JSON.parse(uploadResult.data);
  1432. const fileUrl = response.data.fileUrl;
  1433. // 添加到已上传文档数组
  1434. this.uploadedDocs.push({
  1435. url: fileUrl,
  1436. name: uploadFile.name || '文档'
  1437. });
  1438. // 更新预览文档数组
  1439. this.selectedDocs.push({
  1440. name: uploadFile.name || '文档',
  1441. path: uploadFile.path || uploadFile
  1442. });
  1443. uni.hideLoading();
  1444. uni.showToast({
  1445. title: '上传成功',
  1446. icon: 'success'
  1447. });
  1448. } else {
  1449. throw new Error('Upload failed');
  1450. }
  1451. } catch (error) {
  1452. console.error('文档上传失败:', error);
  1453. uni.hideLoading();
  1454. uni.showToast({
  1455. title: '上传失败',
  1456. icon: 'none'
  1457. });
  1458. }
  1459. },
  1460. // 添加移除文档方法
  1461. removeDoc(index) {
  1462. this.selectedDocs.splice(index, 1);
  1463. this.uploadedDocs.splice(index, 1);
  1464. },
  1465. /* 打开弹窗 */
  1466. async showVoiceSettings() {
  1467. try {
  1468. const response = await uni.request({
  1469. url: `${this.apiUrl}/chatbot/list_voices`,
  1470. method: 'GET'
  1471. });
  1472. if (response.statusCode === 200 && response.data.code === 200) {
  1473. this.voicesData = response.data.data.voices;
  1474. this.languageOptions = Object.keys(this.voicesData);
  1475. // 设置默认值
  1476. if (!this.voiceSettings.language || !this.voicesData[this.voiceSettings.language]) {
  1477. this.voiceSettings.language = this.languageOptions[0];
  1478. }
  1479. // 初始化 rate 和 volume 的值
  1480. if (!this.voiceSettings.rate) {
  1481. this.voiceSettings.rate = '+10%';
  1482. }
  1483. if (!this.voiceSettings.volume) {
  1484. this.voiceSettings.volume = '+20%';
  1485. }
  1486. this.updateVoiceOptions();
  1487. // 打开弹窗
  1488. this.$refs.voiceSettingsPopup.open();
  1489. }
  1490. } catch (error) {
  1491. uni.showToast({
  1492. title: '获取语音配置失败',
  1493. icon: 'none'
  1494. });
  1495. }
  1496. },
  1497. // 更新声音选项
  1498. updateVoiceOptions() {
  1499. this.currentVoices = this.voicesData[this.voiceSettings.language] || [];
  1500. // 如果当前选择的声音不在新的选项中,重置选择
  1501. if (!this.currentVoices.find(v => v.name === this.voiceSettings.voice_name)) {
  1502. if (this.currentVoices.length > 0) {
  1503. this.voiceSettings.voice_name = this.currentVoices[0].name;
  1504. this.voiceSettings.gender = this.currentVoices[0].gender;
  1505. this.updateStyleOptions(this.currentVoices[0]);
  1506. }
  1507. }
  1508. },
  1509. // 更新风格选项
  1510. updateStyleOptions(voice) {
  1511. this.styleOptions = voice.style || [];
  1512. if (!this.styleOptions.includes(this.voiceSettings.style)) {
  1513. this.voiceSettings.style = this.styleOptions[0] || '';
  1514. }
  1515. },
  1516. // 处理语言变化
  1517. handleLanguageChange(e) {
  1518. const newLanguage = this.languageOptions[e.detail.value];
  1519. this.voiceSettings.language = newLanguage;
  1520. this.updateVoiceOptions();
  1521. },
  1522. // 处理声音名称变化
  1523. handleVoiceNameChange(e) {
  1524. const selectedVoice = this.currentVoices[e.detail.value];
  1525. this.voiceSettings.voice_name = selectedVoice.name;
  1526. this.voiceSettings.gender = selectedVoice.gender;
  1527. this.updateStyleOptions(selectedVoice);
  1528. },
  1529. // 处理风格变化
  1530. handleStyleChange(e) {
  1531. this.voiceSettings.style = this.styleOptions[e.detail.value];
  1532. },
  1533. // 保存配置
  1534. async saveVoiceSettings() {
  1535. try {
  1536. const response = await uni.request({
  1537. url: this.apiUrl + '/switch_voice_config', // 确保使用正确的接口地址
  1538. method: 'POST',
  1539. data: {
  1540. language: this.voiceSettings.language,
  1541. voice_name: this.voiceSettings.voice_name,
  1542. gender: this.voiceSettings.gender,
  1543. style: this.voiceSettings.style,
  1544. rate: this.voiceSettings.rate,
  1545. volume: this.voiceSettings.volume
  1546. },
  1547. header: {
  1548. 'content-type': 'application/x-www-form-urlencoded'
  1549. }
  1550. });
  1551. if (response.statusCode === 200 && response.data.code === 200) {
  1552. uni.showToast({
  1553. title: '设置保存成功',
  1554. icon: 'success'
  1555. });
  1556. this.closeVoiceSettings();
  1557. } else {
  1558. throw new Error(response.data.msg || '保存失败');
  1559. }
  1560. } catch (error) {
  1561. console.error('保存设置失败:', error); // 添加错误日志
  1562. uni.showToast({
  1563. title: error.message || '保存失败,请重试',
  1564. icon: 'none'
  1565. });
  1566. }
  1567. },
  1568. // 添加关闭弹窗方法
  1569. closeVoiceSettings() {
  1570. this.$refs.voiceSettingsPopup.close();
  1571. },
  1572. // 添加弹窗状态改变处理方法
  1573. handlePopupChange(e) {
  1574. if (e.show) {
  1575. // 弹窗打开时
  1576. uni.hideTabBar(); // 隐藏底部导航栏
  1577. // #ifdef H5
  1578. // 禁止页面滚动
  1579. document.body.style.overflow = 'hidden';
  1580. // #endif
  1581. } else {
  1582. // 弹窗关闭时
  1583. uni.showTabBar(); // 显示底部导航栏
  1584. // #ifdef H5
  1585. // 恢复页面滚动
  1586. document.body.style.overflow = 'auto';
  1587. // #endif
  1588. }
  1589. },
  1590. // 添加图片预览方法
  1591. previewImage(images, current) {
  1592. uni.previewImage({
  1593. urls: images,
  1594. current: images[current]
  1595. });
  1596. },
  1597. // 添加权限检查方法
  1598. async checkAndRequestPermissions() {
  1599. // #ifdef APP-PLUS
  1600. if (uni.getSystemInfoSync().platform === 'android') {
  1601. const permission = 'android.permission.READ_EXTERNAL_STORAGE';
  1602. const result = await new Promise((resolve) => {
  1603. plus.android.requestPermissions(
  1604. [permission],
  1605. function(resultObj) {
  1606. resolve(resultObj.granted.indexOf(permission) >= 0);
  1607. },
  1608. function(error) {
  1609. console.error('Error requesting permissions:', error);
  1610. resolve(false);
  1611. }
  1612. );
  1613. });
  1614. if (!result) {
  1615. uni.showToast({
  1616. title: '需要存储权限才能选择文件',
  1617. icon: 'none'
  1618. });
  1619. return false;
  1620. }
  1621. return true;
  1622. }
  1623. // #endif
  1624. return true;
  1625. },
  1626. // 修改文件选择方法
  1627. async chooseFromFileSystem() {
  1628. // 先检查权限
  1629. const hasPermission = await this.checkAndRequestPermissions();
  1630. if (!hasPermission) {
  1631. return;
  1632. }
  1633. // 原有的文件选择代码...
  1634. },
  1635. // 获取文档类型的中文释义
  1636. getDocumentType(filename) {
  1637. const extension = filename.split('.').pop().toLowerCase();
  1638. return this.docTypeMap[extension] || '未知文件';
  1639. },
  1640. // 格式化文件名显示
  1641. formatFileName(filename) {
  1642. // 如果文件名过长,截断显示
  1643. const maxLength = 10;
  1644. const name = filename.split('.')[0]; // 获取文件名(不含扩展名)
  1645. if (name.length > maxLength) {
  1646. return name.substring(0, maxLength) + '...';
  1647. }
  1648. return name;
  1649. },
  1650. // 预览视频缩略图
  1651. previewVideoThumbnail(videos, current) {
  1652. // 生成所有视频的缩略图URL数组
  1653. const thumbnailUrls = videos.map(video => video + '?x-oss-process=video/snapshot,t_0,f_jpg');
  1654. uni.previewImage({
  1655. urls: thumbnailUrls,
  1656. current: thumbnailUrls[current],
  1657. longPressActions: {
  1658. itemList: ['发送给朋友', '保存图片', '收藏'],
  1659. success: function(data) {
  1660. console.log('选中了第' + (data.tapIndex + 1) + '个按钮');
  1661. }
  1662. }
  1663. });
  1664. },
  1665. },
  1666. // updated() {
  1667. // const messagesContainer = this.$el.querySelector(".messages");
  1668. // messagesContainer.scrollTop = messagesContainer.scrollHeight;
  1669. // },
  1670. };
  1671. </script>
  1672. <style scoped>
  1673. /* 这里引入 ChatBox.css 的内容 */
  1674. /* @import '/static/css/ChatBox.css'; */
  1675. .chat-container {
  1676. display: flex;
  1677. flex-direction: column;
  1678. height: 100vh;
  1679. /* 或者按照实际需要调整高度 */
  1680. }
  1681. .messages {
  1682. width: 90%;
  1683. /* 设置消息列表宽度为屏幕宽度的 80% */
  1684. max-width: 90%;
  1685. /* 限制最大宽度,避免超出视口 */
  1686. margin: 0 auto;
  1687. /* 上下保持原有的 margin,左右自动调整以居中 */
  1688. overflow-y: auto;
  1689. /* 如果消息太多,允许滚动 */
  1690. height: calc(100vh - 120px);
  1691. /* 高度需要根据输入框和其元素的高度调整 */
  1692. padding: 10px 10px 0;
  1693. /* 或你希望的内边距大小 */
  1694. }
  1695. .message {
  1696. display: flex;
  1697. padding: 10px;
  1698. align-items: flex-start;
  1699. }
  1700. .avatar-container {
  1701. display: flex;
  1702. flex-direction: column;
  1703. align-items: center;
  1704. min-width: 50px;
  1705. /* 确保头像容器有固定宽度 */
  1706. }
  1707. .messages li {
  1708. display: flex;
  1709. align-items: flex-start;
  1710. /* 这将确保所有子素对到容器的顶部 */
  1711. margin-bottom: 10px;
  1712. }
  1713. .avatar-container {
  1714. display: flex;
  1715. /* flex-direction: column; */
  1716. flex-direction: row;
  1717. margin-right: 10px;
  1718. }
  1719. .avatar {
  1720. width: 30px;
  1721. height: 30px;
  1722. border-radius: 50%;
  1723. margin-bottom: 5px;
  1724. }
  1725. .sender-name {
  1726. margin-left: 15px;
  1727. font-size: 12px;
  1728. color: #666;
  1729. margin-top: 2px;
  1730. }
  1731. .message {
  1732. display: flex;
  1733. align-items: flex-start;
  1734. flex-direction: column;
  1735. /* 增加消息间距 */
  1736. }
  1737. .user-avatar {
  1738. /* background-image: url('/static/用户.png'); */
  1739. /* 替换为您的 GIF 文件路径 */
  1740. background-size: cover;
  1741. /* 确保 GIF 覆盖整个头像区域 */
  1742. background-repeat: no-repeat;
  1743. /* 防止 GIF 重复 */
  1744. background-position: center;
  1745. /* 将 GIF 居中 */
  1746. border-radius: 50%;
  1747. }
  1748. .bot-avatar {
  1749. background-image: url('/static/机器人.png');
  1750. /* 替换为您的 GIF 文件路径 */
  1751. background-size: cover;
  1752. /* 确保 GIF 覆盖整个头像区域 */
  1753. background-repeat: no-repeat;
  1754. /* 防止 GIF 重复 */
  1755. background-position: center;
  1756. /* 将 GIF 居中 */
  1757. }
  1758. .message-content {
  1759. background-color: #f0f0f0;
  1760. /* 消息背景 */
  1761. padding-left: 20px;
  1762. /* 上下右内边距 */
  1763. padding-right: 10px;
  1764. /* 上下左右的内边距 */
  1765. padding-top: 10px;
  1766. /* 上下左右的内边距 */
  1767. padding-bottom: 10px;
  1768. /* 上下左右的内边距 */
  1769. max-width: calc(90% - 15px);
  1770. /* 计算头像和间距 */
  1771. border-radius: 10px 10px 10px 10px;
  1772. /* 边框圆角 */
  1773. margin-bottom: 10px;
  1774. /* 和下一条消息之���的间距 */
  1775. /* 消息内容样式 */
  1776. display: flex;
  1777. flex-direction: column;
  1778. /* 排列文本 */
  1779. margin-left: 30px;
  1780. margin-top: 0;
  1781. font-size: 12px;
  1782. word-break: break-all;
  1783. }
  1784. .message-text {
  1785. margin: 0;
  1786. white-space: normal;
  1787. /* 确保文本会换行 */
  1788. word-wrap: break-word;
  1789. /* 长单词或 URL 会被断开以适应容器宽度 */
  1790. }
  1791. .input-container {
  1792. position: fixed;
  1793. bottom: 0;
  1794. left: 0;
  1795. width: 96%;
  1796. background: white;
  1797. padding: 5px 10px;
  1798. box-shadow: 0px -2px 5px rgba(0, 0, 0, 0.1);
  1799. }
  1800. .input-wrapper {
  1801. display: flex;
  1802. align-items: center;
  1803. gap: 10px;
  1804. margin-bottom: 10px;
  1805. }
  1806. .input-textarea {
  1807. flex: 1;
  1808. background: #f5f5f5;
  1809. border-radius: 20px;
  1810. padding: 8px 15px;
  1811. min-height: 20px;
  1812. max-height: 80px;
  1813. font-size: 14px;
  1814. }
  1815. .send-btn {
  1816. min-width: 60px;
  1817. height: 36px;
  1818. line-height: 36px;
  1819. font-size: 14px;
  1820. background: #1296db;
  1821. color: white;
  1822. border-radius: 18px;
  1823. padding: 0 15px;
  1824. margin: 0;
  1825. }
  1826. .sender-name {
  1827. font-weight: bold;
  1828. color: #333;
  1829. font-size: 12px;
  1830. margin-bottom: 5px;
  1831. }
  1832. .btn {
  1833. border: none;
  1834. background-color: #1296db;
  1835. color: #fff;
  1836. }
  1837. .message-content :deep(p) {
  1838. font-size: 12px !important;
  1839. margin: 5px 0 10px;
  1840. padding: 0;
  1841. line-height: 22px;
  1842. font-weight: inherit;
  1843. }
  1844. .message-content :deep(ol) {
  1845. padding-bottom: 10px;
  1846. }
  1847. .message-content :deep(.source-section) {
  1848. margin: 20px 0 3px;
  1849. color: #333;
  1850. font-size: 16px;
  1851. }
  1852. .message-content :deep(ol li) {
  1853. padding-top: 3px;
  1854. }
  1855. .message-content :deep(ol li a) {
  1856. font-size: 14px;
  1857. color: #1296db;
  1858. }
  1859. /* 添加资料来源相关的新样式 */
  1860. .source-section {
  1861. border-top: 1px solid #e5e5e5;
  1862. padding-top: 10px;
  1863. margin-top: 10px;
  1864. }
  1865. .source-title {
  1866. font-size: 14px;
  1867. color: #666;
  1868. margin-bottom: 8px;
  1869. }
  1870. .source-links {
  1871. display: flex;
  1872. flex-direction: column;
  1873. gap: 5px;
  1874. }
  1875. .source-link {
  1876. color: #1296db;
  1877. font-size: 12px;
  1878. cursor: pointer;
  1879. /* text-decoration: underline; */
  1880. }
  1881. .source-link:hover {
  1882. opacity: 0.8;
  1883. }
  1884. /* 添加新建对话按钮样式 */
  1885. .new-chat-btn {
  1886. padding: 0px 20px;
  1887. display: flex;
  1888. justify-content: flex-start;
  1889. background-color: #fff;
  1890. }
  1891. .btn-new {
  1892. background-color: #1296db;
  1893. color: #fff;
  1894. font-size: 14px;
  1895. border-radius: 5px;
  1896. border: none;
  1897. height: 35px;
  1898. margin: 0;
  1899. }
  1900. /* 上传图标 */
  1901. .upload-img {
  1902. width: 30px;
  1903. height: 30px;
  1904. }
  1905. .image-upload-area {
  1906. width: 100%;
  1907. white-space: nowrap;
  1908. margin-bottom: 10px;
  1909. padding: 5px;
  1910. }
  1911. .image-list {
  1912. display: inline-flex;
  1913. gap: 10px;
  1914. }
  1915. .image-preview {
  1916. width: 75px;
  1917. height: 75px;
  1918. position: relative;
  1919. flex-shrink: 0;
  1920. border-radius: 8px;
  1921. /* overflow: hidden; */
  1922. }
  1923. .preview-img {
  1924. width: 100%;
  1925. height: 100%;
  1926. object-fit: cover;
  1927. }
  1928. .delete-btn {
  1929. position: absolute;
  1930. top: 0px;
  1931. right: 0px;
  1932. width: 20px;
  1933. height: 20px;
  1934. background: rgba(0, 0, 0, 0.5);
  1935. border-radius: 50%;
  1936. display: flex;
  1937. align-items: center;
  1938. justify-content: center;
  1939. z-index: 5;
  1940. }
  1941. .delete-icon {
  1942. color: white;
  1943. font-size: 18px;
  1944. }
  1945. .upload-btn {
  1946. width: 75px;
  1947. height: 75px;
  1948. border: 2px dashed #ddd;
  1949. border-radius: 8px;
  1950. display: flex;
  1951. align-items: center;
  1952. justify-content: center;
  1953. flex-shrink: 0;
  1954. }
  1955. .upload-icon {
  1956. width: 30px;
  1957. height: 30px;
  1958. opacity: 0.5;
  1959. }
  1960. /* 添加消息中图片的样式 */
  1961. .message-images {
  1962. display: flex;
  1963. flex-wrap: wrap;
  1964. gap: 5px;
  1965. margin-top: 5px;
  1966. }
  1967. .message-image {
  1968. width: 120px;
  1969. height: 120px;
  1970. border-radius: 8px;
  1971. object-fit: cover;
  1972. }
  1973. /* 添加视频相关样式 */
  1974. .upload-options {
  1975. display: flex;
  1976. justify-content: center;
  1977. align-items: center;
  1978. }
  1979. .upload-icon {
  1980. width: 24px;
  1981. height: 24px;
  1982. opacity: 0.7;
  1983. }
  1984. .video-preview {
  1985. width: 120px;
  1986. height: 120px;
  1987. position: relative;
  1988. flex-shrink: 0;
  1989. }
  1990. .video-thumbnail {
  1991. width: 100%;
  1992. height: 100%;
  1993. border-radius: 8px;
  1994. object-fit: cover;
  1995. }
  1996. .play-icon {
  1997. position: absolute;
  1998. top: 50%;
  1999. left: 50%;
  2000. transform: translate(-50%, -50%);
  2001. width: 30px;
  2002. height: 30px;
  2003. opacity: 0.8;
  2004. }
  2005. /* 添加视频预览相关样式 */
  2006. .video-preview-container {
  2007. width: 100%;
  2008. height: 100%;
  2009. position: relative;
  2010. display: flex;
  2011. align-items: center;
  2012. justify-content: center;
  2013. background-color: #f5f5f5;
  2014. border-radius: 8px;
  2015. }
  2016. .play-icon {
  2017. position: absolute;
  2018. width: 24px;
  2019. height: 24px;
  2020. opacity: 0.8;
  2021. }
  2022. /* 添加音频控件样式 */
  2023. .audio-controls {
  2024. display: flex;
  2025. align-items: center;
  2026. margin-top: 8px;
  2027. gap: 10px;
  2028. }
  2029. .audio-btn {
  2030. width: 30px;
  2031. height: 30px;
  2032. border-radius: 50%;
  2033. /* background: #1296db; */
  2034. display: flex;
  2035. align-items: center;
  2036. justify-content: center;
  2037. cursor: pointer;
  2038. }
  2039. .audio-icon {
  2040. width: 16px;
  2041. height: 16px;
  2042. }
  2043. .audio-progress {
  2044. font-size: 12px;
  2045. color: #666;
  2046. }
  2047. /* 更新消息内容样式 */
  2048. .message-content {
  2049. display: flex;
  2050. flex-direction: column;
  2051. background-color: #f0f0f0;
  2052. padding: 10px 15px;
  2053. border-radius: 10px;
  2054. margin-left: 30px;
  2055. max-width: calc(90% - 15px);
  2056. font-size: 12px;
  2057. }
  2058. /* 添加文档预览相关样式 */
  2059. .doc-preview {
  2060. width: 75px;
  2061. height: 75px;
  2062. position: relative;
  2063. flex-shrink: 0;
  2064. border-radius: 8px;
  2065. overflow: hidden;
  2066. background-color: #f5f5f5;
  2067. }
  2068. .doc-preview-container {
  2069. width: 100%;
  2070. height: 100%;
  2071. display: flex;
  2072. flex-direction: column;
  2073. align-items: center;
  2074. justify-content: center;
  2075. padding: 5px;
  2076. }
  2077. .doc-icon {
  2078. width: 30px;
  2079. height: 30px;
  2080. margin-bottom: 5px;
  2081. }
  2082. .doc-name {
  2083. font-size: 10px;
  2084. color: #666;
  2085. text-align: center;
  2086. overflow: hidden;
  2087. text-overflow: ellipsis;
  2088. white-space: nowrap;
  2089. width: 100%;
  2090. padding: 0 5px;
  2091. }
  2092. /* 设置图标样式 */
  2093. .settings-icon {
  2094. width: 24px;
  2095. height: 24px;
  2096. margin-bottom: 10px;
  2097. opacity: 0.7;
  2098. }
  2099. /* 设置��窗样式 */
  2100. .settings-popup {
  2101. background: #fff;
  2102. border-radius: 12px;
  2103. width: 80vw;
  2104. padding: 20px;
  2105. position: relative;
  2106. z-index: 999;
  2107. /* 确保弹窗在最上层 */
  2108. }
  2109. .settings-header {
  2110. display: flex;
  2111. justify-content: space-between;
  2112. align-items: center;
  2113. margin-bottom: 20px;
  2114. }
  2115. .settings-title {
  2116. font-size: 18px;
  2117. font-weight: bold;
  2118. }
  2119. .close-btn {
  2120. font-size: 24px;
  2121. color: #999;
  2122. }
  2123. .settings-content {
  2124. max-height: 60vh;
  2125. overflow-y: auto;
  2126. }
  2127. .settings-item {
  2128. margin-bottom: 15px;
  2129. }
  2130. .item-label {
  2131. display: block;
  2132. margin-bottom: 5px;
  2133. color: #666;
  2134. font-size: 14px;
  2135. }
  2136. .picker-value {
  2137. height: 35px;
  2138. line-height: 35px;
  2139. padding: 0 10px;
  2140. background: #f5f5f5;
  2141. border-radius: 5px;
  2142. }
  2143. input {
  2144. height: 35px;
  2145. padding: 0 10px;
  2146. background: #f5f5f5;
  2147. border-radius: 5px;
  2148. }
  2149. .save-btn {
  2150. margin-top: 20px;
  2151. background: #1296db;
  2152. color: #fff;
  2153. border: none;
  2154. border-radius: 5px;
  2155. height: 40px;
  2156. line-height: 40px;
  2157. }
  2158. /* 添加遮罩层样式 */
  2159. :deep(.uni-popup__mask) {
  2160. z-index: 998;
  2161. }
  2162. /* 更新弹窗样式以适应不同设备 */
  2163. .settings-popup {
  2164. background: #fff;
  2165. border-radius: 12px;
  2166. width: 80vw;
  2167. padding: 20px;
  2168. position: relative;
  2169. z-index: 999;
  2170. /* 确保弹窗在最上层 */
  2171. }
  2172. .settings-content {
  2173. max-height: 60vh;
  2174. /* 限制内容最大高度 */
  2175. overflow-y: auto;
  2176. }
  2177. /* 确保遮罩层在正确的层级 */
  2178. :deep(.uni-popup__mask) {
  2179. z-index: 998;
  2180. }
  2181. /* 适配小屏幕设备 */
  2182. @media screen and (max-width: 375px) {
  2183. .settings-popup {
  2184. width: 90vw;
  2185. padding: 15px;
  2186. }
  2187. .settings-title {
  2188. font-size: 16px;
  2189. }
  2190. .item-label {
  2191. font-size: 13px;
  2192. }
  2193. }
  2194. /* 适配大屏幕设备 */
  2195. @media screen and (min-width: 768px) {
  2196. .settings-popup {
  2197. width: 60vw;
  2198. max-width: 500px;
  2199. }
  2200. }
  2201. .set_btn {
  2202. display: flex;
  2203. flex-direction: column;
  2204. /* 设置为竖向排列 */
  2205. gap: 10px;
  2206. /* 按钮之间的间距 */
  2207. justify-content: center;
  2208. align-items: center;
  2209. }
  2210. /* 添加图片聊天相关样式 */
  2211. .message-images {
  2212. margin-bottom: 8px;
  2213. }
  2214. .chat-image {
  2215. width: 120px;
  2216. height: 120px;
  2217. border-radius: 8px;
  2218. margin-bottom: 5px;
  2219. }
  2220. /* 视频样式 */
  2221. .message-videos {
  2222. margin-bottom: 8px;
  2223. }
  2224. .video-thumbnail-container {
  2225. position: relative;
  2226. width: 120px;
  2227. height: 120px;
  2228. margin-bottom: 5px;
  2229. }
  2230. .hidden-video {
  2231. position: absolute;
  2232. width: 1px;
  2233. height: 1px;
  2234. opacity: 0;
  2235. pointer-events: none;
  2236. }
  2237. .play-icon-overlay {
  2238. position: absolute;
  2239. top: 0;
  2240. left: 0;
  2241. right: 0;
  2242. bottom: 0;
  2243. display: flex;
  2244. align-items: center;
  2245. justify-content: center;
  2246. background-color: rgba(0, 0, 0, 0.2);
  2247. border-radius: 8px;
  2248. }
  2249. .play-icon {
  2250. width: 40px;
  2251. height: 40px;
  2252. opacity: 0.8;
  2253. }
  2254. /* 调整视频缩略图样式 */
  2255. .chat-image {
  2256. width: 120px;
  2257. height: 120px;
  2258. border-radius: 8px;
  2259. object-fit: cover;
  2260. }
  2261. /* 文档展示 */
  2262. .message-docs {
  2263. display: flex;
  2264. flex-wrap: wrap;
  2265. gap: 5px;
  2266. margin-top: 5px;
  2267. }
  2268. .doc-preview {
  2269. width: 75px;
  2270. height: 75px;
  2271. position: relative;
  2272. flex-shrink: 0;
  2273. border-radius: 8px;
  2274. overflow: hidden;
  2275. background-color: #f5f5f5;
  2276. }
  2277. .doc-preview-container {
  2278. width: 100%;
  2279. height: 100%;
  2280. display: flex;
  2281. flex-direction: column;
  2282. align-items: center;
  2283. justify-content: center;
  2284. padding: 5px;
  2285. }
  2286. .doc-icon {
  2287. width: 30px;
  2288. height: 30px;
  2289. margin-bottom: 5px;
  2290. }
  2291. .doc-name {
  2292. font-size: 10px;
  2293. color: #666;
  2294. text-align: center;
  2295. overflow: hidden;
  2296. text-overflow: ellipsis;
  2297. white-space: nowrap;
  2298. width: 100%;
  2299. padding: 0 5px;
  2300. }
  2301. </style>