cardChat.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <template>
  2. <view>
  3. <uni-menubar :title="nickeName" />
  4. <tm-loadding style="width: 100%;position: fixed;top: 180rpx;" v-if="ajax.loading" :label="ajax.loadText"></tm-loadding>
  5. <view style="background-color: #fff;width: 100%;position: fixed;top: 180rpx;z-index: 999;">
  6. <tm-grid :grid="3" @change="tabChange" color="blue" :list="tabList"></tm-grid>
  7. </view>
  8. <view @click="setUpShow = false" class="cu-chat">
  9. <view class="" style="height: 200rpx;"></view>
  10. <view class="px-15" v-for="(item,index) in chatList" :key="index" :id="`msg-${item.id}`">
  11. <view v-if="item.type == 5">
  12. <view v-if="item.user.uid === userInfo.uid"
  13. style="text-align: center;font-size: 24rpx;color: #9E9E9E;margin: 40rpx 0;">
  14. 已向对方发起交换电话
  15. </view>
  16. <view class="px-50" v-else>
  17. <tm-sheet :margin="[50,32]">
  18. <view class="" style="font-size: 28rpx;">
  19. 对方想获取你的电话?
  20. </view>
  21. <view style="display: flex;justify-content: flex-end;margin-top: 20rpx;">
  22. <tm-button open-type="getPhoneNumber" @getphonenumber="getNumber" theme="blue" size="s">
  23. 同意</tm-button>
  24. </view>
  25. </tm-sheet>
  26. </view>
  27. </view>
  28. <view v-else-if="item.type == 6">
  29. <view v-if="item.user.uid === userInfo.uid"
  30. style="text-align: center;font-size: 24rpx;color: #9E9E9E;margin: 40rpx 0;">
  31. 已向对方发起交换微信
  32. </view>
  33. <view v-else style="text-align: center;font-size: 24rpx;color: #9E9E9E;margin: 40rpx 0;">
  34. 对方想要获取你的微信
  35. </view>
  36. </view>
  37. <view v-else class="rowFlex mt-30 ">
  38. <view v-if="item.user.uid === userInfo.uid" class="cu-item self">
  39. <view class="main">
  40. <view class="action text-grey" v-if="item.state === 0">
  41. <text class="text-sm margin-left-sm">未读</text>
  42. </view>
  43. <view v-if="item.type === 0" class="content bg-blue shadow">
  44. <text>
  45. {{item.msg}}
  46. </text>
  47. </view>
  48. <image class="radius" mode="widthFix" v-if="item.type === 1" :src="item.msg">
  49. </image>
  50. </view>
  51. <view class="cu-avatar radius">
  52. <tm-avatar titl outlined :src="item.user.avatar" :size="80"></tm-avatar>
  53. </view>
  54. <view class="date">{{item.create_time}}</view>
  55. </view>
  56. <view v-else class="cu-item">
  57. <view class="cu-avatar radius">
  58. <tm-avatar titl outlined :src="item.user.avatar" :size="80"></tm-avatar>
  59. </view>
  60. <view class="main">
  61. <view v-if="item.type === 0" class="content bg-blue shadow">
  62. <text>
  63. {{item.msg}}
  64. </text>
  65. </view>
  66. <image class="radius" mode="widthFix" v-if="item.type === 1" :src="item.msg">
  67. </image>
  68. </view>
  69. <view class="date">{{item.create_time}}</view>
  70. </view>
  71. </view>
  72. </view>
  73. </view>
  74. <view class="cu-bar foot input">
  75. <input class="solids-bottom" style="min-height: 100rpx;" adjust-position :focus="false" maxlength="300" cursor-spacing="10"
  76. v-model="inputInfo" placeholder="和TA说点什么吧"></input>
  77. <view class="action">
  78. <view v-if="inputInfo.length>0">
  79. <button class="cu-btn blue shadow" @click="sendOut(0,inputInfo)">发送</button>
  80. </view>
  81. <view v-else @click="setUpShow = !setUpShow">
  82. <tm-icons color="grey-lighten" :size="50" name="fa-plus-square-o"></tm-icons>
  83. </view>
  84. </view>
  85. <view v-if="setUpShow" class="setUp" style="width: 100%;">
  86. <tm-sheet :shadow="24">
  87. <tm-grid :grid="4" @change="change" color="blue" :list="setList"></tm-grid>
  88. </tm-sheet>
  89. </view>
  90. </view>
  91. <view class="cu-tabbar-height"></view>
  92. <tm-dialog v-model="dialogShow" confirmColor="blue" :over-close="false" @confirm="dialogClick"
  93. :content="dialogContent"></tm-dialog>
  94. </view>
  95. </template>
  96. <script>
  97. export default {
  98. data() {
  99. return {
  100. ajax: {
  101. rows:20, //每页数量
  102. page: 1, //页码
  103. flag: true, // 请求开关
  104. loading: true, // 加载中
  105. loadText: '正在获取消息'
  106. },
  107. dialogShow: false,
  108. dialogContent: "",
  109. scrollTop: '',
  110. isHistoryLoading: false,
  111. scrollAnimation: false,
  112. scrollToView: '',
  113. inputInfo: "",
  114. loading: true,
  115. setUpShow: false,
  116. setList: [{
  117. icon: 'icon-picture',
  118. text: '相册',
  119. color: 'grey-darken-1',
  120. fontColor: 'grey-darken-1'
  121. },
  122. {
  123. icon: 'icon-mobile-alt',
  124. text: '相机',
  125. color: 'grey-darken-1',
  126. fontColor: 'grey-darken-1'
  127. },
  128. ],
  129. chatID: '',
  130. toUid: "",
  131. nickeName: "",
  132. page: 1,
  133. chatList: [],
  134. tabList: [{
  135. icon: 'icon-phone',
  136. text: '打电话',
  137. color: 'blue'
  138. },
  139. {
  140. icon: 'icon-headset',
  141. text: '换电话',
  142. color: 'yellow'
  143. },
  144. {
  145. icon: 'icon-aliwangwang',
  146. text: '换微信',
  147. color: 'green'
  148. },
  149. ],
  150. custMobile: "",
  151. isStr: '',
  152. contentViewHeight: 0,
  153. }
  154. },
  155. created() {
  156. let that = this
  157. uni.$on('onMessage', function(data) {
  158. that.chatList.push(data.data)
  159. that.$nextTick(() => {
  160. uni.pageScrollTo({
  161. scrollTop: 999999, // 设置一个超大值,以保证滚动条滚动到底部
  162. duration: 300
  163. });
  164. });
  165. })
  166. },
  167. async onLoad(option) {
  168. if (option) {
  169. this.toUid = option.t_uid
  170. this.nickeName = option.name
  171. let info = await this.$tm.request.post('chat/createroom', {
  172. to_uid: option.t_uid,
  173. type: 1
  174. })
  175. this.chatID = info.data.id
  176. if (option.card_id) {
  177. this.getCardDetail(option.card_id)
  178. }
  179. this.$nextTick(() => {
  180. this.getHistoryMsg();
  181. });
  182. }
  183. },
  184. onPageScroll(e) {
  185. if (e.scrollTop < 50) {
  186. this.getHistoryMsg();
  187. }
  188. },
  189. computed: {
  190. userInfo() {
  191. return this.$tm.vx.state().user.userInfo || {}
  192. }
  193. },
  194. methods: {
  195. // 获取历史消息
  196. async getHistoryMsg() {
  197. if (!this.ajax.flag) {
  198. return; //
  199. }
  200. this.hideLoadTips();
  201. this.ajax.flag = false;
  202. let data = await this.$tm.request.post('chat/roomhistorylist', {
  203. chat_id: this.chatID,
  204. page: this.ajax.page
  205. })
  206. data = data.data.data
  207. // 获取待滚动元素选择器,解决插入数据后,滚动条定位时使用
  208. let selector = '';
  209. if (this.ajax.page > 1) {
  210. // 非第一页,则取历史消息数据的第一条信息元素
  211. selector = `#msg-${this.chatList[0].id}`;
  212. } else {
  213. // 第一页,则取当前消息数据的最后一条信息元素
  214. selector = `#msg-${data[data.length-1].id}`;
  215. }
  216. // 将获取到的消息数据合并到消息数组中
  217. this.chatList = [...data, ...this.chatList];
  218. // 数据挂载后执行,不懂的请自行阅读 Vue.js 文档对 Vue.nextTick 函数说明。
  219. this.$nextTick(() => {
  220. // 设置当前滚动的位置
  221. this.setPageScrollTo(selector);
  222. this.hideLoadTips(true);
  223. if (data.length < this.ajax.rows) {
  224. } else {
  225. this.ajax.page++;
  226. // 延迟 200ms ,以保证设置窗口滚动已完成
  227. setTimeout(() => {
  228. this.ajax.flag = true;
  229. }, 200)
  230. }
  231. })
  232. },
  233. hideLoadTips(flag) {
  234. if (flag) {
  235. this.ajax.loadText = '消息获取成功';
  236. setTimeout(() => {
  237. this.ajax.loading = false;
  238. }, 300);
  239. } else {
  240. this.ajax.loading = true;
  241. this.ajax.loadText = '正在获取消息';
  242. }
  243. },
  244. // 设置页面滚动位置
  245. setPageScrollTo(selector) {
  246. let view = uni.createSelectorQuery().in(this).select(selector);
  247. view.boundingClientRect((res) => {
  248. uni.pageScrollTo({
  249. scrollTop: res.top - 200, // -30 为多显示出大半个消息的高度,示意上面还有信息。
  250. duration: 0
  251. });
  252. }).exec();
  253. },
  254. // 获取名片详情
  255. getCardDetail(id) {
  256. this.$tm.request.post('card/viewread', {
  257. card_id: id
  258. }).then(res => {
  259. if (res.data.card.mobile !== '') {
  260. this.custMobile = res.data.card.mobile
  261. } else {
  262. this.tabList[0].color = "grey";
  263. this.tabList[0].fontColor = "grey"
  264. }
  265. })
  266. },
  267. change(e) {
  268. let that = this
  269. if (e.index === 0) {
  270. uni.chooseImage({
  271. count: 9, //默认9
  272. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  273. sourceType: ['album'], //从相册选择
  274. success: function(res) {
  275. let list = res.tempFilePaths
  276. list.forEach(val => {
  277. uni.uploadFile({
  278. url: 'https://cardoa.platomix.net/api/upload', //仅为示例,非真实的接口地址
  279. filePath: val,
  280. name: 'file',
  281. header: {
  282. 'Authorization': uni.getStorageSync('token'),
  283. },
  284. success: (uploadFileRes) => {
  285. let data = JSON.parse(uploadFileRes.data)
  286. that.sendOut(1, data.data)
  287. }
  288. });
  289. })
  290. }
  291. });
  292. } else if (e.index === 1) {
  293. uni.chooseImage({
  294. count: 1, //默认9
  295. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  296. sourceType: ['camera '], //从相册选择
  297. success: function(res) {
  298. let list = res.tempFilePaths
  299. list.forEach(val => {
  300. uni.uploadFile({
  301. url: 'https://cardoa.platomix.net/api/upload', //仅为示例,非真实的接口地址
  302. filePath: val,
  303. name: 'file',
  304. header: {
  305. 'Authorization': uni.getStorageSync('token'),
  306. 'Content-Type': 'multipart/form-data'
  307. },
  308. success: (uploadFileRes) => {
  309. let data = JSON.parse(uploadFileRes.data)
  310. that.sendOut(1, data.data)
  311. }
  312. });
  313. })
  314. }
  315. });
  316. }
  317. },
  318. // 加载初始消息
  319. getRoomList(chatId, page) {
  320. let that = this
  321. that.$tm.request.post('chat/roomhistorylist', {
  322. chat_id: chatId,
  323. page: page
  324. }).then(res => {
  325. if (res.data.data.length > 0) {
  326. that.chatList = res.data.data
  327. // 滚动到底部
  328. let viewId = that.chatList[res.data.data.length - 1].id
  329. that.$nextTick(function() {
  330. //进入页面滚动到底部
  331. that.scrollToView = 'msg' + viewId
  332. that.$nextTick(function() {
  333. that.scrollAnimation = true;
  334. });
  335. });
  336. }
  337. })
  338. },
  339. // 滑动到顶部获取消息
  340. getdata() {
  341. // if (this.isHistoryLoading) {
  342. // return;
  343. // }
  344. // this.isHistoryLoading = true; //参数作为进入请求标识,防止重复请求
  345. // this.scrollAnimation = false; //关闭滑动动画
  346. let Viewid = this.chatList[1].id; //记住第一个信息ID
  347. this.$tm.request.post('chat/roomhistorylist', {
  348. chat_id: this.chatID,
  349. page: ++this.page
  350. }).then(res => {
  351. let list = res.data.data
  352. if (list.length > 0) {
  353. this.$nextTick(() => {
  354. this.scrollToView = 'msg' + Viewid; //跳转上次的第一行信息位置
  355. this.$nextTick(() => {
  356. this.scrollAnimation = true; //恢复滚动动画
  357. });
  358. });
  359. for (let i = 0; i < list.length; i++) {
  360. this.chatList.unshift(list[i]);
  361. }
  362. }
  363. this.isHistoryLoading = false;
  364. })
  365. },
  366. sendOut(type, msg) {
  367. let data = {
  368. "chat_id": this.chatID, //房间id号
  369. "to_uid": this.toUid,
  370. "msg": msg, //返回文字描述
  371. "type": type //0:文本;1:图片;2:名片;3:商品,4:正在输入
  372. }
  373. this.$tm.request.post('chat/send', data).then(res => {
  374. this.inputInfo = ""
  375. this.setUpShow = false
  376. this.chatList.push(res.data)
  377. this.$nextTick(() => {
  378. uni.pageScrollTo({
  379. scrollTop: 999999, // 设置一个超大值,以保证滚动条滚动到底部
  380. duration: 300
  381. });
  382. });
  383. })
  384. },
  385. // 顶部tab导航
  386. tabChange(e) {
  387. if (e.index == 0 && this.custMobile !== '') {
  388. uni.makePhoneCall({
  389. phoneNumber: this.custMobile //仅为示例
  390. });
  391. } else if (e.index == 1) {
  392. this.isStr = 'phone'
  393. this.dialogShow = true
  394. this.dialogContent = "是否发起与对方交换电话?"
  395. } else if (e.index == 2) {
  396. this.isStr = 'vx'
  397. this.dialogShow = true
  398. this.dialogContent = "是否发起与对方交换微信?"
  399. }
  400. },
  401. dialogClick() {
  402. this.dialogShow = false
  403. if (this.isStr === 'phone') {
  404. let msg = "发起交换电话"
  405. this.sendOut(5, msg)
  406. } else if (this.isStr === 'vx') {
  407. let msg = "发起交换微信"
  408. this.sendOut(6, msg)
  409. }
  410. },
  411. getNumber(res) {
  412. this.$tm.request.post('card/getphone', {
  413. code: res.detail.code
  414. }).then(result => {
  415. this.sendOut(0, result.data.phone_info.phoneNumber)
  416. })
  417. },
  418. getUserInfo(res) {
  419. console.log(res)
  420. },
  421. }
  422. }
  423. </script>
  424. <style lang="scss" scoped>
  425. .cu-chat {
  426. text {
  427. word-break: break-word
  428. }
  429. }
  430. </style>