index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <template>
  2. <view class="">
  3. <uni-drawer ref="drawerLeft" mode="left">
  4. <view class="menuTop" :style="[heightStyle]">
  5. <view :style="[heightStyle]" style="background:white;"></view>
  6. <view class="newChat">
  7. <view class="newChatBtn" @click="createNewChat">
  8. + 新的聊天
  9. </view>
  10. </view>
  11. </view>
  12. <scroll-view class="menuScroll" scroll-y="true" :style="[topStyle]">
  13. <view class="menuItem" v-for="item in allChatList" :key="item.date" :class="item.active?'active':''" @click="() => checkChatList(item)">
  14. <view class="text"><uni-icons :color="item.active?'#007aff':'rgb(51,51,51)'" type="chat" size="16"></uni-icons>
  15. {{ item.title }}</view>
  16. <view @click.stop="() => removeChat(item)">
  17. <uni-icons v-if="allChatList.length > 1" :color="item.active?'#007aff':'rgb(51,51,51)'" type="closeempty" size="16"></uni-icons>
  18. </view>
  19. </view>
  20. </scroll-view>
  21. <view class="menuBottom">
  22. <view class="clearAllChat" @click="clearAllChat">
  23. 清空所有聊天
  24. </view>
  25. </view>
  26. </uni-drawer>
  27. <view class="container">
  28. <view :style="[heightStyle]" style="background:white;"></view>
  29. <uni-nav-bar fixed :border="false" left-icon="bars" title="AIxiu" @clickLeft="showDrawer"/>
  30. <scroll-view
  31. :scroll-with-animation="true"
  32. class="scorllView"
  33. scroll-y="true"
  34. :scroll-into-view="scrollIntoView"
  35. >
  36. <view v-for="(item, index) in curChat.chatList" :key="item.time" class="chatItem" :class="item.type=='gpt'?'gpt':'user'" :id="`item${index}`">
  37. <view class="head" v-if="item.type == 'gpt'">
  38. G
  39. </view>
  40. <view class="content">
  41. <view class="time">{{ item.time }}</view>
  42. <zero-markdown-view class="markdownView" :markdown="item.data.message"></zero-markdown-view>
  43. </view>
  44. <view class="head" v-if="item.type == 'user'">
  45. U
  46. </view>
  47. </view>
  48. <view class="chatItem" v-if="loading">
  49. <view style="position: relative;width:100%">
  50. <zero-loading type="pulse" position="absolute"></zero-loading>
  51. </view>
  52. </view>
  53. </scroll-view>
  54. <view class="bottom">
  55. <textarea class="inputArea" v-model="message" disable-default-padding :always-embed="true" :adjust-position="true" cursor-spacing="60" :style="[inputArea]" placeholder="输入内容" @input="onInput" @focus="onFocus" @blur="onBlur" @linechange="onLineChange"/>
  56. <button class="button" :disabled="disabled" plain="true" @click="sendMessage" :style="[btnBorderColor]">
  57. <uni-icons type="paperplane" size="30" :color="color"></uni-icons>
  58. </button>
  59. </view>
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. import { dateTimeStr } from '@/utils/index.js';
  65. export default {
  66. data() {
  67. return {
  68. drawerLeft: null,
  69. message: '',
  70. borderColor: '',
  71. textAreaHeight: 40,
  72. loading: false,
  73. scrollIntoView: '',
  74. statusBarHeight: 0,
  75. allChatList: [
  76. {
  77. id: new Date().getTime(),
  78. title: '新的聊天',
  79. active: true,
  80. chatList: []
  81. }
  82. ]
  83. }
  84. },
  85. computed: {
  86. curChat() {
  87. return this.allChatList.find(item => item.active)
  88. },
  89. disabled() {
  90. return this.message.length <= 0
  91. },
  92. color() {
  93. return this.disabled?"rgba(0, 0, 0, 0.2)":"#007aff"
  94. },
  95. heightStyle() {
  96. return { "height": `${this.statusBarHeight}px`}
  97. },
  98. topStyle() {
  99. return { "top": `${this.statusBarHeight + 60}px`}
  100. },
  101. inputArea() {
  102. return { "border-color":`${this.borderColor}`,"height": `${this.textAreaHeight}px` }
  103. },
  104. btnBorderColor() {
  105. return {"border-color": `${this.color}`}
  106. },
  107. },
  108. onLoad(){
  109. this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
  110. // 开启小程序原生右上角分享按钮
  111. uni.showShareMenu({
  112. // https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share.html
  113. withShareTicket: true,
  114. menus: ['shareAppMessage', 'shareTimeline'],//开启转发好友和转发朋友圈按钮
  115. });
  116. },
  117. mounted(){
  118. this.getAllChatList()
  119. let that = this
  120. setTimeout(()=>{
  121. that.scrollIntoView = `item${that.curChat.chatList.length - 1}`
  122. },500)
  123. },
  124. methods: {
  125. // 自定义此页面的转发给好友(已经有全局的分享方法,此处会覆盖全局)
  126. onShareAppMessage(res) {
  127. return {
  128. title: 'AIxiu-做您专属的问答助手',
  129. imageUrl: '/static/logo4.png'
  130. }
  131. },
  132. createNewChat() {
  133. this.allChatList.map(i => {
  134. i.active = false
  135. })
  136. this.allChatList.unshift(
  137. {
  138. id: new Date().getTime(),
  139. title: '新的聊天',
  140. active: true,
  141. chatList: []
  142. }
  143. )
  144. },
  145. clearAllChat() {
  146. let that = this
  147. uni.showModal({
  148. title: '提示',
  149. content: '确认清空所有记录吗?',
  150. success: function (res) {
  151. if (res.confirm) {
  152. that.allChatList = [{
  153. id: new Date().getTime(),
  154. title: '新的聊天',
  155. active: true,
  156. chatList: []
  157. }]
  158. that.saveLocalStorage()
  159. } else if (res.cancel) {
  160. console.log('用户点击取消');
  161. }
  162. }
  163. });
  164. },
  165. removeChat(item) {
  166. if(this.allChatList.length <= 1) return false
  167. let that = this
  168. uni.showModal({
  169. title: '提示',
  170. content: '确认删除吗?',
  171. success: function (res) {
  172. if (res.confirm) {
  173. let splingsChat = that.allChatList.filter(i => i.id != item.id)
  174. splingsChat[0].active = true
  175. that.allChatList = splingsChat
  176. that.saveLocalStorage()
  177. } else if (res.cancel) {
  178. console.log('用户点击取消');
  179. }
  180. }
  181. });
  182. },
  183. getAllChatList() {
  184. let historyList = uni.getStorageSync('allChatList')
  185. if (historyList) {
  186. this.allChatList = historyList
  187. } else {
  188. let data = [
  189. {
  190. id: new Date().getTime(),
  191. title: '新的聊天',
  192. active: true,
  193. chatList: []
  194. }
  195. ]
  196. uni.setStorage({
  197. key: 'allChatList',
  198. data
  199. })
  200. this.allChatList = data
  201. }
  202. },
  203. saveLocalStorage() {
  204. let data = this.allChatList
  205. uni.setStorage({
  206. key: 'allChatList',
  207. data
  208. })
  209. },
  210. checkChatList(item) {
  211. this.allChatList.map(i => {
  212. if(i.id == item.id){
  213. i.active = true
  214. } else {
  215. i.active = false
  216. }
  217. })
  218. // let that = this
  219. // setTimeout(()=>{
  220. // that.scrollIntoView = `item${that.curChat.chatList.length - 1}`
  221. // },500)
  222. },
  223. onFocus() {
  224. this.borderColor = "#007aff"
  225. },
  226. onBlur() {
  227. this.borderColor = "rgba(0, 0, 0, 0.2)"
  228. },
  229. showDrawer() {
  230. console.log('showshowshow')
  231. this.$refs.drawerLeft.open()
  232. },
  233. onInput(e) {
  234. //this.message = e.target.value
  235. },
  236. sendMessage() {
  237. this.loading = true
  238. let data = {
  239. message: this.message
  240. }
  241. this.curChat.chatList.push({
  242. type: 'user',
  243. time: dateTimeStr('y/m/d h:i:s'),
  244. data
  245. })
  246. if(this.curChat.title == '新的聊天'){
  247. this.curChat.title = this.message
  248. }
  249. let that = this
  250. setTimeout(()=>{
  251. that.scrollIntoView = `item${that.curChat.chatList.length - 1}`
  252. },100)
  253. this.allChatList.map(i => {
  254. if(i.id == this.curChat.id){
  255. i = this.curChat
  256. }
  257. })
  258. this.saveLocalStorage()
  259. this.message = ''
  260. uni.request({
  261. url: 'https://chat.chuckchen.top/conversation', //仅为示例,并非真实接口地址。
  262. method: 'POST',
  263. data,
  264. header: {
  265. 'Content-Type': 'application/json'
  266. },
  267. success: async (res) => {
  268. this.curChat.chatList.push({
  269. type: 'gpt',
  270. time: dateTimeStr('y/m/d h:i:s'),
  271. data: {
  272. message: res.data.response,
  273. //...res.data
  274. }
  275. })
  276. let that = this
  277. setTimeout(()=>{
  278. that.scrollIntoView = `item${that.curChat.chatList.length - 1}`
  279. },100)
  280. this.allChatList.map(i => {
  281. if(i.id == this.curChat.id){
  282. i = this.curChat
  283. }
  284. })
  285. this.saveLocalStorage()
  286. this.loading = false
  287. },
  288. fail() {
  289. uni.showToast({
  290. title: '请求失败',
  291. icon: 'error',
  292. duration: 2000
  293. })
  294. this.loading = false
  295. }
  296. });
  297. },
  298. onLineChange(e) {
  299. if(e.detail.lineCount < 5){
  300. this.textAreaHeight = 20 + (20 * e.detail.lineCount)
  301. }
  302. }
  303. }
  304. }
  305. </script>
  306. <style>
  307. .bottom{
  308. display: flex;
  309. position: fixed;
  310. bottom:0;
  311. left:0;
  312. width:100%;
  313. padding: 0 10px;
  314. box-sizing: border-box;
  315. align-items: center;
  316. justify-content: center;
  317. background:white;
  318. padding-bottom: constant(safe-area-inset-bottom);
  319. padding-bottom: env(safe-area-inset-bottom);
  320. }
  321. .inputArea {
  322. height: 40px;
  323. line-height: 20px;
  324. border:1px solid rgba(0, 0, 0, 0.2);
  325. border-radius: 6px;
  326. padding: 10px 5px;
  327. box-sizing: border-box;
  328. margin: 10px 0;
  329. flex: 1;
  330. }
  331. .bottom .button{
  332. width:60px;
  333. height:40px;
  334. margin-left:10px;
  335. display: flex;
  336. align-items: center;
  337. }
  338. .scorllView {
  339. height: calc(100vh - 104px);
  340. overflow: auto;
  341. padding:0 6px;
  342. box-sizing: border-box;
  343. padding-bottom: constant(safe-area-inset-bottom);
  344. padding-bottom: env(safe-area-inset-bottom);
  345. font-size: 12px;
  346. }
  347. .chatItem{
  348. margin: 20px 0;
  349. display: flex;
  350. }
  351. .chatItem:last-child {
  352. padding-bottom: 80px;
  353. }
  354. .head{
  355. width: 40px;
  356. height: 40px;
  357. border-radius: 50%;
  358. background: rgba(24, 160, 88, 0.28);
  359. display: flex;
  360. align-items: center;
  361. justify-content: center;
  362. }
  363. .chatItem.user .head{
  364. background: rgba(32, 128, 240, 0.16);
  365. }
  366. .content{
  367. flex: 1;
  368. width: 300px;
  369. }
  370. .content .time{
  371. font-size:12px;
  372. }
  373. .content .markdownView{
  374. background: white;
  375. border-radius: 12px;
  376. margin: 6px 40px 0 6px;
  377. display: inline-block;
  378. max-width: calc(100% - 60px);
  379. font-size:10px;
  380. }
  381. .chatItem.user .content{
  382. text-align: right;
  383. }
  384. .chatItem.gpt .content .time{
  385. padding-left:6px;
  386. }
  387. .chatItem.gpt .content .markdownView{
  388. margin: 6px 40px 0 6px;
  389. }
  390. .chatItem.user .content .time{
  391. text-align: right;
  392. padding-right:6px;
  393. }
  394. .chatItem.user .content .markdownView{
  395. margin: 6px 6px 0 40px;
  396. text-align: left;
  397. }
  398. .menuTop{
  399. display: block;
  400. height: 60px;
  401. width: 100%;
  402. }
  403. .menuTop .newChat{
  404. display: flex;
  405. align-items: center;
  406. justify-content: center;
  407. position:fixed;
  408. height: 60px;
  409. background: white;
  410. border-bottom: 1px solid #eee;
  411. width:100%;
  412. }
  413. .menuTop .newChat .newChatBtn{
  414. width: 80%;
  415. padding: 6px 0;
  416. text-align: center;
  417. border: 1px dashed #007aff;
  418. border-radius: 4px;
  419. color:#333;
  420. }
  421. .menuScroll{
  422. position:absolute;
  423. top: 60px;
  424. bottom: 80px;
  425. background:white;
  426. text-align: center;
  427. }
  428. .menuScroll .menuItem {
  429. display: inline-block;
  430. width: 80%;
  431. padding: 10px;
  432. border: 1px solid #eee;
  433. color: #333;
  434. border-radius: 4px;
  435. margin: 0 auto;
  436. display: flex;
  437. justify-content: space-between;
  438. align-items: center;
  439. margin-bottom: 10px;
  440. box-sizing: border-box;
  441. cursor: pointer;
  442. }
  443. .menuScroll .menuItem .text{
  444. white-space: nowrap;
  445. text-overflow:ellipsis;
  446. overflow:hidden;
  447. }
  448. .menuScroll .menuItem.active{
  449. border: 1px solid #007aff;
  450. color: #007aff;
  451. }
  452. .menuBottom{
  453. position: fixed;
  454. bottom:0;
  455. height: 80px;
  456. background:white;
  457. width:100%;
  458. display: flex;
  459. align-items: center;
  460. justify-content: center;
  461. }
  462. .menuBottom .clearAllChat{
  463. width: 80%;
  464. border:1px solid #eee;
  465. padding: 6px;
  466. text-align: center;
  467. color: #333;
  468. margin-bottom: constant(safe-area-inset-bottom);
  469. margin-bottom: env(safe-area-inset-bottom);
  470. }
  471. </style>