faqizixun.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // 基本信息(包括userAgent)
  2. var baseInfo = null,
  3. // 基础环境信息(包括当前webview、encry相关)
  4. baseEnv = null;
  5. // 咨询医生
  6. var $yishengInput = $("#zixun_yisheng").find("input"),
  7. // 咨询问题
  8. $wentiInput = $("#zixun_wenti").find("textarea"),
  9. // 添加附件图标按钮
  10. $imgArea = $('.add-img'),
  11. // 提交咨询
  12. $sumit = $('#sumit');
  13. // 关闭重新登录检查(防止一个ajax error导致所有请求成功无法回调)
  14. var checkReload = false;
  15. // 获取登录相关信息
  16. var getBaseInfo = function() {
  17. // 登录的相关信息
  18. var userAgent = JSON.parse(plus.storage.getItem("userAgent"));
  19. return {
  20. userAgent: userAgent,
  21. accessData: $.extend({},baseEnv.webview.accessData)
  22. }
  23. },
  24. // 获取基础环境信息
  25. getBaseEnvPromise = function () {
  26. var env = {
  27. webview: plus.webview.currentWebview()
  28. };
  29. // 因为有异步请求,需要等待数据返回之后进行处理,所以返回Promise
  30. return Promise.resolve().then(function(res) {
  31. return env;
  32. });
  33. },
  34. // 获取表单数据
  35. getInputs = function() {
  36. var data = {
  37. docName: $yishengInput.val().trim(), // 姓名
  38. question: $wentiInput.val().trim(),
  39. images: null// 附件
  40. };
  41. return data;
  42. },
  43. // 必输验证
  44. validRequired = function(data) {
  45. var fieldsStr = "question",
  46. fieldMap = {
  47. question: "咨询问题"
  48. },
  49. fields = fieldsStr.split(","),
  50. invalidField;
  51. // 某个字段为""、null、undefined,则该字段不能通过必输校验
  52. invalidField = _.find(fields,function(key) {
  53. return !data[key]&&data[key]!==0;
  54. });
  55. if(invalidField) {
  56. mui.toast(fieldMap[invalidField]+'不能为空');
  57. return false;
  58. } else {
  59. return true;
  60. }
  61. },
  62. // 所有输入验证
  63. validInputsPromise = function() {
  64. // 表单数据
  65. var data = getInputs();
  66. return Promise.resolve()
  67. .then(function() {
  68. if(!validRequired(data)) throw new Error("必输校验失败");
  69. })
  70. .then(function() {
  71. return data;
  72. });
  73. },
  74. // 图片缩放
  75. scaleRefresh = function (dom) {
  76. ImagesZoom.init({
  77. "elem": dom,
  78. "delBack": function(index) {
  79. $(".pic-count").text($(".pic-count").text() - 1); //删除图片后的回调
  80. $(dom).find("li").eq(index).remove();
  81. }
  82. });
  83. },
  84. //获取压缩图片路径
  85. uploadCompressImg = function (cb) {
  86. var imgs = document.querySelectorAll(".add-img li img");
  87. if(imgs.length > 0) {
  88. plus.nativeUI.showWaiting();
  89. var path, prepath = "_doc/press_img/";
  90. for(var i = 0; i < imgs.length; i++) {
  91. path = imgs[i].getAttribute("src");
  92. var aImg = path.split("/");
  93. var imgName = aImg[aImg.length - 1];
  94. compressImg(path, prepath + imgName, cb);
  95. }
  96. return imgs.length;
  97. }
  98. return 0;
  99. },
  100. //压缩图片
  101. compressImg = function (path, dstpath, cb) {
  102. plus.zip.compressImage({
  103. src: path,
  104. dst: dstpath,
  105. quality: 20,
  106. overwrite: true
  107. }, function(succ) {
  108. var url = succ.target;
  109. var size = succ.size;
  110. var width = succ.width;
  111. var height = succ.height;
  112. if(cb && $.isFunction(cb)) {
  113. cb(url);
  114. }
  115. }, function(err) {
  116. console.error("压缩失败:" + err.message);
  117. if(err.message == "文件不存在") {
  118. mui.toast(err.message);
  119. plus.nativeUI.closeWaiting();
  120. plus.webview.currentWebview().reload();
  121. }
  122. });
  123. },
  124. // 上传并获取图片url地址(多张图片以“,”分割)
  125. getImagesPromise = function() {
  126. return new Promise(function(resolve, reject) {
  127. var uploadImgUrl = getCompressImg();
  128. if(uploadImgUrl) {
  129. resolve({urls:uploadImgUrl});
  130. } else {
  131. // 没有附件
  132. resolve({urls: null})
  133. }
  134. });
  135. },
  136. toP2pPage = function(sessionId) {
  137. mui.openWindow({
  138. url: "../../message/html/p2p.html",
  139. id:"p2p",
  140. // 跳转页面传参
  141. extras: {
  142. sessionId: sessionId,
  143. sessionName: baseInfo.accessData.name
  144. }
  145. });
  146. },
  147. bindEvents = function() {
  148. // 添加附件
  149. $imgArea.on('tap', '.add', function() {
  150. showActionSheet($imgArea[0], this);
  151. });
  152. // 提交咨询
  153. $sumit.on('tap', function() {
  154. plus.nativeUI.showWaiting();
  155. var url = server+"/doctor/work_scheduling/is_doctor_working";
  156. var data={doctor:baseInfo.accessData.doctor};
  157. getReqPromise(url,data).then(function(res) {
  158. if(res.status == 200) {
  159. return res.data;
  160. } else {
  161. mui.toast(res.msg);
  162. return null;
  163. }
  164. }).then(function(data) {
  165. var status = data.status;
  166. if(status) {//0:医生不接受咨询 1:医生接受咨询 2:医生当前不在工作时间
  167. if(status=="0"){
  168. plus.nativeUI.closeWaiting();
  169. dialog({
  170. content: '对不起,该医生已暂停接受咨询,您可选择其他医生进行咨询',
  171. cancelValue: '我知道了',
  172. cancel: function () {
  173. return;
  174. }
  175. }).showModal();
  176. return false;
  177. }else if(status=="1"){
  178. validInputsPromise().then(function(data) {
  179. return data;
  180. }).then(function(data) {
  181. var msgSended = false;
  182. var images = [];
  183. var imageNum = uploadCompressImg(function(imgUrl) {
  184. var task = plus.uploader.createUpload(server + "upload/chat", {
  185. method: "post"
  186. }, function(t, sta) {
  187. if(sta == 200) {
  188. var msg = t.responseText;
  189. var oImg = JSON.parse(msg);
  190. var imgUrl = oImg.urls;
  191. var re = new RegExp("\\\\", "g");
  192. imgUrl = imgUrl.replace(re, "/");
  193. images.push(imgUrl);
  194. }
  195. });
  196. task.addFile(imgUrl, {});
  197. task.start();
  198. });
  199. if(imageNum) {
  200. var count = 1000;
  201. var timer = window.setInterval(function() {
  202. count--;
  203. if(count==0 || imageNum == images.length) {
  204. window.clearInterval(timer);
  205. timer = null;
  206. var param = {doctorCode: baseInfo.accessData.doctor, images:images.join(",")}
  207. if(data.question) {
  208. param.symptoms = data.question
  209. }
  210. getReqPromise("doctor/consult/famousAdd",param)
  211. .then(function(res) {
  212. plus.nativeUI.closeWaiting();
  213. if(res.status == 200) {
  214. toP2pPage(res.data.session_id);
  215. } else {
  216. console && console.error(res.msg);
  217. }
  218. });
  219. }
  220. },500)
  221. } else {
  222. data.question && getReqPromise("doctor/consult/famousAdd",{doctorCode: baseInfo.accessData.doctor, symptoms: data.question})
  223. .then(function(res) {
  224. plus.nativeUI.closeWaiting();
  225. if(res.status == 200) {
  226. toP2pPage(res.data.session_id)
  227. } else {
  228. console && console.error(res.msg);
  229. }
  230. });
  231. }
  232. }).catch(function(e) {
  233. plus.nativeUI.closeWaiting();
  234. console && console.error(e);
  235. });
  236. }else if(data=="2"){
  237. plus.nativeUI.closeWaiting();
  238. dialog({
  239. content: '医生不在工作时间内哦,请在医生咨询计划时间段内咨询',
  240. cancelValue: '我知道了',
  241. cancel: function () {
  242. return;
  243. }
  244. }).showModal();
  245. }
  246. }
  247. }).catch(function(e) {
  248. console && console.error(e);
  249. });
  250. });
  251. // 删除图片
  252. mui(".add-img").on("tap", ".icon-del", function() {
  253. var oli = this.parentElement;
  254. var oul = this.parentElement.parentElement;
  255. oul.removeChild(oli);
  256. });
  257. };
  258. // 页面业务处理流程开始
  259. new Promise(function(resolve, reject) {
  260. mui.init({
  261. beforeback: function() {
  262. if($('.imgzoom-pack').css("display")!=="none"){
  263. $('.imgzoom-x').trigger('click');
  264. return false;
  265. }
  266. }
  267. });
  268. mui.plusReady(function() {
  269. // plus已经准备好,可以往下执行
  270. resolve(true);
  271. });
  272. }).then(function() {
  273. // TODO 防止因为其它ajax error导致存在isLoginOut标识,所有请求回调无法执行的问题
  274. window.localStorage.removeItem("isLoginOut");
  275. // 获取基础环境信息
  276. return getBaseEnvPromise().then(function(env) {
  277. baseEnv = env;
  278. }).then(function() {
  279. // 获取登录医生信息
  280. baseInfo = getBaseInfo();
  281. $yishengInput.val(baseInfo.accessData.name||"");
  282. //图片缩放
  283. scaleRefresh(".upload-img");
  284. // 绑定页面事件
  285. bindEvents();
  286. })
  287. }).catch(function(e) {
  288. plus.nativeUI.closeWaiting();
  289. console && console.error(e);
  290. });