new-ask.js 5.3 KB

1
  1. import Event from"../../common/chat/eventTypes";import util from"../../common/utils/utils";import store from"../../store/index";import api from"../../common/api/index";Component({data:{quizList:[],scrollTop:0,answerNumber:3,pickIndex:0,pickList:["查看全部","与我相关"],listData:[],showEdit:!1,showMoreTips:!1,noMoreMessage:!1,btnTips:"",scrollView:""},properties:{skin:{type:String,value:"black",observer(t){"black"!==t&&"white"!==t&&this.setData({skin:"black"})}},liveType:{type:String,observer(t){"live"!==t&&this.setData({showEdit:!1})}},iPhoneX:{type:Boolean,value:!1}},lifetimes:{created(){this.notJoinLists=[],this.clock=null,this.chat=null,this.askContent=0,this.newMessageIndex=-1,this.currentNotReadId="",this.notReadIdList=new Array,this.couldGetList=!0,this.scrollTop=0,this.recentlyId=0,this.needScrollToTop=0,this.initGetListEnabled(),this.windowHeight=wx.getSystemInfoSync().windowHeight,this.chatEvent=this.getChatEvent()},async attached(){store.get({"main.chat":t=>{this.chat=t,t&&this.handleSocketEvent()},"app.userId":t=>{this.setData({userId:t})},"main.channelDetail":t=>{this.setData({channelDetail:t}),this.getQAList().then((t=>{this.setData({listData:t}),t.length<10&&this.setData({noMoreMessage:!0}),this.getScrollViewHeight()}))}})},detached(){clearInterval(this.clock),this.handleSocketEvent(!1)}},methods:{bindPickerChange(t){const e=t.detail.value;this.setData({pickIndex:e}),this.getQAList().then((t=>{this.setData({listData:t,scrollTop:0}),this.initGetListEnabled()}))},async scrollToUpper(){if(!this.couldGetList||!this.upperGetList)return;this.couldGetList=!1;const{listData:t}=this.data,e=t[t.length-1].question.id,s=await this.getQAList({id:e,order:1});this.setData({listData:[...t,...s]}),s.length<10&&(this.upperGetList=!1)},async scrollToLower(){if(!this.couldGetList||!this.lowerGetList)return;this.couldGetList=!1;const{listData:t}=this.data,e=t[0].question.id,s=await this.getQAList({id:e,order:0});this.setData({listData:[...s,...t]}),s.length<10&&(this.lowerGetList=!1,this.setData({noMoreMessage:!0}))},getQAList(t={}){const{roomId:e,sessionId:s}=this.data.channelDetail,{userId:i,pickIndex:a}=this.data,o=Object.assign({roomId:e,sessionId:s,userId:i,getSelf:a,size:10},t);return new Promise((t=>{api.getQAList(o).then((e=>{if(this.couldGetList=!0,200!==e.data.code)return void this.askToast(e.data.message);const s=JSON.parse(JSON.stringify(e.data.data));0===s.length&&t([]),s.forEach(((t,e,s)=>{s[e].question.user=JSON.parse(t.question.user),s[e].question.timestamp=util.returnTime(t.question.timestamp),t.answers.forEach(((t,e,s)=>{s[e].user=JSON.parse(t.user),s[e].timestamp=util.returnTime(t.timestamp)}))})),t(s)})).catch((t=>{this.couldGetList=!0,this.askToast("网络错误,清稍后重试")}))}))},handleSendMsg(t){const e=t.detail.chatTitle,{listData:s}=this.data;this.chat.sendQuestion(e,(t=>{const e=JSON.parse(t);200===e.code&&(s.push({answers:[],question:{content:e.data.content,id:e.data.questionId,timestamp:util.returnTime(e.data.timestamp),userId:e.data.user.userId,user:e.data.user}}),this.setData({listData:s}))}))},chatEditShow(){"live"===this.data.liveType&&this.setData({showEdit:!this.data.showEdit})},viewScroll(t){this.scrollTop=t.detail.scrollTop},showReply(){const{currentNotReadId:t,newMessageIndex:e}=this;-1===e?t&&this.getQAList({id:t,getSelf:0,order:1,getCurrent:1}).then((e=>{this.setData({listData:e,pickIndex:0,showMoreTips:!1}),this.chatScrollToBottom(),e.length<10&&this.getQAList({id:t,getSelf:0,order:0,getCurrent:0,size:10-e.length}).then((t=>{this.setData({listData:[...t,...this.data.listData],scrollTop:0})}))})):this.setData({showMoreTips:!1,scrollToTop:this.needScrollToTop})},handleSocketEvent(t=!0){const e=this.chat,s=this.chatEvent;e&&Object.keys(s).forEach((i=>{e[t?"on":"off"](i,s[i])}))},moreTipsBtnShow(t,e){const s=t===e?"@我有新回复":"有新问答信息";this.setData({showMoreTips:!0,btnTips:s})},getChatEvent(){const t=this;return{[Event.LAUNCH_A](e,s){const{listData:i,userId:a}=t.data,o=JSON.parse(JSON.stringify(s));o.timestamp=util.returnTime(o.timestamp);const{questionId:n,id:h,questionerId:r}=s,l=i.findIndex((t=>t.question.id===n));if(t.recentlyId=h,t.newMessageIndex=l,-1===l)return t.notReadIdList.push({answerId:h,questionId:n}),t.currentNotReadId=n,void t.moreTipsBtnShow(r,a);i[l].answers.push(o),t.setData({listData:i},(function(){util.getDomRect(`#answer${t.recentlyId}`,t).then((e=>{const{top:s,height:i}=e,{otherHeight:o,windowHeight:n,scrollTop:h,viewHeight:l}=t,d=s+i;s>0&&o<d&&d<n||(t.moreTipsBtnShow(r,a),t.needScrollToTop=s<0?Math.abs(h-o+d):d<o?h-s:h+l+s-n)}))}))},[Event.DELETE_QA_ANSWER](e,s){const{listData:i}=t.data;if(0===i.legnth)return;let a=!0;const o=JSON.parse(JSON.stringify(s));for(let e=0;e<i.length;e++)for(let s=0;s<i[e].answers.length;s++)if(i[e].answers[s].id===o.id){i[e].answers.splice(s,1),t.setData({listData:i}),a=!1;break}if(a){const e=t.notReadIdList.findIndex((t=>t.answerId===o.id));-1!==e&&(t.notReadIdList.splice(e,1),t.currentNotReadId="",t.setData({showMoreTips:!1}))}}}},chatScrollToBottom(){util.getDomRect("#askContent",this).then((({height:t})=>{this.setData({scrollToTop:t})}))},getScrollViewHeight(){util.getDomRect("#scrollContent",this).then((({height:t})=>{this.viewHeight=t,this.otherHeight=this.windowHeight-t}))},initGetListEnabled(){this.lowerGetList=!0,this.upperGetList=!0},askToast(t){wx.showToast({title:t,icon:"none",duration:2e3})}}});