您现在的位置是:首页 > 技术教程 正文

第十五届蓝桥杯(Web 应用开发)模拟赛 2 期-大学组(详细分析解答)

admin 阅读: 2024-03-27
后台-插件-广告管理-内容页头部广告(手机)

目录

 1.相不相等

1.1 题目要求

1.2 题目分析

1.3 源代码

2.三行情书

2.1 题目要求

2.2 题目分析

2.3 源代码

3.电影院在线订票

3.1 题目要求

3.2 题目分析

3.3 源代码

 4.老虎坤(不然违规发不出来)

4.1 题目要求

4.2 题目分析

4.3 源代码

5.星际通讯

5.1 题目要求

5.2 题目分析

5.3 源代码

6.蓝桥杯排位

6.1 题目要求

6.2 题目分析

7.拼出一个未来

8.超能英雄联盟

9.实时展示权限日志

10.账户验证


刚做完第一期的模拟题目,第二期又开始发布了,花了点时间做完了,分享下自己的解题思路。

 1.相不相等

1.1 题目要求

请你编写一个名为 expectFn 的函数,用于帮助开发人员测试他们的代码。它可以通过参数 val 接受任何值,并返回一个对象,该对象包含下面两个函数:

  • toBe(val):接受另一个值并在两个值相等( === )时返回 true 。如果它们不相等,则返回 "Not Equal" 。
  • notToBe(val):接受另一个值并在两个值不相等( !== )时返回 true 。如果它们相等,则返回 "Equal" 。

1.2 题目分析

题目要求写的很清楚了,按照其要求做判断条件即可

1.3 源代码

  1. var expectFn = function (val) {
  2. // TODO
  3. return {
  4. toBe: function (value) {
  5. if (val === value) {
  6. return true
  7. } else {
  8. return 'Not Equal'
  9. }
  10. },
  11. notToBe: function (value) {
  12. if (val !== value) {
  13. return true
  14. } else {
  15. return 'Equal'
  16. }
  17. }
  18. }
  19. }

2.三行情书

2.1 题目要求

请完善 style.css 的 TODO 部分,具体要求如下:

  1. 让第一行标题即 .content span 标签中的文字单行显示,多余溢出显示省略号。
  2. 请使用 -webkit-box 语法使得下面的文字即 .content p 标签里的内容显示三行,多余溢出显示省略号。

2.2 题目分析

这里主要是如果写过案例的话,应该就知道怎么写,需要注意的是span元素是单行元素,要想达到效果,需要将其变为块级元素。

2.3 源代码

  1. span {
  2. font-size: 20px;
  3. color: #837362;
  4. /* TODO:补充下面的代码 */
  5. /* 单行显示,多余溢出省略号 */
  6. overflow: hidden;
  7. text-overflow: ellipsis;
  8. white-space: nowrap;
  9. display: block;
  10. /* 使得溢出部分显示省略号 */
  11. }
  12. p {
  13. color: #837362;
  14. display: -webkit-box;
  15. -webkit-box-orient: vertical;
  16. -webkit-line-clamp: 3;
  17. /* 显示的行数 */
  18. overflow: hidden;
  19. }

3.电影院在线订票

3.1 题目要求

  1. 实现异步数据读取和渲染功能,使用 axios 请求 ./data.json(必须使用该路径请求,否则可能会请求不到数据)的数据。
    • 将电影名字 name 渲染到 id 为 movie-name 的节点中。
    • 将电影售价 price 渲染到 id 为 movie-price 的节点中。
    • 将座位信息 seats 渲染到 id 为 seat-area 的节点中,二维数组中每一个子数组代表一行,0 代表没有被他人占位,1 代表已经被订购。
  2. 实现座位选择和总价计算的功能:
    • 被别人订过的座位不能再被选择。
    • 座位被选中后,状态更新,为相应的座位添加选中样式(即 selected),并更新已选择的座位数和总价。
    • 自己所选择的座位可以被取消,并更新已选择的座位数和总价。

3.2 题目分析

  • 第一点是使用axios发送请求获取到数据,这个不会的可以参考一下axios官网的案例学习一下
  • 第二个是根据拿到的数据渲染html的结构,这个的就是创建对应的html结构,最后添加到对应的节点。
  • 第三个是实现作为选择的功能,实现思路就是给所有的座位都绑定一个点击事件,通过事件对象身上是否被选中来替换类名,然后就是渲染数量。

