Index.vue 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. <template>
  2. <div class="chat-index">
  3. <!--左边选项-->
  4. <ul class="chat-menu">
  5. <li class="self">
  6. <img :src="userInfo.userimg">
  7. </li>
  8. <li :class="{active:chatTap=='dialog'}" @click="chatTap='dialog'"><Icon type="md-text" /></li>
  9. <li :class="{active:chatTap=='team'}" @click="chatTap='team'"><Icon type="md-person" /></li>
  10. </ul>
  11. <!--对话列表-->
  12. <ul v-if="chatTap=='dialog'" class="chat-user">
  13. <li class="sreach">
  14. <Input placeholder="搜索" prefix="ios-search"/>
  15. </li>
  16. <li v-for="(dialog, index) in dialogLists"
  17. :key="index"
  18. :class="{active:dialog.username==dialogTarget.username}"
  19. @click="openDialog(dialog)">
  20. <img :src="dialog.userimg">
  21. <div class="user-msg-box">
  22. <div class="user-msg-title">
  23. <span><user-view :username="dialog.username"/></span>
  24. <em>{{formatCDate(dialog.lastdate)}}</em>
  25. </div>
  26. <div class="user-msg-text">{{dialog.lasttext}}</div>
  27. </div>
  28. </li>
  29. <li v-if="dialogLists.length == 0" class="chat-none">{{dialogNoDataText}}</li>
  30. </ul>
  31. <!--联系人列表-->
  32. <ul v-else-if="chatTap=='team'" class="chat-team">
  33. <li class="sreach">
  34. <Input placeholder="搜索" prefix="ios-search"/>
  35. </li>
  36. <li v-for="(lists, key) in teamLists">
  37. <div class="team-label">{{key}}</div>
  38. <ul>
  39. <li v-for="(item, index) in lists" :key="index" @click="openDialog(item)">
  40. <img :src="item.userimg">
  41. <div class="team-username"><user-view :username="item.username"/></div>
  42. </li>
  43. </ul>
  44. </li>
  45. <li v-if="Object.keys(teamLists).length == 0" class="chat-none">{{teamNoDataText}}</li>
  46. </ul>
  47. <!--对话窗口-->
  48. <div v-if="chatTap=='dialog' && dialogTarget.username" class="chat-message">
  49. <div class="manage-title">
  50. <user-view :username="dialogTarget.username"/>
  51. <Dropdown class="manage-title-right" placement="bottom-end" trigger="click" transfer>
  52. <Icon type="ios-more"/>
  53. <DropdownMenu slot="list">
  54. <DropdownItem name="clear">清除聊天记录</DropdownItem>
  55. </DropdownMenu>
  56. </Dropdown>
  57. </div>
  58. <ScrollerY ref="manageLists" class="manage-lists" @on-scroll="messageListsScroll">
  59. <div ref="manageBody" class="manage-body">
  60. <chat-message v-for="(info, index) in messageLists" :key="index" :info="info"></chat-message>
  61. </div>
  62. <div class="manage-lists-message-new" v-if="messageNew > 0" @click="messageBottomGo(true)">有{{messageNew}}条新消息</div>
  63. </ScrollerY>
  64. <div class="manage-send">
  65. <textarea ref="textarea" class="manage-input" v-model="messageText" placeholder="请输入要发送的消息" @keydown="messageSend($event)"></textarea>
  66. </div>
  67. <div class="manage-quick">
  68. <Button type="primary" size="small">表情</Button>
  69. <Button type="warning" size="small">清除聊天记录</Button>
  70. </div>
  71. </div>
  72. </div>
  73. </template>
  74. <style lang="scss" scoped>
  75. .chat-index {
  76. display: flex;
  77. flex-direction: row;
  78. align-items: flex-start;
  79. position: absolute;
  80. top: 0;
  81. left: 0;
  82. right: 0;
  83. bottom: 0;
  84. .chat-none {
  85. height: auto;
  86. color: #666666;
  87. padding: 22px 8px;
  88. text-align: center;
  89. justify-content: center;
  90. &:before {
  91. display: none;
  92. }
  93. }
  94. .chat-menu {
  95. background-color: rgba(28, 29, 31, 0.92);
  96. width: 68px;
  97. height: 100%;
  98. padding-top: 20px;
  99. li {
  100. padding: 12px 0;
  101. text-align: center;
  102. font-size: 28px;
  103. color: #919193;
  104. background-color: transparent;
  105. cursor: pointer;
  106. &.self {
  107. img {
  108. width: 36px;
  109. height: 36px;
  110. border-radius: 3px;
  111. }
  112. }
  113. &.active {
  114. color: #ffffff;
  115. background-color: rgba(255, 255, 255, 0.06);
  116. }
  117. }
  118. }
  119. .chat-user {
  120. width: 248px;
  121. height: 100%;
  122. background-color: #ffffff;
  123. border-right: 1px solid #ededed;
  124. li {
  125. display: flex;
  126. flex-direction: row;
  127. align-items: center;
  128. height: 70px;
  129. padding: 0 12px;
  130. position: relative;
  131. cursor: pointer;
  132. &:before {
  133. content: "";
  134. position: absolute;
  135. left: 0;
  136. right: 0;
  137. bottom: 0;
  138. height: 1px;
  139. background-color: rgba(0, 0, 0, 0.06);
  140. }
  141. &.sreach {
  142. height: 62px;
  143. }
  144. &.active {
  145. &:before {
  146. top: 0;
  147. height: 100%;
  148. }
  149. }
  150. img {
  151. width: 42px;
  152. height: 42px;
  153. border-radius: 4px;
  154. }
  155. .user-msg-box {
  156. flex: 1;
  157. display: flex;
  158. flex-direction: column;
  159. padding-left: 12px;
  160. .user-msg-title {
  161. display: flex;
  162. flex-direction: row;
  163. align-items: center;
  164. justify-content: space-between;
  165. line-height: 24px;
  166. span {
  167. flex: 1;
  168. max-width: 130px;
  169. color: #333333;
  170. font-size: 14px;
  171. white-space: nowrap;
  172. overflow: hidden;
  173. text-overflow: ellipsis;
  174. }
  175. em {
  176. color: #999999;
  177. font-size: 12px;
  178. }
  179. }
  180. .user-msg-text {
  181. max-width: 170px;
  182. color: #999999;
  183. font-size: 12px;
  184. line-height: 24px;
  185. white-space: nowrap;
  186. overflow: hidden;
  187. text-overflow: ellipsis;
  188. }
  189. }
  190. }
  191. }
  192. .chat-team {
  193. width: 248px;
  194. height: 100%;
  195. background-color: #ffffff;
  196. border-right: 1px solid #ededed;
  197. > li {
  198. margin-left: 24px;
  199. position: relative;
  200. &.sreach {
  201. display: flex;
  202. flex-direction: row;
  203. align-items: center;
  204. height: 62px;
  205. margin: 0;
  206. padding: 0 12px;
  207. position: relative;
  208. cursor: pointer;
  209. &:before {
  210. content: "";
  211. position: absolute;
  212. left: 0;
  213. right: 0;
  214. bottom: 0;
  215. height: 1px;
  216. background-color: rgba(0, 0, 0, 0.06);
  217. }
  218. }
  219. .team-label {
  220. padding-left: 4px;
  221. margin-top: 6px;
  222. margin-bottom: 6px;
  223. height: 34px;
  224. line-height: 34px;
  225. border-bottom: 1px solid #efefef;
  226. }
  227. > ul {
  228. > li {
  229. display: flex;
  230. flex-direction: row;
  231. align-items: center;
  232. height: 52px;
  233. cursor: pointer;
  234. img {
  235. width: 30px;
  236. height: 30px;
  237. border-radius: 3px;
  238. }
  239. .team-username {
  240. padding: 0 12px;
  241. font-size: 14px;
  242. white-space: nowrap;
  243. overflow: hidden;
  244. text-overflow: ellipsis;
  245. }
  246. }
  247. }
  248. }
  249. }
  250. .chat-message {
  251. flex: 1;
  252. height: 100%;
  253. background-color: #F3F3F3;
  254. position: relative;
  255. .manage-title {
  256. position: absolute;
  257. top: 0;
  258. left: 0;
  259. z-index: 3;
  260. width: 100%;
  261. height: 62px;
  262. padding: 0 20px;
  263. line-height: 62px;
  264. font-size: 16px;
  265. font-weight: 500;
  266. text-align: left;
  267. background: #ffffff;
  268. border-bottom: 1px solid #ededed;
  269. .manage-title-right {
  270. position: absolute;
  271. top: 0;
  272. right: 0;
  273. z-index: 9;
  274. width: 62px;
  275. height: 62px;
  276. line-height: 62px;
  277. text-align: center;
  278. font-size: 22px;
  279. color: #242424;
  280. }
  281. }
  282. .manage-lists {
  283. position: absolute;
  284. left: 0;
  285. top: 62px;
  286. z-index: 1;
  287. bottom: 120px;
  288. width: 100%;
  289. overflow: auto;
  290. padding: 8px 0;
  291. background-color: #E8EBF2;
  292. .manage-lists-message-new {
  293. position: fixed;
  294. bottom: 130px;
  295. right: 20px;
  296. color: #ffffff;
  297. background-color: rgba(0, 0, 0, 0.6);
  298. padding: 6px 12px;
  299. border-radius: 16px;
  300. font-size: 12px;
  301. cursor: pointer;
  302. }
  303. }
  304. .manage-send {
  305. position: absolute;
  306. left: 0;
  307. bottom: 0;
  308. z-index: 2;
  309. display: flex;
  310. width: 100%;
  311. height: 120px;
  312. background-color: #ffffff;
  313. border-top: 1px solid #e4e4e4;
  314. .manage-input,.manage-input:focus {
  315. flex: 1;
  316. -webkit-appearance: none;
  317. font-size: 14px;
  318. box-sizing: border-box;
  319. padding: 0;
  320. margin: 38px 8px 6px;
  321. border: 0;
  322. line-height: 20px;
  323. box-shadow: none;
  324. resize:none;
  325. outline: 0;
  326. }
  327. .manage-join,
  328. .manage-spin {
  329. position: absolute;
  330. top: 0;
  331. left: 0;
  332. right: 0;
  333. bottom: 0;
  334. background: #ffffff;
  335. display: flex;
  336. align-items: center;
  337. justify-content: center;
  338. }
  339. }
  340. .manage-quick {
  341. position: absolute;
  342. z-index: 2;
  343. left: 0;
  344. right: 0;
  345. bottom: 79px;
  346. padding: 8px;
  347. > button,
  348. .quick-button {
  349. font-size: 12px;
  350. margin-right: 4px;
  351. }
  352. }
  353. @media screen and (max-width: 768px) {
  354. .manage-lists {
  355. bottom: 96px;
  356. .manage-lists-message-new {
  357. bottom: 106px;
  358. }
  359. }
  360. .manage-send {
  361. height: 96px;
  362. }
  363. .manage-quick {
  364. bottom: 54px;
  365. > button,
  366. .quick-button {
  367. margin-right: 2px;
  368. }
  369. }
  370. }
  371. }
  372. }
  373. </style>
  374. <script>
  375. import DrawerTabsContainer from "../DrawerTabsContainer";
  376. import ScrollerY from "../../../_components/ScrollerY";
  377. import ChatMessage from "./message";
  378. export default {
  379. name: 'ChatIndex',
  380. components: {ChatMessage, ScrollerY, DrawerTabsContainer},
  381. data () {
  382. return {
  383. loadIng: 0,
  384. userInfo: {},
  385. chatTap: 'dialog',
  386. dialogTarget: {},
  387. dialogLists: [],
  388. dialogNoDataText: '',
  389. teamReady: false,
  390. teamLists: {},
  391. teamNoDataText: '',
  392. autoBottom: true,
  393. messageNew: 0,
  394. messageText: '',
  395. messageLists: [],
  396. messageNoDataText: '',
  397. }
  398. },
  399. created() {
  400. this.dialogNoDataText = this.$L("数据加载中.....");
  401. this.teamNoDataText = this.$L("数据加载中.....");
  402. this.messageNoDataText = this.$L("数据加载中.....");
  403. },
  404. mounted() {
  405. this.userInfo = $A.getUserInfo((res) => {
  406. this.userInfo = res;
  407. }, false);
  408. //
  409. $A.WS.setOnMsgListener("chat/index", (msgDetail) => {
  410. if (msgDetail.sender == $A.getUserName()) {
  411. return;
  412. }
  413. if (msgDetail.messageType != 'send') {
  414. return;
  415. }
  416. //
  417. let data = $A.jsonParse(msgDetail.content);
  418. let lasttext;
  419. switch (data.type) {
  420. case 'text':
  421. lasttext = data['text'];
  422. break;
  423. case 'image':
  424. lasttext = '[图片]';
  425. break;
  426. default:
  427. lasttext = '[未知类型]';
  428. break;
  429. }
  430. this.openDialog({
  431. username: data.username,
  432. userimg: data.userimg,
  433. }, {
  434. lasttext: lasttext,
  435. lastdate: data.indate
  436. });
  437. if (msgDetail.sender == this.dialogTarget.username) {
  438. this.addMessageData(data, true);
  439. }
  440. })
  441. //
  442. this.getDialogLists();
  443. this.messageBottomAuto();
  444. },
  445. watch: {
  446. chatTap(val) {
  447. if (val === 'team' && this.teamReady == false) {
  448. this.teamReady = true;
  449. this.getTeamLists();
  450. }
  451. },
  452. dialogTarget: {
  453. handler: function () {
  454. this.getDialogMessage();
  455. },
  456. deep: true
  457. }
  458. },
  459. methods: {
  460. formatCDate(v) {
  461. let string = '';
  462. if ($A.runNum(v) > 0) {
  463. if ($A.formatDate('Ymd') === $A.formatDate('Ymd', v)) {
  464. string = $A.formatDate('H:i', v)
  465. } else if ($A.formatDate('Y') === $A.formatDate('Y', v)) {
  466. string = $A.formatDate('m-d H:i', v)
  467. } else {
  468. string = $A.formatDate('Y-m-d H:i', v)
  469. }
  470. }
  471. return string || '';
  472. },
  473. getDialogLists() {
  474. this.loadIng++;
  475. this.dialogNoDataText = this.$L("数据加载中.....");
  476. $A.aAjax({
  477. url: 'chat/dialog/lists',
  478. complete: () => {
  479. this.loadIng--;
  480. },
  481. error: () => {
  482. this.dialogNoDataText = this.$L("数据加载失败!");
  483. },
  484. success: (res) => {
  485. if (res.ret === 1) {
  486. this.dialogLists = res.data;
  487. this.dialogNoDataText = this.$L("没有相关的数据");
  488. } else {
  489. this.dialogLists = [];
  490. this.dialogNoDataText = res.msg
  491. }
  492. }
  493. });
  494. },
  495. getDialogMessage() {
  496. let username = this.dialogTarget.username;
  497. if (username === this.__dialogTargetUsername) {
  498. return;
  499. }
  500. this.__dialogTargetUsername = username;
  501. this.autoBottom = true;
  502. this.messageNew = 0;
  503. this.messageLists = [];
  504. //
  505. this.loadIng++;
  506. this.messageNoDataText = this.$L("数据加载中.....");
  507. $A.aAjax({
  508. url: 'chat/message/lists',
  509. data: {
  510. username: username,
  511. pagesize: 30
  512. },
  513. complete: () => {
  514. this.loadIng--;
  515. },
  516. error: () => {
  517. this.messageNoDataText = this.$L("数据加载失败!");
  518. },
  519. success: (res) => {
  520. if (username != this.dialogTarget.username) {
  521. return;
  522. }
  523. if (res.ret === 1) {
  524. res.data.lists.reverse().forEach((item) => {
  525. this.addMessageData(Object.assign(item.message, {
  526. id: item.id,
  527. }));
  528. });
  529. this.messageNoDataText = this.$L("没有相关的数据");
  530. } else {
  531. this.messageNoDataText = res.msg
  532. }
  533. }
  534. });
  535. },
  536. getTeamLists() {
  537. this.loadIng++;
  538. this.teamNoDataText = this.$L("数据加载中.....");
  539. $A.aAjax({
  540. url: 'users/team/lists',
  541. data: {
  542. sorts: {
  543. key: 'username',
  544. order: 'asc'
  545. },
  546. firstchart: 1,
  547. pagesize: 100,
  548. },
  549. complete: () => {
  550. this.loadIng--;
  551. },
  552. error: () => {
  553. this.teamNoDataText = this.$L("数据加载失败!");
  554. },
  555. success: (res) => {
  556. if (res.ret === 1) {
  557. res.data.lists.forEach((item) => {
  558. if (typeof this.teamLists[item.firstchart] === "undefined") {
  559. this.$set(this.teamLists, item.firstchart, []);
  560. }
  561. this.teamLists[item.firstchart].push(item);
  562. console.log(this.teamLists);
  563. });
  564. this.teamNoDataText = this.$L("没有相关的数据");
  565. } else {
  566. this.teamLists = {};
  567. this.teamNoDataText = res.msg
  568. }
  569. }
  570. });
  571. },
  572. openDialog(user, lastMsg) {
  573. if (typeof lastMsg === "object") {
  574. user = Object.assign(user, lastMsg);
  575. this.dialogLists = this.dialogLists.filter((item) => {return item.username != user.username});
  576. }
  577. let lists = this.dialogLists.filter((item) => {return item.username == user.username});
  578. if (lists.length === 0) {
  579. this.dialogLists.unshift(user);
  580. }
  581. if (typeof lastMsg !== "object") {
  582. this.chatTap = 'dialog';
  583. this.dialogTarget = user;
  584. }
  585. },
  586. messageListsScroll(res) {
  587. if (res.directionreal === 'up') {
  588. if (res.scrollE < 10) {
  589. this.autoBottom = true;
  590. }
  591. } else if (res.directionreal === 'down') {
  592. this.autoBottom = false;
  593. }
  594. },
  595. messageBottomAuto() {
  596. let randString = $A.randomString(8);
  597. window.__messageBottomAuto = randString;
  598. setTimeout(() => {
  599. if (randString === window.__messageBottomAuto) {
  600. window.__messageBottomAuto = null;
  601. if (this.autoBottom) {
  602. this.messageBottomGo();
  603. }
  604. this.messageBottomAuto();
  605. }
  606. }, 1200);
  607. },
  608. messageBottomGo(animation = false) {
  609. this.$nextTick(() => {
  610. this.messageNew = 0;
  611. if (typeof this.$refs.manageLists !== "undefined") {
  612. this.$refs.manageLists.scrollTo(this.$refs.manageBody.clientHeight, animation);
  613. this.autoBottom = true;
  614. }
  615. });
  616. },
  617. addMessageData(data, animation = false) {
  618. data.self = data.username === this.userInfo.username;
  619. this.messageLists.push(data);
  620. //
  621. if (this.autoBottom) {
  622. this.messageBottomGo(animation);
  623. } else {
  624. this.messageNew++;
  625. }
  626. },
  627. messageSend(e) {
  628. if (e.keyCode == 13) {
  629. if (e.shiftKey) {
  630. return;
  631. }
  632. e.preventDefault();
  633. this.messageSubmit();
  634. }
  635. },
  636. messageSubmit() {
  637. let text = this.messageText.trim();
  638. if ($A.count(text) > 0) {
  639. let data = {
  640. id: 0,
  641. type: 'text',
  642. username: this.userInfo.username,
  643. userimg: this.userInfo.userimg,
  644. indate: Math.round(new Date().getTime() / 1000),
  645. text: text
  646. };
  647. $A.WS.sendTo('user', this.dialogTarget.username, data, (res) => {
  648. this.$set(data, res.status === 1 ? 'id' : 'error', res.message)
  649. });
  650. //
  651. this.openDialog(this.dialogTarget, {
  652. lasttext: text,
  653. lastdate: data.indate
  654. });
  655. this.addMessageData(data, true);
  656. }
  657. this.$nextTick(() => {
  658. this.messageText = "";
  659. });
  660. },
  661. }
  662. }
  663. </script>