51-VUE_Rebuild-2

本文介绍了如何在Vue中设置全局变量,通过Vue.prototype进行扩展。同时,详细讲解了如何封装Ajax请求,创建axios的GET/POST接口。此外,文章讨论了Vuex的使用,遵循最佳实践将API调用整合到actions中,强调了actions中参数的实际用途。还提到了Vue中处理图片点击事件的技巧,如添加时间戳防止缓存。最后,提及了登录验证中的图形验证码实现,包括服务器端session的校验和过期时间管理。

1.vue设置全局变量

新建一个Global.vue,然后引入到Main.js,用的是Vue.protoype.自定义的name,然后在其他的vue里面就可以直接用了

2.封装ajax

①新建一个api的dir里面放ajax.js和一个config.js

②进入axios

③写一个类似java里面的用接口搞的一套GET/POST的http请求以及回调

import axios from 'axios'

export default function ajax(url, data = {}, type = 'GET') {
  return new Promise(function (resolve, reject) {
    let promise
    if (type === 'GET') {
      let dataStr = ''
      Object.keys(data).forEach(key => {
        dataStr += key + '=' + data[key] + '&'
      })
      if (dataStr !== '') {
        dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'))
        url = url + '?' + dataStr
      }
      promise = axios.get(url)
    } else {
      promise = axios.post(url)
    }
    promise.then(function (response) {
      resolve(response.data)
    }).catch(function (error) {
      reject(error)
    })
  })
}

3.vuex

在这里,还是安装规范来写vuex,之前写的所有的api,应该放在vuex,用actions的异步给封装,这样更好管理

①vuex的管理js,index.js里面写配置项:

import Vue from 'vue'
import Vuex from 'vuex'

import state from './state'
import mutaions from './mutations'
import getters from './getters'
import actions from './actions'

Vue.use(Vuex)

export default new Vuex.Store({
  state,
  mutaions,
  getters,
  actions
})

②在mutions.js里面引入mutation-types

import {
  RECEIVE_CODE
} from "./mutation-types";


//举了一个例子,暂时先不管
export default {
  [RECEIVE_CODE](code, {param1}) {
    state.paramname1 = param1
  }
}

③在actions里面封装mutations里面的同步方法+api的异步方法

import {
  RECEIVE_CODE
} from "./mutation-types";
import {
  reqqqqq
} from "../api/api";

export default {
  async getDemo({commit, state}) {
    const param3 = '11' +state.paramnxxx
    const result = await reqqqqq(param3)
    if (result.code === 0) {
      const return_code=result.code
      commit(RECEIVE_CODE,{return_code})
    }
  }
}

这里注意,actions里面方法名后面的参数,看起来是入参,但是实际调用actions方法的时候,是不需要写入参的,因为既然用了vuex,那么所有的data都是在vuex里面,根本不需要从外部传参数,这里的{commit,state},commit是一个执行mutations方法的对象,后面的是你需要从store里面拿的数据,也就是state对象,里面有vuex的所有数据,然后在actions自定义方法里面给api.js传参,直接解构state,就可以写入参了,如果不需要使用state,不传也可以

调用的时候,用mapActions和$store都可以,直接调用参数名,无需传参

④给img标签写@click点击事件

url: this.GLOBAL.base_url + 'user/validateCode'+'?random='+Math.random()

这里有一个问题,如果给<img>标签设置图片的方式为src,src里面给一个后台的请求图片的url地址,如果刷新的话,一样的url,但是img不会动,这时候加一个随机数字段即可,更常用的是直接加时间戳

⑤登录

关于登录的验证码问题,不是手机验证码,而是图形验证码,这里应该用一个http请求带username,pwd,CAPTCHA三个参数,而不是发送2次验证码。。。

另外,每一次请求验证码,都会把正确的验证码,放在session里面,而这个session是存在服务器端的,所以当客户端发送验证码的时候,直接在session里面校验就可以了,而且这个session应该是可以设置过期时间的,也就是,验证码其实也可以设置过期时间。

现在开始搞shiro,这个星期shiro搞完了,速度接着搞,下一个项目前端代码量有点大哦

