riku
2025-04-25 b515fae43490ab20977d559e19d4e5f63a4fd96d
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
216
217
218
219
220
import { cluePicUrl } from '../../../config/index';
import { questionForm } from '../clue-item.js';
import { toLabel, toValue } from '../../../common/dataTowns';
 
import { uploadQuestion, updateQuestion } from '../../../services/clue/fetchClue';
 
Page({
  /**
   * 页面的初始数据
   */
  data: {
    validated: false,
    formArray: [],
    // 模式,add: 新增;update:更新
    mode: 'add',
    submitText: '保存',
 
    // 图片上传列表
    fileList: [],
    // 更新模式下,记录被删除的原有图片
    deletedFileList: [],
    // 图片展示方式
    gridConfig: {
      column: 3,
      width: 210,
      height: 210,
    },
    // 图片限制大小
    sizeLimit: { size: 5, unit: 'MB', message: '图片大小不超过5MB' },
 
    // 定位信息
    locations: {
      // 位置名称
      name: '',
      address: '',
      // 使用 gcj02 国测局坐标系
      longitude: '',
      latitude: '',
      coorTxt: '',
    },
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getOpenerEventChannel().on('acceptClueQuestionData', data => {
      const { question, clue, isInternal } = data;
      if (question) {
        this.setData({ uploaded: question.cqUploaded });
        question.cqStreet = toValue(question.cqStreet);
        this.setData({
          locations: {
            address: question.cqAddress,
            longitude: question.cqLongitude,
            latitude: question.cqLatitude,
            coorTxt: question.cqLongitude + ',' + question.cqLatitude,
          },
          fileList: question._filePath.map(v => {
            return {
              url: v,
            };
          }),
        });
      }
      const formArray = questionForm(question);
      const mode = question ? 'update' : 'add';
 
      this.setData({ formArray, mode, clue, isInternal });
    });
  },
 
  // 提交表单
  submit(e) {
    // 额外的定位信息和图片校验
    this.setData({ validated: true });
    const { fileList, locations } = this.data;
    if (locations.address == '' || !fileList || fileList.length == 0) {
      return;
    }
 
    const formObj = e.detail;
    formObj.cId = this.data.clue.cid;
    formObj.cqStreet = toLabel(formObj.cqStreet);
    formObj.cqAddress = locations.address;
    formObj.cqLongitude = locations.longitude;
    formObj.cqLatitude = locations.latitude;
    formObj.cqInternal = this.data.isInternal;
 
    const images = [];
    // 筛选新增的本地图片作为上传图片
    fileList.forEach(v => {
      if (v.url.indexOf(cluePicUrl) == -1) {
        images.push(v.url);
      }
    });
 
    const { mode } = this.data;
    if (mode == 'add') {
      this.create(formObj, images);
    } else {
      this.update(formObj, images);
    }
  },
 
  // 取消表单
  cancel() {
    wx.navigateBack({
      delta: 1,
    });
  },
 
  // 新增
  create(formObj, images) {
    uploadQuestion(formObj, images).then(res => this.modifySuccess(res));
  },
 
  // 更新
  update(formObj, images) {
    // wx.showToast({
    //   title: '问题不允许更新',
    //   duration: 2000,
    //   icon: 'error',
    //   mask: true,
    // });
    // return;
 
    const deleteImgUrl = this.data.deletedFileList.join(';');
    updateQuestion(formObj, deleteImgUrl, images).then(res => this.modifySuccess(res));
  },
 
  // 问题新增或修改成功后
  modifySuccess(res) {
    if (res.success) {
      this.getOpenerEventChannel().emit('uploadOver');
      wx.navigateBack({
        delta: 1,
        success: () => {
          wx.showToast({
            title: this.data.mode == 'add' ? '问题提交成功' : '问题更新成功',
            duration: 2000,
            icon: 'success',
            mask: true,
          });
        },
      });
    } else {
      wx.showToast({
        title: res.message,
        duration: 2000,
        icon: 'error',
        mask: true,
      });
    }
  },
 
  /************************************************************** */
  // 手动修改定位位置描述
  handleLocation(e) {
    const { value } = e.detail;
    // console.log(e);
    // console.log(this.data.locations);
    const { location } = this.data;
    location.address = value;
    this.setData({ locations });
  },
 
  // handleCoorTxt(e) {
  //   console.log(e);
  //   const { value } = e.detail;
  // },
 
  // 从地图选择定位
  chooseLocation() {
    const { locations } = this.data;
    wx.chooseLocation({
      longitude: locations.longitude,
      latitude: locations.latitude,
      success: res => {
        this.setData({
          locations: {
            ...res,
            coorTxt: `${res.longitude},${res.latitude}`,
          },
        });
      },
    });
  },
 
  // 添加图片
  handleAddImg(e) {
    const { fileList } = this.data;
    const { files } = e.detail;
    this.setData({
      fileList: [...fileList, ...files],
    });
  },
  // 移除图片
  handleRemoveImg(e) {
    const { index } = e.detail;
    const { fileList, mode } = this.data;
 
    const [deletedfile] = fileList.splice(index, 1);
 
    // 在更新模式下,记录删除的原有图片,用于更新问题接口的参数
    if (mode == 'update') {
      // 通过文件路径判断是否为远程http路径,
      if (deletedfile.url.indexOf(cluePicUrl) != -1) {
        const originUrl = deletedfile.url.replace(cluePicUrl, '');
        const { deletedFileList } = this.data;
        deletedFileList.push(originUrl);
 
        console.log(deletedFileList);
      }
    }
    this.setData({
      fileList,
    });
  },
});