consulting.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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('wlyyAgentForDoc')).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. var doctorInfo = JSON.parse(window.localStorage.getItem('wlyyAgent'))
  193. if(vm.count==1 && doctorInfo.doctorType==1 && vm.planDetailId && vm.planDetailId!="undefined"){//level:1 专科医生 (只记录第一条数据)
  194. var messages = res.messages[0];
  195. var guidanceParams = {
  196. messageId:messages.id,//消息id
  197. content:messages.content,//聊天内容
  198. planDetailId:vm.planDetailId,//服务项目id
  199. contentType:messages.content_type//消息类型
  200. }
  201. consultingAPI.saveGuidanceMessage(guidanceParams).then(function(rs){
  202. if(rs.status==200){
  203. }else{
  204. layer.msg(rs.msg, {icon: 5})
  205. }
  206. })
  207. }
  208. });
  209. },
  210. connectSocket:function(){
  211. var vm = this;
  212. jQuery.getScript(httpRequest.socketUrl+"/socket.io/socket.io.js").done(function() {
  213. var socket = io.connect(httpRequest.socketUrl );
  214. socket.emit('login', {
  215. userId: vm.curDoctorCode,
  216. password: vm.curDoctorCode,
  217. sessionId: vm.sessionId,
  218. clientType: "pc_doctor"
  219. });
  220. socket.on('message', function (data) {
  221. console.log(data);
  222. if(data.read && (data.read == "all" || data.read == "one") ){
  223. return ;
  224. }
  225. var html = vm.formatMsg(data);
  226. $("#talkBox").append(html);
  227. $("#talkBox").slimscroll({
  228. scrollTo: 'bottom'
  229. });
  230. });
  231. socket.on('error', function (data) {
  232. console.log(data)
  233. });
  234. socket.on('ack', function (data) {
  235. });
  236. }).fail(function() {
  237. // dialog({contentType:'tipsbox', skin:'bk-popup' ,bottom:true, content:"医生实时对话连接失败!"}).show();
  238. });
  239. },
  240. formatMsg:function(reply){
  241. var vm = this;
  242. try{
  243. reply = JSON.parse(reply);
  244. }catch(e){
  245. }
  246. var isSelf = (reply.sender_id==vm.curDoctorCode) ? true : false;
  247. var isSystem = reply.sender_id == 'system';
  248. var html = '';
  249. if(reply.content_type == 7 || reply.content_type == 10 || reply.content_type == 13 || reply.content_type == 14){
  250. var content = reply.content;
  251. if(isSystem){
  252. content = '居民24小时内未回复,系统自动结束咨询';
  253. }
  254. html = template('sys_msg_tmp', {content: content});
  255. }else{
  256. var member;
  257. for(var i=0; i<vm.members.length; i++){
  258. if(reply.sender_id == vm.members[i].id){
  259. member = vm.members[i];
  260. break;
  261. }
  262. }
  263. var img = httpRequest.getImgUrl(member.avatar);
  264. if((img.indexOf('http') == -1) && (img.indexOf("https") == -1)){
  265. img = '../../../images/d-male.png';
  266. }
  267. var content = '';
  268. try{
  269. if(reply.content_type == 18 || reply.content_type == 19){//居民名片、聊天记录
  270. content = "暂不支持此类信息";
  271. }else if(reply.content_type == 3){ //语音
  272. content = {};
  273. content.path = JSON.parse(reply.content).path;
  274. }else if(reply.content_type == 12){ //视频文件
  275. var arr = reply.content.split(",");
  276. content = {};
  277. content.img = arr[0];
  278. content.path = arr[1];
  279. content.time = arr[2];
  280. }else{
  281. if(vm.isString(reply.content)){
  282. content = reply.content;
  283. }else{
  284. content = JSON.parse(reply.content);
  285. }
  286. }
  287. }catch(e){
  288. }
  289. var obj = {
  290. isSelf: isSelf,
  291. time: new Date(reply.timestamp).format('yyyy-MM-dd HH:mm:ss'),
  292. type: reply.content_type,
  293. name: member.name,
  294. img: img,
  295. content: content
  296. };
  297. html = template('msg_tmp', obj);
  298. }
  299. return html;
  300. },
  301. isString:function(str){
  302. return (typeof str=='string')&&str.constructor==String;
  303. },
  304. showWarningMsg:function(msg){
  305. toastr.warning(msg)
  306. }
  307. }
  308. })