zixun-im.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. var winHeight=window.innerHeight;//窗体高度
  2. Date.prototype.format = function(format) {
  3. var date = {
  4. "M+": this.getMonth() + 1,
  5. "d+": this.getDate(),
  6. "h+": this.getHours(),
  7. "m+": this.getMinutes(),
  8. "s+": this.getSeconds(),
  9. "q+": Math.floor((this.getMonth() + 3) / 3),
  10. "S+": this.getMilliseconds()
  11. };
  12. if(/(y+)/i.test(format)) {
  13. format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  14. }
  15. for(var k in date) {
  16. if(new RegExp("(" + k + ")").test(format)) {
  17. format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
  18. date[k] : ("00" + date[k]).substr(("" + date[k]).length));
  19. }
  20. }
  21. return format;
  22. }
  23. var pageIndex = 1;
  24. var docPhoto, docName;
  25. var otherCode, otherName, otherPhoto, otherSex;
  26. /**
  27. * 初始化
  28. */
  29. mui.plusReady(function() {
  30. $("#contain").height(winHeight-45-58)//设定聊天模块度
  31. document.addEventListener("resume", function() {
  32. getMsgList(1, 200,true);
  33. }, false);
  34. var self = plus.webview.currentWebview();
  35. otherCode = self.otherCode;
  36. otherName = self.otherName;
  37. otherSex = self.otherSex;
  38. otherPhoto = self.otherPhoto || (otherSex==1 ? "../../../images/d-male.png" : "../../../images/d-female.png");
  39. $('#title').html("名医咨询-"+otherName);
  40. /*
  41. * 医生缓存信息 姓名和头像
  42. */
  43. var infoStr = plus.storage.getItem("docInfo");
  44. if(infoStr) {
  45. var info = JSON.parse(infoStr);
  46. docName = info.name;
  47. docPhoto = info.photo || (info.sex==1 ? "../../../images/d-male.png" : "../../../images/d-female.png");
  48. }
  49. back2home();
  50. getMsgList(1, 200, true);
  51. });
  52. /**
  53. * 点击结束咨询
  54. */
  55. $('#close_zixun_btn').on('tap',function() {
  56. mui.back();
  57. });
  58. /**
  59. * 点击查看好友详细信息
  60. */
  61. $('#qun_detail').on("tap", function(){
  62. mui.openWindow('../../mine/html/zuyuanziliao.html', 'zuyuanziliao', {
  63. extras: {
  64. docCode: otherCode
  65. }
  66. });
  67. })
  68. /*
  69. * 回复消息
  70. */
  71. function clickReply() {
  72. var text = $("#input_content").val().trim();
  73. $("#input_content").focus()
  74. if(text.length == 0) {
  75. mui.toast("发送内容不能为空,请重新输入");
  76. return;
  77. }
  78. uploadMsg(1, text);
  79. $('#contain').animate({scrollTop:1000000});
  80. }
  81. /**
  82. * 获取当前用户code
  83. */
  84. function getUserId() {
  85. var userInfo = plus.storage.getItem("userAgent");
  86. if(typeof(userInfo) == "undefind" || userInfo == null) {
  87. mui.toast("登录信息过时,请重新登录!");
  88. return;
  89. }
  90. var userJson = $.parseJSON(userInfo);
  91. return userJson.uid;
  92. }
  93. /**
  94. * 获取消息
  95. */
  96. function getMsgList(pageIndex, pageSize,isClear) {
  97. var start = 0;
  98. if(pageIndex > 0) {
  99. start = (pageIndex - 1) * pageSize;
  100. };
  101. count = pageSize;
  102. im.getP2pMsg(getUserId(), otherCode, start, count, function(data) {
  103. showMsg(data,isClear);
  104. $('#contain').animate({scrollTop:1000000});
  105. // 刷新数字角标
  106. // var userId = plus.storage.getItem('im_userid');
  107. // im.getBadgeNumber(userId, function(result) {
  108. // console.log(JSON.stringify(result));
  109. // try {
  110. // if (result && result.badge) {
  111. // plus.runtime.setBadgeNumber(result.badge);
  112. // }
  113. // } catch (e) {
  114. //
  115. // }
  116. // });
  117. });
  118. }
  119. /**
  120. * 发送消息
  121. */
  122. function uploadMsg(type, content) {
  123. im.sendP2p(getUserId(), otherCode, content, type, function(data) {
  124. if(data.errno == 0) {
  125. getMsgList(1, 1,false);
  126. } else {
  127. mui.toast("消息发送失败");
  128. }
  129. })
  130. }
  131. /**
  132. * 显示消息
  133. */
  134. function showMsg(data,isClear) {
  135. if(data == null || data.list == null) {
  136. return;
  137. }
  138. var html = ""; //'<div class="time-tips"><span>' + timer + '</span></div>';
  139. var preTime = 0;
  140. var msgList = data.list;
  141. var typeMsg = 1;
  142. for(var i = msgList.length - 1; i >= 0; i--) {
  143. var doctor_img = "../../../images/d-default.png";
  144. var doctor_name = "";
  145. if(msgList[i].from_uid == otherCode){
  146. doctor_img = otherPhoto;
  147. doctor_name = otherName;
  148. }
  149. else{
  150. doctor_img = docPhoto;
  151. doctor_name = docName;
  152. }
  153. var contentMsg = "";
  154. if(msgList[i].content != null && msgList[i].content != "") {
  155. typeMsg = msgList[i].type;
  156. contentMsg = msgList[i].content;
  157. }
  158. var _class = 'class="chat-right"';
  159. if(getUserId() != msgList[i].from_uid) {
  160. _class = 'class="chat-left"';
  161. }
  162. if(msgList[i].timestamp - preTime > 60 * 1000) {
  163. preTime = msgList[i].timestamp;
  164. var date = new Date();
  165. date.setTime(msgList[i].timestamp);
  166. var time = date.format('yyyy-MM-dd h:m:s');
  167. html += '<div class="time-tips"><span>' + time + '</span></div>';
  168. }
  169. // console.error(contentMsg);
  170. //type:1.文字 2.图片 3.音频
  171. var temp = '<dl ' + _class + '><dt style="height: auto; text-align: center;"><a><img src=" ' + doctor_img + '" class="c-images-cycle" /></a><span class="c-f12">' + doctor_name + '</span></dt>';
  172. switch(typeMsg) {
  173. case 1:
  174. temp += '<dd class="word-bread"><span>' + contentMsg + '</span></dd></dl>';
  175. break;
  176. case 2:
  177. temp += '<dd class="word-bread"><img width="100" height="100" src=" ' + contentMsg + '" /></dd></dl>';
  178. break;
  179. case 3:
  180. temp += '<dd class="word-bread audio" data-audio=" ' + contentMsg + '" onclick="playRecord(this)"><img src="../images/more-tb3.png" alt="" /></dd></dl>';
  181. break;
  182. }
  183. html += temp;
  184. }
  185. $("#input_content").val("");
  186. if(typeMsg != null && typeMsg != 1) {
  187. $(".tw-add-detail").hide(500);
  188. }
  189. if(isClear){
  190. $("#contain").html(html);
  191. }else{
  192. $("#contain").append(html);
  193. }
  194. window.scrollTo(0, $("#contain").height());
  195. // scaleRefresh("#contain dd");
  196. }
  197. /*
  198. * 图片缩放
  199. */
  200. function scaleRefresh(dom) {
  201. ImagesZoom.init({
  202. "elem": dom,
  203. "delBack": function(index) {
  204. }
  205. });
  206. }
  207. /*
  208. * 附加功能
  209. */
  210. $(".tw-add").click(function() {
  211. $(this).find("img").toggle();
  212. $(".tw-add-detail").toggle(500);
  213. var inputHeight = $("#input_div").height();
  214. });
  215. /*
  216. * 发送照片
  217. */
  218. function clickGallery() {
  219. plus.gallery.pick(function(path) {
  220. var lastIdx = path.lastIndexOf("/"),
  221. imgName = path;
  222. if(lastIdx>-1) {
  223. imgName = path.slice(lastIdx+1);
  224. }
  225. plus.zip.compressImage({
  226. src: path,
  227. dst: "_doc/chat/gallery/" + imgName,
  228. quality: 20,
  229. overwrite: true
  230. }, function(e) {
  231. var task = plus.uploader.createUpload(server + "upload/chat", {
  232. method: "post"
  233. }, function(t, sta) {
  234. console.log(JSON.stringify(t))
  235. if(sta == 200) {
  236. var msg = t.responseText;
  237. var oImg = JSON.parse(msg);
  238. var imgUrl = oImg.urls;
  239. var re = new RegExp("\\\\", "g");
  240. imgUrl = imgUrl.replace(re, "/");
  241. uploadMsg(2, imgUrl);
  242. }
  243. });
  244. task.addFile(e.target, {});
  245. task.start();
  246. }, function(err) {
  247. console.error("压缩失败:" + err.message);
  248. });
  249. }, function(err) {});
  250. };
  251. /*
  252. * 拍照
  253. */
  254. function clickCamera() {
  255. var cmr = plus.camera.getCamera();
  256. var res = cmr.supportedImageResolutions[0];
  257. var fmt = cmr.supportedImageFormats[0];
  258. cmr.captureImage(function(path) {
  259. // plus.io.resolveLocalFileSystemURL(path, function(entry) {
  260. plus.io.resolveLocalFileSystemURL(path, function(entry) {
  261. var localUrl = entry.toLocalURL();
  262. var lastIdx = localUrl.lastIndexOf("/"),
  263. imgName = localUrl;
  264. if(lastIdx>-1) {
  265. imgName = localUrl.slice(lastIdx+1);
  266. }
  267. plus.zip.compressImage({
  268. src: localUrl,
  269. dst: "_doc/chat/camera/" + imgName,
  270. quality: 20,
  271. overwrite: true
  272. }, function(e) {
  273. var task = plus.uploader.createUpload(server + "upload/chat", {
  274. method: "post"
  275. }, function(t, sta) {
  276. if(sta == 200) {
  277. var msg = t.responseText;
  278. var oImg = JSON.parse(msg);
  279. var imgUrl = oImg.urls;
  280. var re = new RegExp("\\\\", "g");
  281. imgUrl = imgUrl.replace(re, "/");
  282. console.log(imgUrl);
  283. uploadMsg(2, imgUrl);
  284. }
  285. });
  286. task.addFile(e.target, {});
  287. task.start();
  288. }, function(err) {
  289. console.log("压缩失败: " + err.message);
  290. });
  291. });
  292. }, function(err) {
  293. if(err.code==11 && err.message == "null") {
  294. plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。");
  295. }
  296. console.error("拍照失败:" + err.message);
  297. }, {
  298. index: 1
  299. });
  300. };
  301. /**
  302. * 更新信息事件
  303. */
  304. window.addEventListener("update", function() {
  305. getMsgList(1, 200, true);
  306. });
  307. template.helper("setPhoto", function(p) {
  308. return getImgUrl(p);
  309. });