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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<template>
    <view class="activity-container" :data-theme='theme()' :class="theme() || ''">
        <form @submit="formSubmit" @reset="formReset">
            <view class="activity-form p20 pb150">
                <view class="form-section bg-white radius24">
                    <view class="form-item border-b-e">
                        <textarea class="ww100 f28 lh150" placeholder-class="grary9" style="height: 100rpx;"
                         v-model="formData.name" placeholder="请输入活动标题"></textarea>
                    </view>
                    <view class="form-item">
                        <view class="gray6 f26">请上传活动封面图和分享海报(非必传)</view>
                        <view class="gray9 pb20 pt10 f24">封面图建议尺寸750*350px,分享海报建议尺寸750*1250px</view>
                        <view class="d-b-c">
                            <view class="upload-btn d-c-c d-c pr flex-1 mr10" @click="openUpload('image')">
                                <template v-if="image_path!=''">
                                    <image :src="image_path" mode="aspectFit"></image>
                                    <view class="delete-img" @click.stop="deleteImg('image')">
                                        <text class="iconfont icon-guanbi"></text>
                                    </view>
                                </template>
                                <template v-else>
                                    <view class="icon iconfont icon-xiangji f60"></view>
                                    <view class="gray9">上传封面图</view>
                                </template>
                            </view>
                            <view class="upload-btn d-c-c d-c pr flex-1 ml10" @click="openUpload('pic')">
                                <template v-if="pic_path!=''">
                                    <image :src="pic_path" mode="aspectFit"></image>
                                    <view class="delete-img tc" @click.stop="deleteImg('pic')">
                                        <text class="iconfont icon-guanbi"></text>
                                    </view>
                                </template>
                                <template v-else>
                                    <view class="icon iconfont icon-xiangji f60"></view>
                                    <view class="gray9">上传分享海报</view>
                                </template>
                            </view>
                        </view>
                    </view>
                    <view class="form-item">
                        <textarea class="ww100 f28 lh150 p20 radius12 box-s-b" placeholder-class="grary9" style=" background-color: #f7f7f7; 
                        height: 150rpx;" v-model="formData.describe" placeholder="请输入活动简介(选填)"></textarea>
                    </view>
                </view>
                <view class="form-section bg-white radius24">
                    <view class="form-item d-b-c border-b-e">
                        <view class="item-title">活动分类</view>
                        <picker class="" value="" @change="changeCategory" :range="category" range-key='name' :value="category_index">
                            <view class="d-s-c">
                                <view class="make-item input-box flex-1">{{category[category_index].name|| '请选择'}}</view>
                                <view class="icon iconfont icon-jiantou f24"></view>
                            </view>
                        </picker>
                    </view>
                    <view class="form-item d-b-c border-b-e">
                        <view class="item-title">是否走访企业</view>
                        <switch style="transform: scale(0.7); margin-right: -20rpx;" :checked="is_checked_visit" @change="onChangeVisit" />
                    </view>
                    <view class="form-item d-b-c border-b-e" v-if="is_checked_visit">
                        <view class="item-title">被走访企业</view>
                        <view class="d-s-c" @click="selectSupplier('visit')">
                            <view class="make-item input-box flex-1">{{visit_supplier_name||'请选择'}}</view>
                            <view class="icon iconfont icon-jiantou f24"></view>
                        </view>
                    </view>
                    <view class="form-item d-b-c border-b-e" v-else>
                        <view class="item-title">提供场地企业</view>
                        <view class="d-s-c" @click="selectSupplier('space')">
                            <view class="make-item input-box flex-1">{{space_supplier_name||'请选择'}}</view>
                            <view class="icon iconfont icon-guanbi f24 ml10" @click.stop="cancelSpace" v-if="space_supplier_name"></view>
                            <view class="icon iconfont icon-jiantou f24" v-else></view>
                        </view>
                    </view>
                    <view class="form-item d-b-c border-b-e">
                        <view class="item-title">活动费用(元)</view>
                        <input type="number" v-model="formData.fee" class="tr flex-1" placeholder='请输入活动费用,0表示免费' />
                    </view>
                    <view class="form-item d-b-c border-b-e">
                        <view class="item-title">活动名额(人)</view>
                        <input type="number" v-model="formData.limit_num" class="tr flex-1" placeholder='请输入活动名额,0表示不限制' />
                    </view>
                    <view class="form-item d-b-c border-b-e">
                        <view class="item-title">报名开始时间</view>
                        <view class="d-s-c">
                            <uni-datetime-picker
                                type="datetime"
                                v-model="formData.register_start_time"
                                :border="false"
                                :clearIcon="false"
                            />
                        </view>
                    </view>
                    <view class="form-item d-b-c border-b-e">
                        <view class="item-title">报名结束时间</view>
                        <view class="d-s-c">
                            <uni-datetime-picker
                                type="datetime"
                                v-model="formData.register_end_time"
                                :border="false"
                                :clearIcon="false"
                            />
                        </view>
                    </view>
                    <view class="form-item d-b-c border-b-e">
                        <view class="item-title">活动开始时间</view>
                        <view class="d-s-c">
                            <uni-datetime-picker
                                type="datetime"
                                v-model="formData.activity_start_time"
                                :border="false"
                                :clearIcon="false"
                            />
                        </view>
                    </view>
                    <view class="form-item d-b-c border-b-e">
                        <view class="item-title">活动结束时间</view>
                        <view class="d-s-c">
                            <uni-datetime-picker
                                type="datetime"
                                v-model="formData.activity_end_time"
                                :border="false"
                                :clearIcon="false"
                            />
                        </view>
                    </view>
                    <view class="form-item border-b-e">
                        <view class="d-b-c">
                            <view class="item-title">活动限定范围(米)</view>
                            <input type="number" v-model="formData.radius" class="tr flex-1" placeholder='请输入活动费用,0表示免费' />
                        </view>
                        <view class="gray9 pt10">用户在限定范围内才能进行核销签到等操作,0为不限制</view>
                    </view>
                    <view class="form-item d-b-c border-b-e">
                        <view class="item-title">活动地址</view>
                        <view class="d-s-c flex-1" @click="chooseLocation">
                            <input type="text" v-model="address.location_address" class="tr flex-1" disabled placeholder='请选择活动地点' />
                            <text class="iconfont icon-jiantou f24"></text>
                        </view>
                    </view>
                    <view class="form-item d-b-c border-b-e">
                        <view class="item-title">门牌号</view>
                        <input type="text" v-model="address.house_number" class="tr flex-1" placeholder='例:6栋201室' />
                    </view>
                    <view class="form-item d-b-c border-b-e">
                        <view class="item-title">联系电话</view>
                        <input type="text" v-model="address.phone" class="tr flex-1" placeholder='请输入活动联系电话' />
                    </view>
                </view>
            </view>
            <view class="footer-btn p20 bg-white">
                <button class="theme-btn" form-type="submit" type="default">提交</button>
            </view>
        </form>
        <!-- 上传图片 -->
        <Upload v-if="isupload" :type="upload_type" @getImgs="returnImgsFunc"></Upload>
        <!-- 企业弹窗 -->
        <Supplier :isOpenSupplier="open_supplier" @close="closeSupplierFunc"></Supplier>
    </view>
