评论列表默认加载10条数据 ,添加上拉加载更多,原理之前已经讲过。
    底部输入框使用fixed定位,当我们在真机上预览的时候,会有个情况,就是当软键盘弹起来的时候,输入框一小部分会被遮挡,解决方案是给input添加cursor-spacing="10"属性即可。值可根据需求调整。

    1. <template>
    2. <view class="container comment">
    3. <v-comment :list="commentList"></v-comment>
    4. <!-- 加载更多 -->
    5. <view class="load_more">
    6. <uni-load-more :status="status" />
    7. </view>
    8. <view class="comment_bar">
    9. <input
    10. class="input_box"
    11. placeholder="请点击输入"
    12. cursor-spacing="10"
    13. v-model="content"
    14. @confirm="handleComment"
    15. @focus="handleFocus"
    16. @blur="handleBlur"
    17. @input="handleInput"
    18. />
    19. <uni-icons
    20. @click="handleComment"
    21. class="input_icon"
    22. type="paperplane-filled"
    23. size="20"
    24. />
    25. </view>
    26. </view>
    27. </template>
    28. <script>
    29. import userService from "@/services/user";
    30. export default {
    31. data() {
    32. return {
    33. commentList: [],
    34. params: {
    35. page: 1,
    36. limit: 10,
    37. },
    38. options: {},
    39. status: "more",
    40. content: "",
    41. isInput: false,
    42. };
    43. },
    44. onLoad(option) {
    45. this.options = option;
    46. this.getCommentList();
    47. },
    48. // 监听滚动事件最外层view不能添加css样式overflow:scroll
    49. onReachBottom() {
    50. if (this.status !== "noMore") {
    51. this.status = "loading";
    52. this.params.page++;
    53. this.getCommentList();
    54. }
    55. },
    56. methods: {
    57. async getCommentList() {
    58. try {
    59. const res = await userService.commentList({
    60. ...this.params,
    61. ...this.options,
    62. });
    63. const items = res.data.items;
    64. // 数组解构拼接
    65. this.commentList = [...this.commentList, ...items];
    66. if (items.length < 10) return (this.status = "noMore");
    67. if (items.length >= 10) this.status = "more";
    68. console.log("res", res);
    69. } catch (e) {
    70. console.log("e", e);
    71. }
    72. },
    73. /**
    74. * @description: 发表评论-判断登陆状态
    75. * @return {*}
    76. */
    77. async commentAdd() {
    78. try {
    79. const res = await userService.commentAdd({
    80. data: {
    81. content: this.content,
    82. ...this.options,
    83. },
    84. });
    85. if (res.message == "成功") {
    86. uni.showToast({
    87. title: "评论发送成功",
    88. });
    89. this.content = "";
    90. this.params.page = 1;
    91. this.commentList = [];
    92. this.getCommentList();
    93. } else {
    94. uni.showToast({
    95. title: "评论发送失败",
    96. icon: "error",
    97. });
    98. }
    99. } catch (e) {
    100. console.log("e", e);
    101. }
    102. },
    103. handleComment() {
    104. this.$store.dispatch("goLogin", () => {
    105. this.commentAdd();
    106. });
    107. },
    108. handleFocus() {
    109. this.isInput = true;
    110. },
    111. handleBlur() {
    112. this.isInput = false;
    113. },
    114. handleInput() {
    115. this.isInput = true;
    116. },
    117. },
    118. };
    119. </script>
    120. <style lang="scss" scoped>
    121. @import url("@/static/styles/_global.scss");
    122. .comment {
    123. background: white;
    124. padding: 16px 16px 50px 16px;
    125. height: 100%;
    126. .comment_bar {
    127. position: fixed;
    128. bottom: 0;
    129. left: 0;
    130. width: 100%;
    131. z-index: 999;
    132. height: 60px;
    133. display: flex;
    134. align-items: center;
    135. justify-content: center;
    136. background: linear-gradient(
    137. 180deg,
    138. hsla(0, 0%, 86.7%, 0) 0,
    139. hsla(0, 0%, 86.7%, 0.2) 0.42857rem,
    140. hsla(0, 0%, 86.7%, 0.2) 0.57143rem,
    141. #fff 0.64286rem,
    142. #fff
    143. );
    144. .input_box {
    145. width: 80%;
    146. height: 30px;
    147. line-height: 30px;
    148. background-color: #f2f3f7;
    149. border-radius: 20px;
    150. // text-indent: 10px;
    151. padding-left: 10px;
    152. font-size: 16px;
    153. }
    154. .input_icon {
    155. margin-left: 8px;
    156. }
    157. }
    158. }
    159. </style>