index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <view>
  3. <view class="cHcontainer">
  4. <image class="bgPic" :src="bgPic" v-if="bgPic"></image>
  5. <view class="emptyBg" v-else></view>
  6. </view>
  7. <view class="btnContainer">
  8. <button class="cbtn" data-way="avatar" open-type="chooseAvatar" @chooseavatar="getAvatar" @getuserinfo="getAvatar">使用头像</button>
  9. <button @tap="chooseImage" class="cbtn" data-way="album">选择图片</button>
  10. <button @tap="nextPage" class="cbtn" :disabled="!picChoosed">下一步</button>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. var app = getApp();
  16. export default {
  17. data() {
  18. return {
  19. bgPic: null,
  20. picChoosed: false
  21. };
  22. },
  23. methods: {
  24. assignPicChoosed: function () {
  25. if (this.bgPic) {
  26. this.picChoosed=true;
  27. } else {
  28. this.picChoosed=false;
  29. }
  30. },
  31. getAvatar: function (e) {
  32. const url = e?.detail?.avatarUrl || e?.detail?.userInfo?.avatarUrl;
  33. if (!url) {
  34. uni.showToast({ title: '未获取到头像权限', icon: 'none', duration: 2000 });
  35. return;
  36. }
  37. let finalUrl = url;
  38. // 仅对微信头像网络地址做尺寸升级,避免破坏本地临时路径
  39. if (/qlogo\.cn/.test(url)) {
  40. finalUrl = url.replace(/\/(\d+)$/, '/0');
  41. }
  42. this.bgPic = finalUrl;
  43. this.assignPicChoosed();
  44. },
  45. chooseImage: function (e) {
  46. var that = this;
  47. uni.chooseImage({
  48. count: 1,
  49. sizeType: ['original', 'compressed'],
  50. sourceType: [e.target.dataset.way],
  51. success: (e) => {
  52. const img = e?.tempFilePaths?.[0];
  53. if (!img) {
  54. uni.showToast({ title: '未选择图片', icon: 'none', duration: 2000 });
  55. that.assignPicChoosed();
  56. return;
  57. }
  58. that.bgPic = img;
  59. that.assignPicChoosed();
  60. },
  61. fail: function (e) {
  62. that.assignPicChoosed();
  63. },
  64. complete: function (e) {
  65. that.assignPicChoosed();
  66. }
  67. });
  68. },
  69. nextPage: function () {
  70. app.globalData.bgPic = this.bgPic;
  71. uni.navigateTo({
  72. url: '../imageeditor/imageeditor'
  73. });
  74. }
  75. }
  76. };
  77. </script>
  78. <style>
  79. @import './index.css';
  80. </style>