baoMing.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <!--
  2. # 对 baoMing.vue 代码的严厉批评
  3. ## 代码结构与风格:混乱不堪的“野路子”实现
  4. 首先,代码结构混乱到令人发指。全局变量 that 的使用(第62行和187行)是典型的新手错误,这种做法不仅破坏了Vue组件的封装性,还可能导致内存泄漏和难以追踪的bug。在现代前端开发中,这种写法早已被唾弃,而你却堂而皇之地使用,足见代码质量之低下。
  5. 代码注释严重缺失,关键逻辑如文件上传( chooseFile 方法)、表单提交( submit 方法)等没有任何说明,仿佛在挑战后续维护者的耐心。变量命名更是随心所欲, cr_id 、 modifyId 、 volunteer_id 等命名风格不统一,一会儿下划线,一会儿驼峰,完全无视代码规范的存在。
  6. ## 功能实现:漏洞百出的“豆腐渣工程”
  7. ### 表单验证:形同虚设的防线
  8. 表单验证规则( rules 对象)漏洞百出。身份证号字段( id_card )只做了必填验证,连最基本的18位长度验证都没有(第135-139行被注释掉),这意味着用户可以输入任意长度的字符串,后端将承受巨大的验证压力。更可笑的是,区域选择组件的 name 属性居然被设置为 id_card (第28行),与身份证字段冲突,这会导致表单验证时的逻辑混乱。
  9. 提交表单时, validate(['id']) (第253行和320行)的调用更是让人摸不着头脑——表单中根本没有 id 字段,这种验证调用完全是无效的,相当于给系统开了个后门,让所有表单数据可以未经有效验证就提交。
  10. ### 功能逻辑:支离破碎的实现
  11. 文件上传功能( chooseFile 方法)设计极其简陋。只允许上传一个文件( count: 1 ),且没有文件类型和大小的限制,用户可以上传任意大小的文件,这可能导致服务器资源被恶意消耗。上传成功后,文件路径直接赋值给 valiFormData.fullurl (第240行),但在提交表单时却使用 file: fullurl (第269行),这种字段映射关系混乱不堪。
  12. 志愿者详情查询( details 方法)调用API时只传递了 main_body_id: 1 (第292行),没有传递志愿者ID等关键参数,这会导致后端无法准确查询用户信息,返回的数据极有可能是错误的。修改资料功能( modifySubmit 方法)的实现同样草率, volunteer_id 和 cr_id 的获取逻辑(第334-335行)依赖于多个来源,一旦某个来源为空,就会导致API调用失败。
  13. ### 安全与性能:触目惊心的隐患
  14. 安全方面,代码没有对用户输入进行任何过滤和转义,特别是身份证号、手机号等敏感信息,直接传递给后端,存在SQL注入的风险。文件上传时, wx.chooseMessageFile (第221行)的使用没有限制文件类型,恶意用户可以上传可执行文件或其他危险文件。
  15. 性能方面,表单输入没有使用防抖(debounce)处理,用户在输入时会频繁触发数据更新,增加了浏览器的渲染压力。API调用(如 getCategoryCascadeList 方法)使用了回调函数(第211-213行),而不是现代的 Promise 或 async/await 语法,代码可读性和可维护性极差。
  16. ### 用户体验:粗制滥造的交互
  17. 用户体验方面,代码的表现更是惨不忍睹。表单提交后没有加载状态提示(如loading动画),用户点击“报名”或“确认修改”按钮后,只能傻等,不知道操作是否正在进行。错误提示( that.$common.errorToShow )千篇一律,没有针对具体错误类型给出个性化提示,用户无法快速定位问题所在。
  18. 区域选择组件( uni-data-picker )的 onchange 方法(第202-204行)只是获取了值却没有任何处理, onnodeclick 方法(第205-208行)虽然设置了 region_id ,但没有更新表单数据,这会导致用户选择区域后,表单中没有相应的记录。
  19. ## 技术选型:固步自封的落后实践
  20. 代码使用了uni-app框架,但完全没有遵循框架的最佳实践。例如,没有使用Vue 3的组合式API(Composition API),而是死守着过时的选项式API(Options API),导致代码逻辑分散,难以维护。组件的引用(如 uni-navbar 、 uni-forms 等)没有进行任何封装,直接在模板中使用,增加了代码的耦合度。
  21. 样式部分( <style> 标签)使用了大量的内联样式和固定像素值,没有使用CSS变量或预处理器(如SCSS),导致样式难以统一管理和修改。 /deep/ 选择器(第386行)的使用是一种不良实践,它会破坏组件的样式封装性,导致样式冲突。
  22. ## 总结:亟待重构的“代码垃圾”
  23. 综上所述, baoMing.vue 代码是一个典型的反面教材,从代码结构到功能实现,从安全性能到用户体验,几乎每一个方面都存在严重问题。它不仅反映了开发者对前端开发规范的无知,也暴露了项目管理的混乱。
  24. 这样的代码如果被部署到生产环境,极有可能导致系统崩溃、数据泄露等严重问题。因此,强烈建议对该代码进行全面重构,采用现代前端开发的最佳实践,如使用Vue 3的组合式API、完善表单验证、优化文件上传功能、提升用户体验等。只有这样,才能确保系统的稳定性、安全性和可维护性,为用户提供良好的使用体验。
  25. -->
  26. <template>
  27. <view class="box">
  28. <u-navbar :autoBack="true" title="志愿报名" bgColor="rgba(255,255,255,0)" :placeholder="true" titleStyle="font-weight:bold;color:#000000"></u-navbar>
  29. <view class="ban_box">
  30. <view class="ban_item2">
  31. <image class="top_img" src="https://huli-app.wenlvti.net/app_static/WenWuGuanJia/image/bm_top.png"></image>
  32. </view>
  33. <view class="ban_item1">
  34. <image class="top_img" src="https://huli-app.wenlvti.net/app_static/WenWuGuanJia/image/dt_ban2.png"></image>
  35. </view>
  36. </view>
  37. <!-- 表单 -->
  38. <view class="example">
  39. <uni-forms style="padding: 0 20rpx 0 20rpx" label-position="top" ref="valiForm" :rules="rules" :modelValue="valiFormData">
  40. <uni-forms-item label="您的姓名" label-width="80px" required name="name">
  41. <uni-easyinput v-model="valiFormData.name" placeholder="请输入姓名" />
  42. </uni-forms-item>
  43. <uni-forms-item label="您的联系电话" label-width="100px" required name="mobile">
  44. <uni-easyinput type="number" v-model="valiFormData.mobile" placeholder="请输入联系电话" />
  45. </uni-forms-item>
  46. <uni-forms-item label="您的身份证" label-width="100px" required name="id_card">
  47. <uni-easyinput type="idcard" v-model="valiFormData.id_card" placeholder="请输入身份证号" />
  48. </uni-forms-item>
  49. <uni-forms-item label="您的区域" label-width="100px" required name="id_card">
  50. <uni-data-picker :localdata="regionList" popup-title="请选择班级" @change="onchange" @nodeclick="onnodeclick"></uni-data-picker>
  51. </uni-forms-item>
  52. <uni-forms-item label="住址" label-width="100px" required name="address">
  53. <uni-easyinput v-model="valiFormData.address" placeholder="请输入住址" />
  54. </uni-forms-item>
  55. <uni-forms-item label="单位" label-width="80px" required name="unit_name">
  56. <uni-easyinput v-model="valiFormData.unit_name" placeholder="请输入单位名称" />
  57. </uni-forms-item>
  58. <!-- <uni-forms-item label="备注" label-width="80px" required name="notesVal">
  59. <uni-easyinput v-model="valiFormData.notesVal" placeholder="请输入申请类型(个人、家庭、机构)" />
  60. </uni-forms-item> -->
  61. <uni-forms-item label="认领原因" label-width="80px" required name="intro">
  62. <uni-easyinput type="textarea" v-model="valiFormData.intro" placeholder="请输入认领原因或个人介绍(可上传个人优秀证明、奖项)" />
  63. <view @click="chooseFile" class="scfj">
  64. <view>上传文件</view>
  65. <view>
  66. <uni-icons type="wallet" size="18"></uni-icons>
  67. </view>
  68. </view>
  69. </uni-forms-item>
  70. </uni-forms>
  71. </view>
  72. <view v-if="valiFormData.status == '-1'" class="text-wrapper_3" @click="modifySubmit('valiForm')">
  73. <view class="bm_tit">确认修改</view>
  74. </view>
  75. <view v-else class="text-wrapper_3" @click="submit('valiForm')">
  76. <view class="bm_tit">报名</view>
  77. </view>
  78. </view>
  79. </template>
  80. <script>
  81. let that;
  82. import { mapGetters } from 'vuex'
  83. export default {
  84. data() {
  85. return {
  86. regionList: [],
  87. region_id: '',
  88. cr_id: '' /* 文物id */,
  89. modifyId: '' /* 修改资料的文物id */,
  90. volunteer_id: '' /* 志愿者id */,
  91. /* 表单数据 */
  92. valiFormData: {
  93. name: '',
  94. mobile: '',
  95. address: '',
  96. id_card: '',
  97. unit_name: '',
  98. fullurl: '',
  99. intro: ''
  100. },
  101. /* 校验规则 */
  102. rules: {
  103. // 对name字段进行必填验证
  104. name: {
  105. // name 字段的校验规则
  106. rules: [
  107. // 校验 name 不能为空
  108. {
  109. required: true,
  110. errorMessage: '请输入姓名'
  111. },
  112. // 对name字段进行长度验证
  113. {
  114. minLength: 1,
  115. maxLength: 6,
  116. errorMessage: '{label}长度在 {minLength} 到 {maxLength} 个字符'
  117. }
  118. ],
  119. // 当前表单域的字段中文名,可不输入
  120. label: '姓名',
  121. validateTrigger: 'submit'
  122. },
  123. /* 手机号校验 */
  124. mobile: {
  125. // mobile 字段的校验规则
  126. rules: [
  127. // 校验 mobile 不能为空
  128. {
  129. required: true,
  130. errorMessage: '请输入联系电话'
  131. },
  132. // 对mobile字段进行长度验证
  133. {
  134. minLength: 11,
  135. maxLength: 11,
  136. errorMessage: '{label}长度为 {minLength} 个字符'
  137. }
  138. ],
  139. // 当前表单域的字段中文名,可不输入
  140. label: '手机号',
  141. validateTrigger: 'submit'
  142. },
  143. /* 身份证校验 */
  144. id_card: {
  145. // idCard 字段的校验规则
  146. rules: [
  147. // 校验 idCard 不能为空
  148. {
  149. required: true,
  150. errorMessage: '请输入身份证号'
  151. }
  152. // 对idCard字段进行长度验证
  153. // {
  154. // minLength: 18,
  155. // maxLength: 18,
  156. // errorMessage: '{label}长度为 {minLength} 个字符'
  157. // }
  158. ],
  159. // 当前表单域的字段中文名,可不输入
  160. label: '身份证号',
  161. validateTrigger: 'submit'
  162. },
  163. /* 单位 */
  164. unit_name: {
  165. rules: [
  166. // 校验 unit 不能为空
  167. {
  168. required: true,
  169. errorMessage: '请输入单位名称'
  170. }
  171. ]
  172. },
  173. address: {
  174. rules: [
  175. // 校验 住址 不能为空
  176. {
  177. required: true,
  178. errorMessage: '请输入单位名称'
  179. }
  180. ]
  181. },
  182. // notesVal: {
  183. // rules: [
  184. // // 校验 申请类型 不能为空
  185. // {
  186. // required: true,
  187. // errorMessage: '请输入申请类型'
  188. // }
  189. // ]
  190. // },
  191. intro: {
  192. rules: [
  193. {
  194. required: true,
  195. errorMessage: '请输入认领原因或个人介绍'
  196. }
  197. ]
  198. }
  199. }
  200. };
  201. },
  202. onLoad(o) {
  203. that = this;
  204. this.cr_id = o.id;
  205. this.volunteer_id = o.volunteer_id;
  206. this.modifyId = o.modifyId;
  207. this.details();
  208. this.getCategoryCascadeList();
  209. if (this.isLogin) {
  210. console.log('111');
  211. }
  212. },
  213. computed: {
  214. ...mapGetters(['isLogin'])
  215. },
  216. methods: {
  217. onchange(e) {
  218. const value = e.detail.value;
  219. },
  220. onnodeclick(node) {
  221. this.region_id = node.value;
  222. console.log(node);
  223. },
  224. // 区域
  225. getCategoryCascadeList() {
  226. this.$api.getCategoryCascadeList({ main_body_id: 1, pid: 5, level: 2 }, function (res) {
  227. that.regionList = res.data;
  228. });
  229. },
  230. // 测试上传文件
  231. chooseFile() {
  232. let userToken = '';
  233. let auth = this.$db.get('auth');
  234. userToken = auth.token;
  235. wx.chooseMessageFile({
  236. count: 1,
  237. type: 'file', // 修改为支持的文档类型
  238. // 配置后导致读取不到聊天文件
  239. // extension: ['.doc', '.xlsx', '.docx', '.ppt'],
  240. success(res) {
  241. const tempFilePaths = res.tempFiles;
  242. uni.uploadFile({
  243. url: that.$config.baseUrl + 'api/common/upload?token=' + userToken,
  244. filePath: tempFilePaths[0].path,
  245. name: 'file',
  246. header: {
  247. 'content-type': 'multipart/form-data' // 根据实际情况设置请求头
  248. },
  249. formData: {}, // 如果需要,添加额外的form数据
  250. success: (res) => {
  251. console.log('上传成功,服务器响应数据:', res.data);
  252. let url = JSON.parse(res.data);
  253. that.valiFormData.fullurl = url.data.fullurl;
  254. // console.log('生成的链接', that.valiFormData.fullurl);
  255. that.$common.errorToShow('上传成功');
  256. },
  257. fail: (err) => {
  258. console.error('文件上传失败:', err);
  259. }
  260. });
  261. }
  262. });
  263. },
  264. submit() {
  265. this.$refs.valiForm.validate(['id'], async (err, valiFormData) => {
  266. if (!err) {
  267. console.log('校验成功');
  268. /* 检验成功 */
  269. const { name, mobile, address, id_card, unit_name, intro, fullurl } = this.valiFormData;
  270. this.$api.applyVolunteer(
  271. {
  272. main_body_id: 1,
  273. name: name,
  274. mobile: mobile,
  275. region_id: this.region_id,
  276. address: address,
  277. unit_name: unit_name,
  278. id_card: id_card,
  279. intro: intro,
  280. cr_id: this.cr_id,
  281. file: fullurl
  282. },
  283. function (res) {
  284. if (res.code === 1) {
  285. that.$common.errorToShow(res.msg);
  286. setTimeout(() => {
  287. uni.switchTab({
  288. url: '/pages/shouhu/shouhu'
  289. });
  290. }, 1000);
  291. } else {
  292. that.$common.errorToShow(res.msg);
  293. }
  294. // console.log(res);
  295. }
  296. );
  297. } else {
  298. that.$common.errorToShow('请完善信息');
  299. }
  300. });
  301. },
  302. // 志愿者详情判断是否为被驳回
  303. details() {
  304. this.$api.details({ main_body_id: 1 }, function (res) {
  305. // 被驳回
  306. if (res.data != null && res.data.status == '-1') {
  307. // console.log(res);
  308. that.valiFormData = res.data;
  309. // console.log(that.valiFormData, '被驳回');
  310. }
  311. if (res.data != null && res.data.status == '2') {
  312. that.$common.errorToShow('您已经是志愿者,正在前往守护');
  313. setTimeout(() => {
  314. uni.switchTab({
  315. url: '/pages/shouhu/shouhu'
  316. });
  317. }, 3000);
  318. }
  319. if (res.data != null && res.data.status == '0') {
  320. that.$common.errorToShow('您的资料正在审核中');
  321. setTimeout(() => {
  322. uni.switchTab({
  323. url: '/pages/shouhu/shouhu'
  324. });
  325. }, 3000);
  326. }
  327. });
  328. },
  329. // 修改资料
  330. modifySubmit() {
  331. this.$refs.valiForm.validate(['id'], async (err, valiFormData) => {
  332. if (!err) {
  333. console.log('校验成功');
  334. /* 检验成功 */
  335. const { name, mobile, address, id_card, unit_name, intro } = this.valiFormData;
  336. this.$api.editApplyVolunteer(
  337. {
  338. main_body_id: 1,
  339. name: name,
  340. mobile: mobile,
  341. address: address,
  342. unit_name: unit_name,
  343. id_card: id_card,
  344. intro: intro,
  345. volunteer_id: this.volunteer_id || this.valiFormData.id,
  346. cr_id: this.cr_id || this.valiFormData.cr_list[0].id
  347. },
  348. function (res) {
  349. if (res.code === 1) {
  350. that.$common.errorToShow(res.msg);
  351. } else {
  352. that.$common.errorToShow(res.msg);
  353. }
  354. // console.log(res);
  355. }
  356. );
  357. } else {
  358. that.$common.errorToShow('请完善信息');
  359. }
  360. });
  361. }
  362. }
  363. };
  364. </script>
  365. <style>
  366. .box {
  367. width: 100%;
  368. padding-bottom: 50rpx;
  369. background-image: url('https://huli-app.wenlvti.net/app_static/WenWuGuanJia/image/xy_bgt.png');
  370. background-size: 100% 100%;
  371. background-repeat: repeat-y;
  372. height: auto;
  373. }
  374. .ban_box {
  375. position: relative;
  376. margin-top: 40rpx;
  377. }
  378. .top_img {
  379. width: 100%;
  380. height: 100%;
  381. }
  382. .ban_item1 {
  383. position: absolute;
  384. top: -20rpx;
  385. left: 233rpx;
  386. width: 290rpx;
  387. height: 290rpx;
  388. margin: auto;
  389. }
  390. .ban_item2 {
  391. width: 250rpx;
  392. height: 250rpx;
  393. margin: auto;
  394. }
  395. /deep/.uni-forms-item__error {
  396. color: red !important;
  397. }
  398. .is-input-border {
  399. background-color: #f7dfc0 !important;
  400. }
  401. .uni-forms-item__label {
  402. color: #000000 !important;
  403. }
  404. .text-wrapper_3 {
  405. margin: auto;
  406. margin-top: 50rpx;
  407. height: 80rpx;
  408. flex-direction: column;
  409. width: 240rpx;
  410. background: url('/static/img/dt_bg2.png') no-repeat;
  411. background-size: 100% 100%;
  412. }
  413. .bm_tit {
  414. text-align: center;
  415. font-size: 36rpx;
  416. line-height: 80rpx;
  417. letter-spacing: 6rpx;
  418. text-align: center;
  419. font-weight: 700;
  420. background: linear-gradient(180deg, #af7e44 0%, #934b36 100%);
  421. -webkit-background-clip: text;
  422. -webkit-text-fill-color: transparent;
  423. }
  424. .example {
  425. height: 87%;
  426. margin: 20rpx 32rpx 0 32rpx;
  427. }
  428. .uni-forms {
  429. padding: 0 20rpx 0 20rpx;
  430. }
  431. .uni-forms-item {
  432. margin-bottom: 30rpx !important;
  433. }
  434. .active {
  435. border: 6rpx solid #efb57a;
  436. border-radius: 10rpx;
  437. }
  438. .img {
  439. width: 230rpx;
  440. height: 250rpx;
  441. border: 6rpx solid #efb57a;
  442. border-radius: 10rpx;
  443. }
  444. .map_tit {
  445. display: flex;
  446. align-items: center;
  447. margin-left: 60rpx;
  448. margin-top: 40rpx;
  449. font-size: 40rpx;
  450. font-family: Songti SC, Songti SC;
  451. font-weight: 900;
  452. line-height: 52rpx;
  453. color: #444444;
  454. }
  455. .scarch_box {
  456. width: 430rpx;
  457. height: 82rpx;
  458. padding: 15rpx 0 0 30rpx;
  459. /* background-image: url('/static/img/search_bg1.png');
  460. background-size: 100% 100%; */
  461. }
  462. .rl_box {
  463. width: 90%;
  464. margin: auto;
  465. margin-top: 40rpx;
  466. display: flex;
  467. justify-content: space-between;
  468. }
  469. .rl_item {
  470. width: 188rpx;
  471. height: 102rpx;
  472. background-image: url('/static/img/rl_bg.png');
  473. background-size: 100% 100%;
  474. }
  475. .tit {
  476. text-align: center;
  477. line-height: 102rpx;
  478. font-weight: 700;
  479. font-size: 36rpx;
  480. letter-spacing: 6rpx;
  481. background: linear-gradient(180deg, #af7e44 0%, #934b36 100%);
  482. -webkit-background-clip: text;
  483. -webkit-text-fill-color: transparent;
  484. }
  485. .scfj {
  486. width: 170rpx;
  487. height: 50rpc;
  488. line-height: 50rpx;
  489. align-items: center;
  490. display: flex;
  491. background-color: #f7dfc0;
  492. position: absolute;
  493. top: -60rpx;
  494. right: 2rpx;
  495. border-radius: 5rpx;
  496. justify-content: space-around;
  497. }
  498. .tit2 {
  499. text-align: center;
  500. line-height: 66rpx;
  501. font-weight: 700;
  502. font-size: 32rpx;
  503. letter-spacing: 6rpx;
  504. background: linear-gradient(180deg, #af7e44 0%, #934b36 100%);
  505. -webkit-background-clip: text;
  506. -webkit-text-fill-color: transparent;
  507. }
  508. .is-input-error-border {
  509. border: none !important;
  510. }
  511. .xx_box {
  512. padding: 20rpx;
  513. background-color: #e1bf9a;
  514. width: 660rpx;
  515. height: 560rpx;
  516. padding-top: 80rpx;
  517. }
  518. .xx_tit {
  519. height: 360rpx;
  520. font-size: 30rpx;
  521. padding: 20rpx;
  522. text-indent: 2em;
  523. background-color: #f3e3d3;
  524. overflow: scroll;
  525. }
  526. </style>