agreement.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. require("../../utils/request.js");
  4. const common_config = require("../../common/config.js");
  5. const _sfc_main = {
  6. data() {
  7. return {
  8. agreementContent: "",
  9. loading: true,
  10. error: false,
  11. errorMsg: "加载失败,请重试"
  12. };
  13. },
  14. computed: {
  15. parsedContent() {
  16. if (!this.agreementContent)
  17. return "";
  18. return this.parseMarkdown(this.agreementContent);
  19. }
  20. },
  21. onLoad() {
  22. this.fetchAgreement();
  23. },
  24. methods: {
  25. // 简单的 markdown 解析函数
  26. parseMarkdown(markdown) {
  27. if (!markdown)
  28. return "";
  29. let html = markdown.replace(/^### (.*$)/gim, "<h3>$1</h3>").replace(/^## (.*$)/gim, "<h2>$1</h2>").replace(/^# (.*$)/gim, "<h1>$1</h1>").replace(/\*\*(.*)\*\*/gim, "<strong>$1</strong>").replace(/\*(.*)\*/gim, "<em>$1</em>").replace(/\[([^\]]*)\]\(([^\)]*)\)/gim, '<a href="$2">$1</a>').replace(/```(.*?)```/gims, "<pre><code>$1</code></pre>").replace(/`([^`]*)`/gim, "<code>$1</code>").replace(/^\* (.*$)/gim, "<li>$1</li>").replace(/^\d+\. (.*$)/gim, "<li>$1</li>").replace(/\n\n/gim, "</p><p>").replace(/\n/gim, "<br>");
  30. html = html.replace(/(<li>.*<\/li>)/gim, "<ul>$1</ul>");
  31. if (html && !html.startsWith("<h") && !html.startsWith("<ul") && !html.startsWith("<ol")) {
  32. html = "<p>" + html + "</p>";
  33. }
  34. return html;
  35. },
  36. goBack() {
  37. common_vendor.index.navigateBack();
  38. },
  39. async fetchAgreement() {
  40. this.loading = true;
  41. this.error = false;
  42. const res = await common_vendor.index.request({
  43. url: `${common_config.apiBaseUrl}/api/public/agreements/terms_of_service/`,
  44. method: "GET"
  45. });
  46. console.log(res);
  47. if (res.statusCode == 200) {
  48. this.loading = false;
  49. this.agreementContent = res.data.content || "";
  50. }
  51. }
  52. }
  53. };
  54. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  55. return common_vendor.e({
  56. a: $data.loading
  57. }, $data.loading ? {} : $data.error ? {
  58. c: common_vendor.t($data.errorMsg),
  59. d: common_vendor.o((...args) => $options.fetchAgreement && $options.fetchAgreement(...args))
  60. } : {
  61. e: $options.parsedContent
  62. }, {
  63. b: $data.error
  64. });
  65. }
  66. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  67. wx.createPage(MiniProgramPage);