yangan
2024-09-14 6147ca19d6888b49f36d96f0cf345dfe13255871
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<template>
  <view class="addTo-freightForwarder-drvier">
    <view class="addForm">
      <u--form labelPosition="top" :model="form">
        <u-form-item labelWidth="25%" label="" @click="beforeRead" required>
            <view class="views">
                <view class="upload">
                  <u-upload
                    :fileList="fileList1"
                    @afterRead="afterRead"
                    @delete="deletePic"
                    name="1"
                    :maxCount="1"
                    previewFullImage
                  ></u-upload>
                </view>
                <view class="deleteImg" v-if="fileList1.length" @click="deletePic">
                  <u-icon name="close" color="#000000" ></u-icon>
                </view>
            </view>
        </u-form-item>
      </u--form>
    </view>
    <view class="addBtn"
      ><u-button
        text="确认"
        type="primary"
        @click="addToHuoDaiOrDriver"
        :loading="loading"
      ></u-button
    ></view>
  </view>
</template>
 
<script>
// #ifdef H5
const BaseUrl = window.location.origin;
//#endif
//#ifdef MP-WEIXIN
import { BaseUrl } from "@/api/publicInterface.js";
//#endif
import { mapMutations, mapState } from "vuex";
export default {
  data() {
    return {
      form: {
        userId: "",
        imgPath: "",
        username: "",
      },
      loading: false,
      fileList1: [],
    };
  },
  onLoad(params) {
    let that = this;
    if (params.id) {
      this.form.userId = params.id;
    }
    if (params.username) {
      this.form.username = params.username;
    }
    if (params.imgPath) {
      this.form.imgPath = params.imgPath;
      if (this.fileList1.length == 0 && params.imgPath) {
        this.fileList1.push({
          url: `${BaseUrl}${params.imgPath}`,
        });
      }
    }
  },
  onShow() {
    this.init();
  },
  methods: {
    ...mapMutations(["changeisUploadimg"]),
    init() {},
    addToHuoDaiOrDriver() {
      this.loading = true;
      if (this.form.imgPath) {
        this.$reqPut("faceEdit", this.form, "json").then((result) => {
          if (result.code == 0) {
            this.loading = false;
            this.$u.toast("上传成功");
            let timer = setTimeout(() => {
              uni.navigateBack({
                delta: 1,
              });
            }, 500);
          } else {
            this.$u.toast(result.msg ? result.msg : "上传失败");
          }
        });
      }
    },
    beforeRead() {
      this.changeisUploadimg(true);
    },
    // 新增图片
    async afterRead(event) {
      // 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
      let lists = [].concat(event.file);
      console.log(this.fileList1, event)
      let fileListLen = this[`fileList${event.name}`].length;
      lists.map((item) => {
        this[`fileList${event.name}`].push({
          ...item,
          status: "uploading",
          message: "上传中",
        });
      });
      for (let i = 0; i < lists.length; i++) {
        const result = await this.uploadFilePromise(lists[i].url, event.name);
        let item = this[`fileList${event.name}`][fileListLen];
        this[`fileList${event.name}`].splice(
          fileListLen,
          1,
          Object.assign(item, {
            status: "success",
            message: "上传成功",
            url: result,
          })
        );
        fileListLen++;
      }
    },
    // 删除
    deletePic(event) {
      this.fileList1.splice(0, 1);
    },
    uploadFilePromise(url, num) {
      return new Promise((resolve, reject) => {
        let a = uni.uploadFile({
          url: BaseUrl + "/admin/sys-file/uploadUnToken",
          filePath: url,
          name: "file",
          success: (res) => {
            this.form.imgPath = JSON.parse(res.data).data.url;
            resolve(`${BaseUrl}${JSON.parse(res.data).data.url}`);
          },
        });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
    
/deep/.u-form-item__body__right__content__slot {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  /deep/u-view.u-upload__button[data-v-69e2a36e] {
    margin: 0 !important;
  }
  .views{
      width: 30vh;
      height: 30vh;
      .deleteImg {
        width: 5vh;
        height: 5vh;
        background-color: #ffffff;
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
        position: relative;
        left: 80%;
        bottom: 28vh;
        z-index: 999;
      }
  }
 
  .upload {
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 50%;
    overflow: hidden;
    font-size: 3vh;
  }
}
 
.addBtn {
  width: 40vw;
  position: absolute;
  top: 80%;
  left: 50%;
  transform: translate(-50%, -50%);
}
/deep/ .u-upload__button{
    margin: 0 !important;
}
/deep/ .u-upload{
    width: 30vh;
    height: 30vh;
}
/deep/ .u-upload__wrap__preview__image{
    width: 30vh !important;
    height: 30vh !important;
}
/deep/ .u-upload__button{
    width: 30vh !important;
    height: 30vh !important;
}
 
/deep/ .u-icon__icon {
    font-size: 4vh !important;
    font-weight: bold;
}
/deep/ .uicon-camera-fill{
    font-size: 6vh !important;
}
</style>