Detail.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div>
  3. <!-- 图书id{{bookid}} -->
  4. <BookInfo :info='info'></BookInfo>
  5. <CommentList :comments="comments"></CommentList>
  6. <div class="comment" v-if="showAdd">
  7. <textarea v-model="comment"
  8. class="textarea"
  9. :maxlength="100"
  10. placeholder="请输入图书短评"></textarea>
  11. <div class="location">
  12. 地理位置:
  13. <switch color="#EA5A49" @change="getGeo" :checked="location" />
  14. <span class="text-primary">{{location}}</span>
  15. </div>
  16. <div class="phone">
  17. 手机型号:
  18. <switch color="#EA5A49" @change="getPhone" :checked="phone"/>
  19. <span class="text-primary">{{phone}}</span>
  20. </div>
  21. <button class="btn" @click="addComment">评论</button>
  22. </div>
  23. <div v-else class="text-footer">
  24. 未登录或者已经评论过啦
  25. </div>
  26. <button open-type='share' class="btn">转发给好友</button>
  27. </div>
  28. </template>
  29. <script>
  30. import {get, post, showModal} from '@/util'
  31. import BookInfo from '@/components/BookInfo'
  32. import CommentList from '@/components/CommentList'
  33. export default {
  34. components: {
  35. BookInfo,
  36. CommentList
  37. },
  38. data () {
  39. return {
  40. comments: [],
  41. userinfo: {},
  42. bookid: '',
  43. info: {},
  44. comment: '',
  45. location: '',
  46. phone: ''
  47. }
  48. },
  49. computed: {
  50. showAdd () {
  51. // 未登录
  52. if (!this.userinfo.openId) {
  53. return false
  54. }
  55. // 评论页面里查到有自己的openid
  56. if (this.comments.filter(v => v.openid === this.userinfo.openId).length) {
  57. return false
  58. }
  59. return true
  60. }
  61. },
  62. methods: {
  63. async addComment () {
  64. if (!this.comment) {
  65. return
  66. }
  67. // 评论内容 手机型号 地理位置 图书id 用户openid
  68. const data = {
  69. openid: this.userinfo.openId,
  70. bookid: this.bookid,
  71. comment: this.comment,
  72. phone: this.phone,
  73. location: this.location
  74. }
  75. try {
  76. console.log(data)
  77. await post('/weapp/addcomment', data)
  78. this.comment = ''
  79. this.getComments()
  80. } catch (e) {
  81. showModal('失败', e.msg)
  82. }
  83. },
  84. getGeo (e) {
  85. // NVYWM4sZYGjaHPpO6vwVk0j1v4ijBBKB
  86. const ak = 'NVYWM4sZYGjaHPpO6vwVk0j1v4ijBBKB'
  87. let url = 'http://api.map.baidu.com/geocoder/v2/'
  88. if (e.target.value) {
  89. wx.getLocation({
  90. type: 'wgs84',
  91. success: geo => {
  92. console.log('地理数据', geo)
  93. wx.request({
  94. url,
  95. data: {
  96. ak,
  97. location: `${geo.latitude},${geo.longitude}`,
  98. output: 'json'
  99. },
  100. success: res => {
  101. console.log('百度api返回', res)
  102. if (res.data.status === 0) {
  103. this.location = res.data.result.addressComponent.city
  104. } else {
  105. this.location = '未知地点'
  106. }
  107. }
  108. })
  109. }
  110. })
  111. } else {
  112. this.location = ''
  113. }
  114. },
  115. getPhone (e) {
  116. if (e.target.value) {
  117. const res = wx.getSystemInfoSync()
  118. // console.log(res.model);
  119. this.phone = res.model
  120. } else {
  121. this.phone = ''
  122. }
  123. },
  124. async getDetail () {
  125. const info = await get('/weapp/bookdetail', {id: this.bookid})
  126. wx.setNavigationBarTitle({
  127. title: info.title
  128. })
  129. console.log(info)
  130. this.info = info
  131. },
  132. async getComments () {
  133. const comments = await get('/weapp/commentlist', {bookid: this.bookid})
  134. console.log('comments', comments)
  135. this.comments = comments.list || []
  136. }
  137. },
  138. onShareAppMessage: function (res) {
  139. return {
  140. title: this.info.title,
  141. path: 'pages/detail/main',
  142. imageUrl: '' // 不填写默认页面截图
  143. // success: function (shareTickets) {
  144. // // console.info(shareTickets + '成功');
  145. // console.log(shareTickets + '成功');
  146. // // 转发成功
  147. // },
  148. // fail: function (res) {
  149. // console.log(res + '失败');
  150. // // 转发失败
  151. // },
  152. // complete:function(res){
  153. // console.log(res + '不管成功失败都会执行');
  154. // // 不管成功失败都会执行
  155. // }
  156. }
  157. },
  158. mounted () {
  159. this.bookid = this.$root.$mp.query.id // 跳转页面使用this.$root.$mp.query获取参数
  160. this.getDetail()
  161. this.getComments()
  162. const userinfo = wx.getStorageSync('userInfo')
  163. console.log('userInfo', userinfo)
  164. if (userinfo) {
  165. this.userinfo = userinfo
  166. }
  167. // wx.showShareMenu({
  168. // withShareTicket: true
  169. // });
  170. }
  171. }
  172. </script>
  173. <style lang="scss">
  174. .comment {
  175. margin-top: 10px;
  176. .textarea {
  177. width: 730rpx;
  178. background-color: #eee;
  179. padding: 10px;
  180. }
  181. .location {
  182. margin-top: 10px;
  183. padding: 5px 10px;
  184. }
  185. .phone {
  186. margin-top: 10px;
  187. padding: 5px 10px;
  188. }
  189. .btn {
  190. margin: 10px 0;
  191. }
  192. }
  193. </style>