Browse Source

addbook api post sucess books add err tobast

charblus 5 years ago
parent
commit
ffefdec959
4 changed files with 65 additions and 39 deletions
  1. 34 7
      server/controllers/addbook.js
  2. 2 1
      server/package.json
  3. 21 0
      server/tools/snall.sql
  4. 8 31
      src/pages/me/Me.vue

+ 34 - 7
server/controllers/addbook.js

@@ -1,11 +1,24 @@
 
 const https = require('https')
+const { mysql } = require('../qcloud')
 // 新增图书  1. 获取豆瓣信息  2 入库
 // https://api.douban.com/v2/book/isbn/9787536692930
 module.exports = async (ctx) => {
-  const { isbn, openId } = ctx.request.body
-  console.log('添加图书', isbn, openId)
-  if (isbn && openId) {
+  const { isbn, openid } = ctx.request.body
+  console.log('添加图书', isbn, openid)
+  if (isbn && openid) {
+    // 录入异常情况
+    const findRes = await mysql('books').select().where('isbn', isbn)
+    if (findRes.length) {
+      ctx.state = {
+        code: -1,
+        data: {
+          msg: '图书已存在'
+        }
+      }
+      return
+    }
+
     let url = 'https://api.douban.com/v2/book/isbn/' + isbn
     console.log('链接', url)
     const bookInfo = await getJSON(url)
@@ -15,9 +28,23 @@ module.exports = async (ctx) => {
       return `${v.title} ${v.count}`
     }).join(',')
     const author = bookInfo.author.join(',')
-    console.log({
-      rate, title, image, alt, publisher, summary, price, tags, author
-    })
+    try {
+      // 写入库
+      await mysql('books').insert({
+        isbn, openid, rate, title, image, alt, publisher, summary, price, tags, author
+      })
+      ctx.state.data = {
+        title,
+        msg: 'success'
+      }
+    } catch (e) {
+      ctx.state = {
+        code: -1,
+        data: {
+          msg: '新增失败:' + e.sqlMessage
+        }
+      }
+    }
   }
 }
 
@@ -29,7 +56,7 @@ function getJSON (url) {
         urlData += data
       })
       res.on('end', data => {
-        const bookInfo = JSON.parse(data)
+        const bookInfo = JSON.parse(urlData)
         if (bookInfo.title) {
           resolve(bookInfo)
         }

+ 2 - 1
server/package.json

@@ -6,7 +6,8 @@
     "scripts": {
         "start": "pm2 start process.prod.json --no-daemon",
         "dev": "nodemon --config nodemon.json app.js",
-        "initdb": "npm install && node tools/initdb.js"
+        "initdb": "npm install && node tools/initdb.js",
+        "lint": "eslint --fix --ext .js server"
     },
     "author": "Jason",
     "license": "MIT",

+ 21 - 0
server/tools/snall.sql

@@ -0,0 +1,21 @@
+
+-- show create table books 
+
+DROP TABLE IF EXISTS `books`;
+
+ CREATE TABLE `books` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `isbn` varchar(20) NOT NULL,
+  `openid` varchar(100) NOT NULL,
+  `title` varchar(100) NOT NULL,
+  `image` varchar(100) NOT NULL,
+  `alt` varchar(100) NOT NULL,
+  `publisher` varchar(100) NOT NULL,
+  `summary` varchar(1000) NOT NULL,
+  `price` varchar(100) DEFAULT NULL,
+  `rate` float DEFAULT NULL,
+  `tags` varchar(100) DEFAULT NULL,
+  `author` varchar(100) DEFAULT NULL,
+  `count` int(11) NOT NULL DEFAULT '0',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;

+ 8 - 31
src/pages/me/Me.vue

@@ -11,7 +11,7 @@
 </template>
 
 <script>
-import {showSuccess} from '@/util'
+import {showSuccess, post, showModal} from '@/util'
 import config from '@/config'
 import qcloud from 'wafer2-client-sdk'
 import YearProgress from '@/components/YearProgress'
@@ -27,21 +27,24 @@ export default {
       }
     }
   },
+  created () {
+    this.onShow();
+  },
   methods: {
     async addBook(isbn) {
+     
       const res = await post('/weapp/addbook', {
         isbn,
         openid: this.userInfo.openId
       })
-      if(res.code == 0&& res.data.title) {
-        showSuccess('添加成功', `${res.data.title}添加成功`)
-      }
+      console.log(res);
+      showModal('添加成功', `${res.title}添加成功`)
     },
     scanBook () {
       wx.scanCode({
         success: (res) => {
           if (res.result) {
-            // console.log(res.result)
+            console.log(res.result)
             this.addBook(res.result);
           }
         }
@@ -78,32 +81,6 @@ export default {
   }
 }
 
-// import qcloud from 'wafer2-client-sdk'
-// import config from '@/config'
-
-// export default {
-//   data () {
-//     return {
-//       userInfo: { }
-//     }
-//   },
-//   methods: {
-//     login(){
-//       console.log('1122334455667799')
-//       // qcloud.setLoginUrl(config.loginUrl);
-//       // qcloud.login({
-//       //     success: function (userInfo) {
-//       //         console.log('登录成功', userInfo);
-//       //         wx.setStorageSync('userInfo', userInfo)
-//       //     },
-//       //     fail: function (err) {
-//       //         console.log('登录失败', err);
-//       //     }
-//       // });
-//       // this.userInfo = wx.getStorageSync('userInfo')
-//     }
-//   }
-// }
 </script>