chat.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <template>
  2. <view class="page page--divider">
  3. <view style="display: none;">{{$store.state.hackUpdate}}</view>
  4. <load-more status="loading" v-if="initializing" />
  5. <chat-panel :store-key="storeKey" :thistype="thistype" :atme="atme" :at_msg_id='at_msg_id' @close='atuser1'
  6. :sending="sending" :sendingdata="sendingdata" :manager="ismanage" :no_add="no_add"
  7. @click-avatar="openProfile" @setSending='setSending' :boardheight='boardheight' ref="panel"
  8. @chooseQuote="setQuote" />
  9. <view class="placeholder"></view>
  10. <v-sender ref='sender' v-if="!initializing" v-model="message" @send="sendTextMessage" :groupid="group.id"
  11. :isgroup="1" @face="sendVoice" @sendImage="sendImg" @sendVedio="sendVedio" @emoji="sendEmoji"
  12. @clearQute="clearQute" :no-speaking="no_speaking" :speaking_tips='speaking_tips'
  13. :forbid-redpack="group.forbid_redpack==1 && !group.owner && !ismanage?true:false"
  14. @handleEmotion="handleEmotion" @input="inputMessage" @keyboardheight='keyboardheight'
  15. @senderheight="set_sheight" :sendtype='type' />
  16. <GroupUser ref='groupuser' :groupid='group.id' :show='groupshow' @atuser='atuser'></GroupUser>
  17. </view>
  18. </template>
  19. <script>
  20. import GroupUser from '../../components/GroupUser.vue';
  21. import ChatPanel from '../../components/chat-panel.vue'
  22. import chat from "../../library/chat.js"
  23. import {
  24. mapState,
  25. mapGetters
  26. } from 'vuex';
  27. import api from "../../library/index.js"
  28. import config from "../../config.js"
  29. export default {
  30. components: {
  31. ChatPanel,
  32. GroupUser
  33. },
  34. data() {
  35. return {
  36. quoteItem: {}, //引用内容
  37. userid: uni.getStorageSync('access_token'),
  38. user: uni.getStorageSync('userInfo'),
  39. group: {},
  40. message: '',
  41. initializing: false,
  42. showPickMoney: '',
  43. getPick: '',
  44. pickErrorText: '',
  45. redpackInfo: {},
  46. sending: false,
  47. sendingdata: {},
  48. ismanage: false,
  49. no_speaking: false,
  50. no_add: false,
  51. sender: [],
  52. receiver: [],
  53. boardheight: 0,
  54. groupshow: 0,
  55. oldstr: '',
  56. atusers: {},
  57. type: '',
  58. speaking_tips: '',
  59. atme: 0,
  60. at_msg_id: 0,
  61. thistype: '',
  62. storeKey: '',
  63. list: [
  64. ['微笑', '撇嘴', '色', '发呆', '得意', '流泪', '害羞', '闭嘴'],
  65. ['睡', '大哭', '尴尬', '发怒', '调皮', '呲牙', '惊讶', '难过'],
  66. ['酷', '冷汗', '抓狂', '吐', '偷笑', '可爱', '白眼', '傲慢'],
  67. ['饥饿', '困', '惊恐', '流汗', '憨笑', '大兵', '奋斗', '咒骂'],
  68. ['疑问', '嘘', '晕', '折磨', '衰', '骷髅', '敲打', '再见'],
  69. ['擦汗', '抠鼻', '鼓掌', '糗大了', '坏笑', '左哼哼', '右哼哼', '哈欠'],
  70. ['鄙视', '委屈', '快哭了', '阴险', '亲亲', '吓', '可怜', '菜刀'],
  71. ['西瓜', '啤酒', '篮球', '乒乓', '咖啡', '饭', '猪头', '玫瑰', ],
  72. ['凋谢', '示爱', '爱心', '心碎', '蛋糕', '闪电', '炸弹', '刀'],
  73. ['足球', '瓢虫', '便便', '月亮', '太阳', '礼物', '拥抱', '强'],
  74. ['弱', '握手', '胜利', '抱拳', '勾引', '拳头', '差劲', '爱你'],
  75. ['NO', 'OK', '爱情', '飞吻', '跳跳', '发抖', '怄火', '转圈'],
  76. ['磕头', '回头', '跳绳', '挥手', '激动', '闭嘴', '笑哭', '吐舌'],
  77. ['耶', '跳舞', '恐惧', '失望', '脸红', '无语', '奸笑', '嘿哈'],
  78. ['鬼混', '福', '合十', '强壮', '红包', '发财', '庆祝', '礼物']
  79. ],
  80. reg: /\S{1,3}/gi,
  81. lists: [],
  82. listss: []
  83. };
  84. },
  85. onLoad(opts) {
  86. // console.log(opts);
  87. if (opts.atme > 0) this.atme = opts.atme
  88. if (opts.msg_id > 0) this.at_msg_id = opts.msg_id
  89. uni.setNavigationBarTitle({
  90. title: decodeURI(opts.nickname)
  91. })
  92. this.group = opts;
  93. this.group.id = parseInt(this.group.id);
  94. this.storeKey = 'G' + this.group.id;
  95. uni.setStorageSync('cache_key', this.storeKey);
  96. this.joingroup();
  97. // console.log(opts);
  98. this.$socket.on('group_update', (res) => {
  99. this.getInfo();
  100. this.getGroupUsers();
  101. })
  102. this.sender = uni.getStorageSync('userInfo');
  103. // #ifdef APP-PLUS
  104. let currentWebview = this.$mp.page.$getAppWebview();
  105. currentWebview.setStyle({
  106. titleNView: {
  107. titleText: ''
  108. },
  109. })
  110. // #endif
  111. },
  112. onUnload() {
  113. uni.setStorageSync('cache_key', '');
  114. },
  115. onShow() {
  116. this.getInfo();
  117. this.getGroupUsers();
  118. var value = '';
  119. this.list = this.list.map((line, index) => {
  120. this.listss = [];
  121. line = line.map((item, idx) => {
  122. value = item;
  123. item = item.replace(/\S{1,3}/gi, this.getemotion(item));
  124. this.listss.push({
  125. "name": item,
  126. "index": index,
  127. "value": value,
  128. "idx": idx
  129. })
  130. return item;
  131. })
  132. this.lists.push(this.listss);
  133. return line;
  134. })
  135. console.log(this.list, 'this.list');
  136. },
  137. methods: {
  138. getemotion(res) {
  139. let word = res.replace(/\[|\]/gi, '')
  140. const list = ['微笑', '撇嘴', '色', '发呆', '得意', '流泪', '害羞', '闭嘴', '睡', '大哭', '尴尬', '发怒', '调皮', '呲牙', '惊讶',
  141. '难过', '酷', '冷汗', '抓狂', '吐', '偷笑', '可爱', '白眼', '傲慢', '饥饿', '困', '惊恐', '流汗', '憨笑', '大兵', '奋斗',
  142. '咒骂', '疑问', '嘘', '晕', '折磨', '衰', '骷髅', '敲打', '再见', '擦汗', '抠鼻', '鼓掌', '糗大了', '坏笑', '左哼哼', '右哼哼',
  143. '哈欠', '鄙视', '委屈', '快哭了', '阴险', '亲亲', '吓', '可怜', '菜刀', '西瓜', '啤酒', '篮球', '乒乓', '咖啡', '饭', '猪头',
  144. '玫瑰', '凋谢', '示爱', '爱心', '心碎', '蛋糕', '闪电', '炸弹', '刀', '足球', '瓢虫', '便便', '月亮', '太阳', '礼物', '拥抱',
  145. '强', '弱', '握手', '胜利', '抱拳', '勾引', '拳头', '差劲', '爱你', 'NO', 'OK', '爱情', '飞吻', '跳跳', '发抖', '怄火',
  146. '转圈', '磕头', '回头', '跳绳', '挥手', '激动',
  147. '闭嘴', '笑哭', '吐舌', '耶', '跳舞', '恐惧', '失望', '脸红', '无语', '奸笑', '嘿哈', '鬼混', '福', '合十', '强壮', '红包',
  148. '发财', '庆祝', '礼物'
  149. ]
  150. let index = list.indexOf(word);
  151. var index1 = index + 100;
  152. return '^' + index1 + '^';
  153. },
  154. // 清除qute
  155. clearQute() {
  156. this.quoteItem = {}
  157. },
  158. setQuote(item) {
  159. if (item.message.type == 'text') {
  160. if (!item.message.content.type) {
  161. item.ttype = 'text'
  162. // 空格替换回车
  163. item.message.qute = item.message.content.replace(/<br>/g, " ").replace(/&nbsp;/g, ' ')
  164. } else if (item.message.content.type == "remind") {
  165. // 去除html 标签 && 去除 回车替换成空格
  166. if(item.message.content.remind && item.message.content.remind.nickname) {
  167. item.message.qute = item.message.content.content.replace(/<span.*?>.*?<\/span>/g, '@' + item.message.content.remind.nickname).replace(
  168. /<br>/g, " ").replace(/&nbsp;/g, ' ')
  169. }else {
  170. item.message.qute = item.message.content.content.replace(/<span.*?>|<\/span>/g, '@' + '').replace(
  171. /<br>/g, " ").replace(/&nbsp;/g, ' ')
  172. }
  173. item.ttype = 'remind'
  174. }
  175. }
  176. if (item.message.type == 'emotion') {
  177. item.ttype = 'emotion'
  178. console.log(item.message.content,'item.message.content');
  179. item.message.qute = item.message.content.content.replace(/<br>/g, " ")
  180. var content = item.message.qute.replace(/\[\S{1,3}\]/gi, this.getemotion).split('^');
  181. var imgs = [];
  182. for (var i = 0; i < content.length; i++) {
  183. if (content[i] >= 100 && content[i] <= 222) {
  184. imgs.push({
  185. 'type': 'img',
  186. 'content': "/static/emoji/" + content[i] + '.gif'
  187. })
  188. } else {
  189. imgs.push({
  190. 'type': 'text',
  191. 'content': content[i]
  192. })
  193. }
  194. }
  195. item.message.face = imgs;
  196. console.log('content', content);
  197. }
  198. if(item.message.type == 'image') {
  199. console.log('引用图片');
  200. item.ttype = 'image'
  201. item.message.qute = item.message.content
  202. }
  203. this.$refs.sender.setQute(item)
  204. this.quoteItem = item
  205. console.log("设置item", item);
  206. },
  207. // 检测输入值
  208. inputMessage(e) {
  209. // console.log(e);
  210. //console.log(this.oldstr,e);
  211. if (e.substr(e.length - 1, 1) == '@' && this.oldstr != e && e.length > 0 && e.length > this.oldstr
  212. .length) {
  213. this.groupshow = 1;
  214. uni.hideKeyboard();
  215. } else {
  216. this.groupshow = 0;
  217. }
  218. this.oldstr = e;
  219. this.emotion = e;
  220. },
  221. atuser(e) {
  222. this.groupshow = 0;
  223. if (e.nickname) {
  224. this.$refs.sender.setinputvalue(e.nickname);
  225. this.atusers = e;
  226. }
  227. },
  228. atuser1(e) {
  229. console.log(e);
  230. if (e.nickname) {
  231. this.$refs.sender.setinputvalue('@' + e.nickname);
  232. this.atusers = e;
  233. }
  234. },
  235. set_sheight(e) {
  236. this.$refs.panel.setAutoHeight(e);
  237. },
  238. keyboardheight(e) {
  239. this.boardheight = e;
  240. this.$refs.panel.setAutoHeight(0);
  241. },
  242. setSending(e) {
  243. this.sending = e;
  244. },
  245. group_init(id) {
  246. var group = uni.getStorageSync('group_' + id);
  247. if (group.id > 0 && group.id == id) this.group_set(group);
  248. },
  249. group_set(group) {
  250. this.group = group;
  251. if (group.deny_id == null) group.deny_id = '';
  252. if (group.manager_id == null) group.manager_id = '';
  253. if (this.user.status == 1 || (this.user.status == 2 && parseInt(this.user.lock_time) >= Date.parse(
  254. new Date()) / 1000)) {
  255. this.no_speaking = true;
  256. this.speaking_tips = '您的账号已被冻结';
  257. } else if ((group.no_speaking == 1) && !group.owner && group.manager_id.indexOf(this.userid) <= -1) {
  258. this.no_speaking = true;
  259. this.speaking_tips = '全体禁言,限群主和管理员可发消息';
  260. } else if ((group.deny_id.indexOf(this.userid) > -1) && !group.owner && group.manager_id.indexOf(this
  261. .userid) <= -1) {
  262. this.no_speaking = true;
  263. this.speaking_tips = '您已被禁言';
  264. } else {
  265. this.no_speaking = false;
  266. }
  267. if (group.manager_id.indexOf(this.userid) > -1 || group.owner) this.ismanage = true;
  268. else this.ismanage = false;
  269. if (group.is_owner == 1) var type = 'owner';
  270. else if (group.is_manager == 1) var type = 'manager';
  271. else var type = 'user';
  272. this.thistype = type;
  273. // console.log(this.group);
  274. if ((group.no_add == 1) && this.ismanage == false)
  275. this.no_add = true;
  276. else this.no_add = false;
  277. uni.setNavigationBarTitle({
  278. title: `${group.name}(${group.people_count}人)`
  279. })
  280. uni.setStorageSync('group_' + group.id, group);
  281. },
  282. getInfo() {
  283. if (this.group.id > 0) {
  284. this.group_init(this.group.id);
  285. api.getGroupInfo({
  286. group_id: this.group.id,
  287. from_id: this.userid,
  288. }).then(res => {
  289. this.group_set(res.data);
  290. })
  291. }
  292. },
  293. handleEmotion(value) {
  294. //console.log(value);
  295. this.emotion = value;
  296. this.type = 'emotion';
  297. },
  298. joingroup() {
  299. let data = {
  300. GroupId: this.group.id,
  301. type: 'joinGroup'
  302. }
  303. this.getGroupUsers()
  304. this.$socket.send(data);
  305. },
  306. getGroupUsers() {
  307. api.getGroupUsers({
  308. group_id: this.group.id,
  309. userid: this.userid,
  310. }).then(res => {
  311. uni.setStorageSync(this.group.id + '_group_members', res.data);
  312. })
  313. },
  314. toHtml(str) {
  315. str = str.replace(/ /g, "&nbsp;");
  316. str = str.replace(/\n/g, "<br>");
  317. return str;
  318. },
  319. sendImg(e) {
  320. this.commitMessage('image', e);
  321. },
  322. sendVedio(e) {
  323. this.commitMessage('vedio', e);
  324. },
  325. // 发送文本消息
  326. sendTextMessage() {
  327. let that = this;
  328. let addStr = '';
  329. console.log(that.quoteItem,'发送that.quoteItem');
  330. if (this.quoteItem && this.quoteItem.id) {
  331. addStr += '-qute-nickname=' + that.quoteItem.sender.nickname + 'q&qcontent=' + that.quoteItem
  332. .message
  333. .qute + 'q&qttype=' + that.quoteItem.ttype
  334. }
  335. if (this.type == "emotion") {
  336. this.emotionInfo = {
  337. "type": "emotion",
  338. "value": this.emotion + addStr,
  339. "content": this.emotion + addStr
  340. }
  341. console.log('-------------表情---------------', this.emotionInfo);
  342. this.commitMessage('emotion', this.emotionInfo);
  343. } else {
  344. var msg = this.toHtml(this.message) + addStr;
  345. console.log('---------文本发送---------', msg);
  346. this.commitMessage('text', msg,1);
  347. }
  348. this.quoteItem = {}
  349. this.$refs.sender.clearQute()
  350. // this.commitMessage('text', this.message);
  351. },
  352. commitMessage(type, sendData,ttype=0) {
  353. this.message = '';
  354. if (type == 'image') {
  355. var mid = sendData.mid;
  356. sendData = sendData.src;
  357. } else if (type == 'vedio') {
  358. var mid = sendData.mid;
  359. // sendData=sendData.src;
  360. } else if (type == 'voice') {
  361. var mid = sendData.mid;
  362. } else {
  363. var mid = 'm' + Math.random().toString(36).substring(2);
  364. }
  365. var atuser = 0;
  366. if (type == 'text' && parseInt(this.atusers.id) > 0) {
  367. if (sendData.indexOf(this.atusers.nickname) > -1) {
  368. let arr = sendData.split('-qute-')
  369. if(arr[0].indexOf('@' + this.atusers.nickname) != -1) {
  370. console.log('包含');
  371. var content = sendData.replace('@' + this.atusers.nickname, '@{' + this.atusers.id + '}');
  372. }else {
  373. var content = sendData
  374. }
  375. sendData = {
  376. type: 'remind',
  377. remind: this.atusers,
  378. content: content
  379. }
  380. this.oldstr = '';
  381. this.atusers = {};
  382. }
  383. }
  384. if (type == 'text' && sendData == '') return false;
  385. let data = {
  386. userid: this.userid,
  387. group_id: this.group.id,
  388. type: 'group',
  389. msgtype: type,
  390. content: sendData,
  391. mid: mid
  392. }
  393. var sendlocal = 0;
  394. if ((type == 'image' && sendData.indexOf(config.imgUri) <= -1) || (type == 'vedio' && sendData.src
  395. .indexOf(config.imgUri) <= -1) || (type == 'voice' && sendData.url.indexOf(config.imgUri) <= -
  396. 1)) {
  397. sendlocal = 1;
  398. } else {
  399. this.$socket.send(data);
  400. }
  401. this.type = 'text';
  402. if (type !== 'image' && type !== 'voice') sendlocal = 1;
  403. var that = this;
  404. if (sendlocal == 1) {
  405. // setTimeout(function(){
  406. var arr = {};
  407. arr.id = that.group.id;
  408. arr.cache_key = 'G' + arr.id;
  409. arr['self'] = 1;
  410. arr['isloading'] = 1;
  411. arr['receiver_id'] = 0
  412. arr['sender_id'] = that.sender.id;
  413. arr['group_id'] = that.group.id;
  414. arr['timestamp'] = parseInt(new Date().getTime() / 1000);
  415. arr['time'] = '';
  416. arr['message'] = {
  417. type: type,
  418. content: sendData
  419. }
  420. arr['sender'] = that.sender;
  421. arr['receiver'] = that.group;
  422. arr['nickname'] = that.sender.nickname;
  423. arr['avatar'] = that.sender.avatar;
  424. arr['msg_id'] = '';
  425. arr['_mid'] = mid;
  426. // that.$refs.panel.sendmsg({ data:arr});
  427. that.sendingdata = arr;
  428. that.sending = true;
  429. // console.log(arr)
  430. // that.sendmsg(arr);
  431. // },20,that,mid)
  432. }
  433. },
  434. sendVoice(e) {
  435. this.commitMessage('voice', e);
  436. },
  437. sendEmoji(e) {
  438. this.commitMessage('face', e)
  439. },
  440. openProfile(e) {
  441. let params = {
  442. id: e.id,
  443. status: 'friend'
  444. };
  445. return this.$jump('friend.detail', params)
  446. }
  447. },
  448. onNavigationBarButtonTap() {
  449. this.$refs.panel.endVedio();
  450. this.$jump('group.detail', {
  451. id: this.group.id,
  452. 'from': 'chat'
  453. });
  454. }
  455. }
  456. </script>