chat-kf-page.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //首页组件
  2. var chatKfIndex = {
  3. delimiters:["<{","}>"],
  4. data: function(){
  5. return {
  6. visitors: [],
  7. }
  8. },
  9. methods: {
  10. init(){
  11. this.getKefuInfo();
  12. },
  13. kfOnline() {
  14. let messsage = {}
  15. messsage.type = "kfOnline";
  16. messsage.data = this.$parent.kefuInfo;
  17. this.$parent.socket.send(JSON.stringify(messsage));
  18. },
  19. receiveMessage(e) {
  20. const retData = JSON.parse(e.data);
  21. switch (retData.type) {
  22. case "allUsers":
  23. this.visitors = retData.data;
  24. break;
  25. case "userOnline":
  26. this.visitors.push(retData.data);
  27. break;
  28. }
  29. },
  30. //初始化websocket
  31. initConn() {
  32. if(this.$parent.socket==null){
  33. this.$parent.socket = new ReconnectingWebSocket(this.$parent.server);
  34. }
  35. this.$parent.socket.onopen=this.kfOnline;
  36. this.$parent.socket.onmessage = this.receiveMessage;
  37. },
  38. //获取客服信息
  39. getKefuInfo(){
  40. let _this=this;
  41. $.ajax({
  42. type:"get",
  43. url:"/kefuinfo",
  44. headers:{
  45. "token":TOKEN
  46. },
  47. success: function(data) {
  48. if(data.code==200 && data.result!=null){
  49. _this.$parent.kefuInfo.id=data.result.id;
  50. _this.$parent.kefuInfo.name=data.result.name;
  51. _this.$parent.kefuInfo.avator=data.result.avator;
  52. _this.initConn();
  53. }
  54. if(data.code!=200){
  55. _this.$message({
  56. message: data.msg,
  57. type: 'error'
  58. });
  59. }
  60. }
  61. });
  62. },
  63. },
  64. created: function () {
  65. this.init();
  66. },
  67. template:$("#chatKfIndex").html()
  68. };
  69. //详情组件
  70. var chatKfBox = {
  71. delimiters:["<{","}>"],
  72. data: function(){
  73. return {
  74. visitorId:null,
  75. msgList: [],
  76. messageContent: "",
  77. face: [],
  78. }
  79. },
  80. methods: {
  81. receiveMessage(e) {
  82. const retData = JSON.parse(e.data);
  83. switch (retData.type) {
  84. case "message":
  85. alert(e.data);
  86. break;
  87. }
  88. },
  89. init(){
  90. //获取当前客户消息
  91. this.visitorId=this.$route.params.visitorId;
  92. this.getMesssagesByVisitorId(this.$route.params.visitorId);
  93. this.$parent.socket.onmessage = this.receiveMessage;
  94. },
  95. //获取信息列表
  96. getMesssagesByVisitorId(visitorId) {
  97. let _this = this;
  98. $.ajax({
  99. type: "get",
  100. url: "/messages?visitorId=" + visitorId,
  101. headers: {
  102. "token": TOKEN
  103. },
  104. success: function (data) {
  105. if (data.code == 200 && data.result != null) {
  106. let msgList = data.result;
  107. _this.msgList = [];
  108. for (let i = 0; i < msgList.length; i++) {
  109. let visitorMes = msgList[i];
  110. let content = {}
  111. if (visitorMes["mes_type"] == "kefu") {
  112. content.is_kefu = true;
  113. content.avator = visitorMes["kefu_avator"];
  114. content.name = visitorMes["kefu_name"];
  115. } else {
  116. content.is_kefu = false;
  117. content.avator = visitorMes["visitor_avator"];
  118. content.name = visitorMes["visitor_name"];
  119. }
  120. content.content = replaceContent(visitorMes["content"]);
  121. content.time = visitorMes["time"];
  122. _this.msgList.push(content);
  123. _this.scrollBottom();
  124. }
  125. }
  126. if (data.code != 200) {
  127. _this.$message({
  128. message: data.msg,
  129. type: 'error'
  130. });
  131. }
  132. }
  133. });
  134. },
  135. //发送给客户
  136. chatToUser() {
  137. this.messageContent=this.messageContent.trim("\r\n");
  138. if(this.messageContent==""||this.messageContent=="\r\n"||this.currentGuest==""){
  139. return;
  140. }
  141. let _this=this;
  142. let mes = {};
  143. mes.type = "kefu";
  144. mes.content = this.messageContent;
  145. mes.from_id = _this.$parent.kefuInfo.id;
  146. mes.to_id = this.visitorId;
  147. $.post("/message",mes,function(){
  148. _this.messageContent = "";
  149. });
  150. let content = {}
  151. content.avator = _this.$parent.kefuInfo.avator;
  152. content.name = _this.$parent.kefuInfo.name;
  153. content.content = replaceContent(this.messageContent);
  154. content.is_kefu = true;
  155. content.time = '';
  156. this.msgList.push(content);
  157. this.scrollBottom();
  158. },
  159. //上传图片
  160. uploadImg (url){
  161. let _this=this;
  162. $('#uploadImg').after('<input type="file" accept="image/gif,image/jpeg,image/jpg,image/png" id="uploadImgFile" name="file" style="display:none" >');
  163. $("#uploadImgFile").click();
  164. $("#uploadImgFile").change(function (e) {
  165. var formData = new FormData();
  166. var file = $("#uploadImgFile")[0].files[0];
  167. formData.append("imgfile",file); //传给后台的file的key值是可以自己定义的
  168. filter(file) && $.ajax({
  169. url: url || '',
  170. type: "post",
  171. data: formData,
  172. contentType: false,
  173. processData: false,
  174. dataType: 'JSON',
  175. mimeType: "multipart/form-data",
  176. success: function (res) {
  177. if(res.code!=200){
  178. _this.$message({
  179. message: res.msg,
  180. type: 'error'
  181. });
  182. }else{
  183. _this.messageContent+='img[' + res.result.path + ']';
  184. _this.chatToUser();
  185. }
  186. },
  187. error: function (data) {
  188. console.log(data);
  189. }
  190. });
  191. });
  192. },
  193. //表情点击事件
  194. faceIconClick(index){
  195. $('.faceBox').hide();
  196. this.messageContent+="face"+this.face[index].name;
  197. },
  198. //滚到底部
  199. scrollBottom(){
  200. this.$nextTick(() => {
  201. $('body').scrollTop($("body")[0].scrollHeight);
  202. });
  203. },
  204. },
  205. created: function () {
  206. this.init();
  207. },
  208. template:$("#chatBox").html()
  209. };
  210. var routes = [
  211. { path: '/',component:chatKfIndex}, // 这个表示会默认渲染
  212. {path:'/chatKfBox/:visitorId',component:chatKfBox},
  213. ];
  214. var router = new VueRouter({
  215. routes: routes
  216. })
  217. new Vue({
  218. router,
  219. el: '#app',
  220. data: function(){
  221. return{
  222. server:getWsBaseUrl()+"/chat_server",
  223. socket:null,
  224. kefuInfo:{},
  225. }
  226. },
  227. methods:{
  228. },
  229. created: function () {
  230. },
  231. })