3.3 源代码

  1. /* TODO:
  2. 1. 完成数据请求,生成电影名,价格以及座位情况
  3. 2. 绑定点击事件,实现订票功能
  4. */
  5. let data = {}
  6. axios
  7. .get('../data.json')
  8. .then((res) => {
  9. console.log(res)
  10. data = res.data
  11. movieNameNode.innerHTML = data.name
  12. moviePriceNode.innerHTML = data.price
  13. //创建节点渲染数据
  14. data.seats.forEach((item) => {
  15. let row = document.createElement('div')
  16. row.className = 'row'
  17. item.forEach((item) => {
  18. let seat = document.createElement('div')
  19. seat.className = 'seat'
  20. row.appendChild(seat)
  21. if (item) {
  22. seat.classList.add('occupied')
  23. }
  24. })
  25. seatAreaNode.appendChild(row)
  26. })
  27. })
  28. .catch((err) => {
  29. console.log(err)
  30. })
  31. // 获取座位区域节点
  32. const seatAreaNode = document.getElementById('seat-area')
  33. // 获取电影名节点
  34. const movieNameNode = document.getElementById('movie-name')
  35. // 获取电影票价节点
  36. const moviePriceNode = document.getElementById('movie-price')
  37. // 获取已订电影票数量节点
  38. const count = document.getElementById('count')
  39. // 获取已订电影票总价节点
  40. const total = document.getElementById('total')
  41. // 获取所有座位节点
  42. const seatNodes = document.querySelectorAll('.seat')
  43. // 初始化已选择座位数和总价
  44. let selectedSeatsCount = 0
  45. let totalPrice = 0
  46. // 监听座位点击事件
  47. seatAreaNode.addEventListener('click', (event) => {
  48. const clickedSeat = event.target
  49. // 检查是否点击的是座位
  50. if (clickedSeat.classList.contains('seat') && !clickedSeat.classList.contains('occupied')) {
  51. // 切换座位的选中状态
  52. clickedSeat.classList.toggle('selected')
  53. // 更新已选择座位数和总价
  54. if (clickedSeat.classList.contains('selected')) {
  55. selectedSeatsCount++
  56. totalPrice += data.price
  57. } else {
  58. selectedSeatsCount--
  59. totalPrice -= data.price
  60. }
  61. // 更新显示
  62. updateDisplay()
  63. }
  64. })
  65. // 更新显示函数
  66. function updateDisplay() {
  67. count.textContent = selectedSeatsCount
  68. total.textContent = totalPrice
  69. }

 4.老虎坤(不然违规发不出来)

4.1 题目要求

找到 js/index.js 中的 GetResult 函数,完成此函数,实现以下目标:

点击开始后,可以通过 GetResult的三个参数 r1、r2、r3 计算出滚动后每一列图片的停留位置。当最后停留的图片都相同时,意味着玩家中了大奖!文字框(class = textPanel)显示“恭喜你,中奖了”,否则显示:“很遗憾,未中奖”。

参数介绍:r1、r2、r3 表示的是三列元素下的 li 的最后停留位置,分别对应第一列(id=sevenFirst)、第二列(id=sevenSecond)、第三列(id=sevenThird)。以第一列为例,最终显示的元素是 sevenFirst 下的第 r 个 li 元素。请使用显示的 li 元素的 data-point 属性判断三张图片是否相同。当 data-point 属性对应的值相同时,说明这三张图片相同。

4.2 题目分析

这一题很明确的考察点就是获取元素身上的属性,需要用到getAttribute()方法,通过获取到的属性对比是否一样,如果三个相同则中奖。

4.3 源代码

  1. if (sevenFirst.children[r1 - 1].getAttribute('data-point') == sevenSecond.children[r2 - 1].getAttribute('data-point')
  2. && sevenFirst.children[r1 - 1].getAttribute('data-point') == sevenThird.children[r3 - 1].getAttribute('data-point')) {
  3. textPanel.innerHTML = '恭喜你,中奖了'
  4. } else {
  5. textPanel.innerHTML = '很遗憾,未中奖'
  6. }

5.星际通讯

5.1 题目要求

完善 index.js 中的 translate 函数,完善其中的 TODO 部分:

translate 函数接收一个字符串参数 alienMessage,其中包含一系列外星人的密文。函数根据特定的翻译规则将密文翻译成人类语言,并返回翻译后的结果字符串。外星人密文翻译规则存放在 codonTable 变量中。

5.2 题目分析

这个题目就是js的切割 遍历 

5.3 源代码

  1. // 密语规则
  2. const codonTable = {
  3. IIX: '人类',
  4. VII: '哈喽',
  5. III: '你好',
  6. IXI: '赞',
  7. XVI: '嗨',
  8. CUV: '打击',
  9. XII: '夜晚',
  10. IVI: '我',
  11. XIC: '想',
  12. XIV: '交个朋友',
  13. VIX: '月亮',
  14. XCI: '代码',
  15. XIX: '祈福',
  16. XVI: '和',
  17. XXI: 'stop'
  18. }
  19. /**
  20. * @param {string} alienMessage 外星人的密文
  21. * @return {string} 翻译结果
  22. */
  23. const translate = (alienMessage) => {
  24. // TODO:待补充代码
  25. // 检查密文是否为空
  26. if (!alienMessage) {
  27. return ''
  28. }
  29. // 切分密文
  30. const codons = []
  31. for (let i = 0; i < alienMessage.length; i += 3) {
  32. codons.push(alienMessage.slice(i, i + 3))
  33. }
  34. // 初始化翻译结果
  35. let translation = ''
  36. // 遍历密文
  37. for (const codon of codons) {
  38. // 检查是否为停止符号
  39. if (codon === 'XXI') {
  40. break
  41. }
  42. // 查找翻译表
  43. const translationResult = codonTable[codon]
  44. // 如果找到翻译结果,则添加到最终结果中
  45. if (translationResult) {
  46. translation += translationResult
  47. } else {
  48. // 未找到对应翻译结果,返回无效密语
  49. return '无效密语'
  50. }
  51. }
  52. return translation
  53. }
  54. // 请注意:以下代码用于检测,请勿删除。
  55. try {
  56. module.exports = translate
  57. } catch (e) {}

