index.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <view class="u-demo">
  3. <view class="u-demo-wrap">
  4. <view class="u-demo-title">演示效果</view>
  5. <view class="u-demo-area">
  6. <u-toast ref="uToast"></u-toast>
  7. <view class="u-avatar-wrap">
  8. <image @tap="preAvatar" class="u-avatar-demo" v-if="avatar" :src="avatar" mode="aspectFill"></image>
  9. </view>
  10. <u-button @click="chooseAvatar">选择图片</u-button>
  11. </view>
  12. </view>
  13. <!-- <view class="u-config-wrap">
  14. <view class="u-config-title u-border-bottom">
  15. 参数配置
  16. </view>
  17. <view class="u-config-item">
  18. <view class="u-item-title">生成图片质量</view>
  19. <u-subsection :current="1" :list="['0.3', '0.7', '1']" @change="qualityChange"></u-subsection>
  20. </view>
  21. <view class="u-config-item">
  22. <view class="u-item-title">自定义参数</view>
  23. <u-subsection :current="1" :list="['是', '否']" @change="styleChange"></u-subsection>
  24. </view>
  25. </view> -->
  26. </view>
  27. </template>
  28. <script>
  29. export default {
  30. data() {
  31. return {
  32. avatar: 'https://cdn.uviewui.com/uview/common/logo.png',
  33. }
  34. },
  35. created() {
  36. uni.$on('uAvatarCropper', path => {
  37. this.avatar = path;
  38. // 可以在此上传到服务端
  39. // uni.uploadFile({
  40. // url: 'http://192.168.100.17/index.php/index/index/upload',
  41. // filePath: path,
  42. // name: 'file',
  43. // complete: (res) => {
  44. // console.log(res);
  45. // }
  46. // });
  47. })
  48. },
  49. methods: {
  50. chooseAvatar() {
  51. this.$u.route({
  52. url: '/uview-ui/components/u-avatar-cropper/u-avatar-cropper',
  53. params: {
  54. // 输出图片宽度,高等于宽,单位px
  55. destWidth: 300,
  56. // 裁剪框宽度,高等于宽,单位px
  57. rectWidth: 200,
  58. // 输出的图片类型,如果'png'类型发现裁剪的图片太大,改成"jpg"即可
  59. fileType: 'jpg',
  60. }
  61. })
  62. },
  63. qualityChange(index) {
  64. this.quality = index == 0 ? 0.3 : index == 1 ? 0.7 : 1;
  65. },
  66. styleChange(index) {
  67. if (index == 0) {
  68. this.rectHeight = this.rectWidth = this.destHeight = this.destWidth = 200;
  69. this.boundStyle = {
  70. lineWidth: 8,
  71. borderColor: this.$u.color['error'],
  72. mask: 'rgba(0, 0, 0, 0.8)'
  73. }
  74. } else {
  75. this.rectHeight = this.rectWidth = this.destHeight = this.destWidth = 400;
  76. this.boundStyle = {
  77. lineWidth: 4,
  78. borderColor: 'rgb(245, 245, 245)',
  79. mask: 'rgba(0, 0, 0, 0.35)'
  80. }
  81. }
  82. },
  83. // 预览图片
  84. preAvatar() {
  85. wx.previewImage({
  86. current: '', // 当前显示图片的 http 链接
  87. urls: [this.avatar] // 需要预览的图片 http 链接列表
  88. })
  89. }
  90. }
  91. }
  92. </script>
  93. <style lang="scss" scoped>
  94. .wrap {
  95. padding: 24rpx;
  96. }
  97. .u-avatar-wrap {
  98. overflow: hidden;
  99. margin-bottom: 20rpx;
  100. }
  101. .u-avatar-demo {
  102. width: 150rpx;
  103. height: 150rpx;
  104. border-radius: 100rpx;
  105. }
  106. </style>