message.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <template>
  2. <view>
  3. <u-navbar title="留言"></u-navbar>
  4. <view class="message">
  5. <scroll-view id="scrollview" class="chat-window" scroll-y="true" :style="{height: style.contentViewHeight + 'px'}" :scroll-with-animation="true" :scroll-top="scrollTop">
  6. <view class="cu-chat">
  7. <view class="messageRecord">
  8. <template v-for="(item ,index) in list" >
  9. <view :key="item.id" class="messageRecord-time">{{item.createTimeN}}</view>
  10. <view class="messageRecord-item "
  11. :class="
  12. beMemberId==item.meMemberId? 'messageRecord-left':'messageRecord-right'
  13. "
  14. :key="item.id">
  15. <view class="messageRecord-img" v-if="beMemberId==item.meMemberId">
  16. <u-avatar :src="item.meMemberFaceImage" mode="square" size="80"></u-avatar>
  17. </view>
  18. <view class="messageRecord-news">
  19. <h4>{{item.meMemberRealName}}</h4>
  20. <view class="messageRecord-text">
  21. {{item.content}}
  22. </view>
  23. </view>
  24. <view class="messageRecord-img" v-if="beMemberId!=item.meMemberId" >
  25. <u-avatar :src="item.meMemberFaceImage" mode="square" size="80"></u-avatar>
  26. </view>
  27. </view>
  28. </template>
  29. </view>
  30. </view>
  31. </scroll-view>
  32. <view class="messageInput">
  33. <view class="messageInput-box">
  34. <u-input maxlength="100" v-model="keyword" />
  35. </view>
  36. <view class="messageInput-btn" @click="submit">
  37. <u-icon custom-prefix="custom-icon" name="send-plane-fill" color="#fff"></u-icon>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. import * as API from '@/apis/leaving.js'
  45. export default {
  46. data() {
  47. return {
  48. beMemberId:"",
  49. src1: '/static/img/sexMan.png',
  50. src2:'/static/img/sexWoman.png',
  51. form:{
  52. },
  53. keyword:"",
  54. newstype:'',
  55. typelist: [],
  56. pageIndex: 1,
  57. recordsTotal: 0,
  58. list: [],
  59. tabList: [{
  60. name: '通知公告'
  61. }, {
  62. name: '活动报道'
  63. },{
  64. name: '婚恋课堂'
  65. },{
  66. name: '问题解答'
  67. }],
  68. scrollTopH:'',
  69. scrollTop:'',
  70. current: 0,
  71. wrapList: [
  72. ],
  73. style: {
  74. pageHeight: 0,
  75. contentViewHeight: 0,
  76. footViewHeight: 90,
  77. mitemHeight: 0,
  78. },
  79. pageSize:20,
  80. submitbl:false,
  81. }
  82. },
  83. onPullDownRefresh() {
  84. if (this.list.length < this.recordsTotal) {
  85. this.myLoadmore();
  86. }
  87. },
  88. onLoad(op) {
  89. this.beMemberId=op.id;
  90. const res = uni.getSystemInfoSync(); //获取手机可使用窗口高度     api为获取系统信息同步接口
  91. this.style.pageHeight = res.windowHeight;
  92. this.style.contentViewHeight = res.windowHeight - uni.getSystemInfoSync().screenWidth / 750 * (300); //像素   因为给出的是像素高度 然后我们用的是upx  所以换算一下 
  93. },
  94. methods: {
  95. scrollToBottom() {
  96. let that = this;
  97. let query = uni.createSelectorQuery();
  98. query.selectAll('.cu-chat').boundingClientRect();
  99. query.select('#scrollview').boundingClientRect();
  100. query.exec((res) => {
  101. that.style.mitemHeight = 0;
  102. // that.style.mitemHeight = that.style.mitemHeight + res[0].height + 40
  103. res[0].forEach((rect) => that.style.mitemHeight = that.style.mitemHeight + rect.height + 40) //获取所有内部子元素的高度
  104. // 因为vue的虚拟DOM 每次生成的新消息都是之前的,所以采用异步setTimeout    主要就是添加了这定时器
  105. setTimeout(() => {
  106. // console.log("111",that.style.mitemHeight);
  107. // console.log("222",that.style.contentViewHeight);
  108. if (that.style.mitemHeight > (that.style.contentViewHeight - 100)) { //判断子元素高度是否大于显示高度
  109. that.scrollTop = that.style.mitemHeight - that.style.contentViewHeight //用子元素的高度减去显示的高度就获益获得序言滚动的高度
  110. }
  111. }, 100)
  112. })
  113. },
  114. submit(){
  115. uni.showLoading({
  116. title: "加载中",
  117. mask: true,
  118. })
  119. var data = {
  120. beMemberId:this.beMemberId,
  121. content:this.keyword,
  122. };
  123. API.submit(data).then((res) => {
  124. this.keyword=""
  125. uni.hideLoading()
  126. this.submitbl=true
  127. this.getList(true)
  128. }).catch(error => {
  129. uni.showToast({
  130. title: error,icon: "none"
  131. })
  132. })
  133. },
  134. getList(bl) {
  135. if(!bl){
  136. if(this.submitbl){
  137. this.submitbl=false
  138. return
  139. }
  140. }
  141. var data = {
  142. beMemberId:this.beMemberId,
  143. pageSize:this.pageSize,
  144. pageIndex:1
  145. };
  146. API.chatList(data).then((res) => {
  147. var showTimeData=new Map()
  148. this.list = res.data.data;
  149. for(var i in this.list ){
  150. var item=this.list[i]
  151. var key=item.createTime
  152. if(!showTimeData.get(key)){
  153. showTimeData.set(key,item)
  154. item.createTimeN= key;
  155. }else{
  156. item.createTimeN="";
  157. }
  158. }
  159. console.log(this.list)
  160. this.$nextTick(()=>{
  161. this.scrollToBottom(); //创建后调用回到底部方法
  162. })
  163. setTimeout(()=>{
  164. this.getList()
  165. },1000*10)
  166. this.recordsTotal = res.data.recordsTotal
  167. }).catch(error => {
  168. uni.showToast({
  169. title: error,icon: "none"
  170. })
  171. })
  172. },
  173. myLoadmore() {
  174. this.pageIndex += 1;
  175. this.getList()
  176. },
  177. onReady() {
  178. this.getList()
  179. }
  180. },
  181. }
  182. </script>
  183. <style lang="scss" scoped>
  184. page{
  185. background-color: #f5f5f5;
  186. }
  187. .messageRecord-news h4{
  188. margin-bottom: 5px;
  189. font-weight: normal;
  190. font-size: 12px;
  191. }
  192. .message-head{
  193. background-color: #fff;
  194. display: flex;
  195. justify-content: space-around;
  196. height: 120rpx;
  197. .message-head-item{
  198. display: flex;
  199. flex-direction: column;
  200. justify-content: center;
  201. align-items: center;
  202. span{
  203. margin-top:6rpx;
  204. font-size: 20rpx;
  205. color:#666;
  206. }
  207. }
  208. }
  209. .message{
  210. // height: calc(100vh - 288rpx);
  211. overflow-y: auto;
  212. background-color: #f5f5f5;
  213. position: relative;
  214. padding: 30rpx;
  215. padding-bottom: 80px;
  216. .messageRecord{
  217. .messageRecord-item{
  218. margin-bottom: 40rpx;
  219. }
  220. .messageRecord-pic{
  221. width:auto;
  222. border-radius: 5px;
  223. overflow: hidden;
  224. max-height: 120px;
  225. }
  226. .messageRecord-img{
  227. overflow: hidden;
  228. height: 80rpx;
  229. min-width:100rpx;
  230. }
  231. .messageRecord-text{
  232. background-color: #fff;
  233. padding:18rpx 30rpx;
  234. border-radius: 10rpx;
  235. font-size: 26rpx;
  236. min-height:40px ;
  237. position: relative;
  238. margin-left: 5px;
  239. }
  240. .messageRecord-left{
  241. display: flex;
  242. .messageRecord-text::after{
  243. content: '';
  244. position: absolute;
  245. width: 0;
  246. height: 0;
  247. left: -8px;
  248. top: 10px;
  249. border-top: 10px solid transparent;
  250. border-right: 8px solid #fff;
  251. border-bottom: 10px solid transparent;
  252. }
  253. }
  254. .messageRecord-right{
  255. display: flex;
  256. justify-content: flex-end;
  257. position: relative;
  258. .messageRecord-img{
  259. text-align: right;
  260. }
  261. .messageRecord-news h4{
  262. text-align: right;
  263. }
  264. .messageRecord-text{
  265. background: #ff5e5e;
  266. color:#fff;
  267. position: relative;
  268. margin-right: 5px;
  269. &:after{
  270. content: '';
  271. position: absolute;
  272. width: 0;
  273. height: 0;
  274. right: -8px;
  275. top: 10px;
  276. border-top: 10px solid transparent;
  277. border-left: 8px solid #ff5e5e;
  278. border-bottom: 10px solid transparent;
  279. }
  280. }
  281. }
  282. }
  283. .messageInput{
  284. display: flex;
  285. align-items: center;
  286. position: fixed;
  287. padding: 20rpx;
  288. left: 0;
  289. right: 0;
  290. bottom:0;
  291. background-color: #fff;
  292. min-height: 50rpx;
  293. border-top: 1px solid #eee;
  294. }
  295. .u-size-mini{
  296. height:56rpx!important;
  297. padding: 0 24rpx!important;
  298. }
  299. .messageInput-box{
  300. flex: 1;
  301. background-color: #f5f5f5;
  302. border-radius:32rpx;
  303. margin: 0 20rpx;
  304. padding-left: 20px;
  305. }
  306. .messageRecord-time{
  307. text-align: center;
  308. font-size: 24rpx;
  309. color:#999;
  310. margin: 30rpx 0;
  311. }
  312. }
  313. .u-input--border{
  314. border:none;
  315. }
  316. .messageInput-btn{
  317. background-color: #ff5e5e;
  318. height: 28px;
  319. width: 28px;
  320. border-radius: 50%;
  321. text-align: center;
  322. line-height: 28px;
  323. }
  324. </style>