6.蓝桥杯排位

6.1 题目要求

  1. 根据请求的数据正确完成左侧热力地图。
  2. 右侧战力榜中柱形图中,根据 power 字段的值对所有学校进行排序,取出排在前 10 名的学校,从左到右降序排列。
  3. 完成数据请求(数据来源 ./mock/map.json),map.json 中存放的数据为省市对应的学校数据

6.2 题目分析

根据要求获取数据,然后替换options中的配置即可

  1. const { createApp, ref, onMounted } = Vue
  2. const app = createApp({
  3. setup() {
  4. const chartsData = ref([])
  5. onMounted(() => {
  6. // TODO:待补充代码 请求数据,并正确渲染柱形图和地图
  7. axios
  8. .get('../mock/map.json')
  9. .then((res) => {
  10. chartsData.value = res.data
  11. showChartBar()
  12. showChinaMap()
  13. })
  14. .catch((err) => {
  15. console.log(err)
  16. })
  17. })
  18. // 展示柱状图
  19. const showChartBar = () => {
  20. const myChart = echarts.init(document.getElementById('chart'))
  21. let data = chartsData.value.map((item, index) => {
  22. return item.school_power
  23. })
  24. console.log(data)
  25. let result = data.flat(1).sort((a, z) => {
  26. return z.power - a.power
  27. })
  28. let arr = result.slice(0, 10)
  29. let school = arr.map((item) => {
  30. return item.name
  31. })
  32. let power = arr.map((item) => {
  33. return item.power
  34. })
  35. console.log(school)
  36. console.log(power)
  37. // 指定配置和数据
  38. const option = {
  39. xAxis: {
  40. type: 'category',
  41. axisLabel: { interval: 0, rotate: 40 },
  42. // TODO:待修改 柱状图 x 轴数据 -> 前 10 学校名称
  43. data: school
  44. },
  45. grid: {
  46. left: '3%',
  47. right: '4%',
  48. bottom: '3%',
  49. containLabel: true
  50. },
  51. yAxis: {
  52. type: 'value',
  53. boundaryGap: [0, 0.01]
  54. },
  55. series: [
  56. {
  57. // TODO:待修改 柱状图 y 轴数据->学校战力值
  58. data: power,
  59. type: 'bar',
  60. showBackground: true,
  61. backgroundStyle: {
  62. color: 'rgba(180, 180, 180, 0.2)'
  63. },
  64. itemStyle: {
  65. color: '#8c7ae6'
  66. }
  67. }
  68. ]
  69. }
  70. // 把配置给实例对象
  71. myChart.setOption(option)
  72. // 根据浏览器大小切换图表尺寸
  73. window.addEventListener('resize', function () {
  74. myChart.resize()
  75. })
  76. }
  77. // 展示地图
  78. const showChinaMap = () => {
  79. const chinaMap = echarts.init(document.getElementById('chinaMap'))
  80. // 进行相关配置
  81. const mapOption = {
  82. tooltip: [
  83. {
  84. backgroundColor: '#fff',
  85. subtext: 'aaa',
  86. borderColor: '#ccc',
  87. padding: 15,
  88. formatter: (params) => {
  89. return params.name + '热度值:' + params.value + '
    '
    + params.data.school_count + '所学校已加入备赛'
  90. },
  91. textStyle: {
  92. fontSize: 18,
  93. fontWeight: 'bold',
  94. color: '#464646'
  95. },
  96. subtextStyle: {
  97. fontSize: 12,
  98. color: '#6E7079'
  99. }
  100. }
  101. ],
  102. geo: {
  103. // 这个是重点配置区
  104. map: 'china', // 表示中国地图
  105. label: {
  106. normal: {
  107. show: false // 是否显示对应地名
  108. }
  109. },
  110. itemStyle: {
  111. normal: {
  112. borderColor: 'rgb(38,63,168)',
  113. borderWidth: '0.4',
  114. areaColor: '#fff'
  115. },
  116. emphasis: {
  117. //鼠标移入的效果
  118. areaColor: 'rgb(255,158,0)',
  119. shadowOffsetX: 0,
  120. shadowOffsetY: 0,
  121. shadowBlur: 20,
  122. borderWidth: 0,
  123. shadowColor: 'rgba(0, 0, 0, 0.5)'
  124. }
  125. }
  126. },
  127. visualMap: {
  128. show: true,
  129. left: 'center',
  130. top: 'bottom',
  131. type: 'piecewise',
  132. align: 'bottom',
  133. orient: 'horizontal',
  134. pieces: [
  135. {
  136. gte: 80000,
  137. color: 'rgb(140,122,230)'
  138. },
  139. {
  140. min: 50000,
  141. max: 79999,
  142. color: 'rgba(140,122,230,.8)'
  143. },
  144. {
  145. min: 30000,
  146. max: 49999,
  147. color: 'rgba(140,122,230,.6)'
  148. },
  149. {
  150. min: 10000,
  151. max: 29999,
  152. color: 'rgba(140,122,230,.4)'
  153. },
  154. {
  155. min: 1,
  156. max: 9999,
  157. color: 'rgba(140,122,230,.2)'
  158. }
  159. ],
  160. textStyle: {
  161. color: '#000',
  162. fontSize: '11px'
  163. }
  164. },
  165. series: [
  166. {
  167. type: 'map',
  168. geoIndex: 0,
  169. // TODO:待修改 地图对应数据
  170. data: chartsData.value.map((item) => {
  171. return {
  172. name: item.name,
  173. school_count: item.school_count,
  174. value: item.value
  175. }
  176. })
  177. }
  178. ]
  179. }
  180. // 把配置给实例对象
  181. chinaMap.setOption(mapOption)
  182. }
  183. return {
  184. chartsData,
  185. showChartBar,
  186. showChinaMap
  187. }
  188. }
  189. })
  190. app.mount('#app')

