src/me.vue can obtain the ISBN of the book normally. The problem lies in the async addBook method under the same page. After logging in normally, when adding a book, the route of / weapp/addbook can not be triggered. N methods are tried, and the directory server/controllers/addbook.js route is configured, but it can not be accessed at all in actual operation, which is quite depressing.
related codes
/ / Me.vue
<template>
<div class="container">
<div class="userinfo">
<img :src="userinfo.avatarUrl">
{{userinfo.nickName}}
</div>
<YearProgress></YearProgress>
<button v-if="userinfo.openId" @click="scanBook" class="btn"></button>
<button v-else open-type="getUserInfo" lang="zh_CN" class="btn" @getuserinfo="login" ></button>
</div>
</template>
<script>
import qcloud from "wafer2-client-sdk"
import YearProgress from "@/components/YearProgress"
import {showSuccess, post} from "@/util"
import config from "@/config"
export default {
components: {
YearProgress
},
data () {
return {
userinfo: {
avatarUrl: "http://image.shengxinjing.cn/rate/unlogin.png",
nickName: ""
}
}
},
methods: {
async addBook (isbn) {
console.log(isbn)
const res = await post("/weapp/addbook",{
isbn,
openid:this.userinfo.openId
})
// showModal("",`${res.title}`)
},
scanBook () {
wx.scanCode({
success: (res) => {
if (res.result) {
this.addBook(res.result)
}
}
})
},
getWxLogin: function ({encryptedData, iv, userinfo}) {
const self = this
wx.login({
success: function (loginResult) {
console.log("loginResult", loginResult)
var loginParams = {
code: loginResult.code,
encryptedData: encryptedData,
iv: iv
}
qcloud.setLoginUrl(config.loginUrl)
qcloud.requestLogin({
loginParams,
success () {
//
qcloud.request({
url: config.userUrl,
login: true,
success (userRes) {
showSuccess("")
wx.setStorageSync("userinfo", userRes.data.data)
self.userinfo = userRes.data.data
}
})
},
fail (error) {
console.log(error)
// showModal("", error)
}
})
},
fail: function (loginError) {
// showModal("", error)
console.log(loginError)
}
})
},
login (e) {
const self = this
//
wx.getSetting({
success: function (res) {
//
if (res.authSetting["scope.userInfo"]) {
//
wx.checkSession({
success: function () {
//
showSuccess("")
},
fail: function () {
//
qcloud.clearSession()
//
//
var options = {
encryptedData: e.mp.detail.encryptedData,
iv: e.mp.detail.iv,
userinfo: e.mp.detail.userInfo
}
self.getWxLogin(options)
}
})
} else {
// showModal("", e.mp.detail.errMsg)
console.log("", e.mp.detail.errMsg)
}
}
})
}
},
onShow () {
let userinfo = wx.getStorageSync("userinfo")
if (userinfo) {
this.userinfo = userinfo
}
}
}
</script>
<style lang="scss">
.container{
padding:0 30rpx;
.userinfo{
margin-top: 100rpx;
text-align: center;
img {
width: 150rpx;
height: 150rpx;
margin: 20rpx;
border-radius: 50%
}
}
}
</style>
server directory of the wafer2-quickstart-nodejs-master plug-in, my-project/server/routes/index.js
/**
* ajax
*/
const router = require("koa-router")({
prefix: "/weapp"
})
const controllers = require("../controllers")
// sdk
// Koa
const { auth: { authorizationMiddleware, validationMiddleware } } = require("../qcloud")
// --- Demo --- //
//
router.get("/login", authorizationMiddleware, controllers.login)
//
router.get("/user", validationMiddleware, controllers.user)
// --- Demo --- //
// url wx.uploadFile
router.post("/upload", controllers.upload)
// --- Demo --- //
// GET
router.get("/tunnel", controllers.tunnel.get)
// POST
router.post("/tunnel", controllers.tunnel.post)
// --- Demo --- //
// GET
router.get("/message", controllers.message.get)
// POST
router.post("/message", controllers.message.post)
router.get("/demo", controllers.demo)
router.post("/addbook", controllers.addbook)
module.exports = router
server directory of the wafer2-quickstart-nodejs-master plug-in, my-project/server/controllers/addbook.js
const https = require("https")
module.exports = async (ctx) => {
const { isbn, openid } = ctx.request.body
console.log("", isbn, openid)
if (isbn && openid) {
let url = "https://api.douban.com/v2/book/isbn/" + isbn
console.log(url)
const bookinfo = await getJSON(url)
const rate = bookinfo.rating.average
const { title, image, alt, publisher, summary, price } = bookinfo
console.log(bookinfo)
} else {
console.log(22222)
}
}
function getJSON (url) {
return new Promise((resolve, reject) => {
https.get(url, res => {
let urlData = ""
res.on("data", data => {
urlData += data
})
res.on("end", data => {
const bookinfo = JSON.parse(data)
if (bookinfo.title) {
resolve(bookinfo)
}
reject(bookinfo)
})
})
})
}
Project Master configuration json
{
"name": "mpvue-demo",
"version": "1.0.0",
"description": "A Mpvue project",
"author": "3581729170@qq.com",
"private": true,
"scripts": {
"dev": "node build/dev-server.js",
"start": "node build/dev-server.js",
"build": "node build/build.js",
"lint": "eslint --fix --ext .js,.vue src"
},
"dependencies": {
"mpvue": "^1.0.11",
"vuex": "^3.0.1",
"wafer2-client-sdk": "^1.1.3"
},
"devDependencies": {
"babel-core": "^6.22.1",
"babel-eslint": "^8.2.3",
"babel-loader": "^7.1.1",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-preset-env": "^1.3.2",
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.22.0",
"chalk": "^2.4.0",
"connect-history-api-fallback": "^1.3.0",
"copy-webpack-plugin": "^4.5.1",
"css-loader": "^0.28.11",
"cssnano": "^3.10.0",
"eslint": "^4.19.1",
"eslint-config-standard": "^11.0.0",
"eslint-friendly-formatter": "^4.0.1",
"eslint-loader": "^2.0.0",
"eslint-plugin-html": "^4.0.3",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.4.0",
"eslint-plugin-standard": "^3.0.1",
"eventsource-polyfill": "^0.9.6",
"express": "^4.16.3",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^1.1.11",
"friendly-errors-webpack-plugin": "^1.7.0",
"glob": "^7.1.2",
"html-webpack-plugin": "^3.2.0",
"http-proxy-middleware": "^0.18.0",
"mpvue-loader": "1.0.13",
"mpvue-template-compiler": "^1.0.11",
"mpvue-webpack-target": "^1.0.0",
"node-sass": "^4.9.2",
"optimize-css-assets-webpack-plugin": "^3.2.0",
"ora": "^2.0.0",
"portfinder": "^1.0.13",
"postcss-loader": "^2.1.4",
"postcss-mpvue-wxss": "^1.0.0",
"prettier": "~1.12.1",
"px2rpx-loader": "^0.1.10",
"rimraf": "^2.6.0",
"sass-loader": "^7.0.3",
"semver": "^5.3.0",
"shelljs": "^0.8.1",
"uglifyjs-webpack-plugin": "^1.2.5",
"url-loader": "^1.0.1",
"vue-style-loader": "^4.1.0",
"webpack": "^3.11.0",
"webpack-bundle-analyzer": "^2.2.1",
"webpack-dev-middleware-hard-disk": "^1.12.0",
"webpack-merge": "^4.1.0",
"webpack-mpvue-asset-plugin": "0.0.2"
},
"engines": {
"node": ">= 4.0.0",
"npm": ">= 3.0.0"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}
config.json of the project
{
"description": "",
"setting": {
"urlCheck": false,
"es6": false,
"postcss": true,
"minified": true,
"newFeature": true
},
"miniprogramRoot": "./dist/",
"qcloudRoot": "./server/",
"compileType": "miniprogram",
"appid": "wx73a95869c3086bce",
"projectname": "mpvue-demo",
"condition": {
"search": {
"current": -1,
"list": []
},
"conversation": {
"current": -1,
"list": []
},
"game": {
"currentL": -1,
"list": []
},
"miniprogram": {
"current": -1,
"list": []
}
}
}
the complete code package is here (including the identifiable book QR code)-> Baidu online disk