123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <template>
- <view>
- <view class="as-gravity-center" style="display: flex;flex-direction: row;flex-wrap: wrap;position: relative;"
- :style="'justify-content:' + (imageList.length === 0 ? 'center': 'space-between')">
- <!-- 嵌套这层就over了 -->
- <view :style="'margin-left: ' + (retract + 10) + 'rpx;margin-right: '+ retract + 'rpx'"
- style="justify-content: flex-start; align-items: center;display: flex;flex-direction: row;flex-wrap: wrap;">
-
- <block v-for="(image,indexs) in imageList" :key="indexs">
- <view :style="'width:' + innerWidth + 'px;' +'height:' + innerWidth + 'px'"
- style="display: block;margin-left: 5px;margin-top: 5px; opacity:0.7;">
- <view class="as-gravity-center" @tap="previewImage(indexs,imageList)"
- :style="'width:' + (isDelete ? innerWidth / 2 : innerWidth / 2) + 'px;' +'height:' + innerWidth + 'px'"
- style="position: absolute;z-index: 3;opacity:1;">
- <image v-show="isDelete" src="/static/image/big.png" style="width: 12px;height: 12px;">
- </image>
- </view>
- <view v-show="isDelete" class="as-gravity-center" @tap="removeImg(indexs,imageList)"
- :style="'width:' + innerWidth / 2 + 'px;' +'height:' + innerWidth + 'px;' + 'margin-left: '+ innerWidth / 2 +'px;'"
- style="position: absolute;z-index: 3;opacity:1;">
- <image src="/static/image/remove.png" style="width: 12px;height: 12px;">
- </image>
- </view>
-
- <view :style="'width:' + innerWidth + 'px;' +'height:' + innerWidth + 'px'"
- style="position: absolute;z-index: 2;background-color: #B2B2B2;opacity:0.5;"></view>
- <image :style="'width:' + innerWidth + 'px;' +'height:' + innerWidth + 'px'"
- class="as-radius-8px" style="position: relative; display: block;"
- :src="isUrl ? urls + image : image" :data-src="isUrl ? urls + image : image">
- </image>
- </view>
- </block>
-
- <view v-if="isChoose" class="layout4 as-gravity-center" @tap="chooseImage(item)"
- :style="'width:' + innerWidth + 'px;height:' + innerWidth + 'px'"
- style="display: flex;flex-direction: column;margin-top: 8px;margin-left: 5px;">
- <image src="/static/image/photo.png" style="height: 24px;width: 28.5px;"></image>
- <view class="text8" style="margin-top: 8px;">上传图片</view>
- </view>
- </view>
- </view>
- </view>
- </template>
-
- <script>
- export default {
- name: "form-image",
- props: {
- showImageList: {
- /* 图片数组*/
- type: Array,
- default: function() {
- return []
- }
- },
- imgWidth: {
- /*获取屏幕宽度*/
- type: Number,
- default: 0,
- required: false
- },
- retract: {
- /*缩进*/
- type: Number,
- default: 0
- },
- isChoose: {
- /*是否选择图片*/
- type: Boolean,
- default: true
- },
- isDelete: {
- /* 是否具备删除*/
- type: Boolean,
- default: true
- },
- isUrl: {
- /* 是否拼接图片地址*/
- type: Boolean,
- default: false
- },
- count: {
- /* 图片上传数量*/
- type: Number,
- default: 9
- }
- },
- created() {
- let {
- showImageList
- } = this
- if (showImageList.length > 0) {
- this.imageList = showImageList
- }
- let _this = this
-
- if (isNaN(this.imgWidth) || this.imgWidth === 0) { //判断是否不是数字
- uni.getSystemInfo({
- success: function(res) {
- _this.innerWidth = res.windowWidth / 4 - 18
- }
- })
- } else {
- _this.innerWidth = this.imgWidth - 18
- }
-
- },
- mounted() { //当视图运行完后执行
-
- },
- data() {
- return {
- sourceTypeIndex: 2,
- innerWidth: 0,
- imageList: [],
- item: {},
- urls: '',
- update: true
- };
- },
- watch: { //计算属性
- imageList(val) {
- console.log('图片数据改变了', this.imageList.length);
- }
- },
- methods: {
- previewImage(e, item) { //放大图片
- // var current = this.isUrl ? this.urls + item[e] : item[e]
- if (this.isUrl) {
- for (var i = 0; i < item.length; i++) {
- if (item[i].indexOf("http") == -1) {
- item[i] = this.urls + item[i]
- }
- }
- }
- var current = item[e]
- uni.previewImage({
- current: current,
- urls: item
- })
- },
- removeImg(e, item) { //删除图片
- item.splice(e, 1)
- },
- chooseImage: function(item) { //选择相机或相册时
- let _this = this
- if (_this.imageList === undefined) {
- _this.imageList = []
- }
- if (_this.imageList.length === this.count) {
- uni.showToast({
- icon: 'none',
- title: '最多只能选择' + _this.count + '张图片'
- })
- return
- }
- /* 图片选择 */
- uni.chooseImage({
- count: _this.count,
- success: (res) => { //选择成功
- _this.imageList = _this.imageList.concat(res.tempFilePaths);
- const tempFilePaths = res.tempFilePaths;
- const imgUrl = '';
- _this.$emit('backImg', _this.imageList) //测试环境下(正式环境请切换至接口返回地址)
- //task_img
- for (var i = 0; i < tempFilePaths.length; i++) {
- /* 图片上传 */
- uni.uploadFile({
- url: 'https://www.example.com/upload', //仅为示例,非真实的接口地址
- filePath: tempFilePaths[i],
- name: 'file',
- header: {
- 'X-Access-Token': uni.getStorageSync('token')
- },
- formData: {
- biz: 'app'
- },
- success: (uploadFileRes) => {
- let imgData = JSON.parse(uploadFileRes.data)
- /* 数据拼接 */
- if (imgUrl === undefined || imgUrl === '') {
- imgUrl = imgData.message
- } else {
- imgUrl += ',' + imgData.message
- }
- _this.$emit('backImg', _this.imageList) //正式环境下
- },
- fail: (res) => {
- console.log(res);
- }
- });
- }
- },
- fail: (err) => {
- if (err.errMsg.indexOf('cancel') !== '-1') {
- return;
- }
- uni.getSetting({
- success: (res) => {
- let authStatus = false;
- switch (this.sourceTypeIndex) {
- case 0:
- authStatus = res.authSetting['scope.camera'];
- break;
- case 1:
- authStatus = res.authSetting['scope.album'];
- break;
- case 2:
- authStatus = res.authSetting['scope.album'] && res
- .authSetting['scope.camera'];
- break;
- default:
- break;
- }
- if (!authStatus) {
- uni.showModal({
- title: '授权失败',
- content: '需要从您的相机或相册获取图片,请在设置界面打开相关权限',
- success: (res) => {
- if (res.confirm) {
- uni.openSetting()
- }
- }
- })
- }
- }
- })
- }
- })
- }
- }
- }
- </script>
-
- <style>
- .text8 {
- font-size: 11px;
- font-family: PingFang SC;
- color: #C8C8C8;
- opacity: 1;
- }
-
- .layout4 {
- background: #F8F9FA;
- opacity: 1;
- border-radius: 8px;
- }
- </style>
|