7.拼出一个未来

题目要求和分析就不写了 ,代码中给了注释

  1. // 声明一个数组,包含了所有的拼图块数据
  2. var puzzlePieces = [
  3. { src: './images/img1.png', id: 1 },
  4. { src: './images/img2.png', id: 2 },
  5. { src: './images/img3.png', id: 3 },
  6. { src: './images/img4.png', id: 4 },
  7. { src: './images/img5.png', id: 5 },
  8. { src: './images/img6.png', id: 6 },
  9. { src: './images/img7.png', id: 7 },
  10. { src: './images/img8.png', id: 8 },
  11. { src: './images/img9.png', id: 9 }
  12. ]
  13. // 定义一个打乱数组的函数
  14. function shuffleArray(array) {
  15. for (let i = array.length - 1; i > 0; i--) {
  16. const j = Math.floor(Math.random() * (i + 1))
  17. ;[array[i], array[j]] = [array[j], array[i]]
  18. }
  19. return array
  20. }
  21. // 使用定义的函数打乱拼图块数组
  22. puzzlePieces = shuffleArray(puzzlePieces)
  23. // 获取拼图容器元素
  24. var container = document.getElementById('puzzle-container')
  25. // 遍历拼图块数据数组
  26. puzzlePieces.forEach(function (pieceData) {
  27. // 创建一个新的拼图块元素
  28. var piece = document.createElement('div')
  29. piece.classList.add('puzzle-piece')
  30. piece.setAttribute('draggable', 'true')
  31. // 创建一个新的图片元素
  32. var image = document.createElement('img')
  33. image.src = pieceData.src
  34. image.dataset.id = pieceData.id
  35. // 将图片元素添加到拼图块元素中
  36. piece.appendChild(image)
  37. // 将拼图块元素添加到父容器元素中
  38. container.appendChild(piece)
  39. })
  40. // 获取所有的拼图块元素,并转换为数组
  41. const puzzleArray = Array.from(document.querySelectorAll('.puzzle-piece'))
  42. // 获取成功消息元素
  43. const successMessage = document.getElementById('success-message')
  44. // 为每个拼图块元素添加拖拽事件监听器
  45. puzzleArray.forEach((piece) => {
  46. piece.addEventListener('dragstart', dragStart)
  47. piece.addEventListener('dragover', dragOver)
  48. piece.addEventListener('drop', drop)
  49. })
  50. // 声明一个变量用来保存正在拖动的拼图块
  51. let draggedPiece = null
  52. // 定义开始拖动事件的处理函数
  53. function dragStart(event) {
  54. draggedPiece = this
  55. event.dataTransfer.setData('text/plain', null)
  56. }
  57. // 定义在拖动过程中的处理函数,阻止默认行为
  58. function dragOver(event) {
  59. event.preventDefault()
  60. }
  61. // 定义拖放事件的处理函数
  62. function drop(event) {
  63. // 检查是否拖动的拼图块不是当前目标拼图块
  64. // draggedPiece 被拖动的拼图块元素。this 目标位置的拼图块元素。
  65. let num = 0
  66. if (draggedPiece !== this) {
  67. // TODO:待补充代码
  68. // 交换图片的 src 属性和 data-id 属性
  69. let tempSrc = draggedPiece.querySelector('img').src
  70. let tempDataId = draggedPiece.querySelector('img').dataset.id
  71. draggedPiece.querySelector('img').src = this.querySelector('img').src
  72. draggedPiece.querySelector('img').dataset.id = this.querySelector('img').dataset.id
  73. this.querySelector('img').src = tempSrc
  74. this.querySelector('img').dataset.id = tempDataId
  75. // 检查是否拼图成功
  76. puzzleArray.forEach((item, index) => {
  77. if (parseInt(item.children[0].getAttribute('data-id')) === index + 1) {
  78. num++
  79. }
  80. })
  81. if (num === 9) {
  82. successMessage.classList.remove('hide')
  83. successMessage.classList.add('show')
  84. } else {
  85. successMessage.classList.remove('show')
  86. successMessage.classList.add('hide')
  87. }
  88. // 重置正在拖动的拼图块
  89. draggedPiece = null
  90. }
  91. }

