message.vue 8.3 KB

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