ソースを参照

detail comment post and [server] post /addcomment

charblus 5 年 前
コミット
06377864e2

+ 19 - 0
server/controllers/addcomment.js

@@ -0,0 +1,19 @@
+const { mysql } = require('../qcloud')
+
+module.exports = async (ctx) => {
+  const {bookid, comment, openid, location, phone} = ctx.request.body
+  console.log(bookid, comment, openid, location, phone)
+  try {
+    await mysql('comments').insert({bookid, comment, openid, location, phone})
+    ctx.state.data = {
+      msg: 'success'
+    }
+  } catch (e) {
+    ctx.state = {
+      code: -1,
+      data: {
+        msg: '评论失败' + e.sqlMessage
+      }
+    }
+  }
+}

+ 2 - 0
server/routes/index.js

@@ -40,5 +40,7 @@ router.post('/addbook', controllers.addbook)
 router.get('/booklist', controllers.booklist)
 router.get('/bookdetail', controllers.bookdetail)
 router.get('/top', controllers.top)
+// 评论
+router.post('/addcomment', controllers.addcomment)
 
 module.exports = router

+ 12 - 0
server/tools/snall.sql

@@ -19,3 +19,15 @@ DROP TABLE IF EXISTS `books`;
   `count` int(11) NOT NULL DEFAULT '0',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
+
+DROP TABLE IF EXISTS `comments`;
+
+CREATE TABLE `comments` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `openid` varchar(100) NOT NULL,
+  `bookid` varchar(100) NOT NULL,
+  `comment` varchar(200) NOT NULL,
+  `phone` varchar(20) DEFAULT NULL,
+  `location` varchar(20) DEFAULT NULL,
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

+ 5 - 0
src/app.json

@@ -6,6 +6,11 @@
     "pages/detail/main"
 
   ],
+  "permission": {
+    "scope.userLocation": {
+      "desc": "你的位置信息将用于小程序位置接口的效果展示"
+    }
+  },
   "window": {
     "backgroundTextStyle": "light",
     "navigationBarBackgroundColor": "#EA5149",

+ 64 - 4
src/pages/detail/Detail.vue

@@ -18,11 +18,12 @@
          <switch color="#EA5A49" @change="getPhone" :checked="phone"/>
          <span class="text-primary">{{phone}}</span>
       </div>
+      <button class="btn" @click="addComment">评论</button>
     </div>
   </div>
 </template>
 <script>
-import {get} from '@/util'
+import {get, post, showModal} from '@/util'
 import BookInfo from '@/components/BookInfo'
 export default {
   components: {
@@ -30,6 +31,7 @@ export default {
   },
   data () {
     return {
+      userinfo: {},
       bookid: '',
       info: {},
       comment: '',
@@ -38,8 +40,58 @@ export default {
     }
   },
   methods: {
-    getGeo () {
-      
+    async addComment () {
+      if (!this.comment) {
+        return;
+      }
+      // 评论内容 手机型号 地理位置 图书id 用户openid
+      const data = {
+        openid: this.userinfo.openId,
+        bookid: this.bookid,
+        comment: this.comment,
+        phone: this.phone,
+        location: this.location
+      }
+      try {
+        console.log(data)
+        await post('/weapp/addcomment', data);
+        this.comment = '';
+      } catch (e) {
+        showModal('失败', e.msg)
+      }
+    },
+    getGeo (e) {
+      // NVYWM4sZYGjaHPpO6vwVk0j1v4ijBBKB
+      const ak = 'NVYWM4sZYGjaHPpO6vwVk0j1v4ijBBKB';
+      let url = 'http://api.map.baidu.com/geocoder/v2/';
+
+      if(e.target.value) {
+        wx.getLocation({
+          type: 'wgs84',
+          success: geo => {
+            console.log('地理数据', geo);
+            wx.request({
+              url,
+              data: {
+                ak,
+                location: `${geo.latitude},${geo.longitude}`,
+                output: 'json'
+              },
+              success: res => {
+                console.log('百度api返回', res)
+                if (res.data.status === 0) {
+                  this.location = res.data.result.addressComponent.city;
+                } else {
+                  this.location = '未知地点';
+                }
+              }
+            })
+            
+          }
+        })
+      } else {
+        this.location = '';
+      }
     },
     getPhone(e) {
       if(e.target.value) {
@@ -82,6 +134,11 @@ export default {
   mounted () {
     this.bookid = this.$root.$mp.query.id;  // 跳转页面使用this.$root.$mp.query获取参数
     this.getDetail();
+    const userinfo = wx.getStorageSync('userInfo')
+    console.log('userInfo', userinfo);
+    if (userinfo) {
+      this.userinfo = userinfo;
+    }
     // wx.showShareMenu({
     //   withShareTicket: true
     // });
@@ -92,7 +149,7 @@ export default {
 .comment {
   margin-top: 10px;
   .textarea {
-    width: 730px;
+    width: 730rpx;
     background-color: #eee;
     padding: 10px;
   }
@@ -104,5 +161,8 @@ export default {
     margin-top: 10px;
     padding: 5px 10px;
   }
+  .btn {
+     margin: 10px 0;
+  }
 }
 </style>