8.超能英雄联盟

HeroList:

  1. // TODO:补全代码,实现目标效果
  2. const HeroList = {
  3. template: `
  4. 可选英雄

    • {{item.name}}
    • {{item.ability}}
    • {{item.strength}}
  5. `,
  6. setup() {
  7. //第一步获取数据
  8. const store = useHeroStore()
  9. axios
  10. .get('./js/heroes.json')
  11. .then((res) => {
  12. store.heroes = res.data
  13. })
  14. .catch((err) => {
  15. console.log(err)
  16. })
  17. return {
  18. store
  19. }
  20. }
  21. }
  22. // TODOEnd

TeamList:

  1. // TODO:补全代码,实现目标效果
  2. const TeamList = {
  3. template: `
  4. 我的队伍

    • {{item.name}}
    • {{item.strength}}
  5. 当前队伍战斗力:{{store.totalStrength}}

  6. `,
  7. setup() {
  8. const store = useHeroStore()
  9. return {
  10. store
  11. }
  12. }
  13. }
  14. // TODOEnd

store.js:

  1. const { defineStore } = Pinia
  2. const { ref } = Vue
  3. const useHeroStore = defineStore('hero', {
  4. state: () => ({
  5. heroes: [], //英雄列表
  6. team: [] // 队伍列表
  7. }),
  8. // TODO:补全代码,实现目标效果
  9. getters: {
  10. //计算出战力总和strength
  11. totalStrength() {
  12. return this.team.reduce((total, hero) => {
  13. return total + hero.strength
  14. }, 0)
  15. }
  16. },
  17. actions: {
  18. add(id) {
  19. this.heroes[id - 1].btn = true
  20. this.team.push(this.heroes[id - 1])
  21. },
  22. removeHero(id) {
  23. this.heroes[id - 1].btn = false
  24. //移出team中的元素
  25. this.team = this.team.filter((hero) => hero.id !== id)
  26. },
  27. sort() {
  28. //按照实力排序strength
  29. this.team.sort((a, b) => {
  30. return b.strength - a.strength
  31. })
  32. }
  33. }
  34. // TODOEnd
  35. })

9.实时展示权限日志

这个题目我不知道为啥,明明在线测试基本上满足题目要求了,但是就是不能完全通过

