index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <view class="page_container">
  3. <custom-nav-bar :title="previewUrl ? '效果阅览' : '设置聊天背景'">
  4. <view v-show="previewUrl" class="nav_right_action" slot="more">
  5. <text v-show="!loading" @click="finishSet">确定</text>
  6. <u-loading-icon v-show="loading" />
  7. </view>
  8. </custom-nav-bar>
  9. <view v-if="!previewUrl" class="setting_row" style="padding: 0">
  10. <setting-item @click="toChoose" title="从相册中选择" />
  11. <setting-item
  12. @click="resetDefault"
  13. title="恢复默认背景"
  14. :border="false"
  15. />
  16. </view>
  17. <view
  18. v-else
  19. class="preview_container"
  20. :style="{ 'background-image': `url(${previewUrl})` }"
  21. >
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import IMSDK from "openim-uniapp-polyfill";
  27. import CustomNavBar from "../../../components/CustomNavBar/index.vue";
  28. import SettingItem from "../../../components/SettingItem/index.vue";
  29. import { copyFileToDoc, toastWithCallback } from "../../../util/common";
  30. export default {
  31. name: "",
  32. components: {
  33. CustomNavBar,
  34. SettingItem,
  35. },
  36. data() {
  37. return {
  38. previewUrl: "",
  39. loading: false,
  40. };
  41. },
  42. methods: {
  43. finishSet() {
  44. this.loading = true;
  45. copyFileToDoc(this.previewUrl)
  46. .then((path) => {
  47. const bgMap = uni.getStorageSync("IMBgMap") || {};
  48. bgMap[this.$store.getters.storeCurrentConversation.conversationID] =
  49. path;
  50. uni.setStorage({
  51. key: "IMBgMap",
  52. data: bgMap,
  53. });
  54. toastWithCallback("设置成功!", () => {
  55. uni.reLaunch({
  56. url: "/pages/conversation/conversationList/index",
  57. });
  58. this.previewUrl = "";
  59. this.loading = false;
  60. });
  61. })
  62. .catch((err) => uni.$u.toast("设置失败!"));
  63. },
  64. toChoose() {
  65. uni.chooseImage({
  66. count: 1,
  67. sizeType: ["compressed"],
  68. sourceType: ["album"],
  69. success: ({ tempFilePaths, tempFiles }) => {
  70. this.previewUrl = tempFilePaths[0];
  71. console.log(tempFilePaths);
  72. console.log(tempFiles);
  73. },
  74. fail: function (err) {
  75. console.log(err);
  76. },
  77. });
  78. },
  79. resetDefault() {
  80. const bgMap = uni.getStorageSync("IMBgMap") || {};
  81. bgMap[this.$store.getters.storeCurrentConversation.conversationID] = "";
  82. uni.setStorage({
  83. key: "IMBgMap",
  84. data: bgMap,
  85. });
  86. uni.$u.toast("重置成功!");
  87. uni.reLaunch({
  88. url: "/pages/conversation/conversationList/index",
  89. });
  90. },
  91. },
  92. onBackPress() {
  93. if (this.previewUrl) {
  94. this.previewUrl = "";
  95. return true;
  96. }
  97. return false;
  98. },
  99. };
  100. </script>
  101. <style lang="scss" scoped>
  102. .page_container {
  103. background-color: #f8f8f8;
  104. .nav_right_action {
  105. font-size: 14px;
  106. padding-right: 44rpx;
  107. }
  108. .setting_row {
  109. background-color: #fff;
  110. padding: 0 44rpx;
  111. margin: 12rpx 0;
  112. }
  113. .preview_container {
  114. flex: 1;
  115. background-repeat: no-repeat;
  116. background-size: 100% 100%;
  117. }
  118. }
  119. </style>