scale.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. const {
  2. post,
  3. config,
  4. getUrlArgObject
  5. } = require('./promise.js');
  6. const $ = require("jquery");
  7. let hasCalc = false;
  8. $(function () {
  9. var scaleInfo;
  10. // 用GetQueryString方法从地址栏获取参数,暂时写死
  11. var msg = JSON.parse(getUrlArgObject('msg'))
  12. var params = msg
  13. if (msg.scaleName) {
  14. post(config.pushScale, params).then((res) => {
  15. const data = res.data.data
  16. if (res.data.code == 0) {
  17. scaleInfo = data;
  18. for (var i = 0; i < scaleInfo.length; i++) {
  19. if (scaleInfo[i].type == 1) {
  20. var scaleList = JSON.parse(scaleInfo[i].content)
  21. renderCalcu(scaleList)
  22. } else {
  23. $('.content').append(scaleInfo[i].content)
  24. }
  25. }
  26. $('.content img').bind('contextmenu', function(){
  27. return false
  28. })
  29. }
  30. })
  31. }
  32. })
  33. function renderCalcu(scaleList) {
  34. $("h1").html(scaleList.scaleName);
  35. if(+scaleList.scaleType === 1 && +scaleList.Calc === 1) {
  36. renderScaleType1(scaleList)
  37. bindScaleType1(scaleList)
  38. } else if(+scaleList.scaleType === 2 && +scaleList.Calc === 0) {
  39. renderScaleType2(scaleList)
  40. bindScaleType2(scaleList)
  41. }
  42. }
  43. function renderScaleType1(scaleList) {
  44. var str = ''
  45. for (var j = 0; j < scaleList.group.length; j++) {
  46. str += '<div class="groupBox"> <div class="groupName">' + scaleList.group[j].groupName + '</div>';
  47. for (var x = 0; x < scaleList.group[j].rows.length; x++) {
  48. for (var y = 0; y < scaleList.group[j].rows[x].row.length; y++) {
  49. var str2 = ''
  50. var hasSelect = false
  51. for (var z = 0; z < scaleList.group[j].rows[x].row[y].details.length; z++) {
  52. str2 += '<span class="groupRowRadio"><input type="radio" data-index="' + j + '_' + x + '_' + y + '_' + z + '" name="' + j +'_'+ x +'_'+ y + '" id="' + j +'_' + x +'_' + y +'_'+ z + '"'
  53. if (scaleList.group[j].rows[x].row[y].details[z].select == 1) {
  54. hasSelect = true
  55. str2 += ' checked="checked"'
  56. }
  57. str2 += ' />' + '<label for="' + j +'_'+ x +'_' + y +'_' + z + '">' + scaleList.group[j].rows[x].row[y].details[z].detailName + '(' + scaleList.group[j].rows[x].row[y].details[z].score + ')' + '</label>'
  58. if (scaleList.group[j].rows[x].row[y].details[z].state == 1) {
  59. str2 += '<span class="groupRowRecommend">智能推荐</span>'
  60. }
  61. str2 += '</span>'
  62. }
  63. str += '<div class="groupRowWrapper '
  64. if(+scaleList.group[j].rows[x].required === 1&&hasCalc&&!hasSelect) {
  65. str += 'noSelect '
  66. }
  67. str +='' + j +'_'+ x + '" data-group="' + j +'_' + x + '" id="' + j +'_'+ x +'_'+ y + '">'
  68. if(scaleList.group[j].rows[x].row.length > 1 && y=== 0) {
  69. str += '<div class="tips">本组选项可多选或根据需要选择某一条选项</div>'
  70. }
  71. str +='<div class="groupRowName"> <span class="groupRowIndex">'
  72. if (y == 0) {
  73. str += (x + 1) + '.'
  74. }
  75. str += '</span>' + scaleList.group[j].rows[x].row[y].name + '</div><div class="groupRowBox">' + str2 + '</div>' + '</div>'
  76. }
  77. }
  78. if (scaleList.group[j].groupCalculate.isShow == 1) {
  79. str += '<div class="calcu">计分:' + scaleList.group[j].groupCalculate.result.value + ' ' + scaleList.group[j].groupCalculate.result.text + '</div>'
  80. }
  81. str += '</div>'
  82. }
  83. var calcuStr = '<div class="allCalcuBox"><span class="allCalcu">总分:'
  84. if (scaleList.calculate && scaleList.calculate.result) {
  85. calcuStr += scaleList.calculate.result.value + ' ' + scaleList.calculate.result.text
  86. }
  87. calcuStr += '</span><span class="calcuBtn">得分</span></div>'
  88. if($(".calcuWrapper")[0]) {
  89. $(".calcuWrapper").html(str + calcuStr)
  90. } else {
  91. var allStr = '<div class="calcuWrapper">' + str + calcuStr + '</div>'
  92. $('.content').append(allStr)
  93. }
  94. }
  95. function bindScaleType1(scaleList) {
  96. $('input').on('change', function (e) {
  97. var indexList = $(this).attr('data-index').split('_')
  98. for (let i = 0; i < scaleList.group[indexList[0]].rows[indexList[1]].row[indexList[2]].details.length; i++) {
  99. scaleList.group[indexList[0]].rows[indexList[1]].row[indexList[2]].details[i].select = 0
  100. }
  101. if(+scaleList.group[indexList[0]].rows[indexList[1]].metux === 1) {
  102. for(let i = 0; i < scaleList.group[indexList[0]].rows[indexList[1]].row.length; i++) {
  103. for(let j = 0; j < scaleList.group[indexList[0]].rows[indexList[1]].row[i].details.length; j++) {
  104. scaleList.group[indexList[0]].rows[indexList[1]].row[i].details[j].select = 0
  105. }
  106. }
  107. }
  108. scaleList.group[indexList[0]].rows[indexList[1]].row[indexList[2]].details[indexList[3]].select = 1
  109. const className = $(this).parent().parent().parent().attr('data-group')
  110. for (let i = 0; i < $('.' + className).length; i++) {
  111. $('.' + className).eq(i).removeClass('noSelect')
  112. }
  113. renderScaleType1(scaleList)
  114. bindScaleType1(scaleList)
  115. })
  116. $('.calcuBtn').on('click', function () {
  117. hasCalc = true
  118. let allSelect = true
  119. for (let i = 0; i < scaleList.group.length; i++) {
  120. for (let j = 0; j < scaleList.group[i].rows.length; j++) {
  121. if (scaleList.group[i].rows[j].required == 1) {
  122. let itemSelect = false
  123. for (let x = 0; x < scaleList.group[i].rows[j].row.length; x++) {
  124. for (let y = 0; y < scaleList.group[i].rows[j].row[x].details.length; y++) {
  125. if (scaleList.group[i].rows[j].row[x].details[y].select == 1) {
  126. itemSelect = true
  127. }
  128. }
  129. }
  130. if (!itemSelect) {
  131. allSelect = false
  132. $('.' + i+'_' + j).addClass('noSelect')
  133. }
  134. }
  135. }
  136. }
  137. if (!allSelect) {
  138. $('.modal').css('display', 'block');
  139. let timer = setTimeout(() => {
  140. $('.modal').css('display', 'none');
  141. }, 2000);
  142. $('.closeModal').click(function () {
  143. $('.modal').css('display', 'none');
  144. if (timer) {
  145. clearTimeout(timer)
  146. }
  147. })
  148. }
  149. if (allSelect) {
  150. getCalcuResult(scaleList)
  151. }
  152. })
  153. }
  154. function renderScaleType2(scaleList) {
  155. let str = ''
  156. for( let i = 0; i < scaleList.rows.length; i++){
  157. let str2 = ''
  158. for(let j = 0; j < scaleList.rows[i].details.length; j++) {
  159. str2 += '<div class="groupRowRadio groupRowRadioType2"><input type="radio" data-index="' + i + '_' + j +'" name="' + i + '_' + '" id="' + i+'_' + j
  160. if (scaleList.rows[i].details[j].select == 1) {
  161. str2 += ' checked="checked"'
  162. }
  163. str2 += '" />' + '<label for="' + i +'_'+ j + '">' + scaleList.rows[i].details[j].detailName + '(' + scaleList.rows[i].details[j].result + ')' + '</label>'
  164. if (scaleList.rows[i].details[j].state == 1) {
  165. str2 += '<span class="groupRowRecommend">智能推荐</span>'
  166. }
  167. str2 += '</div>'
  168. }
  169. str += `
  170. <div class="groupBox groupBoxType2">
  171. <div class="groupName">
  172. ${scaleList.rows[i].name}
  173. </div>
  174. ${str2}
  175. </div>
  176. `
  177. }
  178. var calcuStr =`<div class="calcResult"></div>`
  179. var allStr = '<div class="calcuWrapper">' + str + calcuStr + '</div>'
  180. $('.content').append(allStr)
  181. }
  182. function bindScaleType2(scaleList) {
  183. $('input').on('change', function (e) {
  184. var indexList = $(this).attr('data-index').split('_')
  185. for (var i = 0; i < scaleList.rows[indexList[0]].details.length; i++) {
  186. scaleList.rows[indexList[0]].details[i].select = 0
  187. }
  188. scaleList.rows[indexList[0]].details[indexList[1]].select = 1
  189. let resultStr ="结果:"
  190. let resultStrNum = 0
  191. for(let i = 0; i < scaleList.rows.length; i++) {
  192. for(let j = 0; j < scaleList.rows[i].details.length; j++) {
  193. if(scaleList.rows[i].details[j].select === 1) {
  194. resultStrNum++
  195. resultStr += `${scaleList.rows[i].details[j].detailName}(${scaleList.rows[i].details[j].result}),`
  196. }
  197. }
  198. }
  199. if(resultStrNum > 0) {
  200. resultStr = resultStr.slice(0,-1)
  201. }
  202. $('.calcResult').html(resultStr)
  203. })
  204. }
  205. function getCalcuResult(data) {
  206. const param = {
  207. type: 1,
  208. data: data
  209. }
  210. post(config.calculate, param).then((res) => {
  211. const dataResult = res.data.data
  212. if (res.data.code == 0) {
  213. data.calculate.result = dataResult.calcalculate.result
  214. for (let i = 0; i < data.group.length; i++) {
  215. for (let j = 0; j < dataResult.group.length; j++) {
  216. if (data.group[i].groupName == dataResult.group[j].groupName) {
  217. data.group[j].groupCalculate.result = dataResult.group[i].groupCalculate.result
  218. }
  219. }
  220. }
  221. if ($('.calcu').length) {
  222. for (let i = 0; i < $('.calcu').length; i++) {
  223. let calcuItemName = $('.calcu').eq(i).parent().find('.groupName').html();
  224. let calcuItem = data.group.filter(function (item) {
  225. return item.groupName == calcuItemName
  226. })[0]
  227. $('.calcu').eq(i).html('计分:' + calcuItem.groupCalculate.result.value + ' ' + calcuItem.groupCalculate.result.text)
  228. }
  229. }
  230. $('.allCalcu').eq(0).html('总分:' + data.calculate.result.value + ' ' + data.calculate.result.text)
  231. } else {
  232. alert(res.msg)
  233. }
  234. })
  235. }