node.js:

  1. /**
  2. * 请完成下面的 TODO 部分,其他代码请勿改动
  3. */
  4. const fs = require('fs')
  5. const http = require('http')
  6. const path = require('path')
  7. const dataUrl = path.resolve(__dirname, '../data.json')
  8. const loggerUrl = path.resolve(__dirname, '../logger.json')
  9. // 获取唯一的id
  10. function getLoggerId() {
  11. return Buffer.from(Date.now().toString()).toString('base64') + Math.random().toString(36).substring(2)
  12. }
  13. /**
  14. * 该方法统一了服务器返回的消息格式,并返回给客户端
  15. * @param {*} res 响应 response
  16. * @param {*} code 状态码,默认为 0 代表没有错误,如果有错误固定为 404
  17. * @param {*} msg 错误消息,固定为空字符串即可 ''
  18. * @param {*} data 响应体,为 js 对象,若 data 为 utf-8 编码时需要使用 eval(data) 处理
  19. */
  20. function send(res, code, msg, data) {
  21. const responseObj = {
  22. code,
  23. msg,
  24. data
  25. }
  26. const da = JSON.stringify(responseObj)
  27. res.setHeader('Content-Type', 'application/json;charset=utf-8')
  28. res.write(da)
  29. res.end()
  30. }
  31. function handleStatic(res, pathName, part) {
  32. const content = fs.readFileSync(path.resolve(__dirname, pathName))
  33. let contentType = 'text/html'
  34. switch (part) {
  35. case 'css':
  36. contentType = 'text/css'
  37. break
  38. case 'js':
  39. contentType = 'text/js'
  40. break
  41. }
  42. res.writeHead(200, 'Content-Type', contentType)
  43. res.write(content)
  44. res.end()
  45. }
  46. const server = http.createServer((req, res) => {
  47. res.setHeader('Access-Control-Allow-Origin', '*')
  48. if (req.url === '/') {
  49. handleStatic(res, '../index.html', '')
  50. } else if (req.url === '/css/index.css') {
  51. handleStatic(res, `..${req.url}`, 'css')
  52. } else if (req.url === '/js/index.js') {
  53. handleStatic(res, `..${req.url}`, 'js')
  54. } else if (req.url === '/js/axios.min.js') {
  55. handleStatic(res, `..${req.url}`, 'js')
  56. } else if (req.url === '/js/vue3.global.min.js') {
  57. handleStatic(res, `..${req.url}`, 'js')
  58. }
  59. if (req.method === 'GET' && req.url === '/users') {
  60. // TODO 处理获取文件内容的操作
  61. //读取data.json中的数据
  62. let fileContent = fs.readFileSync(dataUrl, 'utf-8')
  63. let data = JSON.parse(fileContent)
  64. if (fileContent) {
  65. //将读取到的数据转化为json格式
  66. //将json格式的数据响应给客户端
  67. send(res, 0, '', data)
  68. }
  69. } else if (req.method === 'PUT' && req.url === '/editUser') {
  70. let fileContent = fs.readFileSync(dataUrl, 'utf-8')
  71. let data = JSON.parse(fileContent)
  72. let body = ''
  73. req.on('readable', () => {
  74. let chunk = ''
  75. if (null !== (chunk = req.read())) {
  76. body += chunk
  77. }
  78. })
  79. req.on('end', () => {
  80. if (body) {
  81. // TODO 处理更改文件数据并将最新的文件数据响应给客户端
  82. //处理put请求
  83. let bodyData = JSON.parse(body)
  84. //修改data.json中的数据
  85. data.forEach((item) => {
  86. if (item.id == bodyData.id) {
  87. item.power = bodyData.power
  88. }
  89. })
  90. //存储文件数据到data.json中
  91. fs.writeFileSync(dataUrl, JSON.stringify(data))
  92. send(res, 0, '', data)
  93. }
  94. })
  95. } else if (req.method === 'POST' && req.url === '/logger') {
  96. let body = ''
  97. req.on('readable', () => {
  98. let chunk = ''
  99. if (null !== (chunk = req.read())) {
  100. body += chunk
  101. }
  102. })
  103. req.on('end', () => {
  104. let fileContentLog = fs.readFileSync(loggerUrl, 'utf-8')
  105. //判断是否有日志
  106. let dataLog = []
  107. if (fileContentLog) {
  108. dataLog = JSON.parse(fileContentLog)
  109. }
  110. let fileContentUser = fs.readFileSync(dataUrl, 'utf-8')
  111. let dataUser = JSON.parse(fileContentUser)
  112. if (body) {
  113. // TODO 处理新增日志
  114. let bodyData = JSON.parse(body)
  115. let dataJson = {
  116. id: getLoggerId(),
  117. msg: bodyData.data,
  118. // 时间格式为:2023/6/6 上午8:10:35
  119. time: `${getTime()}`
  120. }
  121. //存储日志
  122. dataLog.unshift(dataJson)
  123. // 并在该对象转化成 JSON 格式的末尾添加换行符(如不添加换行符会导致检测不通过)
  124. fs.writeFileSync(loggerUrl, JSON.stringify(dataLog, null, 2) + '\n')
  125. send(res, 0, '', dataJson)
  126. }
  127. })
  128. }
  129. })
  130. function getTime() {
  131. // 获取当前时间
  132. const currentDate = new Date()
  133. // 获取年、月、日、时、分、秒
  134. const year = currentDate.getFullYear()
  135. const month = currentDate.getMonth() + 1 // 月份是从 0 开始的,所以要加 1
  136. const day = currentDate.getDate()
  137. const hours = currentDate.getHours()
  138. //获取是上午还是下午
  139. const amPm = hours >= 12 ? '下午' : '上午'
  140. const minutes = currentDate.getMinutes()
  141. const seconds = currentDate.getSeconds()
  142. // 格式化时间
  143. const formattedTime = `${year}/${month}/${day} ${amPm}${hours}:${minutes}:${seconds}`
  144. return formattedTime
  145. }
  146. server.listen(8080, () => {
  147. console.log('Server running on port 8080')
  148. })

