ep1.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. const {FieldCheck, FormVerify} = require("../lib/cjs/index.js")
  2. const {paramsRules} = require("./rule")
  3. const {color, ColorValue} = require("./colorConsole");
  4. let fieldCheck = new FieldCheck(paramsRules);
  5. let t_form = {
  6. pType: {
  7. val: 'all',
  8. oldVal: 'all',
  9. init: 'all',
  10. msg: '',
  11. state: 0,
  12. options: [
  13. {text: '全部', key: 'all', value: 'all'},
  14. {text: '新闻', key: 'news', value: 'news'},
  15. {text: '解决方案', key: 'solution', value: 'solution'},
  16. ]
  17. },
  18. type: {
  19. val: '',
  20. init: '',
  21. msg: '',
  22. state: 0,
  23. options: [
  24. {
  25. "text": "全部",
  26. "key": "all",
  27. "value": "all",
  28. "disabled": true
  29. },
  30. {
  31. "type_id": 1,
  32. "type_logo": null,
  33. "type_name": "解决方案",
  34. "parent_type": 1,
  35. "type_name_en": "Plan and case",
  36. "type_sort": 1,
  37. "date_time": "1482569003",
  38. "type_key": "sol",
  39. "seo_key": null,
  40. "text": "解决方案",
  41. "key": "sol",
  42. "typeKey": "sol",
  43. "href": "/sol",
  44. "value": "sol"
  45. },
  46. {
  47. "type_id": 3,
  48. "type_logo": null,
  49. "type_name": "公司动态",
  50. "parent_type": 2,
  51. "type_name_en": "Company Dynamic",
  52. "type_sort": 0,
  53. "date_time": "1482307803",
  54. "type_key": "com",
  55. "seo_key": null,
  56. "text": "公司动态",
  57. "key": "com",
  58. "typeKey": "com",
  59. "href": "/com",
  60. "value": "com"
  61. },
  62. {
  63. "type_id": 4,
  64. "type_logo": null,
  65. "type_name": "行业资讯",
  66. "parent_type": 2,
  67. "type_name_en": "Industry information",
  68. "type_sort": 0,
  69. "date_time": "1482307826",
  70. "type_key": "in",
  71. "seo_key": null,
  72. "text": "行业资讯",
  73. "key": "in",
  74. "typeKey": "in",
  75. "href": "/in",
  76. "value": "in"
  77. },
  78. {
  79. "type_id": 2,
  80. "type_logo": null,
  81. "type_name": "应用案例",
  82. "parent_type": 1,
  83. "type_name_en": "输电线路在线监测",
  84. "type_sort": 0,
  85. "date_time": "1482307826",
  86. "type_key": "acs",
  87. "seo_key": null,
  88. "text": "应用案例",
  89. "key": "acs",
  90. "typeKey": "acs",
  91. "href": "/acs",
  92. "value": "acs"
  93. },
  94. {
  95. "type_id": 12,
  96. "type_logo": null,
  97. "type_name": "产品的应用",
  98. "parent_type": 2,
  99. "type_name_en": "产品的应用",
  100. "type_sort": 0,
  101. "date_time": "1638180062",
  102. "type_key": "pa",
  103. "seo_key": null,
  104. "text": "产品的应用",
  105. "key": "pa",
  106. "typeKey": "pa",
  107. "href": "/pa",
  108. "value": "pa"
  109. },
  110. {
  111. "type_id": 14,
  112. "type_logo": null,
  113. "type_name": "电力案例",
  114. "parent_type": 1,
  115. "type_name_en": "电力案例",
  116. "type_sort": 0,
  117. "date_time": "1663818099",
  118. "type_key": "epower",
  119. "seo_key": null,
  120. "text": "电力案例",
  121. "key": "epower",
  122. "typeKey": "epower",
  123. "href": "/epower",
  124. "value": "epower"
  125. }
  126. ],
  127. disables: ['all'],
  128. },
  129. title: {
  130. val: '',
  131. init: '',
  132. msg: '',
  133. state: 0
  134. },
  135. author: {
  136. val: '',
  137. init: '',
  138. msg: '',
  139. state: 0
  140. },
  141. source: {
  142. val: '',
  143. init: '',
  144. msg: '',
  145. state: 0
  146. },
  147. cover: {
  148. val: '',
  149. init: '',
  150. msg: '',
  151. reCheckField: 'fileData',
  152. state: 0
  153. },
  154. remark: {
  155. val: '',
  156. init: '',
  157. msg: '',
  158. state: 0
  159. },
  160. }
  161. let formVerify = new FormVerify(t_form, fieldCheck);
  162. formVerify.onLog = (msg)=>{
  163. console.log(`${color('[INFO]', ColorValue.green) } ${color(msg, ColorValue.bright)}`);
  164. }
  165. let testFn = (field, val)=>{
  166. let errMsg = '';
  167. t_form[field].val = val;
  168. errMsg = formVerify.checkItem(field);
  169. console.log(`------ ${field}: ${val} ------`);
  170. // console.log(`${ColorValue.green}2222`)
  171. console.log(`[${!
  172. errMsg?color('pass', ColorValue.green)
  173. :color('failed', ColorValue.red)}] ${errMsg || ''}`);
  174. console.log();
  175. }
  176. console.log(color('\n-----------测试单项验证------------', ColorValue.bright))
  177. testFn('remark', '12345');
  178. testFn('type', 'all');
  179. testFn('type', t_form.type.options[1].value);
  180. testFn('type', '11111');
  181. testFn('cover', '1233');
  182. console.log(color('\n-----------测试获取结果------------', ColorValue.bright))
  183. console.log(formVerify.getFormData());
  184. t_form.type.val = t_form.type.options[1].value;
  185. console.log(formVerify.getFormData());
  186. console.log(color('\n-----------测试重置------------', ColorValue.bright))
  187. formVerify.init();
  188. console.log(formVerify.getFormData());
  189. console.log(color('\n-----------测试整体验证------------', ColorValue.bright))
  190. t_form.type.val = t_form.type.options[1].value;
  191. t_form.cover.val = '1233';
  192. t_form.remark.val = '12345';
  193. console.log(formVerify.check());
  194. console.log(formVerify.getFormData());
  195. console.log(color('\n-----------测试整体验证------------', ColorValue.bright))
  196. t_form.type.val = t_form.type.options[0].value;
  197. t_form.cover.val = '1233';
  198. t_form.remark.val = '12345';
  199. console.log(formVerify.check());
  200. console.log(formVerify.getFormData());