user.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <view>
  3. <cu-custom :bgColor="fetch.bgcolor" :isBack="true"><block slot="backText">返回</block><block slot="content">{{fetch.title}}</block></cu-custom>
  4. <view class="cu-chat">
  5. <graceLoading :loadingType="loadingType"></graceLoading>
  6. <block v-for="(v, k) in fetch.chat" :key="k">
  7. <view class="cu-item self" v-if="v.self">
  8. <view class="main">
  9. <view class="content bg-green shadow" v-if="v.text">
  10. <text>{{v.text}}</text>
  11. </view>
  12. <view v-if="v.pic">
  13. <image @click="Dever.viewPic(pic, v.pic)" :src="v.pic" class="radius chat-pic" mode="widthFix"></image>
  14. </view>
  15. </view>
  16. <view class="cu-avatar radius" :style="{backgroundImage:'url('+fetch.user[v.uid].avatar+')'}" @click="Dever.viewPic([fetch.user[v.uid].avatar], fetch.user[v.uid].avatar)"></view>
  17. <view class="date">{{v.date}}</view>
  18. </view>
  19. <view class="cu-item" v-if="!v.self">
  20. <view class="cu-avatar radius" :style="{backgroundImage:'url('+fetch.user[v.uid].avatar+')'}" @click="Dever.viewPic([fetch.user[v.uid].avatar], fetch.user[v.uid].avatar)"></view>
  21. <view class="main">
  22. <view class="content shadow" v-if="v.text">
  23. <text>{{v.text}}</text>
  24. </view>
  25. <view v-if="v.pic">
  26. <image @click="Dever.viewPic(pic, v.pic)" :src="v.pic" class="radius chat-pic" mode="widthFix"></image>
  27. </view>
  28. </view>
  29. <view class="date">{{v.date}}</view>
  30. </view>
  31. </block>
  32. </view>
  33. <view class="cu-bar foot input" :style="[{bottom:InputBottom+'px'}]">
  34. <view class="action" style="display:none;">
  35. <text class="cuIcon-sound text-grey"></text>
  36. </view>
  37. <view class="action" @click="upload">
  38. <text class="cuIcon-picfill text-grey"></text>
  39. </view>
  40. <input class="solid-bottom" :adjust-position="false" :focus="false" maxlength="300" cursor-spacing="10"
  41. @focus="InputFocus" @blur="InputBlur" v-model="msg"></input>
  42. <view class="action" style="display:none;">
  43. <text class="cuIcon-emojifill text-grey"></text>
  44. </view>
  45. <button class="cu-btn bg-green shadow" @click="send()">发送</button>
  46. </view>
  47. <graceFixedMsg v-if="noticeShow" :msg="notice" :width="280" @tapme="noticeClick" :bottom="120"></graceFixedMsg>
  48. </view>
  49. </template>
  50. <script>
  51. export default {
  52. data() {
  53. return {
  54. InputBottom: 0,
  55. uid : 0,
  56. msg : '',
  57. fetch : {
  58. chat : [],
  59. bgcolor : '',
  60. title : '聊天',
  61. },
  62. loadingType: 1,
  63. bottomState : false,
  64. pic : [],
  65. notice : '',
  66. noticeShow : false,
  67. };
  68. },
  69. onLoad(option) {
  70. this.uid = option.uid;
  71. this.fetch.title = option.title;
  72. this.Dever.config.code = option.code;
  73. this.im();
  74. this.getInfo(1);
  75. },
  76. onHide() {
  77. },
  78. onPageScroll(e) {
  79. if (e.scrollTop == 0 && this.loadingType != 2) {
  80. this.bottomState = false;
  81. this.loadingType = 1;
  82. this.getInfo(2);
  83. }
  84. },
  85. onReachBottom() {
  86. this.bottomState = true;
  87. },
  88. methods: {
  89. InputFocus(e) {
  90. this.InputBottom = e.detail.height
  91. },
  92. InputBlur(e) {
  93. this.InputBottom = 0
  94. },
  95. im : function() {
  96. var self = this;
  97. this.Dever.im('chat', false, function(t) {
  98. if (t.type == 'init') {
  99. self.Dever.post('app/community/?l=chat.init', {code:self.Dever.config.code, client_id: t.client_id, uid:self.uid, noloading:1});
  100. } else if (t.type == 'ping') {
  101. } else if (t.type == 'msg') {
  102. self.add(t, 2);
  103. }
  104. });
  105. },
  106. getInfo : function(page) {
  107. var self = this;
  108. if (self.loadingType == 2) {
  109. return;
  110. }
  111. this.Dever.page([page,'chat'], this, 'app/community/?l=chat.getList', {code:this.Dever.config.code, uid: this.uid, noloading:1, concat:-1}, function(t) {
  112. if (t.chat) {
  113. for (var i in t.chat) {
  114. if (t.chat[i].pic) {
  115. self.pic.push(t.chat[i].pic);
  116. }
  117. }
  118. }
  119. if (self.Dever.pageData.status == 0) {
  120. self.loadingType = 2;
  121. } else {
  122. self.loadingType = 3;
  123. }
  124. if (page == 1) {
  125. self.bottom();
  126. }
  127. });
  128. },
  129. send : function(pic) {
  130. var self = this;
  131. var data = {};
  132. data.code = this.Dever.config.code;
  133. data.uid = this.uid;
  134. data.noloading = 1;
  135. if (!pic) {
  136. data.msg = this.msg;
  137. } else {
  138. data.pic = pic;
  139. }
  140. self.Dever.post('app/community/?l=chat.send', data, function(t) {
  141. self.msg = '';
  142. self.add(t.msg, 1);
  143. });
  144. },
  145. add : function(t, type) {
  146. if (t.pic) {
  147. this.pic.push(t.pic);
  148. }
  149. this.fetch.chat.push(t);
  150. if (type == 1 || this.bottomState) {
  151. this.bottom();
  152. } else {
  153. this.notice = '你有新的消息';
  154. this.noticeShow = true;
  155. }
  156. },
  157. noticeClick : function() {
  158. this.noticeShow = false;
  159. this.bottom();
  160. },
  161. upload : function() {
  162. var self = this;
  163. this.Dever.upload('pic', 1, function(type, file) {
  164. self.send(file);
  165. });
  166. },
  167. bottom : function() {
  168. var self = this;
  169. setTimeout(() => {
  170. var query = uni.createSelectorQuery();
  171. query.select('.cu-chat').boundingClientRect();
  172. query.exec((res) => {
  173. var height = res[0].height + res[0].bottom;
  174. self.bottomState = true;
  175. uni.pageScrollTo({
  176. scrollTop: height,
  177. duration: 50
  178. });
  179. })
  180. });
  181. }
  182. }
  183. }
  184. </script>
  185. <style>
  186. page{
  187. padding-bottom: 100upx;
  188. }
  189. .chat-pic {
  190. width: 150rpx;
  191. }
  192. </style>