index.js:

  1. /**
  2. * 请完成下面的 TODO 部分,其他代码请勿改动
  3. */
  4. // 对响应进行统一处理,如果不调用该函数,可能导致判题出错
  5. // 参数为服务器的响应对象
  6. function parseRes(res) {
  7. return (res.json && res.json()) || res.data
  8. }
  9. const App = {
  10. setup() {
  11. const { onMounted } = Vue
  12. const data = Vue.reactive({
  13. userList: [], //用户数组
  14. loggerList: [] //日志数组
  15. })
  16. const getPowerText = (power) => {
  17. return power ? '可以登录' : '禁止登录'
  18. }
  19. const handleChange = async (e) => {
  20. if (e.target.tagName !== 'INPUT') {
  21. return
  22. }
  23. // TODO 处理发送请求修改当前用户的权限并更新一条日志记录
  24. //处理put请求
  25. let res = await axios.put(`/editUser`, {
  26. id: e.target.dataset.id,
  27. power: e.target.checked
  28. })
  29. if (res.status == 200) {
  30. data.userList = parseRes(res.data)
  31. } else {
  32. console.log('修改失败')
  33. }
  34. //调用post请求,添加一条修改日志
  35. //用id找出用户名
  36. let userName = data.userList.find((item) => item.id == e.target.dataset.id).name
  37. let postRes = await axios.post('/logger', {
  38. data: `超级管理员将用户${userName}设置为${getPowerText(e.target.checked)}权限`
  39. })
  40. if (postRes.status == 200) {
  41. //将数据放在数组首
  42. let a = parseRes(postRes.data)
  43. data.loggerList.unshift(a)
  44. } else {
  45. console.log('添加日志失败')
  46. }
  47. }
  48. // TODO 在页面挂载之前请求用户数据并修改对应的响应数据
  49. //利用axios获取数据
  50. const getUserData = async () => {
  51. let res = await axios.get('/users')
  52. if (res.status == 200) {
  53. data.userList = res.data.data
  54. } else {
  55. getUserData()
  56. }
  57. }
  58. onMounted(() => {
  59. getUserData()
  60. })
  61. return {
  62. data,
  63. handleChange,
  64. getPowerText,
  65. getUserData
  66. }
  67. }
  68. }
  69. const app = Vue.createApp(App)
  70. app.mount(document.querySelector('#app'))

