add-consult.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. var d = dialog({contentType:'load', skin:'bk-popup'});
  2. var request = GetRequest(),
  3. doctorCode = request["doctorCode"],
  4. doctorName = decodeURI(request["doctorName"]),
  5. jobName = decodeURI(request["jobName"]),
  6. patientName = window.localStorage.getItem('nowPatientName');
  7. var serverId = ""; //微信图片上传获取的图片id
  8. $(function(){
  9. $("#lbl_patient").text(patientName);
  10. var text = doctorName;
  11. if(jobName){
  12. text += "("+jobName+")"
  13. }
  14. $("#lbl_doctor").text(text);
  15. bindEvents();
  16. getWxSign();
  17. })
  18. function bindEvents(){
  19. //提交咨询
  20. $("#commit").click(function() {
  21. if($(this).hasClass("active")){
  22. var txtContent = $("#txtContent").val();
  23. if(txtContent){
  24. txtContent = utf16toEntities(txtContent.replace(/\s+/g,""));
  25. }
  26. var data = {
  27. type: 1,
  28. symptoms: txtContent || "",
  29. voice: ""
  30. };
  31. if (validate(data)) {
  32. //验证通过执行
  33. d.showModal();
  34. var images = getImages();
  35. if(images.length == 0){
  36. $("#commit").css("pointer-events","none");
  37. serverId = '';
  38. doSubmit(data);
  39. }
  40. else{
  41. uploadImage(data);
  42. }
  43. }
  44. }
  45. });
  46. }
  47. //验证信息
  48. function validate(data) {
  49. if (data.symptoms.length < 10) {
  50. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请至少用10个字描述您的症状'}).show();
  51. return false;
  52. }
  53. if (data.symptoms.length > 500) {
  54. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请在500字以内完成您的症状描述'}).show();
  55. return false;
  56. }
  57. // if(!$(".input-group-pack > input[type=checkbox]").attr("checked")){
  58. // dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请勾选"我已阅读咨询说明"'}).show();
  59. // return false;
  60. // }
  61. return true;
  62. }
  63. function chooseImage(){
  64. wx.chooseImage({
  65. count: 9-getImages().length,
  66. success: function (res) {
  67. for (var i in res.localIds) {
  68. appendFile(res.localIds[i]);
  69. }
  70. }
  71. });
  72. }
  73. function uploadImage(data){
  74. $("#commit").css("pointer-events","none");
  75. var images = getImages();
  76. if (images.length == 0) {
  77. return;
  78. }
  79. var i = 0, length = images.length;
  80. serverId = "";
  81. function upload() {
  82. wx.uploadImage({
  83. localId: images[i],
  84. isShowProgressTips: 0,
  85. success: function (res) {
  86. i++;
  87. if(serverId.length == 0){
  88. serverId = res.serverId;
  89. }
  90. else{
  91. serverId =serverId + "," + res.serverId;
  92. }
  93. if (i < length) {
  94. upload();
  95. }
  96. if(i == images.length){
  97. doSubmit(data);
  98. }
  99. },
  100. fail: function (res) {
  101. $("#commit").css("pointer-events","");
  102. alert(JSON.stringify(res));
  103. }
  104. });
  105. }
  106. upload();
  107. }
  108. // 添加文件
  109. function appendFile(p) {
  110. var amount = getImages().length;
  111. if (amount >= 8) {
  112. $("#add_img_li").hide();
  113. }
  114. if(amount < 9){
  115. var $li = $('<li>' + ' <img src="' + p + '" data-src="' + p + '" onclick="viewImg(this)">' + ' <a href="javascript:;" class="del-img" onclick="delImg(this)"><i class="iconfont icon-laji"></i></a>' + '</li>');
  116. var $add_img_li = $("#add_img_li");
  117. $add_img_li.before($li);
  118. $("#image_tips").text(getImages().length + "/9");
  119. }
  120. }
  121. //获取需要上传的图片
  122. function getImages() {
  123. var images = [];
  124. $("#img_ul").find("img").each(function() {
  125. var imgSrc = $(this).attr("data-src");
  126. images.push(imgSrc);
  127. });
  128. return images;
  129. }
  130. //查看图片
  131. function viewImg(dom) {
  132. var $img = $(dom);
  133. var thissrc = $img.attr("data-src");
  134. var mWid = $(window).width();
  135. var mHei = $(window).height();
  136. var nHtml = '<div class="delimgpop"><div class="del-img-box"><div class="del-img-con"><img class="del-pop-img" src="' + thissrc + '" style="max-width:' + mWid + 'px; max-height:' + mHei + 'px;"></div></div></div>';
  137. $("body").append(nHtml);
  138. $(".delimgpop").click(function() {
  139. $(this).remove()
  140. });
  141. };
  142. //删除图片
  143. function delImg(dom) {
  144. var $li = $(dom).parent();
  145. $li.remove();
  146. $("#image_tips").text(getImages().length + "/9");
  147. var amount = getImages().length;
  148. if(amount <= 8){
  149. $("#add_img_li").show();
  150. }
  151. return;
  152. }
  153. //微信sdk配置出错
  154. wx.error(function (res) {
  155. alert("wx.error:" + res.errMsg);
  156. });
  157. //提交咨询
  158. function doSubmit(data) {
  159. data.mediaIds = serverId;
  160. data.doctor = doctorCode;
  161. var url = 'patient/consult/add';//新增三师或家庭
  162. sendPost(url, data, 'json', 'post', submitFailed, submitSuccess);
  163. }
  164. function submitSuccess(res) {
  165. if (res.status == 200) {
  166. $("#commit").css("pointer-events","");
  167. d.close();
  168. //页面跳转到消息页面
  169. localStorage.setItem("signInfo_tab", 0);
  170. var direct_url = "../../qygl/html/sign_info.html?doctor="+doctorCode+"&consult="+res.data.consult;
  171. window.location.href = direct_url;
  172. } else {
  173. submitFailed(res);
  174. }
  175. }
  176. function submitFailed(res) {
  177. $("#commit").css("pointer-events","");
  178. d.close();
  179. if (res && res.msg) {
  180. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:res.msg}).show();
  181. } else {
  182. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'提交失败'}).show();
  183. }
  184. }
  185. function getWxSign(){
  186. //从后台那边获取签名等信息
  187. var params = {};
  188. params.pageUrl = window.location.href;
  189. $.ajax(server + "weixin/getSign", {
  190. data: params,
  191. dataType: "json",
  192. type: "post",
  193. success: function(res){
  194. if (res.status == 200) {
  195. var t = res.data.timestamp;
  196. var noncestr = res.data.noncestr;
  197. var signature = res.data.signature;
  198. wx.config({
  199. // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  200. appId: appId, // 必填,公众号的唯一标识
  201. timestamp: t, // 必填,生成签名的时间戳
  202. nonceStr: noncestr, // 必填,生成签名的随机串
  203. signature: signature,// 必填,签名,见附录1
  204. jsApiList: [
  205. 'chooseImage',
  206. 'uploadImage'
  207. ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  208. });
  209. }
  210. else{
  211. // dialog({
  212. // title:'提示',
  213. // skin:"ui-dialog ax-popup pror",
  214. // content:"获取微信签名失败",
  215. // ok: function (){}
  216. // }).showModal();
  217. }
  218. }
  219. });
  220. }