faqizixun.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // 基本信息(包括userAgent)
  2. var baseInfo = null,
  3. // 基础环境信息(包括当前webview、encry相关)
  4. baseEnv = null;
  5. // 咨询医生
  6. var $yishengInput = $("#zixun_yisheng").find("input"),
  7. // 咨询问题
  8. $wentiInput = $("#goutong_info").find("textarea"),
  9. // 添加附件图标按钮
  10. $imgArea = $('.add-img'),
  11. // 提交咨询
  12. $sumit = $('#submit_btn');
  13. var self;
  14. var docInfo;
  15. var imgIndex, imgs, imgUrls;
  16. // 关闭重新登录检查(防止一个ajax error导致所有请求成功无法回调)
  17. var checkReload = false;
  18. //保存患者咨询的内容
  19. var consultLog = {};
  20. // 获取登录相关信息
  21. var getBaseInfo = function() {
  22. docInfo = JSON.parse(plus.storage.getItem("docInfo"));
  23. // 登录的相关信息
  24. var userAgent = JSON.parse(plus.storage.getItem("userAgent"));
  25. return {
  26. userAgent: userAgent,
  27. accessData: $.extend({},baseEnv.webview.accessData)
  28. }
  29. },
  30. // 获取基础环境信息
  31. getBaseEnvPromise = function () {
  32. var env = {
  33. webview: plus.webview.currentWebview()
  34. };
  35. // 因为有异步请求,需要等待数据返回之后进行处理,所以返回Promise
  36. return Promise.resolve().then(function(res) {
  37. return env;
  38. });
  39. },
  40. // 获取表单数据
  41. getInputs = function() {
  42. var data = {
  43. // docName: $yishengInput.val().trim(), // 姓名
  44. question: $wentiInput.val().trim(),
  45. images: null// 附件
  46. };
  47. return data;
  48. },
  49. // 必输验证
  50. validRequired = function(data) {
  51. var fieldsStr = "question",
  52. fieldMap = {
  53. question: "沟通情况"
  54. },
  55. fields = fieldsStr.split(","),
  56. invalidField;
  57. // 某个字段为""、null、undefined,则该字段不能通过必输校验
  58. invalidField = _.find(fields,function(key) {
  59. return !data[key]&&data[key]!==0;
  60. });
  61. if(invalidField) {
  62. mui.toast(fieldMap[invalidField]+'不能为空');
  63. return false;
  64. } else {
  65. return true;
  66. }
  67. },
  68. // 所有输入验证
  69. validInputsPromise = function() {
  70. // 表单数据
  71. var data = getInputs();
  72. return Promise.resolve()
  73. .then(function() {
  74. if(!(self.type == 1 || self.aa == 3)){ //健管求助全科时,求助内容非必填
  75. if(!validRequired(data)) throw new Error("必输校验失败");
  76. }
  77. })
  78. .then(function() {
  79. return data;
  80. });
  81. },
  82. // 图片缩放
  83. scaleRefresh = function (dom) {
  84. ImagesZoom.init({
  85. "elem": dom,
  86. "delBack": function(index) {
  87. $(".pic-count").text($(".pic-count").text() - 1); //删除图片后的回调
  88. $(dom).find("li").eq(index).remove();
  89. }
  90. });
  91. },
  92. //获取压缩图片路径
  93. uploadCompressImg = function (cb) {
  94. imgIndex = 0;
  95. imgs = document.querySelectorAll(".add-img li img");
  96. if(imgs.length > 0) {
  97. compressImg(cb);
  98. } else {
  99. cb();
  100. }
  101. },
  102. upImg = function(imgUrl, cb) {
  103. var task = plus.uploader.createUpload(server + "upload/chat", {
  104. method: "post"
  105. }, function(t, sta) {
  106. if(sta == 200) {
  107. var msg = t.responseText;
  108. var oImg = JSON.parse(msg);
  109. var imgUrl = oImg.urls;
  110. var re = new RegExp("\\\\", "g");
  111. imgUrl = imgUrl.replace(re, "/");
  112. self.type==1?
  113. imgUrls.push({content: imgUrl, type: 2})
  114. : imgUrls.push(imgUrl);
  115. cb();
  116. } else {
  117. mui.toast("上传图片失败!");
  118. }
  119. });
  120. task.addFile(imgUrl, {});
  121. task.start();
  122. },
  123. //压缩图片
  124. compressImg = function (cb) {
  125. if(imgIndex >= imgs.length){
  126. if(cb) cb();
  127. return;
  128. }
  129. var path, prepath = "_doc/press_img/";
  130. path = imgs[imgIndex].getAttribute("src");
  131. imgIndex++;
  132. var aImg = path.split("/");
  133. var imgName = aImg[aImg.length - 1];
  134. plus.zip.compressImage({
  135. src: path,
  136. dst: prepath + imgName,
  137. quality: 20,
  138. overwrite: true
  139. }, function(succ) {
  140. var url = succ.target;
  141. var size = succ.size;
  142. var width = succ.width;
  143. var height = succ.height;
  144. upImg(url, function(){
  145. compressImg(cb);
  146. });
  147. }, function(err) {
  148. console.error("压缩失败:" + err.message);
  149. if(err.message == "文件不存在") {
  150. mui.toast(err.message);
  151. plus.nativeUI.closeWaiting();
  152. plus.webview.currentWebview().reload();
  153. }
  154. });
  155. },
  156. // 上传并获取图片url地址(多张图片以“,”分割)
  157. getImagesPromise = function() {
  158. return new Promise(function(resolve, reject) {
  159. var uploadImgUrl = getCompressImg();
  160. if(uploadImgUrl) {
  161. resolve({urls:uploadImgUrl});
  162. } else {
  163. // 没有附件
  164. resolve({urls: null})
  165. }
  166. });
  167. },
  168. //获取患者咨询的内容
  169. getConsultLog = function(){
  170. sendPost("doctor/consult/getConsultLog", {
  171. consultCode:self.consultInfo.consult
  172. }, null, function(res){
  173. consultLog = res.data;
  174. });
  175. },
  176. // 发送消息
  177. sendMsg = function( data){
  178. Promise.all(_.map(imgUrls,function(msg){
  179. return new Promise(function(resolve, reject) {
  180. im.sendGroupMsg(docInfo.code, data.code, msg.content, msg.type,function() {
  181. resolve(true);
  182. }, 2, function() {
  183. resolve(true);
  184. })
  185. })
  186. })).then(function() {
  187. plus.nativeUI.closeWaiting();
  188. //跳转求助im
  189. var zhidingzixun = plus.webview.getWebviewById("zhidingzixun");
  190. if(zhidingzixun)
  191. mui.fire(zhidingzixun, 'refresh')
  192. mui.openWindow({
  193. url:"../../message/html/qiuzhuqunliao.html",
  194. id: "qiuzhuqunliao"+ new Date().getTime(),
  195. extras: {
  196. groupCode: data.code,
  197. groupName: data.members[0].groupName,
  198. openId: self.openId
  199. }
  200. })
  201. }).catch(function(){
  202. plus.nativeUI.closeWaiting();
  203. })
  204. },
  205. closeOpener = function(wv){
  206. if(wv){
  207. if(self.openId == wv.id){
  208. wv.close();
  209. } else {
  210. closeOpener(wv.opener());
  211. wv.close();
  212. }
  213. }
  214. },
  215. bindEvents = function() {
  216. // 添加附件
  217. $imgArea.on('tap', '.add', function() {
  218. showActionSheet($imgArea[0], this);
  219. });
  220. // 提交咨询
  221. $sumit.on('tap', function() {
  222. plus.nativeUI.showWaiting();
  223. validInputsPromise().then(function(data) {
  224. return data;
  225. }).then(function(data) {
  226. //type 1咨询 2求助
  227. if(self.type == 1){
  228. var parms = {
  229. type: self.type,
  230. talkDoctor: self.doctor,
  231. talkDoctorName: self.doctorName,
  232. doctorType: self.doctorType,
  233. consult: self.consultInfo.consult,
  234. patient: self.consultInfo.patient,
  235. patientName: self.consultInfo.name,
  236. patientInclude: self.type==1? 1: "",
  237. name: self.consultInfo.name + "咨询(" + new Date().format('MM-dd')+")"
  238. }
  239. sendPost("/doctor/talkgroup/create", parms, null, function(res1){
  240. if(res1.status == 200){
  241. //将患者的咨询信息也发布到讨论组里,类型值为8
  242. var c = 0;
  243. Promise.all(_.map(consultLog,function(log){
  244. //解决将健管的回复的内容也发布到讨论组中
  245. if(log.type != 1){
  246. //咨询的信息,文本类型值为8,图片类型值为9
  247. var type = 8;
  248. if(log.chatType == 2){
  249. type = 9;
  250. }
  251. c = c+1;
  252. return new Promise(function(resolve, reject) {
  253. im.sendGroupMsg(self.consultInfo.patient, res1.data.code, log.content, type,function() {
  254. console.log(c);
  255. resolve(true);
  256. }, 2, function() {
  257. resolve(true);
  258. })
  259. });
  260. }
  261. })).then(function() {
  262. im.sendPrivateMsg(
  263. docInfo.code,
  264. self.consultInfo.patient,
  265. docInfo.name+"已邀请"+ self.doctorName +"参与咨询:"+res1.data.code, 5,
  266. function(res){
  267. imgUrls = [];
  268. if(data.question){
  269. imgUrls.push({content: "沟通情况:"+data.question, type: 6});
  270. }
  271. uploadCompressImg(function() {
  272. sendMsg(res1.data);
  273. });
  274. }, function(res){
  275. plus.nativeUI.closeWaiting();
  276. mui.toast("发送求助信息失败!");
  277. throw new Error("发送求助信息失败!"+ JSON.stringify(res));
  278. })
  279. });
  280. } else {
  281. plus.nativeUI.closeWaiting();
  282. mui.toast("求助失败!");
  283. throw new Error("求助失败!"+ JSON.stringify(res1));
  284. }
  285. })
  286. } else {
  287. imgUrls = [];
  288. var isSwitch = $('#switch').hasClass('mui-active');
  289. uploadCompressImg(function() {
  290. sendPost("/doctor/consult/seekHelp", {
  291. symptoms: data.question,
  292. isSend: isSwitch? 1 : 0,
  293. doctorCode: self.doctor,
  294. images: imgUrls? imgUrls.join(",") : "",
  295. oldConsultCode: self.consultInfo.consult
  296. }, null, function(res){
  297. if(res.status == 200){
  298. mui.toast("求助成功!");
  299. //跳转求助im
  300. var zhidingzixun = plus.webview.getWebviewById("zhidingzixun");
  301. if(zhidingzixun)
  302. mui.fire(zhidingzixun, 'refresh')
  303. var wv = mui.openWindow({
  304. url:"../../message/html/p2p.html",
  305. id: "p2p",
  306. extras: {
  307. otherCode: self.doctor,
  308. otherName: self.doctorName,
  309. otherSex: self.doctorSex,
  310. otherPhoto: self.doctorPhoto,
  311. from: "faqiqiuzhu"
  312. }
  313. })
  314. mui.later(function(){
  315. closeOpener(self);
  316. }, 1000);
  317. wv.show();
  318. } else {
  319. plus.nativeUI.closeWaiting();
  320. mui.toast("求助失败!");
  321. throw new Error("求助失败!"+ JSON.stringify(res));
  322. }
  323. })
  324. });
  325. }
  326. }).catch(function(res){
  327. console.error("err:"+JSON.stringify(res))
  328. plus.nativeUI.closeWaiting();
  329. })
  330. });
  331. // 删除图片
  332. mui(".add-img").on("tap", ".icon-del", function() {
  333. var oli = this.parentElement;
  334. var oul = this.parentElement.parentElement;
  335. oul.removeChild(oli);
  336. });
  337. };
  338. // 页面业务处理流程开始
  339. new Promise(function(resolve, reject) {
  340. mui.init({
  341. beforeback: function() {
  342. if($('.imgzoom-pack').css("display")!=="none"){
  343. $('.imgzoom-x').trigger('click');
  344. return false;
  345. }
  346. }
  347. });
  348. mui.plusReady(function() {
  349. // plus已经准备好,可以往下执行
  350. resolve(true);
  351. });
  352. }).then(function() {
  353. // TODO 防止因为其它ajax error导致存在isLoginOut标识,所有请求回调无法执行的问题
  354. window.localStorage.removeItem("isLoginOut");
  355. // 获取基础环境信息
  356. return getBaseEnvPromise().then(function(env) {
  357. baseEnv = env;
  358. }).then(function() {
  359. self = plus.webview.currentWebview();
  360. if(self.type == 2){
  361. $('#oldConsultCheck').show();
  362. }
  363. // 获取登录医生信息
  364. baseInfo = getBaseInfo();
  365. // $yishengInput.val(baseInfo.accessData.name||"");
  366. $('#zixun_wenti textarea').val(self.consultInfo.title);
  367. if(self.type == 1 || self.aa == 3){
  368. $("#notice").show();
  369. //获得患者咨询的内容
  370. getConsultLog();
  371. }
  372. //图片缩放
  373. scaleRefresh(".upload-img");
  374. // 绑定页面事件
  375. bindEvents();
  376. })
  377. }).catch(function(e) {
  378. plus.nativeUI.closeWaiting();
  379. console && console.error(e);
  380. });