Address.php
3.2 KB
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
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/5/13
* Time: 16:14
*/
namespace app\index\controller;
use app\common\controller\HomeBase;
class Address extends HomeBase
{
public function add(){
$user_id = $this->getUserId();
$param = $this->request->param();
$validate = new \think\Validate([
'name' => 'require',
'province_id' => 'require',
'city_id' => 'require',
'county_id' => 'require',
'phone' => 'require',
'address' => 'require',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
$addressModel = new \app\index\model\Address();
$param['user_id'] = $user_id;
$param['createtime'] = time();
$param['updatetime'] = time();
$param['is_default'] = "2";
$result = $addressModel->insertData($param);
if(empty($result)){
$this->error('sql执行失败');
}
$this->success('SUCCESS','',['address_id'=>$result]);
}
public function edit(){
$user_id = $this->getUserId();
$param = $this->request->param();
$validate = new \think\Validate([
'address_id' => 'require',
'name' => 'require',
'province_id' => 'require',
'city_id' => 'require',
'county_id' => 'require',
'phone' => 'require',
'address' => 'require',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
$addressModel = new \app\index\model\Address();
$param['updatetime'] = time();
$result = $addressModel->updateData(['id'=>$param['address_id'],'user_id'=>$user_id],$param);
if(empty($result)){
$this->error('sql执行失败');
}
$this->success('SUCCESS','',['address_id'=>$param['address_id']]);
}
public function update(){
$user_id = $this->getUserId();
$param = $this->request->param();
$validate = new \think\Validate([
'name' => 'require',
'province_id' => 'require',
'city_id' => 'require',
'county_id' => 'require',
'phone' => 'require',
'address' => 'require',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
$param['updatetime'] = time();
$addressModel = new \app\index\model\Address();
if(!empty($param['address_id'])){
$address_id = $param['address_id'];
unset($param['address_id']);
$result = $addressModel->updateData(['id'=>$address_id,'user_id'=>$user_id],$param);
}else{
unset($param['address_id']);
$param['user_id'] = $user_id;
$param['createtime'] = time();
$param['is_default'] = "2";
$result = $addressModel->insertData($param);
$address_id = $result;
}
if(empty($result)){
$this->error('sql执行失败');
}
$data = $addressModel->findData(['id'=>$address_id]);
$this->success('SUCCESS','',$data);
}
}