imagex-uploader.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <template>
  2. <view class="imagex-uploader-container">
  3. <view class="imagex-uploader-files">
  4. <view v-for="(item, index) in fileList" :key="index">
  5. <view class="imagex-uploader-file"
  6. :class="[item.uploadPercent < 100 ? 'imagex-uploader-file-status' : '']"
  7. @click="previewImage(index)">
  8. <image class="imagex-uploader-img" :style="previewStyle" :src="item.path" mode="aspectFill" />
  9. <view class="imagex-img-removeicon right" @click.stop="removeImage(index)" v-show="enableMove">×
  10. </view>
  11. <view class="imagex-loader-filecontent" v-if="item.uploadPercent < 100">{{ item.uploadPercent }}%
  12. </view>
  13. </view>
  14. </view>
  15. <view v-show="fileList.length < uploadLimit" hover-class="imagex-uploader-hover"
  16. class="imagex-uploader-inputbox" @click="chooseImage" :style="uploadStyle">
  17. <view><text class="iconfont" style="color: #666;"></text></view>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import ImagexUploader from './uploader.js';
  24. export default {
  25. data() {
  26. return {
  27. tempFilePaths: [],
  28. upload_cache_list: [],
  29. fileList: []
  30. };
  31. },
  32. name: 'imagex-uploader',
  33. props: {
  34. // 服务ID
  35. serviceId: {
  36. type: String,
  37. default: ''
  38. },
  39. // stsToken
  40. stsToken: {
  41. type: Object,
  42. default: function() {
  43. return {};
  44. }
  45. },
  46. // 上传地区
  47. region: {
  48. type: String,
  49. default: 'cn',
  50. },
  51. // userId
  52. userId: {
  53. type: String,
  54. default: '',
  55. },
  56. // appId
  57. appId: {
  58. type: Number,
  59. default: undefined,
  60. },
  61. // 预览图样式
  62. previewStyle: {
  63. type: Object,
  64. default: function() {
  65. return {
  66. width: '162rpx',
  67. height: '162rpx'
  68. }
  69. }
  70. },
  71. // 上传样式
  72. uploadStyle: {
  73. type: Object,
  74. default: function() {
  75. return {
  76. width: '162rpx',
  77. height: '162rpx'
  78. }
  79. }
  80. },
  81. // 上传数量限制
  82. uploadLimit: {
  83. type: Number,
  84. default: 9
  85. },
  86. // 是否自动上传
  87. autoUpload: {
  88. type: Boolean,
  89. default: true
  90. },
  91. // 是否显示移除
  92. enableMove: {
  93. type: Boolean,
  94. default: true
  95. },
  96. // 服务器预览图片
  97. defaultImageList: {
  98. type: Array,
  99. default: () => {
  100. return [];
  101. }
  102. },
  103. },
  104. async created() {
  105. let _self = this;
  106. this.fileList = this.fileList.concat(this.defaultImageList);
  107. this.defaultImageList.map(item => {
  108. this.upload_cache_list.push(item.path);
  109. });
  110. this.emit();
  111. },
  112. methods: {
  113. uploadImage(fileInfo) {
  114. let _self = this;
  115. const promises = fileInfo.tempFiles.map(function(file, index) {
  116. return promisify(upload)({
  117. info: {
  118. tempFile: file,
  119. tempFilePath: fileInfo.tempFilePaths[index],
  120. },
  121. _self: _self
  122. });
  123. });
  124. uni.showLoading({
  125. title: `正在上传...`
  126. });
  127. Promise.all(promises)
  128. .then(function(data) {
  129. uni.hideLoading();
  130. _self.upload_cache_list.push(...data);
  131. _self.emit();
  132. })
  133. .catch(function(res) {
  134. uni.hideLoading();
  135. });
  136. },
  137. chooseImage() {
  138. let _self = this;
  139. uni.chooseImage({
  140. sizeType: ['compressed', 'original'],
  141. sourceType: ['album', 'camera'],
  142. success: function(res) {
  143. for (let i = 0, len = res.tempFiles.length; i < len; i++) {
  144. res.tempFiles[i]['uploadPercent'] = 0;
  145. _self.fileList.push(res.tempFiles[i]);
  146. }
  147. _self.tempFilePaths = res.tempFilePaths;
  148. _self.upload(res, _self.autoUpload);
  149. },
  150. fail: function(err) {
  151. console.log(err);
  152. }
  153. });
  154. },
  155. async upload(fileInfo, autoUpload) {
  156. let _self = this;
  157. autoUpload ? await _self.uploadImage(fileInfo) : console.warn(
  158. `传输参数:this.$refs.xx.upload(true)才可上传,默认false`);
  159. },
  160. previewImage(idx) {
  161. let _self = this;
  162. let preview = [];
  163. for (let i = 0, len = _self.fileList.length; i < len; i++) {
  164. // step3.这里修改服务器返回字段 !!!
  165. preview.push(_self.fileList[i].path);
  166. }
  167. uni.previewImage({
  168. current: idx,
  169. urls: preview
  170. });
  171. },
  172. removeImage(idx) {
  173. let _self = this;
  174. _self.fileList.splice(idx, 1);
  175. _self.upload_cache_list.splice(idx, 1);
  176. _self.emit();
  177. },
  178. emit() {
  179. let _self = this;
  180. _self.$emit('change', _self.upload_cache_list);
  181. }
  182. }
  183. };
  184. const promisify = api => {
  185. return function(options, ...params) {
  186. return new Promise(function(resolve, reject) {
  187. api(
  188. Object.assign({}, options, {
  189. success: resolve,
  190. fail: reject
  191. }),
  192. ...params
  193. );
  194. });
  195. };
  196. };
  197. const upload = function(options) {
  198. let info = options.info;
  199. let _self = options._self;
  200. let imagexUploader = new ImagexUploader({
  201. region: _self.region,
  202. appId: _self.appId,
  203. userId: _self.userId,
  204. imageConfig: {
  205. serviceId: _self.serviceId,
  206. },
  207. });
  208. try {
  209. imagexUploader.start({
  210. path: info.tempFilePath,
  211. size: info.tempFile.size,
  212. type: 'image',
  213. stsToken: _self.stsToken,
  214. success: function(data) {
  215. console.log('upload success', data);
  216. if (options.success) {
  217. options.success(data);
  218. }
  219. },
  220. fail: function(data) {
  221. console.log('upload fail', data);
  222. if (options.fail) {
  223. options.fail(data);
  224. }
  225. },
  226. progress: function(data) {
  227. console.log('progress: ', data);
  228. let targetIndex = _self.fileList.findIndex(file => file.path === info.tempFilePath);
  229. if (targetIndex >= 0) {
  230. _self.fileList[targetIndex]['uploadPercent'] = data.progress;
  231. _self.$set(_self.fileList, targetIndex, _self.fileList[targetIndex]);
  232. }
  233. }
  234. });
  235. } catch (e) {
  236. console.log('error', e)
  237. }
  238. };
  239. </script>
  240. <style lang="scss">
  241. .iconfont {
  242. font-size: 16px;
  243. font-style: normal;
  244. -webkit-font-smoothing: antialiased;
  245. -moz-osx-font-smoothing: grayscale;
  246. }
  247. .imagex-uploader-container {
  248. padding: 20rpx;
  249. }
  250. .imagex-uploader-files {
  251. display: flex;
  252. flex-wrap: wrap;
  253. }
  254. .imagex-uploader-file {
  255. position: relative;
  256. margin-right: 16rpx;
  257. margin-bottom: 16rpx;
  258. }
  259. .imagex-uploader-file-status:after {
  260. content: ' ';
  261. position: absolute;
  262. top: 0;
  263. right: 0;
  264. bottom: 0;
  265. left: 0;
  266. background-color: rgba(0, 0, 0, 0.5);
  267. }
  268. .imagex-uploader-img {
  269. display: block;
  270. }
  271. .imagex-uploader-input {
  272. position: absolute;
  273. z-index: 1;
  274. top: 0;
  275. left: 0;
  276. width: 100%;
  277. height: 100%;
  278. opacity: 0;
  279. }
  280. .imagex-uploader-inputbox {
  281. position: relative;
  282. margin-bottom: 16rpx;
  283. box-sizing: border-box;
  284. background-color: #ededed;
  285. display: flex;
  286. flex-wrap: wrap;
  287. align-items: center;
  288. justify-content: center;
  289. background-image: url('data:image/svg+xml;base64,PHN2ZyB0PSIxNjE3MDM4NjEwOTgzIiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHAtaWQ9IjEzMjk4IiB3aWR0aD0iMzIiIGhlaWdodD0iMzIiPjxwYXRoIGQ9Ik0xMjggNTU0LjQ5Nmg3NjhjMjMuNTUyIDAgNDIuNDk2LTE4Ljk0NCA0Mi40OTYtNDIuNDk2cy0xOC45NDQtNDIuNDk2LTQyLjQ5Ni00Mi40OTZIMTI4Yy0yMy41NTIgMC00Mi40OTYgMTguOTQ0LTQyLjQ5NiA0Mi40OTZzMTguOTQ0IDQyLjQ5NiA0Mi40OTYgNDIuNDk2ek00NjkuNTA0IDEyOHY3NjhjMCAyMy41NTIgMTguOTQ0IDQyLjQ5NiA0Mi40OTYgNDIuNDk2czQyLjQ5Ni0xOC45NDQgNDIuNDk2LTQyLjQ5NlYxMjhjMC0yMy41NTItMTguOTQ0LTQyLjQ5Ni00Mi40OTYtNDIuNDk2cy00Mi40OTYgMTguOTQ0LTQyLjQ5NiA0Mi40OTZ6IiBwLWlkPSIxMzI5OSIgZmlsbD0iI2JmYmZiZiI+PC9wYXRoPjwvc3ZnPg==');
  290. background-repeat: no-repeat;
  291. background-position: center;
  292. }
  293. .imagex-img-removeicon {
  294. position: absolute;
  295. color: #fff;
  296. width: 40upx;
  297. height: 40upx;
  298. line-height: 40upx;
  299. z-index: 2;
  300. text-align: center;
  301. background-color: #e54d42;
  302. &.right {
  303. top: 0;
  304. right: 0;
  305. }
  306. }
  307. .imagex-loader-filecontent {
  308. position: absolute;
  309. top: 50%;
  310. left: 50%;
  311. transform: translate(-50%, -50%);
  312. color: #fff;
  313. z-index: 9;
  314. }
  315. .imagex-uploader-file:nth-child(4n + 0) {
  316. margin-right: 0;
  317. }
  318. .imagex-uploader-inputbox>view {
  319. text-align: center;
  320. }
  321. .imagex-uploader-hover {
  322. box-shadow: 0 0 0 #e5e5e5;
  323. background: #e5e5e5;
  324. }
  325. </style>