npm ERR! code 1 npm ERR! path D:\environment2025.7.4\workcode\vs-vue-sbyxjd\node_modules\node-sass npm ERR! command failed npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node scripts/build.js npm ERR! Building: D:\nvm\nodejs\node.exe D:\environment2025.7.4\workcode\vs-vue-sbyxjd\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --l ibsass_ldflags= --libsass_library= npm ERR! gyp info it worked if it ends with ok npm ERR! gyp verb cli [ npm ERR! gyp verb cli 'D:\\nvm\\nodejs\\node.exe', npm ERR! gyp verb cli 'D:\\environment2025.7.4\\workcode\\vs-vue-sbyxjd\\node_modules\\node-gyp\\bin\\node-gyp.js', npm ERR! gyp verb cli 'rebuild', npm ERR! gyp verb cli '--verbose', npm ERR! gyp verb cli '--libsass_ext=', npm ERR! gyp verb cli '--libsass_cflags=', npm ERR! gyp verb cli '--libsass_ldflags=', npm ERR! gyp verb cli '--libsass_library=' npm ERR! gyp verb cli ] npm ERR! gyp info using node-gyp@7.1.2 npm ERR! gyp info using node@16.20.2 | win32 | x64 npm ERR! gyp verb command rebuild [] npm ERR! gyp verb command clean [] npm ERR! gyp verb clean removing "build" directory npm ERR! gyp verb command configure [] npm ERR! gyp verb find Python Python is not set from command line or npm configuration npm ERR! gyp verb find Python Python is not set from environment variable PYTHON npm ERR! gyp verb find Python checking if "python3" can be used npm ERR! gyp verb find Python - executing "python3" to get executable path npm ERR! gyp verb find Python - "python3" is not in PATH or produced an error npm ERR! gyp verb find Python checking if "python" can be used npm ERR! gyp verb find Python - executing "python" to get executable path npm ERR! gyp verb find Python - "python" is not in PATH or produced an error npm ERR! gyp verb find Python checking if "python2" can be used npm ERR! gyp verb find Python - executing "python2" to get executable path npm ERR! gyp verb find Python - "python2" is not in PATH or produced an error npm ERR! gyp verb find Python checking if Python is C:\Python37\python.exe npm ERR! gyp verb find Python - executing "C:\Python37\python.exe" to get version npm ERR! gyp verb find Python - "C:\Python37\python.exe" could not be run npm ERR! gyp verb find Python checking if Python is C:\Python27\python.exe npm ERR! gyp verb find Python - executing "C:\Python27\python.exe" to get version npm ERR! gyp verb find Python - "C:\Python27\python.exe" could not be run npm ERR! gyp verb find Python checking if the py launcher can be used to find Python npm ERR! gyp verb find Python - executing "py.exe" to get Python executable path npm ERR! gyp verb find Python - "py.exe" is not in PATH or produced an error npm ERR! gyp ERR! find Python npm ERR! gyp ERR! find Python Python is not set from command line or npm configuration npm ERR! gyp ERR! find Python Python is not set from environment variable PYTHON npm ERR! gyp ERR! find Python checking if "python3" can be used npm ERR! gyp ERR! find Python - "python3" is not in PATH or produced an error npm ERR! gyp ERR! find Python checking if "python" can be used npm ERR! gyp ERR! find Python - "python" is not in PATH or produced an error npm ERR! gyp ERR! find Python checking if "python2" can be used npm ERR! gyp ERR! find Python - "python2" is not in PATH or produced an error npm ERR! gyp ERR! find Python checking if Python is C:\Python37\python.exe npm ERR! gyp ERR! find Python - "C:\Python37\python.exe" could not be run npm ERR! gyp ERR! find Python checking if Python is C:\Python27\python.exe npm ERR! gyp ERR! find Python - "C:\Python27\python.exe" could not be run npm ERR! gyp ERR! find Python checking if the py launcher can be used to find Python npm ERR! gyp ERR! find Python - "py.exe" is not in PATH or produced an error npm ERR! gyp ERR! find Python npm ERR! gyp ERR! find Python ********************************************************** npm ERR! gyp ERR! find Python You need to install the latest version of Python. npm ERR! gyp ERR! find Python Node-gyp should be able to find and use Python. If not, npm ERR! gyp ERR! find Python you can try one of the following options: npm ERR! gyp ERR! find Python - Use the switch --python="C:\Path\To\python.exe" npm ERR! gyp ERR! find Python (accepted by both node-gyp and npm) npm ERR! gyp ERR! find Python - Set the environment variable PYTHON npm ERR! gyp ERR! find Python - Set the npm configuration variable python: npm ERR! gyp ERR! find Python npm config set python "C:\Path\To\python.exe" npm ERR! gyp ERR! find Python For more information consult the documentation at: npm ERR! gyp ERR! find Python https://github.com/nodejs/node-gyp#installation npm ERR! gyp ERR! find Python ********************************************************** npm ERR! gyp ERR! find Python npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error: Could not find any Python installation to use npm ERR! gyp ERR! stack at PythonFinder.fail (D:\environment2025.7.4\workcode\vs-vue-sbyxjd\node_modules\node-gyp\lib\find-python.js:302:47) npm ERR! gyp ERR! stack at PythonFinder.runChecks (D:\environment2025.7.4\workcode\vs-vue-sbyxjd\node_modules\node-gyp\lib\find-python.js:136:21) npm ERR! gyp ERR! stack at PythonFinder.<anonymous> (D:\environment2025.7.4\workcode\vs-vue-sbyxjd\node_modules\node-gyp\lib\find-python.js:200:18) npm ERR! gyp ERR! stack at PythonFinder.execFileCallback (D:\environment2025.7.4\workcode\vs-vue-sbyxjd\node_modules\node-gyp\lib\find-python.js:266:16) npm ERR! gyp ERR! stack at exithandler (node:child_process:410:5) npm ERR! gyp ERR! stack at ChildProcess.errorhandler (node:child_process:422:5) npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:513:28) npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12) npm ERR! gyp ERR! stack at onErrorNT (node:internal/child_process:485:16) npm ERR! gyp ERR! stack at processTicksAndRejections (node:internal/process/task_queues:83:21) npm ERR! gyp ERR! System Windows_NT 10.0.22631 npm ERR! gyp ERR! command "D:\\nvm\\nodejs\\node.exe" "D:\\environment2025.7.4\\workcode\\vs-vue-sbyxjd\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" npm ERR! gyp ERR! cwd D:\environment2025.7.4\workcode\vs-vue-sbyxjd\node_modules\node-sass npm ERR! gyp ERR! node -v v16.20.2 npm ERR! gyp ERR! node-gyp -v v7.1.2 npm ERR! gyp ERR! not ok npm ERR! Build failed with error code: 1 npm ERR! A complete log of this run can be found in: npm ERR! D:\nvm\nodejs\node_cache\_logs\2025-07-07T07_48_51_091Z-debug-0.log Process finished with exit code 1
07-08
npm install ffi-napi ref-napi express cors body-parser --save npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: 'body-parser@2.2.1', npm WARN EBADENGINE required: { node: '>=18' }, npm WARN EBADENGINE current: { node: 'v16.20.0', npm: '8.19.4' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: 'express@5.2.1', npm WARN EBADENGINE required: { node: '>= 18' }, npm WARN EBADENGINE current: { node: 'v16.20.0', npm: '8.19.4' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: 'content-disposition@1.0.1', npm WARN EBADENGINE required: { node: '>=18' }, npm WARN EBADENGINE current: { node: 'v16.20.0', npm: '8.19.4' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: 'finalhandler@2.1.1', npm WARN EBADENGINE required: { node: '>= 18.0.0' }, npm WARN EBADENGINE current: { node: 'v16.20.0', npm: '8.19.4' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: 'merge-descriptors@2.0.0', npm WARN EBADENGINE required: { node: '>=18' }, npm WARN EBADENGINE current: { node: 'v16.20.0', npm: '8.19.4' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: 'mime-types@3.0.2', npm WARN EBADENGINE required: { node: '>=18' }, npm WARN EBADENGINE current: { node: 'v16.20.0', npm: '8.19.4' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: 'router@2.2.0', npm WARN EBADENGINE required: { node: '>= 18' }, npm WARN EBADENGINE current: { node: 'v16.20.0', npm: '8.19.4' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: 'send@1.2.0', npm WARN EBADENGINE required: { node: '>= 18' }, npm WARN EBADENGINE current: { node: 'v16.20.0', npm: '8.19.4' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: 'serve-static@2.2.0', npm WARN EBADENGINE required: { node: '>= 18' }, npm WARN EBADENGINE current: { node: 'v16.20.0', npm: '8.19.4' } npm WARN EBADENGINE } npm ERR! code 1 npm ERR! path D:\Projects\ruoyi\PrinterHelper\PrinterSever\node_modules\ffi-napi npm ERR! command failed npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp-build npm ERR! nothing.c npm ERR! win_delay_load_hook.cc npm ERR! nothing.vcxproj -> D:\Projects\ruoyi\PrinterHelper\PrinterSever\node_modules\ffi-napi\build\Release\\nothing.lib npm ERR! Preprocessing assembly file ..\..\..\deps\libffi\src\x86\win64_intel.preasm npm ERR! '"call"' 不是内部或外部命令,也不是可运行的程序 npm ERR! 或批处理文件。 npm ERR! D:\Projects\ruoyi\PrinterHelper\PrinterSever\node_modules\ffi-napi\build\deps\libffi\ffi.targets(34,5): error MSB3721: 命令“call "call" "../../../deps/libffi/preprocess_asm.cmd" "../../../deps/libffi/include" "../../../deps/libffi/config/win/x64" "..\..\..\deps\libffi\src\x86\win64_intel.preasm" "Release\obj\ffi\win64_intel.asm"”已退出,返回代码为 1。 [D:\Projects\ruoyi\PrinterHelper\PrinterSever\node_modules\ffi-napi\build\deps\libffi\ffi.vcxproj] npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@10.2.0 npm ERR! gyp info using node@16.20.0 | win32 | x64 npm ERR! gyp info find Python using Python version 3.8.10 found at "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe" npm ERR! gyp http GET https://nodejs.org/dist/v17.0.0/node-v17.0.0-headers.tar.gz npm ERR! gyp http 200 https://nodejs.org/dist/v17.0.0/node-v17.0.0-headers.tar.gz npm ERR! gyp http GET https://nodejs.org/dist/v17.0.0/SHASUMS256.txt npm ERR! gyp http GET https://nodejs.org/dist/v17.0.0/win-x64/node.lib npm ERR! gyp http 200 https://nodejs.org/dist/v17.0.0/SHASUMS256.txt npm ERR! gyp http 200 https://nodejs.org/dist/v17.0.0/win-x64/node.lib npm ERR! gyp info find VS using VS2022 (17.11.35327.3) found at: npm ERR! gyp info find VS "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools" npm ERR! gyp info find VS run with --verbose for detailed information npm ERR! gyp info spawn C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe npm ERR! gyp info spawn args [ npm ERR! gyp info spawn args 'D:\\node_global\\node_modules\\node-gyp\\gyp\\gyp_main.py', npm ERR! gyp info spawn args 'binding.gyp', npm ERR! gyp info spawn args '-f', npm ERR! gyp info spawn args 'msvs', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args 'D:\\Projects\\ruoyi\\PrinterHelper\\PrinterSever\\node_modules\\ffi-napi\\build\\config.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args 'D:\\node_global\\node_modules\\node-gyp\\addon.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args 'C:\\Users\\Administrator\\AppData\\Local\\node-gyp\\Cache\\17.0.0\\include\\node\\common.gypi', npm ERR! gyp info spawn args '-Dlibrary=shared_library', npm ERR! gyp info spawn args '-Dvisibility=default', npm ERR! gyp info spawn args '-Dnode_root_dir=C:\\Users\\Administrator\\AppData\\Local\\node-gyp\\Cache\\17.0.0', npm ERR! gyp info spawn args '-Dnode_gyp_dir=D:\\node_global\\node_modules\\node-gyp', npm ERR! gyp info spawn args '-Dnode_lib_file=C:\\\\Users\\\\Administrator\\\\AppData\\\\Local\\\\node-gyp\\\\Cache\\\\17.0.0\\\\<(target_arch)\\\\node.lib', npm ERR! gyp info spawn args '-Dmodule_root_dir=D:\\Projects\\ruoyi\\PrinterHelper\\PrinterSever\\node_modules\\ffi-napi', npm ERR! gyp info spawn args '-Dnode_engine=v8', npm ERR! gyp info spawn args '--depth=.', npm ERR! gyp info spawn args '--no-parallel', npm ERR! gyp info spawn args '--generator-output', npm ERR! gyp info spawn args 'D:\\Projects\\ruoyi\\PrinterHelper\\PrinterSever\\node_modules\\ffi-napi\\build', npm ERR! gyp info spawn args '-Goutput_dir=.' npm ERR! gyp info spawn args ] npm ERR! gyp info spawn C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\MSBuild.exe npm ERR! gyp info spawn args [ npm ERR! gyp info spawn args 'build\\binding.sln', npm ERR! gyp info spawn args '/clp:Verbosity=minimal', npm ERR! gyp info spawn args '/nologo', npm ERR! gyp info spawn args '/p:Configuration=Release;Platform=x64' npm ERR! gyp info spawn args ] npm ERR! gyp ERR! build error npm ERR! gyp ERR! stack Error: `C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\MSBuild.exe` failed with exit code: 1 npm ERR! gyp ERR! stack at ChildProcess.<anonymous> (D:\node_global\node_modules\node-gyp\lib\build.js:216:23) npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:513:28) npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:293:12) npm ERR! gyp ERR! System Windows_NT 10.0.26200 npm ERR! gyp ERR! command "D:\\nodejs\\node.exe" "D:\\node_global\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" npm ERR! gyp ERR! cwd D:\Projects\ruoyi\PrinterHelper\PrinterSever\node_modules\ffi-napi npm ERR! gyp ERR! node -v v16.20.0 npm ERR! gyp ERR! node-gyp -v v10.2.0 npm ERR! gyp ERR! not ok npm ERR! A complete log of this run can be found in: npm ERR! D:\node_cache\_logs\2025-12-08T08_03_51_617Z-debug-0.log
最新发布
12-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值