consulting.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. uploadImage(){
  119. $('#file_head').click()
  120. },
  121. bindEvents:function(){
  122. var vm = this;
  123. template.helper('getSourceUrl', function(str){
  124. return httpRequest.getImgUrl(str);
  125. });
  126. //发送图片
  127. $("#file_head").on('change', function(){
  128. var file = this.files[0];
  129. //先上传图片去服务器,然后再发送消息
  130. var fd=new FormData();
  131. fd.append("action", "UploadVMKImagePath");
  132. fd.append("file", file); //加入文件对象
  133. fd.append("type", '2');
  134. var ajaxObj = {
  135. data: fd,
  136. cache: false,
  137. processData: false,
  138. contentType: false
  139. }
  140. consultingAPI.uploadImage(ajaxObj).then(function(res){
  141. if(res.status == 200){
  142. vm.sendMessage(2, res.urls);
  143. var obj = {
  144. content: res.urls,
  145. content_type: '2',
  146. sender_id: vm.curDoctorCode,
  147. timestamp: new Date().getTime()
  148. }
  149. var html = vm.formatMsg(obj);
  150. $("#talkBox").append(html);
  151. $("#talkBox").slimscroll({
  152. scrollTo: 'bottom'
  153. });
  154. }else{
  155. layer.msg(res.msg, {icon: 5})
  156. }
  157. });
  158. });
  159. },
  160. sendClick:function(){
  161. var vm = this;
  162. var $this = $(this);
  163. var text = $.trim($("#input_content").text());
  164. if(text.length == 0){
  165. vm.showWarningMsg('发送内容不能为空');
  166. return false;
  167. }else{
  168. vm.sendMessage(1, text);
  169. $("#input_content").text('');
  170. var obj = {
  171. content: text,
  172. content_type: '1',
  173. sender_id: vm.curDoctorCode,
  174. timestamp: new Date().getTime()
  175. }
  176. var html = vm.formatMsg(obj);
  177. $("#talkBox").append(html);
  178. $("#talkBox").slimscroll({
  179. scrollTo: 'bottom'
  180. });
  181. }
  182. },
  183. sendMessage:function(contentType, content){
  184. var vm = this;
  185. var params = {
  186. sender_id: vm.curDoctorCode,
  187. sender_name: vm.curDoctorName,
  188. content_type: contentType,
  189. content: content,
  190. view: 0
  191. };
  192. consultingAPI.sendMessage(vm.sessionId, {data: params}).then(function(res){
  193. console.log(res);
  194. vm.count++;
  195. var doctorInfo = JSON.parse(window.localStorage.getItem('wlyyAgent'))
  196. if(vm.count==1 && doctorInfo.doctorType==1 && vm.planDetailId && vm.planDetailId!="undefined"){//level:1 专科医生 (只记录第一条数据)
  197. var messages = res.messages[0];
  198. var guidanceParams = {
  199. messageId:messages.id,//消息id
  200. content:messages.content,//聊天内容
  201. planDetailId:vm.planDetailId,//服务项目id
  202. contentType:messages.content_type//消息类型
  203. }
  204. consultingAPI.saveGuidanceMessage(guidanceParams).then(function(rs){
  205. if(rs.status==200){
  206. }else{
  207. layer.msg(rs.msg, {icon: 5})
  208. }
  209. })
  210. }
  211. });
  212. },
  213. connectSocket:function(){
  214. var vm = this;
  215. jQuery.getScript(httpRequest.socketUrl+"/socket.io/socket.io.js").done(function() {
  216. var socket = io.connect(httpRequest.socketUrl );
  217. socket.emit('login', {
  218. userId: vm.curDoctorCode,
  219. password: vm.curDoctorCode,
  220. sessionId: vm.sessionId,
  221. clientType: "pc_doctor"
  222. });
  223. socket.on('message', function (data) {
  224. console.log(data);
  225. if(data.read && (data.read == "all" || data.read == "one") ){
  226. return ;
  227. }
  228. var html = vm.formatMsg(data);
  229. $("#talkBox").append(html);
  230. $("#talkBox").slimscroll({
  231. scrollTo: 'bottom'
  232. });
  233. });
  234. socket.on('error', function (data) {
  235. console.log(data)
  236. });
  237. socket.on('ack', function (data) {
  238. });
  239. }).fail(function() {
  240. // dialog({contentType:'tipsbox', skin:'bk-popup' ,bottom:true, content:"医生实时对话连接失败!"}).show();
  241. });
  242. },
  243. formatMsg:function(reply){
  244. var vm = this;
  245. try{
  246. reply = JSON.parse(reply);
  247. }catch(e){
  248. }
  249. var isSelf = (reply.sender_id==vm.curDoctorCode) ? true : false;
  250. var isSystem = reply.sender_id == 'system';
  251. var html = '';
  252. if(reply.content_type == 7 || reply.content_type == 10 || reply.content_type == 13 || reply.content_type == 14){
  253. var content = reply.content;
  254. if(isSystem){
  255. content = '居民24小时内未回复,系统自动结束咨询';
  256. }
  257. html = template('sys_msg_tmp', {content: content});
  258. }else{
  259. var member;
  260. for(var i=0; i<vm.members.length; i++){
  261. if(reply.sender_id == vm.members[i].id){
  262. member = vm.members[i];
  263. break;
  264. }
  265. }
  266. var img = httpRequest.getImgUrl(member.avatar);
  267. if((img.indexOf('http') == -1) && (img.indexOf("https") == -1)){
  268. img = '../../../images/d-male.png';
  269. }
  270. var content = '';
  271. try{
  272. if(reply.content_type == 18 || reply.content_type == 19){//居民名片、聊天记录
  273. content = "暂不支持此类信息";
  274. }else if(reply.content_type == 3){ //语音
  275. content = {};
  276. content.path = JSON.parse(reply.content).path;
  277. }else if(reply.content_type == 12){ //视频文件
  278. var arr = reply.content.split(",");
  279. content = {};
  280. content.img = arr[0];
  281. content.path = arr[1];
  282. content.time = arr[2];
  283. }else{
  284. if(vm.isString(reply.content)){
  285. content = reply.content;
  286. }else{
  287. content = JSON.parse(reply.content);
  288. }
  289. }
  290. }catch(e){
  291. }
  292. var obj = {
  293. isSelf: isSelf,
  294. time: new Date(reply.timestamp).format('yyyy-MM-dd HH:mm:ss'),
  295. type: reply.content_type,
  296. name: member.name,
  297. img: img,
  298. content: content
  299. };
  300. html = template('msg_tmp', obj);
  301. }
  302. return html;
  303. },
  304. isString:function(str){
  305. return (typeof str=='string')&&str.constructor==String;
  306. },
  307. showWarningMsg:function(msg){
  308. toastr.warning(msg)
  309. }
  310. }
  311. })