consulting.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. var Request = GetRequest()
  2. new Vue({
  3. el: '#main',
  4. data: {
  5. sessionId:null,
  6. sessionType:2,//1对1会话
  7. curDoctorCode:JSON.parse(window.localStorage.getItem('wlyyAgent')).uid,
  8. curDoctorName:"",
  9. otherCode: Request["otherCode"],//对方医生code
  10. otherName: decodeURIComponent(Request["otherName"]) || "",//对方医生姓名
  11. planDetailId:Request["planDetailId"],//项目id
  12. members:[],
  13. otherInfo:null,//对方的医生信息
  14. firstMsg:null,
  15. page:1,
  16. pagesize:10,
  17. count:0
  18. },
  19. mounted: function() {
  20. this.createSession();
  21. this.bindEvents();
  22. },
  23. methods:{
  24. createSession:function(){
  25. var vm = this;
  26. var p = {}
  27. p[vm.curDoctorCode] = 0;
  28. p[vm.otherCode] = 0;
  29. var data = {
  30. session_type: vm.sessionType, session_name: vm.otherName, participants: JSON.stringify(p)
  31. }
  32. consultingAPI.createSession(data).then(function(res){
  33. if(res.status==200){
  34. vm.sessionId = res.data.id;
  35. vm.connectSocket();//创建socket连接
  36. vm.getMembers();//获取成员列表
  37. }else{
  38. layer.msg(res.msg, {icon: 5})
  39. }
  40. });
  41. },
  42. getMembers:function(){
  43. var vm = this;
  44. consultingAPI.getParticipants(vm.sessionId).then(function(rs){
  45. var members = {};
  46. var isSTF=false;//是否是专科对家医聊天,需要隐藏发送名片入口
  47. $.each( rs , function(i, v) {
  48. if(v.level==1) isSTF=true;
  49. if(v.id!=vm.curDoctorCode){
  50. vm.otherInfo = v;
  51. vm.otherCode = v.id;
  52. }else {
  53. vm.curDoctorName = v.name;
  54. }
  55. vm.members.push({
  56. id:v.id,
  57. code: v.id,
  58. name: v.name,
  59. sex: v.sex,
  60. avatar: v.avatar,
  61. role: v.role,
  62. is_patient: v.is_patient,//0 医生,1 患者
  63. last_fetch_time:v.last_fetch_time
  64. });
  65. });
  66. //获取消息
  67. vm.getMessage(true);
  68. });
  69. },
  70. getMessage:function(isInit){
  71. var vm = this;
  72. var params = {
  73. page: vm.page,
  74. pagesize: vm.pagesize,
  75. end_msg_id: '',
  76. start_msg_id: '',
  77. user: vm.curDoctorCode,
  78. session_id: vm.sessionId,
  79. content_type: '',
  80. isoffset: ''
  81. }
  82. consultingAPI.getMessages(params).then(function(res){
  83. var list = res;
  84. if (list && list.length > 0) {
  85. id = list[list.length - 1].id;
  86. var html = '',
  87. length = list.length;
  88. for (var j = list.length-1; j >= 0; j--) {
  89. var reply = list[j];
  90. html += vm.formatMsg(reply);
  91. }
  92. if(isInit){
  93. $("#talkBox").append(html);
  94. $("#talkBox").slimScroll({
  95. height: '100%',
  96. width: '100%',
  97. alwaysVisible: true,
  98. start : 'bottom',
  99. }).bind('slimscroll', function(e, pos) {
  100. if(pos == 'top'){
  101. if(length == 10){
  102. vm.page++;
  103. vm.getMessage();
  104. }
  105. }
  106. });
  107. }else{
  108. $("#talkBox").prepend(html);
  109. $("#talkBox").slimScroll({
  110. scrollTo: 'bottom'
  111. });
  112. }
  113. $(".fancybox").fancybox({openEffect:"none",closeEffect:"none"});
  114. plyr.setup();
  115. }
  116. });
  117. },
  118. bindEvents:function(){
  119. var vm = this;
  120. template.helper('getSourceUrl', function(str){
  121. return httpRequest.getImgUrl(str);
  122. });
  123. //发送图片
  124. $("#file_head").on('change', function(){
  125. var file = this.files[0];
  126. //先上传图片去服务器,然后再发送消息
  127. var fd=new FormData();
  128. fd.append("action", "UploadVMKImagePath");
  129. fd.append("file", file); //加入文件对象
  130. fd.append("type", '2');
  131. var ajaxObj = {
  132. data: fd,
  133. cache: false,
  134. processData: false,
  135. contentType: false
  136. }
  137. consultingAPI.uploadImage(ajaxObj).then(function(res){
  138. if(res.status == 200){
  139. vm.sendMessage(2, res.urls);
  140. var obj = {
  141. content: res.urls,
  142. content_type: '2',
  143. sender_id: vm.curDoctorCode,
  144. timestamp: new Date().getTime()
  145. }
  146. var html = vm.formatMsg(obj);
  147. $("#talkBox").append(html);
  148. $("#talkBox").slimscroll({
  149. scrollTo: 'bottom'
  150. });
  151. }else{
  152. layer.msg(res.msg, {icon: 5})
  153. }
  154. });
  155. });
  156. },
  157. sendClick:function(){
  158. var vm = this;
  159. var $this = $(this);
  160. var text = $.trim($("#input_content").text());
  161. if(text.length == 0){
  162. vm.showWarningMsg('发送内容不能为空');
  163. return false;
  164. }else{
  165. vm.sendMessage(1, text);
  166. $("#input_content").text('');
  167. var obj = {
  168. content: text,
  169. content_type: '1',
  170. sender_id: vm.curDoctorCode,
  171. timestamp: new Date().getTime()
  172. }
  173. var html = vm.formatMsg(obj);
  174. $("#talkBox").append(html);
  175. $("#talkBox").slimscroll({
  176. scrollTo: 'bottom'
  177. });
  178. }
  179. },
  180. sendMessage:function(contentType, content){
  181. var vm = this;
  182. var params = {
  183. sender_id: vm.curDoctorCode,
  184. sender_name: vm.curDoctorName,
  185. content_type: contentType,
  186. content: content,
  187. view: 0
  188. };
  189. consultingAPI.sendMessage(vm.sessionId, {data: params}).then(function(res){
  190. console.log(res);
  191. vm.count++;
  192. if(vm.count==1 && JSON.parse(window.localStorage.getItem('wlyyAgent')).doctorType==1 && vm.planDetailId && vm.planDetailId!="undefined"){//level:1 专科医生 (只记录第一条数据)
  193. var messages = res.messages[0];
  194. var guidanceParams = {
  195. messageId:messages.id,//消息id
  196. content:messages.content,//聊天内容
  197. planDetailId:vm.planDetailId,//服务项目id
  198. contentType:messages.content_type//消息类型
  199. }
  200. consultingAPI.saveGuidanceMessage(guidanceParams).then(function(rs){
  201. if(rs.status==200){
  202. }else{
  203. layer.msg(rs.msg, {icon: 5})
  204. }
  205. })
  206. }
  207. });
  208. },
  209. connectSocket:function(){
  210. var vm = this;
  211. jQuery.getScript(httpRequest.socketUrl+"/socket.io/socket.io.js").done(function() {
  212. var socket = io.connect(httpRequest.socketUrl );
  213. socket.emit('login', {
  214. userId: vm.curDoctorCode,
  215. password: vm.curDoctorCode,
  216. sessionId: vm.sessionId,
  217. clientType: "pc_doctor"
  218. });
  219. socket.on('message', function (data) {
  220. console.log(data);
  221. if(data.read && (data.read == "all" || data.read == "one") ){
  222. return ;
  223. }
  224. var html = vm.formatMsg(data);
  225. $("#talkBox").append(html);
  226. $("#talkBox").slimscroll({
  227. scrollTo: 'bottom'
  228. });
  229. });
  230. socket.on('error', function (data) {
  231. console.log(data)
  232. });
  233. socket.on('ack', function (data) {
  234. });
  235. }).fail(function() {
  236. // dialog({contentType:'tipsbox', skin:'bk-popup' ,bottom:true, content:"医生实时对话连接失败!"}).show();
  237. });
  238. },
  239. formatMsg:function(reply){
  240. var vm = this;
  241. try{
  242. reply = JSON.parse(reply);
  243. }catch(e){
  244. }
  245. var isSelf = (reply.sender_id==vm.curDoctorCode) ? true : false;
  246. var isSystem = reply.sender_id == 'system';
  247. var html = '';
  248. if(reply.content_type == 7 || reply.content_type == 10 || reply.content_type == 13 || reply.content_type == 14){
  249. var content = reply.content;
  250. if(isSystem){
  251. content = '居民24小时内未回复,系统自动结束咨询';
  252. }
  253. html = template('sys_msg_tmp', {content: content});
  254. }else{
  255. var member;
  256. for(var i=0; i<vm.members.length; i++){
  257. if(reply.sender_id == vm.members[i].id){
  258. member = vm.members[i];
  259. break;
  260. }
  261. }
  262. var img = httpRequest.getImgUrl(member.avatar);
  263. if((img.indexOf('http') == -1) && (img.indexOf("https") == -1)){
  264. img = '../../../images/d-male.png';
  265. }
  266. var content = '';
  267. try{
  268. if(reply.content_type == 18 || reply.content_type == 19){//居民名片、聊天记录
  269. content = "暂不支持此类信息";
  270. }else if(reply.content_type == 3){ //语音
  271. content = {};
  272. content.path = JSON.parse(reply.content).path;
  273. }else if(reply.content_type == 12){ //视频文件
  274. var arr = reply.content.split(",");
  275. content = {};
  276. content.img = arr[0];
  277. content.path = arr[1];
  278. content.time = arr[2];
  279. }else{
  280. if(vm.isString(reply.content)){
  281. content = reply.content;
  282. }else{
  283. content = JSON.parse(reply.content);
  284. }
  285. }
  286. }catch(e){
  287. }
  288. var obj = {
  289. isSelf: isSelf,
  290. time: new Date(reply.timestamp).format('yyyy-MM-dd HH:mm:ss'),
  291. type: reply.content_type,
  292. name: member.name,
  293. img: img,
  294. content: content
  295. };
  296. html = template('msg_tmp', obj);
  297. }
  298. return html;
  299. },
  300. isString:function(str){
  301. return (typeof str=='string')&&str.constructor==String;
  302. },
  303. showWarningMsg:function(msg){
  304. toastr.warning(msg)
  305. }
  306. }
  307. })