comment-modal.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <tm-poup v-model="isShow" position="bottom" height="71vh" @change="handleOnChange">
  3. <view class="title text-align-center">{{ calcTitle }}</view>
  4. <tm-form @submit="fnOnSubmit">
  5. <tm-input name="content" :vertical="true" required :height="220" input-type="textarea"
  6. bg-color="grey-lighten-5" :maxlength="200" :borderBottom="false" placeholder="请输入内容,不超过200字符..."
  7. v-model="form.content"></tm-input>
  8. <tm-input name="author" align="right" required title="我的昵称" placeholder="请输入您的昵称..."
  9. v-model="form.author"></tm-input>
  10. <tm-input name="avatar" align="right" title="我的头像" placeholder="请输入您的头像..."
  11. v-model="form.avatar"></tm-input>
  12. <tm-input name="email" align="right" title="邮箱地址" placeholder="请输入您的邮箱..." v-model="form.email"></tm-input>
  13. <tm-input name="authorUrl" align="right" title="我的网站" placeholder="请输入您的网址..."
  14. v-model="form.authorUrl"></tm-input>
  15. <view class="pa-24 pl-30 pr-30">
  16. <tm-button navtie-type="form" theme="bg-gradient-blue-accent" block>提交
  17. </tm-button>
  18. </view>
  19. </tm-form>
  20. </tm-poup>
  21. </template>
  22. <script>
  23. import tmPoup from '@/tm-vuetify/components/tm-poup/tm-poup.vue';
  24. import tmForm from '@/tm-vuetify/components/tm-form/tm-form.vue';
  25. import tmInput from '@/tm-vuetify/components/tm-input/tm-input.vue';
  26. import tmSwitch from '@/tm-vuetify/components/tm-switch/tm-switch.vue';
  27. import tmButton from '@/tm-vuetify/components/tm-button/tm-button.vue';
  28. export default {
  29. name: 'CommentModal',
  30. components: {
  31. tmPoup,
  32. tmForm,
  33. tmInput,
  34. tmSwitch,
  35. tmButton
  36. },
  37. props: {
  38. show: {
  39. type: Boolean,
  40. default: false
  41. },
  42. isComment: {
  43. type: Boolean,
  44. default: false
  45. },
  46. title: {
  47. type: String,
  48. default: ""
  49. },
  50. postName: {
  51. type: String,
  52. default: ""
  53. }
  54. },
  55. data() {
  56. return {
  57. isShow: false,
  58. params: {
  59. postName: '',
  60. title: '', // 被回复的标题 type=user =用户名 否则为文章标题
  61. form: '',
  62. formPage: '', // 来自哪个页面
  63. type: 'post' // 来源文章/页面 还是用户 user=用户
  64. },
  65. form: {
  66. allowNotification: true,
  67. author: '', // 作者
  68. avatar: '',
  69. authorUrl: '', // 作者主页
  70. content: '', // 评论内容
  71. email: '', // 邮件
  72. postName: ""
  73. }
  74. }
  75. },
  76. computed: {
  77. calcTitle() {
  78. if (this.isComment) {
  79. return this.title
  80. }
  81. return `回复用户:${this.title}`
  82. }
  83. },
  84. watch: {
  85. show: {
  86. immediate: true,
  87. handler: function (newVal) {
  88. if (!newVal) return;
  89. this.isShow = true;
  90. this.handleResetForm();
  91. this.form.postName = this.postName;
  92. let visitor = uni.getStorageSync('Visitor')
  93. if (!visitor) return;
  94. visitor = JSON.parse(visitor)
  95. this.form.author = visitor.author;
  96. this.form.avatar = visitor.avatar;
  97. this.form.email = visitor.email;
  98. this.form.authorUrl = visitor.authorUrl;
  99. }
  100. }
  101. },
  102. methods: {
  103. handleResetForm() {
  104. this.params = {
  105. postName: '',
  106. title: '',
  107. form: '',
  108. formPage: '',
  109. type: 'post'
  110. }
  111. this.form = {
  112. allowNotification: true,
  113. author: '',
  114. avatar: '',
  115. authorUrl: '',
  116. content: '',
  117. email: '',
  118. postName: ""
  119. }
  120. },
  121. fnOnSubmit(e) {
  122. if (e === false) {
  123. return uni.$tm.toast('请检查所有的必填项是否填写完整!');
  124. }
  125. // if (!this.form.email) {
  126. // return uni.$tm.toast('未填写邮箱地址,将无法接收提醒!');
  127. // }
  128. // if (this.form.email && !uni.$tm.test.email(this.form.email)) {
  129. // return uni.$tm.toast('请填写正确的邮箱地址!');
  130. // }
  131. // if (this.form.authorUrl && !uni.$tm.test.url(this.form.authorUrl)) {
  132. // return uni.$tm.toast('请输入正确的Url地址!');
  133. // }
  134. this.fnHandle();
  135. },
  136. handleSetVisitor() {
  137. uni.setStorageSync('Visitor', JSON.stringify({
  138. author: this.form.author,
  139. avatar: this.form.avatar,
  140. email: this.form.email,
  141. authorUrl: this.form.authorUrl,
  142. }))
  143. },
  144. fnHandle() {
  145. uni.showLoading({
  146. title: '正在提交...'
  147. });
  148. // 评论
  149. if (this.isComment) {
  150. const commentForm = {
  151. allowNotification: true,
  152. raw: this.form.content,
  153. content: this.form.content,
  154. owner: {
  155. avatar: this.form.avatar,
  156. displayName: this.form.author,
  157. email: this.form.email,
  158. website: this.form.authorUrl,
  159. },
  160. subjectRef: {
  161. group: "content.halo.run",
  162. kind: "Post",
  163. name: this.form.postName,
  164. version: "v1alpha1",
  165. }
  166. }
  167. this.$httpApi.v2.addPostComment(commentForm)
  168. .then(res => {
  169. uni.$tm.toast('评论成功,可能需要审核!');
  170. // 更新评论者信息
  171. this.handleSetVisitor();
  172. this.handleClose(true)
  173. this.handleResetForm()
  174. })
  175. .catch(err => {
  176. uni.$tm.toast("评论失败");
  177. });
  178. return;
  179. }
  180. // 回复
  181. const replyForm = {
  182. allowNotification: true,
  183. raw: this.form.content,
  184. content: this.form.content,
  185. owner: {
  186. avatar: this.form.avatar,
  187. displayName: this.form.author,
  188. email: this.form.email,
  189. website: this.form.authorUrl,
  190. },
  191. quoteReply: this.form.postName
  192. }
  193. this.$httpApi.v2.addPostCommentReply(this.form.postName, replyForm)
  194. .then(res => {
  195. uni.$tm.toast('回复成功,可能需要审核!');
  196. // 更新评论者信息
  197. this.handleSetVisitor();
  198. this.handleClose(true)
  199. this.handleResetForm()
  200. })
  201. .catch(err => {
  202. uni.$tm.toast("回复失败");
  203. });
  204. },
  205. handleOnChange(isOpen) {
  206. this.isShow = isOpen;
  207. if (!isOpen) {
  208. this.$emit("on-close", {
  209. refresh: false
  210. })
  211. }
  212. },
  213. handleClose(refresh = false) {
  214. this.isShow = false;
  215. this.$emit("on-close", {
  216. refresh: refresh
  217. })
  218. }
  219. }
  220. }
  221. </script>
  222. <style lang="scss" scoped>
  223. .title {
  224. margin: 24rpx 0;
  225. }
  226. </style>