Detail.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <div>
  3. <!-- 图书id{{bookid}} -->
  4. <BookInfo :info='info'></BookInfo>
  5. <!-- <button open-type='share' class="btn">转发给好友</button> -->
  6. <div class="comment">
  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>
  24. </template>
  25. <script>
  26. import {get, post, showModal} from '@/util'
  27. import BookInfo from '@/components/BookInfo'
  28. export default {
  29. components: {
  30. BookInfo
  31. },
  32. data () {
  33. return {
  34. userinfo: {},
  35. bookid: '',
  36. info: {},
  37. comment: '',
  38. location: '',
  39. phone: ''
  40. }
  41. },
  42. methods: {
  43. async addComment () {
  44. if (!this.comment) {
  45. return;
  46. }
  47. // 评论内容 手机型号 地理位置 图书id 用户openid
  48. const data = {
  49. openid: this.userinfo.openId,
  50. bookid: this.bookid,
  51. comment: this.comment,
  52. phone: this.phone,
  53. location: this.location
  54. }
  55. try {
  56. console.log(data)
  57. await post('/weapp/addcomment', data);
  58. this.comment = '';
  59. } catch (e) {
  60. showModal('失败', e.msg)
  61. }
  62. },
  63. getGeo (e) {
  64. // NVYWM4sZYGjaHPpO6vwVk0j1v4ijBBKB
  65. const ak = 'NVYWM4sZYGjaHPpO6vwVk0j1v4ijBBKB';
  66. let url = 'http://api.map.baidu.com/geocoder/v2/';
  67. if(e.target.value) {
  68. wx.getLocation({
  69. type: 'wgs84',
  70. success: geo => {
  71. console.log('地理数据', geo);
  72. wx.request({
  73. url,
  74. data: {
  75. ak,
  76. location: `${geo.latitude},${geo.longitude}`,
  77. output: 'json'
  78. },
  79. success: res => {
  80. console.log('百度api返回', res)
  81. if (res.data.status === 0) {
  82. this.location = res.data.result.addressComponent.city;
  83. } else {
  84. this.location = '未知地点';
  85. }
  86. }
  87. })
  88. }
  89. })
  90. } else {
  91. this.location = '';
  92. }
  93. },
  94. getPhone(e) {
  95. if(e.target.value) {
  96. const res = wx.getSystemInfoSync();
  97. // console.log(res.model);
  98. this.phone = res.model;
  99. } else {
  100. this.phone = '';
  101. }
  102. },
  103. async getDetail () {
  104. const info = await get('/weapp/bookdetail', {id: this.bookid})
  105. wx.setNavigationBarTitle({
  106. title: info.title
  107. })
  108. console.log(info)
  109. this.info = info
  110. }
  111. },
  112. onShareAppMessage: function(res) {
  113. return {
  114. title: this.info.title,
  115. path: 'pages/detail/main',
  116. imageUrl:'', // 不填写默认页面截图
  117. // success: function (shareTickets) {
  118. // // console.info(shareTickets + '成功');
  119. // console.log(shareTickets + '成功');
  120. // // 转发成功
  121. // },
  122. // fail: function (res) {
  123. // console.log(res + '失败');
  124. // // 转发失败
  125. // },
  126. // complete:function(res){
  127. // console.log(res + '不管成功失败都会执行');
  128. // // 不管成功失败都会执行
  129. // }
  130. }
  131. },
  132. mounted () {
  133. this.bookid = this.$root.$mp.query.id; // 跳转页面使用this.$root.$mp.query获取参数
  134. this.getDetail();
  135. const userinfo = wx.getStorageSync('userInfo')
  136. console.log('userInfo', userinfo);
  137. if (userinfo) {
  138. this.userinfo = userinfo;
  139. }
  140. // wx.showShareMenu({
  141. // withShareTicket: true
  142. // });
  143. }
  144. }
  145. </script>
  146. <style lang="scss">
  147. .comment {
  148. margin-top: 10px;
  149. .textarea {
  150. width: 730rpx;
  151. background-color: #eee;
  152. padding: 10px;
  153. }
  154. .location {
  155. margin-top: 10px;
  156. padding: 5px 10px;
  157. }
  158. .phone {
  159. margin-top: 10px;
  160. padding: 5px 10px;
  161. }
  162. .btn {
  163. margin: 10px 0;
  164. }
  165. }
  166. </style>