questionnaire.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. //var myScroll;
  2. var recordTimeObj = recordTimeObj(); // 录音倒计时
  3. var userInfo = JSON.parse(localStorage.getItem(agentName));
  4. var ct_id;
  5. var pb_id=0;
  6. var type = 0;
  7. var quesNum = 1;
  8. var quesArray=[]; //用于上一题的访问
  9. var MIN_SOUND_TIME = 500;
  10. var startTimestamp = null;
  11. var recordTimer = null;
  12. var realStartTime = 0;
  13. $(function(){
  14. getPermission();
  15. getWechatInfo();
  16. $('.questionnaire-list').delegate('.questionnaire-box input[type="radio"]', 'click', function(){
  17. quesNum ++;
  18. var dc_answer = [$(this).val()];
  19. var pb_id = quesArray[quesArray.length-1];
  20. submitQuestion(pb_id, dc_answer);
  21. });
  22. $('#prev_question').on('click',function(){
  23. quesNum --;
  24. getQuestion('prev')
  25. });
  26. $('#next_question').on('click',function(){
  27. quesNum ++;
  28. var $inputs = $('.questionnaire-box input[name="question"]:checked');
  29. if($inputs.length < 1){
  30. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'未选择答案'}).show();
  31. }else{
  32. var dc_answer = [];
  33. $.each($inputs, function(i,input){
  34. dc_answer.push($(input).val());
  35. });
  36. var pb_id = quesArray[quesArray.length-1];
  37. //getQuestion();
  38. submitQuestion(pb_id, dc_answer);
  39. }
  40. });
  41. $('.btn-more-questionnaire').on('click', function(){
  42. // 展开更多检测
  43. //$('.questionnaire-more').hide();
  44. $('.questionnaire-more-box').show();
  45. });
  46. $('#btnRecord').on('click', function(){
  47. // 显示录音界面
  48. resetRecordSeconds();
  49. $('.record-dialog').addClass('fadeIn');
  50. setTimeout(function() {
  51. $('.record-dialog').removeClass('hidden');
  52. }, 0);
  53. setTimeout(function() {
  54. $('.record-dialog').removeClass('fadeIn');
  55. }, 800);
  56. });
  57. $('.record-btn').find('.back').on('click', function(){
  58. // 关闭录音界面
  59. closeRecord();
  60. });
  61. $('.record-btn').find('.start').on('click', function(){
  62. // 开始录音
  63. var that = $(this);
  64. startTimestamp = new Date().getTime();
  65. recordTimer = setTimeout(function(){
  66. wx.startRecord({
  67. success: function(){
  68. realStartTime = new Date().getTime();
  69. that.hide();
  70. $('.record-btn').find('.stop').show();
  71. //中间秒数的样式
  72. recordTimeObj.recordTime('start', function(){
  73. });
  74. },
  75. fail: function(){
  76. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'录音失败!请重试'}).show();
  77. }
  78. });
  79. wx.onVoiceRecordEnd({
  80. // 录音时间超过15秒没有停止的时候会执行 complete 回调
  81. complete: function (res) {
  82. serverId = res.localId;
  83. $('.record-result').show();
  84. },
  85. fail: function(){
  86. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'录音失败!请重试'}).show();
  87. }
  88. });
  89. },MIN_SOUND_TIME);
  90. stopRecordTimer = setTimeout(function(){
  91. wx.stopRecord({
  92. success: function (res) {
  93. serverId = res.localId;
  94. recordTimeObj.recordTime('stop');
  95. $('.record-result').show();
  96. },
  97. fail: function(){
  98. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'录音失败!请重试'}).show();
  99. //重置样式
  100. resetRecordSeconds();
  101. }
  102. });
  103. },15500)
  104. });
  105. $('.record-btn').find('.stop').on('click', function(){
  106. // 停止录音
  107. var that = $(this);
  108. stopRecordTimer && clearTimeout(stopRecordTimer);
  109. var endTimestamp = new Date().getTime();
  110. var times = endTimestamp - startTimestamp;
  111. var realTimes = endTimestamp - realStartTime;
  112. if(times < MIN_SOUND_TIME || realTimes < MIN_SOUND_TIME){
  113. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'录音时间太短!'}).show();
  114. //重置样式
  115. resetRecordSeconds();
  116. startTimestamp = 0;
  117. realStartTime = 0;
  118. //小于300ms,不录音
  119. recordTimer && clearTimeout(recordTimer);
  120. wx.stopRecord({
  121. success: function (res) {
  122. },
  123. fail: function(){}
  124. });
  125. }else{
  126. wx.stopRecord({
  127. success: function (res) {
  128. serverId = res.localId;
  129. recordTimeObj.recordTime('stop');
  130. $('.record-result').show();
  131. },
  132. fail: function(){
  133. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'录音失败!请重试'}).show();
  134. //重置样式
  135. resetRecordSeconds();
  136. }
  137. });
  138. }
  139. });
  140. $('.record-result-btn').find('.cancel').on('click', function(){
  141. // 重新录音
  142. $('.record-result').hide();
  143. resetRecordSeconds();
  144. //如果录音是在播放的状态,停止播放
  145. if($('.record-result-btn').find('.btn-action').hasClass('pause')){
  146. wx.stopVoice({
  147. localId: serverId // 需要停止的音频的本地ID,由stopRecord接口获得
  148. });
  149. }
  150. });
  151. $('.record-result-btn').find('.ok').on('click', function(){
  152. // 确认录音
  153. var st = $('.record-time .time').find('span').html();
  154. var st1 = st.split('.')[0];
  155. var st2 = st.split('.')[1];
  156. var et1 = 14 - st1;
  157. var et2 = 10 - st2;
  158. var t = et2 < 10 ? ( et1 + '.0' + et2 ) : ( (et1 + 1) + '.00' );
  159. closeRecord();
  160. setTimeout(function() {
  161. $('.record-result').hide();
  162. }, 10);
  163. // 录音展示
  164. $('#recordContent').find('.seconds').html(t);
  165. $('#recordContent').find('.upload').hide();
  166. $('#recordContent').find('.file').show();
  167. //如果录音在播放状态下,停止播放
  168. if($('.record-result-btn').find('.btn-action').hasClass('pause')){
  169. wx.stopVoice({
  170. localId: serverId // 需要停止的音频的本地ID,由stopRecord接口获得
  171. });
  172. }
  173. });
  174. //播放录音
  175. $('.record-result-btn').find('.btn-action').on('click', function(){
  176. var that = $(this);
  177. if(that.hasClass('play')){//播放
  178. that.removeClass('play').addClass('pause');
  179. wx.playVoice({
  180. localId: serverId // 需要播放的音频的本地ID,由stopRecord接口获得
  181. });
  182. }else if(that.hasClass('pause')){
  183. that.removeClass('pause').addClass('play');
  184. wx.pauseVoice({//暂停
  185. localId: serverId // 需要暂停的音频的本地ID,由stopRecord接口获得
  186. });
  187. }
  188. //录音播放完毕
  189. wx.onVoicePlayEnd({
  190. success: function (res) {
  191. var localId = res.localId; // 返回音频的本地ID
  192. that.removeClass('pause').addClass('play');
  193. }
  194. });
  195. });
  196. $('.questionnaire-more-box').on('click', '.remove', function(){
  197. // 删除文件,重新上传
  198. var that = $(this);
  199. var pObj = that.parents('.file');
  200. var loadBox = pObj.siblings('.upload');
  201. pObj.find('img').attr('src', ' ');
  202. pObj.find('img').attr('data-src', ' ');
  203. pObj.hide();
  204. loadBox.show();
  205. });
  206. $('#uploadFace').on('change', function(){
  207. // 面部图片上传
  208. var that = $(this);
  209. $('.btn-bar').addClass('hidden');
  210. clip_photo(that, 1);
  211. });
  212. $('#uploadTongue').on('change', function(){
  213. // 舌部图片上传
  214. var that = $(this);
  215. $('.btn-bar').addClass('hidden');
  216. clip_photo(that, 2);
  217. });
  218. //点击提交,提交录音
  219. $('#btn_submit').on('click',function(){
  220. wx.uploadVoice({
  221. localId: serverId, // 需要上传的音频的本地ID,由stopRecord接口获得
  222. isShowProgressTips: 1, // 默认为1,显示进度提示
  223. success: function (res) {
  224. var wechatServerId = res.serverId; // 返回音频的服务器端ID
  225. submitVoice(wechatServerId);
  226. }
  227. });
  228. });
  229. });
  230. function closeRecord(){
  231. // 关闭录音界面
  232. $('.record-dialog').addClass('bounceOutRight');
  233. setTimeout(function() {
  234. $('.record-dialog').addClass('hidden');
  235. $('.record-dialog').removeClass('bounceOutRight');
  236. }, 800);
  237. }
  238. function resetRecordSeconds(){
  239. // 重置倒计时样式
  240. $('.record-time .time').find('span').html('15.00');
  241. $('.record-time').find('.pie').attr('style', '');
  242. $('.record-btn').find('.stop').hide();
  243. $('.record-btn').find('.start').show();
  244. }
  245. //剪切图片
  246. function clip_photo(obj, type){
  247. $('#wrapper').addClass('hidden');
  248. var photo = obj[0];
  249. var code = userInfo.uid;
  250. lrz(photo.files[0]).then(function (rst) {
  251. var url = rst.base64;
  252. new AlloyCrop({
  253. image_src: url,
  254. width: document.documentElement.clientWidth - 100,
  255. height: document.documentElement.clientWidth - 100,
  256. ok_text: "保存",
  257. cancel_text: "取消",
  258. output: 1,
  259. ok: function (base64, canvas) {
  260. $("#wrapper").removeClass("hidden");
  261. setTimeout(function(){
  262. $('.btn-bar').removeClass('hidden')
  263. },0);
  264. var data=base64.split(',')[1];
  265. data=window.atob(data);
  266. var ia = new Uint8Array(data.length);
  267. for (var i = 0; i < data.length; i++) {
  268. ia[i] = data.charCodeAt(i);
  269. }
  270. var blob=new Blob([ia],{type:"image/png",endings:'transparent'});
  271. var fd=new FormData();
  272. fd.append('ct_id',ct_id);
  273. fd.append('type',type);
  274. fd.append('patientCode',code);
  275. fd.append('file',blob,'image.png');
  276. var new_url = URL.createObjectURL(blob);
  277. appendFile(new_url, obj);
  278. obj.val('');
  279. $.ajax(server_jm+'/medicine/physicalExamination/dillphoneimgdata', {
  280. data: fd,
  281. dataType: 'json',
  282. contentType: false,
  283. cache: false,
  284. processData: false,
  285. beforeSend: function(request) {
  286. //request.setRequestHeader("userAgent", userAgent);
  287. },
  288. type: 'post',
  289. error: function(res) {
  290. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'图片上传失败!'}).show();
  291. if(res.status == 999 || res.status == 998 || res.status == 997){
  292. loginUrl(res.status);
  293. return;
  294. }
  295. clickCount = 0;
  296. },
  297. success: function(res) {
  298. if(res.status == 999 || res.status == 998 || res.status == 997){
  299. loginUrl(res.status);
  300. return;
  301. }
  302. var params = {};
  303. params.photo = res.urls;
  304. var patientUrl = res.urls;
  305. clickCount = 0;
  306. }
  307. });
  308. },
  309. cancel: function () {
  310. $("#wrapper").removeClass("hidden");
  311. obj.val('');
  312. setTimeout(function(){
  313. $('.btn-bar').removeClass('hidden')
  314. },0);
  315. return;
  316. }
  317. });
  318. })
  319. .catch(function (err){
  320. // 处理失败会执行
  321. //console.log(err)
  322. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:err}).show();
  323. });
  324. }
  325. function appendFile(p, obj) {
  326. var pObj = obj.parents('.upload');
  327. var fileBox = pObj.siblings('.file');
  328. pObj.hide();
  329. fileBox.show();
  330. fileBox.find('img').attr('src', p);
  331. fileBox.find('img').attr("data-src",p);
  332. //iscrollRefresh();
  333. }
  334. //验证权限
  335. function getPermission(){
  336. var code = userInfo.uid;
  337. //判断是否有访问权限,若有,则进入答题页面
  338. var loading = dialog({contentType:'load', skin:'bk-popup'});
  339. loading.show();
  340. sendPost(server_jm+"/medicine/physicalExamination/insertslip", {patientCode: code}, "json", "get", function(){
  341. }, function(data){
  342. loading.close();
  343. if(data.isSignJM==2){//已签约的集美市民---->>进入第一题试题
  344. ct_id = data.recordset.ct_id;//咨询编号 ,需要保存下来,之后接口有用到
  345. //获取题目
  346. getQuestion();
  347. }else if(data.isSignJM==0 || data.isSignJM==3){ //未签约集美,且非厦门居民--->>跳转到敬请期待页面
  348. // window.location.href='./expectancy-page.html';
  349. }else{//跳转到签约说明页面 data.isSignJM==1
  350. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'只有与集美区进行家庭医生签约的用户才可享受此签约特色服务'}).show();
  351. //window.location.href='http://www.xmtyw.cn/wlyy/wx/html/qygl/html/signing-share2.html';
  352. }
  353. });
  354. }
  355. function getQuestion(quesType){
  356. var code = userInfo.uid;
  357. var pbId = pb_id;
  358. if(quesType == 'prev'){
  359. if(quesArray.length<=2){
  360. pbId = 0;
  361. }else{
  362. pbId = quesArray[quesArray.length-3];
  363. }
  364. }
  365. if(quesNum >= 2 && $('#prev_question').hasClass('hidden')){
  366. $('#prev_question').removeClass('hidden');
  367. }else if(quesNum < 2){
  368. $('#prev_question').addClass('hidden');
  369. }
  370. var loading = dialog({contentType:'load', skin:'bk-popup'});
  371. loading .show();
  372. var width = parseFloat($('.questionnaire-progress').css('width'));
  373. var left = parseFloat($('.progress-box').css('left'));
  374. sendPost(server_jm+"/medicine/physicalExamination/findQuestion", {
  375. pb_id:pbId,
  376. ct_id:ct_id,
  377. patientCode:code
  378. }, "json", "get", function(){
  379. }, function(data){
  380. loading.close();
  381. if(data.status==200){
  382. var question = data.recordset;//这边还需要判断是否是最后一题 最后一题会返回空对象 {} 返回空对象,跳转到面部等检查页面
  383. var pb_code = question.pb_id; //查找 第一题的题目pb_id为0 ,但是提交第一题的答案是提交这个pb_code
  384. if(pb_code){
  385. pb_id = pb_code;
  386. if(quesType == 'prev'){
  387. quesArray.pop();
  388. var minLeft = -parseFloat($('.progress-box').css('margin-left'));
  389. if(quesNum>1 && left > minLeft+30){
  390. left -= 30;
  391. $('.progress-box').css('left',left+'px');
  392. }else if(quesNum == 1){
  393. $('.progress-box').css('left',minLeft+'px');
  394. }
  395. }else{
  396. if(quesNum>1 && left < width-30){
  397. left += 30;
  398. $('.progress-box').css('left',left+'px');
  399. }
  400. quesArray.push(pb_code);
  401. }
  402. $('#questionnaire_list').html('');
  403. var choice = question.pb_alternative.split(/\r+|\s+/);
  404. var title = question.pb_title;
  405. var cHtml = '';
  406. var ctype = '';
  407. var className = '';
  408. if(question.i_multiselect == '1'){
  409. cHtml = '(单选)';
  410. ctype = 'radio';
  411. className = 'single-choice';
  412. }else{
  413. cHtml = '(多选)';
  414. ctype = 'checkbox';
  415. className = 'multi-choice';
  416. type = 1;
  417. }
  418. if(choice && choice.length>0){
  419. var sHtml = '';
  420. $.each(choice, function(i, item){
  421. sHtml += '<li>'
  422. +'<label>'
  423. +'<input value="'+item+'" type="'+ctype+'" name="question">'
  424. +'<span></span>'
  425. +item
  426. +'</label>'
  427. +'</li>'
  428. })
  429. }
  430. var html = '<div class="questionnaire-box '+className+'">'
  431. +'<div class="title">'
  432. +quesNum+'.'+title+'<span>'+cHtml+'</span><font>*</font>'
  433. +'</div>'
  434. +'<div class="answer">'
  435. +'<ul>'
  436. +sHtml
  437. +'</ul>'
  438. +'</div>'
  439. +'</div>';
  440. $('#questionnaire_list').append(html);
  441. }else{//不是最后一题
  442. //dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'最后一题了!'}).show();
  443. $('#questionnaire_list').html('');
  444. $('.btn-bar').removeClass('hidden');
  445. $('.bottom-btn').addClass('hidden');
  446. $('.questionnaire-more-box').show();
  447. $('.progress-box').css('left',width+'px');
  448. }
  449. }else{
  450. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:data.msg}).show();
  451. }
  452. });
  453. }
  454. //提交试卷
  455. function submitQuestion(pb_id, dc_answer, quesType){
  456. var code = userInfo.uid;
  457. sendPost(server_jm+"/medicine/physicalExamination/handleExam", {
  458. ct_id: ct_id,
  459. pb_id: pb_id,
  460. type: type,
  461. dc_answer: JSON.stringify(dc_answer),
  462. patientCode: code
  463. }, "json", "get", function(){
  464. }, function(data){
  465. if(data.status==200){
  466. getQuestion(quesType);
  467. }else{
  468. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'提交失败!'+data.msg}).show();
  469. }
  470. });
  471. }
  472. //提交录音
  473. function submitVoice(mediaId){
  474. var code = userInfo.uid;
  475. var loading = dialog({contentType:'load', skin:'bk-popup'});
  476. loading .show();
  477. sendPost(server_jm+"/medicine/physicalExamination/dealVoice", {
  478. mediaId: mediaId,
  479. ct_id: ct_id,
  480. patientCode: code,
  481. type: 3
  482. }, "json", "get", function(){
  483. }, function(data){
  484. loading.close();
  485. if(data.status==200){
  486. window.location.href='./medical-examination-result.html?ctId='+ct_id;
  487. }else{
  488. window.location.href='./submit-fail.html';
  489. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'提交失败!'+data.msg}).show();
  490. }
  491. });
  492. }
  493. function getWechatInfo(){
  494. //从后台那边获取签名等信息
  495. var params = {};
  496. var url1 = window.location.href;
  497. params.pageUrl = url1;
  498. $.ajax("/wlyy/weixin/getSign", {
  499. data: params,
  500. dataType: "json",
  501. type: "post",
  502. success: function(res){
  503. if (res.status == 200) {
  504. var t = res.data.timestamp;
  505. var noncestr = res.data.noncestr;
  506. var signature = res.data.signature;
  507. wx.config({
  508. appId: appId, // 必填,公众号的唯一标识
  509. timestamp: t, // 必填,生成签名的时间戳
  510. nonceStr: noncestr, // 必填,生成签名的随机串
  511. signature: signature,// 必填,签名,见附录1
  512. jsApiList: [
  513. 'chooseImage',
  514. 'uploadImage',
  515. 'startRecord',
  516. 'stopRecord',
  517. 'onVoiceRecordEnd',
  518. 'playVoice',
  519. 'pauseVoice',
  520. 'stopVoice',
  521. 'onVoicePlayEnd',
  522. 'uploadVoice',
  523. 'getNetworkType'
  524. ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  525. });
  526. // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
  527. wx.ready(function(){
  528. wx.getNetworkType({
  529. success: function (res) {
  530. networkStatus = res.networkType; // 返回网络类型2g,3g,4g,wifi
  531. }
  532. });
  533. });
  534. }
  535. }
  536. });
  537. }