index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="changeLogo">
  3. <view class="changeLogo-center">
  4. <!-- <image class="" src="https://img1.baidu.com/it/u=1845374591,668063497&fm=253&fmt=auto&app=138&f=JPEG?w=501&h=500"/> -->
  5. <image class="" :src="shopInfo.shopLogo" />
  6. </view>
  7. <view class="changeLogo-btn">
  8. <view class="btn" @click="photograph('camera')">
  9. <image class="" src="@/static/image/user/logo_1.png" />
  10. <text>拍照</text>
  11. </view>
  12. <view class="btn" @click="photograph('album')">
  13. <image class="" src="@/static/image/user/logo_2.png" />
  14. <text>打开相册</text>
  15. </view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. import { updateShopInfo } from "@/config/index.js";
  21. export default {
  22. created() {
  23. // 获取本地存储的数据
  24. this.shopInfo = uni.getStorageSync("shopInfo");
  25. console.log(this.shopInfo);
  26. },
  27. data() {
  28. return {
  29. shopInfo: {},
  30. };
  31. },
  32. methods: {
  33. photograph(str) {
  34. uni.chooseImage({
  35. count: 6, //默认9
  36. sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
  37. sourceType: [str], //从相册选择
  38. success: (res) => {
  39. this.getImage(res.tempFilePaths[0]);
  40. },
  41. });
  42. },
  43. // 图片上传接口
  44. async getImage(file) {
  45. // 加载状态
  46. this.$loading.show("更新中");
  47. uni.uploadFile({
  48. url: `${process.env.UNI_BASE_URL}/file/upload`, //仅为示例,非真实的接口地址
  49. filePath: file,
  50. header: {
  51. "Content-Type": "application/json; charset=UTF-8",
  52. "Authorization-Business": uni.getStorageSync("storage_key"),
  53. },
  54. name: "file",
  55. formData: {
  56. user: "test",
  57. },
  58. success: (uploadFileRes) => {
  59. this.save(JSON.parse(uploadFileRes.data).data.url);
  60. },
  61. });
  62. },
  63. // 保存更新
  64. async save(logo) {
  65. let obj = { ...this.shopInfo };
  66. obj.shopLogo = logo;
  67. try {
  68. let res = await updateShopInfo(obj);
  69. if (res.code == "") {
  70. this.$showToast("店铺LOGO修改成功", "success");
  71. // 对本地存储的数据进行更换
  72. let uploadInfo = JSON.parse(res.json);
  73. this.shopInfo.shopLogo = uploadInfo.shopLogo;
  74. uni.setStorageSync("shopInfo", uploadInfo);
  75. }
  76. } finally {
  77. this.$loading.hide();
  78. }
  79. },
  80. },
  81. };
  82. </script>
  83. <style lang="scss" scoped>
  84. .changeLogo {
  85. width: 100vw;
  86. min-height: 100vh;
  87. background-color: #000;
  88. position: relative;
  89. .changeLogo-center {
  90. width: 100%;
  91. position: absolute;
  92. top: 262rpx;
  93. left: 0;
  94. image {
  95. width: 100%;
  96. height: 750rpx;
  97. display: block;
  98. }
  99. }
  100. .changeLogo-btn {
  101. width: 100%;
  102. position: absolute;
  103. bottom: 108rpx;
  104. left: 0;
  105. padding: 0 75rpx;
  106. box-sizing: border-box;
  107. @include flex(space-between);
  108. .btn {
  109. width: 276rpx;
  110. height: 108rpx;
  111. background-color: rgba(255, 255, 255, 0.4);
  112. @include flex(center, null, 27rpx);
  113. border-radius: 24rpx;
  114. image {
  115. width: 42rpx;
  116. height: 32rpx;
  117. display: block;
  118. }
  119. text {
  120. font-size: 32rpx;
  121. color: #e6e4e5;
  122. }
  123. }
  124. }
  125. }
  126. </style>