10.账户验证

  1. DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>账户验证title>
  6. <link rel="stylesheet" type="text/css" href="./css/index.css" />
  7. <link rel="stylesheet" href="./css/element-plus@2.3.7/index.css">
  8. <script src="./js/vue3.global.js">script>
  9. <script src="./css/element-plus@2.3.7/index.full.js">script>
  10. <script type="importmap">
  11. {
  12. "imports": {
  13. "vue-demi": "./js/index.mjs",
  14. "vue": "./js/vue.esm-browser.prod.js",
  15. "pinia": "./js/pinia.esm-browser.js"
  16. }
  17. }
  18. script>
  19. <script src="./js/pinia.esm-browser.js" type="module">script>
  20. head>
  21. <body>
  22. <div id="app">
  23. <div class="header">
  24. <img class="back-btn" src="images/arrow.png" />
  25. <span id="main_title">使用手机号登录span>
  26. <span class="blank">span>
  27. div>
  28. <component :is="showName">component>
  29. div>
  30. <template id="phone">
  31. <div>
  32. <ul class="phone">
  33. <span>输入手机号码span>
  34. <li>
  35. <input v-model="phoneVal" type="text" autofocus id="numberInput" />
  36. li>
  37. <li>
  38. <input v-model="isSure" type="checkbox" name="" id="checkbox" />
  39. <span>已阅读并同意
  40. <a href="javascript:;">服务协议a>
  41. <a href="javascript:;">隐私保护指引a>
  42. span>
  43. li>
  44. <button id="btn" @click="nextStep">下一步button>
  45. ul>
  46. div>
  47. template>
  48. <template id="check">
  49. <ul class="number">
  50. <span>输入短信验证码span>
  51. <li class="hassend">已向
  52. <i>{{ handlePhoneVal }}i>
  53. 发送验证码
  54. li>
  55. <li class="code-container">
  56. <input v-for="(item, index) in verificationCodeInput" :key="index" v-model="item" @input="handleInput(index)"
  57. @keydown="handleKeyDown(index)" class="code" type="number" min="0" max="9" ref="codeInput{{index}}"
  58. required />
  59. li>
  60. <a href="javascript:;" id="resend" @click="resendCode">重新发送a>
  61. ul>
  62. template>
  63. <template id="success">
  64. <div class="success">
  65. <ul>
  66. <div>验证成功!div>
  67. <div>5s后将自动跳转div>
  68. ul>
  69. div>
  70. template>
  71. body>
  72. <script type="module">
  73. import { createPinia } from 'pinia';
  74. import { createApp, ref, reactive, provide, inject, onBeforeMount } from 'vue';
  75. const { ElNotification } = ElementPlus;
  76. const app = createApp({
  77. setup() {
  78. const data = reactive({
  79. showName: 'phone',
  80. });
  81. const code = ref([]);
  82. const phoneVal = ref('');
  83. const createCode = function () {
  84. let res = '';
  85. function* _create() {
  86. let count = 0;
  87. while (++count <= 6) {
  88. yield Math.floor(Math.random() * 10);
  89. }
  90. }
  91. for (const iterator of _create()) {
  92. res += iterator;
  93. }
  94. return res;
  95. };
  96. const handlePhone = (num) => {
  97. let res = '';
  98. for (let idx in num) {
  99. if (idx > 2 && idx < num.length - 2) {
  100. res += '*';
  101. } else {
  102. res += num[idx];
  103. }
  104. }
  105. return res;
  106. };
  107. provide('code', code);
  108. provide('phoneVal', phoneVal);
  109. provide('createCode', createCode);
  110. provide('data', data);
  111. provide('handlePhone', handlePhone);
  112. return {
  113. ...data,
  114. };
  115. },
  116. });
  117. app.use(ElementPlus);
  118. app.use(createPinia());
  119. app.component('phone', {
  120. template: '#phone',
  121. setup() {
  122. const isSure = ref('');
  123. const phoneVal = inject('phoneVal');
  124. const code = inject('code');
  125. const createCode = inject('createCode');
  126. const data = inject('data');
  127. function verifyPhone(num) {
  128. if (num.length !== 11) return false;
  129. return num[0] === '1' && num[1] === '8';
  130. }
  131. return {
  132. isSure,
  133. phoneVal,
  134. nextStep() {
  135. if (!isSure.value)
  136. return ElNotification({
  137. title: '发送失败',
  138. message: '请先阅读并同意下方协议',
  139. type: 'error',
  140. });
  141. if (!verifyPhone(phoneVal.value))
  142. return ElNotification({
  143. title: '发送失败',
  144. message: '无效的手机号码',
  145. type: 'error',
  146. });
  147. code.value = createCode();
  148. ElNotification({
  149. title: '发送成功',
  150. message: '您在验证码为' + code.value,
  151. type: 'success',
  152. });
  153. data.showName = 'check';
  154. },
  155. };
  156. },
  157. });
  158. app.component('check', {
  159. template: '#check',
  160. setup() {
  161. const phoneVal = inject('phoneVal');
  162. const handlePhoneVal = inject('handlePhone')(phoneVal.value);
  163. const data = inject('data');
  164. const code = inject('code');
  165. const createCode = inject('createCode');
  166. const verificationCodeInput = Array(6).fill('');
  167. onBeforeMount(() => {
  168. setTimeout(() => {
  169. const oCodeIptList = [...document.getElementsByClassName('code')];
  170. oCodeIptList[0].focus();
  171. oCodeIptList.map((item) => {
  172. item.oninput = function () {
  173. if (item.value) {
  174. item?.nextElementSibling && item?.nextElementSibling.focus();
  175. } else {
  176. item?.previousElementSibling && item?.previousElementSibling.focus();
  177. }
  178. trackVal();
  179. };
  180. });
  181. function trackVal() {
  182. const val = verificationCodeInput.join('');
  183. if (val.length === 6) {
  184. if (val === code.value) {
  185. ElNotification({
  186. title: '验证成功',
  187. message: '欢迎回来',
  188. type: 'success',
  189. });
  190. data.showName = 'success';
  191. } else {
  192. ElNotification({
  193. title: '验证失败',
  194. message: '您输入的验证码有误',
  195. type: 'error',
  196. });
  197. verificationCodeInput.fill('');
  198. oCodeIptList[0].focus();
  199. }
  200. }
  201. }
  202. });
  203. });
  204. return {
  205. handlePhoneVal,
  206. verificationCodeInput,
  207. handleInput(index) {
  208. if (index < 5 && verificationCodeInput[index].length === 1) {
  209. this.$refs[`codeInput${index + 1}`]?.focus();
  210. } else if (index > 0 && verificationCodeInput[index].length === 0) {
  211. this.$refs[`codeInput${index - 1}`]?.focus();
  212. }
  213. trackVal();
  214. },
  215. handleKeyDown(index) {
  216. if (event.key === 'Backspace' && index > 0) {
  217. this.$refs[`codeInput${index - 1}`]?.focus();
  218. }
  219. },
  220. trackVal() {
  221. const val = verificationCodeInput.join('');
  222. if (val.length === 6) {
  223. if (val === code.value) {
  224. ElNotification({
  225. title: '验证成功',
  226. message: '欢迎回来',
  227. type: 'success',
  228. });
  229. data.showName = 'success';
  230. } else {
  231. ElNotification({
  232. title: '验证失败',
  233. message: '您输入的验证码有误',
  234. type: 'error',
  235. });
  236. verificationCodeInput.fill('');
  237. this.$refs['codeInput0']?.focus();
  238. }
  239. }
  240. },
  241. resendCode() {
  242. code.value = createCode();
  243. ElNotification({
  244. title: '发送成功',
  245. message: '您的验证码为' + code.value,
  246. type: 'success',
  247. });
  248. },
  249. };
  250. },
  251. });
  252. app.component('success', {
  253. template: '#success',
  254. });
  255. app.mount('#app');
  256. script>
  257. html>

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索
排行榜