| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- Component({
- properties: {
- //初始化图片路径
- value: {
- type: Array,
- value: [],
- observer(val) {
- this.initImages()
- }
- },
- //禁用删除
- forbidDel: {
- type: Boolean,
- value: false
- },
- //禁用添加
- forbidAdd: {
- type: Boolean,
- value: false
- },
- //服务器地址
- serverUrl: {
- type: String,
- value: ""
- },
- //限制数
- limit: {
- type: Number,
- value: 9
- },
- //original 原图,compressed 压缩图,默认二者都有
- sizeType: {
- type: Array,
- value:['original', 'compressed']
- },
- //album 从相册选图,camera 使用相机,默认二者都有。如需直接开相机或直接选相册,请只使用一个选项
- sourceType: {
- type: Array,
- value:['album', 'camera']
- },
- //可上传图片类型,默认为空,不限制 Array<String> [jpg,png,gif]
- imageFormat: {
- type: Array,
- value:[]
- },
- //单张图片大小限制 MB
- size: {
- type: Number,
- value: 4
- },
- //项目名,默认为 file
- fileKeyName: {
- type: String,
- value: "file"
- },
- //HTTP 请求 Header, header 中不能设置 Referer。
- header: {
- type: Object,
- value: {}
- },
- //HTTP 请求中其他额外的 form data
- formData: {
- type: Object,
- value: {}
- }
- },
- lifetimes: {
- ready: function () {
- this.initImages()
- }
- },
- data: {
- //图片地址
- imageList: [],
- //上传状态:1-上传成功 2-上传中 3-上传失败
- statusArr: []
- },
- /**
- * 组件的方法列表
- */
- methods: {
- initImages() {
- let imgArr = [...this.data.value]
- let status = []
- for (let item of imgArr) {
- status.push("1")
- }
- this.setData({
- imageList: [...imgArr],
- statusArr: status
- })
- },
- // 重新上传
- reUpLoad(e) {
- let index = Number(e.currentTarget.dataset.index)
- let value = `statusArr[${index}]`
- this.setData({
- [value]: "2"
- })
- this.change()
- this.uploadImage(index, this.data.imageList[index]).then(() => {
- this.change()
- }).catch(() => {
- this.change()
- })
- },
- change() {
- let status = ~this.data.statusArr.indexOf("2") ? 2 : 1
- if (status != 2 && ~this.data.statusArr.indexOf("3")) {
- // 上传失败
- status = 3
- }
- this.triggerEvent('complete', {
- status: status,
- imgArr: this.data.imageList
- })
- },
- toast(text) {
- text && wx.showToast({
- title: text,
- icon: "none"
- });
- },
- chooseImage: function () {
- let _this = this;
- wx.chooseImage({
- count: _this.data.limit - _this.data.imageList.length,
- sizeType: _this.data.sizeType,
- sourceType: _this.data.sourceType,
- success: function (e) {
- let imageArr = [];
- let status = []
- for (let i = 0; i < e.tempFiles.length; i++) {
- let len = _this.data.imageList.length;
- if (len >= _this.data.limit) {
- _this.toast(`最多可上传${_this.data.limit}张图片`);
- break;
- }
- //过滤图片类型
- let path = e.tempFiles[i].path;
- if (_this.data.imageFormat.length > 0) {
- let format = path.split(".")[(path.split(".")).length - 1];
- if (_this.data.imageFormat.indexOf(format) == -1) {
- let text = `只能上传 ${_this.data.imageFormat.join(',')} 格式图片!`
- _this.toast(text);
- continue;
- }
- }
- //过滤超出大小限制图片
- let size = e.tempFiles[i].size;
- if (_this.data.size * 1024 * 1024 < size){
- let err=`单张图片大小不能超过:${_this.data.size}MB`
- _this.toast(err);
- continue;
- }
- imageArr.push(path)
- status.push("2")
- }
- _this.setData({
- imageList: _this.data.imageList.concat(imageArr),
- statusArr: _this.data.statusArr.concat(status)
- })
- _this.change()
- let start = _this.data.imageList.length - imageArr.length
- for (let j = 0; j < imageArr.length; j++) {
- let index = start + j
- //服务器地址
- if (_this.data.serverUrl) {
- _this.uploadImage(index, imageArr[j]).then(() => {
- _this.change()
- }).catch(() => {
- _this.change()
- })
- } else {
- //无服务器地址则直接返回成功
- let value = `statusArr[${index}]`
- _this.setData({
- [value]: "1"
- })
- _this.change()
- }
- }
- }
- })
- },
- uploadImage: function (index, url) {
- let _this = this;
- let status = `statusArr[${index}]`;
- return new Promise((resolve, reject) => {
- wx.uploadFile({
- url: this.data.serverUrl,
- name: this.data.fileKeyName,
- header: this.data.header,
- formData: this.data.formData,
- filePath: url,
- success: function (res) {
- console.log(res)
- if (res.statusCode == 200) {
- //返回结果 此处需要按接口实际返回进行修改
- let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}")
- //判断code,以实际接口规范判断
- if (d.code % 100 === 0) {
- // 上传成功 d.url 为上传后图片地址,以实际接口返回为准
- if (d.url) {
- let value = `imageList[${index}]`
- _this.setData({
- [value]: d.url
- })
- }
- _this.setData({
- [status]: d.url ? "1" : "3"
- })
- } else {
- // 上传失败
- _this.setData({
- [status]: "3"
- })
- }
- resolve(index)
- } else {
- _this.setData({
- [status]: "3"
- })
- reject(index)
- }
- },
- fail: function (res) {
- _this.setData({
- [status]: "3"
- })
- reject(index)
- }
- })
- })
- },
- delImage: function (e) {
- let index = Number(e.currentTarget.dataset.index)
- let imgList = [...this.data.imageList]
- let status = [...this.data.statusArr]
- imgList.splice(index, 1)
- status.splice(index, 1)
- this.setData({
- imageList: imgList,
- statusArr: status
- })
- this.triggerEvent("remove", {
- index: index
- })
- this.change()
- },
- previewImage: function (e) {
- let index = Number(e.currentTarget.dataset.index)
- if (!this.data.imageList.length) return;
- wx.previewImage({
- current: this.data.imageList[index],
- loop: true,
- urls: this.data.imageList
- })
- }
- }
- })
|