鉴权完成,并修复小程序接口拦截器问题
This commit is contained in:
@@ -1,7 +1,2 @@
|
|||||||
module.exports = () => {
|
module.exports = () => {
|
||||||
// app.beforeStart(async () => {
|
|
||||||
// console.log(app.config)
|
|
||||||
// const mysqlConfig = await app.configCenter.fetch("mysql")
|
|
||||||
// app.database = app.mysql.createInstance(mysqlConfig)
|
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
|
|||||||
11
server/app/controller/product.js
Normal file
11
server/app/controller/product.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
const Controller = require("egg").Controller
|
||||||
|
|
||||||
|
class Product extends Controller {
|
||||||
|
async list() {
|
||||||
|
const {ctx: {query}} = this
|
||||||
|
this.ctx.body = {code: 0, data: []}
|
||||||
|
this.ctx.status = 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Product
|
||||||
41
server/app/controller/wxmp.js
Normal file
41
server/app/controller/wxmp.js
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
const Controller = require("egg").Controller
|
||||||
|
|
||||||
|
const wxmpConfig = {
|
||||||
|
appid: "wx68ef6cfaa104652a",
|
||||||
|
secret: "4dccf2429685eb8787b984d61ae69119"
|
||||||
|
}
|
||||||
|
const table = "sys_user"
|
||||||
|
/**
|
||||||
|
* 微信小程序code转unionid
|
||||||
|
* @param ctx
|
||||||
|
* @returns {Promise<*>}
|
||||||
|
*/
|
||||||
|
const code2Openid = async ctx => {
|
||||||
|
const result = await ctx.curl("https://api.weixin.qq.com/sns/jscode2session", {
|
||||||
|
data: {
|
||||||
|
...wxmpConfig,
|
||||||
|
js_code: ctx.query.code, // 登录时获取的 code
|
||||||
|
grant_type: 'authorization_code' // 授权类型,此处只需填写 authorization_code
|
||||||
|
},
|
||||||
|
dataType: "json"
|
||||||
|
})
|
||||||
|
return result.data?.openid
|
||||||
|
}
|
||||||
|
|
||||||
|
class WeixinMP extends Controller {
|
||||||
|
async token() {
|
||||||
|
const {ctx: {query}, app} = this
|
||||||
|
if (!!query.code) {
|
||||||
|
const wechatOpenId = await code2Openid(this.ctx)
|
||||||
|
if (!wechatOpenId) return this.ctx.body = {code: 1, msg: "鉴权失败,无法获取openid"}
|
||||||
|
const user = await app.mysql.get(table, {wechatOpenId})
|
||||||
|
const {name, avatar} = query
|
||||||
|
const result = await this.service.db.addOrUpdate(table, {name, avatar, wechatOpenId, id: user?.id})
|
||||||
|
const token = app.jwt.sign({id: result.data?.id}, app.config.jwt.secret)
|
||||||
|
this.ctx.body = {code: 0, data: "bearer " + token}
|
||||||
|
} else this.ctx.body = {code: 1, msg: "缺少必要参数(code)"}
|
||||||
|
this.ctx.status = 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = WeixinMP
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
module.exports = (app) => {
|
module.exports = (app) => {
|
||||||
const {router, controller} = app;
|
const {router, controller, jwt} = app;
|
||||||
console.log('启动接口...')
|
console.log('启动接口...')
|
||||||
for (const file in controller) {
|
for (const file in controller) {
|
||||||
for (const item in controller[file]) {
|
for (const item in controller[file]) {
|
||||||
console.log(`初始化接口:/${file}/${item}`)
|
console.log(`初始化接口:/${file}/${item}`)
|
||||||
router.post(`/api/${file}/${item}`, controller[file][item])
|
item == "token" ? router.post(`/api/${file}/${item}`, controller[file][item]) :
|
||||||
|
router.post(`/api/${file}/${item}`, jwt, controller[file][item])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log("接口初始化完毕")
|
console.log("接口初始化完毕")
|
||||||
|
|||||||
@@ -22,8 +22,17 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
security: {
|
security: {
|
||||||
csrf: {
|
csrf: {
|
||||||
queryName: "token",
|
enable: false,
|
||||||
ignore: ctx => ctx.headers.passport === "c799f2d92de34b97"//md5编码:kubbo&flora
|
ignoreJSON: true,
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
//登录鉴权
|
||||||
|
jwt: {
|
||||||
|
secret: "c799f2d92de34b97"
|
||||||
|
},
|
||||||
|
//跨域设置
|
||||||
|
cors: {
|
||||||
|
origin: '*',
|
||||||
|
allowMethods: 'GET,HEAD,POST',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,14 @@
|
|||||||
exports.mysql = {
|
module.exports = {
|
||||||
enable: true,
|
mysql: {
|
||||||
package: 'egg-mysql'
|
enable: true,
|
||||||
|
package: 'egg-mysql',
|
||||||
|
},
|
||||||
|
jwt: {
|
||||||
|
enable: true,
|
||||||
|
package: 'egg-jwt',
|
||||||
|
},
|
||||||
|
cors: {
|
||||||
|
enable: true,
|
||||||
|
package: 'egg-cors',
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,9 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "和老婆的创业项目",
|
"description": "和老婆的创业项目",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "egg-bin dev"
|
"dev": "egg-bin dev",
|
||||||
|
"start": "egg-scripts start --daemon",
|
||||||
|
"stop": "egg-scripts stop"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"egg",
|
"egg",
|
||||||
@@ -13,7 +15,11 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"egg": "^3.9.2",
|
"egg": "^3.9.2",
|
||||||
|
"egg-cors": "^2.2.3",
|
||||||
|
"egg-jwt": "^3.1.7",
|
||||||
"egg-mysql": "^3.3.0",
|
"egg-mysql": "^3.3.0",
|
||||||
|
"egg-passport": "^2.1.1",
|
||||||
|
"egg-scripts": "^2.17.0",
|
||||||
"uuid": "^9.0.0"
|
"uuid": "^9.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import adapter from 'axios-miniprogram-adapter'
|
import adapter from 'axios-miniprogram-adapter'
|
||||||
|
import {mainStore} from "./pinia";
|
||||||
|
import util from "./util";
|
||||||
|
|
||||||
const instance = axios.create({
|
const instance = axios.create({
|
||||||
baseURL: "http://localhost:7001",
|
baseURL: "http://localhost:7001",
|
||||||
@@ -7,9 +9,9 @@ const instance = axios.create({
|
|||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
adapter
|
adapter
|
||||||
})
|
})
|
||||||
|
const store = mainStore()
|
||||||
const getToken = () => {
|
const getToken = () => {
|
||||||
let vuex = uni.getStorageSync("vuex")
|
return store?.token || null
|
||||||
return !!vuex ? JSON.parse(vuex).token : null
|
|
||||||
}
|
}
|
||||||
const source = axios.CancelToken.source();
|
const source = axios.CancelToken.source();
|
||||||
instance.interceptors.request.use(config => {
|
instance.interceptors.request.use(config => {
|
||||||
@@ -26,5 +28,21 @@ instance.interceptors.request.use(config => {
|
|||||||
console.error(err)
|
console.error(err)
|
||||||
return Promise.reject(err)
|
return Promise.reject(err)
|
||||||
})
|
})
|
||||||
|
instance.interceptors.response.use(
|
||||||
|
function (response) {
|
||||||
|
util.$hideLoading();
|
||||||
|
if (response.data.code === 1) {
|
||||||
|
util.$toast({title: response.data.msg, duration: 3000});
|
||||||
|
} else if (response.data.code == 2) {
|
||||||
|
//首次静默登录异常不做任何返回
|
||||||
|
} else if (response.data.code === 401) {
|
||||||
|
store.token = null
|
||||||
|
} else {
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
function (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
);
|
||||||
export default instance
|
export default instance
|
||||||
|
|||||||
Reference in New Issue
Block a user