</template>
 
<script>
    import Upload from '@/components/upload/upload.vue';
    import Supplier from './popup/supplier';
    import uDatetimePicker from '@/uni_modules/uni-datetime-picker/components/uni-datetime-picker/uni-datetime-picker';
    export default {
        components: {
            uDatetimePicker,
            Upload,
            Supplier
        },
        data() {
            return {
                loading: false,
                /*是否上传图片*/
                isupload: false,
                image_path: '',
                pic_path: '',
                formData: {
                    name: '',
                    category_id: '',
                    image_id: '', // 封面
                    pic_id: '', // 分享海报
                    sort: 100,
                    is_visit: 0, // 是否需要走访企业
                    visit_supplier_id: '', //    被走访企业
                    space_supplier_id: '', // 提供场地企业
                    background_color: '',
                    status: 1,
                    content: '',
                    describe: '',
                    fee: 0,
                    limit_num: 0,
                    address: '',
                    longitude: '',
                    latitude: '',
                    phone: '',
                    register_start_time: '',
                    register_end_time: '',
                    activity_start_time: '',
                    activity_end_time: '',
                    radius: 0, // 活动限定范围,在范围内才能进行相关操作,如签到、下单
                },
                /*活动类别*/
                category: [],
                categoryForPicker: {},
                category_index: '', // 选择的活动分类索引
                upload_type: 'image', // 封面image 分享海报pic
                open_supplier: false,
                visit_supplier_name: '', // 走访企业
                space_supplier_name: '', // 提供场地企业
                supplier_type: 'visit',
                is_checked_visit: false,
                address: {}, // 临时获取的地址
                location_address: '', // 从地图获取到的地址
            };
        },
        created() {
            this.getCategoryList();
        },
        methods: {
            // 提交
            formSubmit() {
                let self = this;
                let formData = this.formData;
                formData.location_address = self.address.location_address;
                formData.house_number = self.address.house_number;
                formData.address = self.location_address + self.address.house_number;
                formData.is_visit = self.is_checked_visit ? 1 : 0;
                if (formData.name == '') {
                    uni.showToast({
                        title: '请输入活动标题',
                        duration: 1000,
                        icon: 'none'
                    });
                    return false;
                }
                if (formData.category_id == '') {
                    uni.showToast({
                        title: '请选择活动分类',
                        duration: 1000,
                        icon: 'none'
                    });
                    return false;
                }
                if (formData.is_visit && !formData.visit_supplier_id) {
                    uni.showToast({
                        title: '请选择被走访的企业',
                        duration: 1000,
                        icon: 'none'
                    });
                    return false;
                }
                if (formData.start_time == '') {
                    uni.showToast({
                        title: '请选择活动开始时间',
                        duration: 1000,
                        icon: 'none'
                    });
                    return false;
                }
                if (formData.end_time == '') {
                    uni.showToast({
                        title: '请选择活动结束时间',
                        duration: 1000,
                        icon: 'none'
                    });
                    return false;
                }
                if (formData.location_address == '') {
                    uni.showToast({
                        title: '请选择活动地点',
                        duration: 1000,
                        icon: 'none'
                    });
                    return false;
                }
                // if (formData.phone == '') {
                //     uni.showToast({
                //         title: '请输入活动联系电话',
                //         duration: 1000,
                //         icon: 'none'
                //     });
                //     return false;
                // }
                // let reg = /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/;
                // if (!reg.test(formData.phone)) {
                //     uni.showToast({
                //         title: '联系电话格式不正确',
                //         duration: 1000,
                //         icon: 'none'
                //     });
                //     return false;
                // }
                uni.showLoading({
                    title: '正在提交',
                    mask: true
                });
                self._post('branch.admin.activity/add', {formData: JSON.stringify(formData)}, function(res) {
                    self.showSuccess(res.msg, function() {
                        self.gotoPage('/pages/branch/admin/activity/index')
                    }, null, function(){
                        uni.hideLoading();
                    });
                });
            },
            
            /*上传*/
            openUpload(t) {
              this.upload_type = t;
              this.isupload = true;
            },
        
            /*获取图片*/
            returnImgsFunc(e) {
              let self = this;
              if (e != null) {
                if (self.upload_type == 'image') {
                  this.formData.image_id = e[0].file_id;
                  this.image_path = e[0].file_path;
                } else {
                  this.formData.pic_id = e[0].file_id;
                  this.pic_path = e[0].file_path;
                }
              }
              this.isupload = false;
            },
            
            /*删除图片*/
            deleteImg(n){
                this[n + '_path'] = '';
                this.formData[n + '_id'] = '';
            },
        
            /*获取活动类别*/
            getCategoryList() {
              let self = this;
              self._get(
                  'branch.admin.category/lists', {},function(res) {
                      self.loading = false;
                      self.category = res.data.list;
                    // self.categoryForPicker = self.category.map(item => {
                    //     return item.name;
                    // });
                  }
              );
            },
            
            // 选择活动分类
            changeCategory(e) {
                this.category_index = e.target.value;
                this.formData.category_id = this.category[this.category_index].category_id;
            },
            
            // 打开企业选择弹窗
            selectSupplier(e) {
                this.supplier_type = e;
                this.open_supplier = true;
            },
            
            // 关闭企业选择弹窗
            closeSupplierFunc(e) {
                if (e != null) {
                    if (this.supplier_type == 'visit') {
                      this.formData.visit_supplier_id = e.shop_supplier_id;
                      this.visit_supplier_name = e.name;
                    } else {
                      this.formData.space_supplier_id = e.shop_supplier_id;
                      this.space_supplier_name = e.name;
                    }
                }
                this.open_supplier = false;
            },
            
            // 是否走访企业
            onChangeVisit(e) {
                this.is_checked_visit = e.target.value;
                if (this.is_checked_visit) {
                    this.space_supplier_name = '';
                    this.formData.space_supplier_id = '';
                } else {
                    this.visit_supplier_name = '';
                    this.formData.visit_supplier_id = '';
                }
            },
            
            // 取消选择的提供场地企业
            cancelSpace() {
              this.formData.space_supplier_id = '';
              this.space_supplier_name = '';
            },
            
            // 选择活动时间
            changeTime(e) {
                console.log(this.formData.start_time)
            },
            
            // 打开地图选择地址 by lyzflash
            chooseLocation(n) {
                let self=this;
                uni.chooseLocation({
                    success: function (res) {
                        console.log(res)
                        self.formData.longitude=res.longitude;
                        self.formData.latitude=res.latitude;
                        self.location_address=res.address;
                        self.address.house_number=res.name;
                        // 整理地址
                        self.organizeLocationAddress();
                    },
                    fail:function () {
                        console.log('获取失败')
                    }
                });
            },
            
            // 整理地址(分离省市区) by lyzflash
            organizeLocationAddress() {
                let self = this;
                self._get('user.address/setLocationAddress', {
                    address: self.location_address
                }, function(res) {
                    self.$set(self.address, 'location_address', res.data.short_address);
                    // self.province_id = res.data.cityCode[0];
                    // self.city_id = res.data.cityCode[1];
                    // self.region_id = res.data.cityCode[2];
                    self.region = res.data.region;
                });
            },
        }
    }
</script>
 
<style lang="scss" scoped>
.activity-form {
        
    .form-section {
        padding: 0 20rpx;
        
        + .form-section {
            margin-top: 20rpx;
        }
        
        .form-item {
            padding: 20rpx 0;
            font-size: 26rpx;
            
            .upload-btn {
                height: 300rpx;
                border-radius: 12rpx;
                overflow: hidden;
                border: 1rpx solid #eee;
                
                image {
                    width: 360rpx;
                    height: 300rpx;
                }
                
                .delete-img {
                    position: absolute;
                    width: 50rpx;
                    height: 50rpx;
                    background: rgba(0,0,0,.5);
                    border-radius: 50rpx;
                    line-height: 50rpx;
                    right: 0;
                    top: 0;
                }
            }
        }
    }
    
}
.footer-btn {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    box-sizing: border-box;
    z-index: 9;
    border-top: 1rpx solid #eeeeee;
}
</style>