xyz-choose-image.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <view class="chooseImage " style="display: flex;flex-wrap: wrap;">
  3. <view style="position: relative;" v-for="(item, index) in imageList" :key="index" :style="{ width: size + 'rpx', height: size + 'rpx' }">
  4. <image :src="imageList[index]" :style="{ width: size + 'rpx', height: size + 'rpx' }" @click="viewImg(imageList[index])"></image>
  5. <view class="icon_close " style="position: absolute;" @click="delImg(index)"><i class="iconfont " style="">&#xe635;</i></view>
  6. </view>
  7. <view v-if="uploadFlag && imageList.length < num" class="text_center" @click="chooseImage">
  8. <image src="https://www.xyzgy.xyz/image/upload.png" mode="widthFix" :style="{ width: size + 'rpx' }"></image>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. props: {
  15. size: {
  16. //图片的尺寸
  17. type: Number,
  18. default: 200
  19. },
  20. uploadFlag: {
  21. type: Boolean,
  22. default: true
  23. },
  24. num: {
  25. //上传图片数量
  26. type: Number,
  27. default: 9
  28. },
  29. isSave: {
  30. //是否记录用户的选择记录
  31. type: Boolean,
  32. default: true
  33. },
  34. saveStr: {
  35. //记录用户的缓存字段
  36. type: String,
  37. default: 'chooseImage'
  38. },
  39. isBase64: { //是否转base64 受数据传输长度限制,不建议在组件中使用,如果一定要使用,在返回结果中自己转换
  40. type: Boolean,
  41. default: true
  42. },
  43. imgList: {
  44. type: Array
  45. // default: []
  46. }
  47. },
  48. watch: {
  49. imgList (val) {
  50. this.imageList = val
  51. }
  52. },
  53. data() {
  54. return {
  55. imageList: [],
  56. base64: ''
  57. };
  58. },
  59. mounted () {
  60. this.imageList = this.imgList
  61. },
  62. methods: {
  63. chooseImage: async function() {
  64. let _this = this;
  65. await _this.getImage();
  66. this.$emit('chooseImage', _this.imgList);
  67. },
  68. getImage() {
  69. let _this = this;
  70. let _count = _this.num - _this.imgList.length;
  71. return new Promise((resolve, reject) => {
  72. uni.chooseImage({
  73. count: _count, //默认9
  74. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  75. sourceType: ['album', 'camera'], //从相册选择
  76. success: function(res) {
  77. if (_this.isBase64) {
  78. //#ifdef MP-WEIXIN
  79. uni.getFileSystemManager().readFile({
  80. filePath: res.tempFilePaths[0], //选择图片返回的相对路径
  81. encoding: 'base64', //编码格式
  82. success: function(ress) {
  83. //成功的回调
  84. console.log(ress)
  85. let base64 = 'data:image/jpeg;base64,' + ress.data;
  86. if (_this.imgList.length != 0) {
  87. _this.imgList = _this.imgList.concat(base64);
  88. } else {
  89. _this.imgList = [base64];
  90. }
  91. },
  92. fail:function(err){
  93. console.log(err)
  94. }
  95. });
  96. //#endif
  97. //#ifndef MP-WEIXIN
  98. if (_this.imgList.length != 0) {
  99. _this.imgList = _this.imgList.concat(res.tempFilePaths);
  100. } else {
  101. _this.imgList = res.tempFilePaths;
  102. }
  103. // 暂时没有基于chooseImaged解决方法
  104. // let r = new Blob(res.tempFilePaths, {
  105. // type: 'image/jpeg'
  106. // });
  107. // console.log(res.tempFilePaths)
  108. // console.log(r)
  109. // let reader = new FileReader();
  110. // reader.readAsDataURL(r);
  111. // reader.onload = function(e) {
  112. // console.info(reader.result);
  113. // let base64 = reader.result;
  114. // //
  115. // };
  116. //#endif
  117. } else {
  118. if (_this.imgList.length != 0) {
  119. _this.imgList = _this.imgList.concat(res.tempFilePaths);
  120. } else {
  121. _this.imgList = res.tempFilePaths;
  122. }
  123. }
  124. if (_this.isSave) {
  125. uni.setStorageSync(_this.saveStr, _this.imgList.join(','));
  126. }
  127. resolve(_this.imgList);
  128. }
  129. });
  130. });
  131. },
  132. delImg(idx) {
  133. this.imgList.splice(idx, 1);
  134. this.imgList = this.imgList;
  135. if (this.isSave) {
  136. uni.setStorageSync(this.saveStr, this.imgList.join(','));
  137. }
  138. this.$emit('delImg', this.imgList);
  139. },
  140. viewImg(path) {
  141. uni.previewImage({
  142. urls: this.imgList,
  143. current: path
  144. });
  145. }
  146. },
  147. onLoad() {
  148. if (this.isSave) {
  149. let str = uni.getStorageSync(this.saveStr);
  150. if (str != '') {
  151. str = str.split(',');
  152. if (str.length > this.num) {
  153. str = str.slice(0, this.num);
  154. }
  155. this.imgList = str;
  156. }
  157. } else {
  158. uni.removeStorageSync(this.saveStr);
  159. }
  160. }
  161. };
  162. </script>
  163. <style lang="less" scoped>
  164. @font-face {
  165. font-family: 'iconfont'; /* project id 1035847 */
  166. src: url('https://at.alicdn.com/t/font_1035847_ne3azjcnkk.eot');
  167. src: url('https://at.alicdn.com/t/font_1035847_ne3azjcnkk.eot?#iefix') format('embedded-opentype'), url('https://at.alicdn.com/t/font_1035847_ne3azjcnkk.woff2') format('woff2'),
  168. url('https://at.alicdn.com/t/font_1035847_ne3azjcnkk.woff') format('woff'), url('https://at.alicdn.com/t/font_1035847_ne3azjcnkk.ttf') format('truetype'),
  169. url('https://at.alicdn.com/t/font_1035847_ne3azjcnkk.svg#iconfont') format('svg');
  170. }
  171. .iconfont {
  172. font-family: 'iconfont' !important;
  173. font-size: 20px;
  174. font-style: normal;
  175. -webkit-font-smoothing: antialiased;
  176. -webkit-text-stroke-width: 0.2px;
  177. -moz-osx-font-smoothing: grayscale;
  178. cursor: pointer;
  179. }
  180. .chooseImage {
  181. > view {
  182. margin: 0 20px 20px 0;
  183. }
  184. .icon_close {
  185. width: 20px;
  186. height: 20px;
  187. right: -10px;
  188. top: -10px;
  189. .iconfont {
  190. background: #ccc;
  191. border-radius: 50%;
  192. padding: 4px;
  193. font-size: 10px;
  194. }
  195. }
  196. }
  197. </style>