SupplierAccountApiController.php
8.4 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
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
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Filter\SupplierAccountFilter;
use App\Http\Services\LogService;
use App\Http\Services\SupplierAccountService;
use App\Http\Services\SupplierSubAccountService;
use App\Http\Transformers\SupplierLogTransformer;
use App\Http\Validators\SupplierAccountValidator;
use App\Model\LogModel;
use App\Model\RedisModel;
use App\Model\SupplierAccountModel;
use App\Model\SupplierChannelModel;
use App\Model\SupplierLogModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
//通用API,比如获取品牌列表,分类列表等
class SupplierAccountApiController extends Controller
{
public function Entrance(Request $request, $id)
{
$this->$id($request, $id);
}
//获取供应商信息变更记录
public function GetSupplierAccountList($request)
{
$service = new SupplierAccountService();
$list = $service->getSupplierAccountList($request);
$this->response(0, 'ok', $list['data'], $list['total']);
}
//添加
public function AddSupplierAccount($request)
{
$data = $request->only([
'supplier_code',
'mobile',
'password_raw',
'a_type',
]);
$validator = new SupplierAccountValidator();
$check = $validator->checkSave($request);
if ($check !== true) {
$this->response(-1, $check);
}
$supplierModel = new SupplierChannelModel();
$supplierId = $supplierModel->where('supplier_code', $data['supplier_code'])->value('supplier_id');
$data['supplier_id'] = $supplierId;
$data['a_type'] = empty($data['a_type']) ? 0 : 1;
$data['create_time'] = time();
$data['create_uid'] = $request->user->userId;
$data['password'] = Hash::make($data['password_raw']);
$model = new SupplierAccountModel();
$id = $model->insertGetId($data);
if ($id) {
//还要插入一个redis
// $redis = new RedisModel();
$redis = Redis::connection('user');
$redis->hset('yunxin_api_user_mobile', $data['mobile'], $id);
$accountService = new SupplierAccountService();
$accountService->pushToInitData($data['supplier_code']);
$this->response(0, '添加成功');
}
$this->response(-1, '添加失败');
}
//修改
public function UpdateSupplierAccount($request)
{
$data = $request->only([
'id',
'supplier_code',
'mobile',
'password_raw',
'a_type',
]);
$validator = new SupplierAccountValidator();
$check = $validator->checkSave($request);
if ($check !== true) {
$this->response(-1, $check);
}
$supplierModel = new SupplierChannelModel();
$supplierId = $supplierModel->where('supplier_code', $data['supplier_code'])->value('supplier_id');
$data['supplier_id'] = $supplierId;
$data['a_type'] = empty($data['a_type']) ? 0 : 1;
$data['update_time'] = time();
$data['create_uid'] = $request->user->userId;
$data['password'] = Hash::make($data['password_raw']);
$model = new SupplierAccountModel();
$result = $model->where('id', $data['id'])->update($data);
if ($result) {
//还要插入一个redis
$redis = Redis::connection('user');
$redis->hset('yunxin_api_user_mobile', $data['mobile'], $data['id']);
$accountService = new SupplierAccountService();
$accountService->pushToInitData($data['supplier_code']);
$this->response(0, '修改成功');
}
$this->response(-1, '修改失败', $result);
}
//启用
public function EnableSupplierAccount($request)
{
$id = $request->get('id');
$model = new SupplierAccountModel();
$data['update_time'] = date('Y-m-d H:i:s');
$data['a_status'] = 1;
$result = $model->where('id', $id)->update($data);
if ($result) {
$this->response(0, '启用成功');
}
$this->response(-1, '启用失败', $result);
}
//禁用
public function DisableSupplierAccount($request)
{
$id = $request->get('id');
$model = new SupplierAccountModel();
$data['update_time'] = date('Y-m-d H:i:s');
$data['a_status'] = 0;
$result = $model->where('id', $id)->update($data);
if ($result) {
$this->response(0, '禁用成功');
}
$this->response(-1, '禁用失败', $result);
}
//获取供应商账号
public function GetSupplierAccount($request)
{
$id = $request->input('id');
$type = $request->input('type');
$account = SupplierAccountModel::where('id', $id)->first()->toArray();
$logService = new LogService();
$temp = ($type == 'mobile') ? '账号' : '密码';
$content = $request->user->name . ' 查看了供应商账号的' . $temp;
$logService->AddLog($account['supplier_id'], LogModel::VIEW_OPERATE, '查看供应商基本资料', $content);
$this->response(0, 'ok', $account);
}
//子账号管理
public function GetSupplierSubAccountList($request)
{
$map = $request->all();
$result = (new SupplierSubAccountService())->getSupplierSubAccountList($map);
$this->response(0, 'ok', $result['list'], $result['total']);
}
public function SaveSupplierSubAccount($request)
{
$data = $request->only([
'account_id',
'sbat_id',
'mobile',
'password_raw',
'name',
'email',
'user_rule',
'user_message_rule',
]);
$rules = [
"mobile" => "required|regex:/^1[0-9][0-9]{9}$/",
"password_raw" => "required",
"name" => "required",
"email" => "required|email",
];
$messages = [
'supplier_code.required' => '请选择一个供应商',
'mobile.required' => '登录账号不能为空',
'mobile.regex' => '账号格式必须为手机号',
'email.email' => '邮箱格式不对',
'password_raw.required' => '账号密码不能为空',
];
$validator = Validator::make($data, $rules, $messages);
//判断联系方式的表单验证
if ($validator->fails()) {
$this->response(-1, $validator->errors()->first());
}
$data['user_rule'] = $data['user_rule'] ?: [];
dd($data['user_rule']);
$data['user_rule'] = json_encode($data['user_rule']);
$data['user_message_rule'] = $data['user_message_rule'] ?: [];
$data['user_message_rule'] = json_encode($data['user_message_rule']);
$accountId = $data['account_id'];
unset($data['account_id']);
if (empty($data['sbat_id'])) {
unset($data['sbat_id']);
$result = (new SupplierSubAccountService())->addSupplierSubAccount($accountId, $data);
} else {
$result = (new SupplierSubAccountService())->updateSupplierSubAccount($accountId, $data);
}
if ($result === true) {
$this->response(0, '操作成功');
} else {
$this->response(-1, $result);
}
}
//修改子账号状态
public function ChangeSupplierSubAccountStatus($request)
{
$accountId = $request->input('account_id');
$sbatId = $request->input('sbat_id');
$status = $request->input('status');
$result = (new SupplierSubAccountService())->changeSupplierSubAccountStatus($accountId, $sbatId, $status);
if ($result === true) {
$this->response(0, '操作成功');
}
$this->response(-1, '操作失败', $result);
}
//保存消息菜单
public function SaveSupplierAccountMessagePerm($request)
{
$accountId = $request->get('account_id');
$userMessageRule = $request->input('user_message_rule');
$result = (new SupplierSubAccountService())->saveMessageMenu($accountId, $userMessageRule);
if ($result === true) {
$this->response(0, '保存成功');
}
$this->response(-1, '保存失败', $result);
}
}