CNLiveCreateShopClassifyViewController.swift
13.6 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
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
//
// CNLiveCreateShopClassifyViewController.swift
// metaCode
//
// Created by zx on 2023/11/17.
// 创建/编辑 店铺分类控制器
import Foundation
///分类类型
enum CNLiveCreateType:Int {
///一级分类
case oneLevelClassify
///二级分类
case twoLevelClassify
}
class CNLiveCreateShopClassifyViewController:CNLiveBaseViewController, UITableViewDataSource, UITableViewDelegate{
/// 分类类型,是一级还是二级
var classifyType:CNLiveCreateType?
/// 当创建二级分类时,需要传上一级分类的id
var superClassifyId:String = ""
/// 添加/编辑/删除 成功回调
var successCompleteBlock:successComplete?
let kCNLiveShopClassifyInputTitelCellID = "kCNLiveShopClassifyInputTitelCellID"
let kCNLiveShopClassifyInputSortCellID = "kCNLiveShopClassifyInputSortCellID"
let kCNLiveShopClassifySwitchTableViewCellID = "kCNLiveShopClassifySwitchTableViewCellID"
let kCNLiveShopClassilyIconTableViewCellID = "kCNLiveShopClassilyIconTableViewCellID"
/// 编辑状态
var isEdite:Bool = false
/// 编辑的数据
var shopClassifyModel:CNLiveShopClassifyModel?
/// 表
// MARK: - lazy
lazy var tableView : UITableView = {
let tableView = UITableView(frame: CGRect(x: 0, y: KNavigationHeight, width: KSreenWidth, height: KSreenHeight-KNavigationHeight-KTabBarHeight), style: .plain)
tableView.backgroundColor = HexColor("#F6F6F6")
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView()
tableView.separatorStyle = .none
tableView.tableFooterView = UIView.init()
tableView.register(CNLiveShopClassifyInputTitleCell.self, forCellReuseIdentifier:kCNLiveShopClassifyInputTitelCellID )
tableView.register(CNLiveShopClassifyInputSortCell.self, forCellReuseIdentifier:kCNLiveShopClassifyInputSortCellID )
tableView.register(CNLiveShopClassifySwitchTableViewCell.self, forCellReuseIdentifier: kCNLiveShopClassifySwitchTableViewCellID)
tableView.register(CNLiveShopClassilyIconTableViewCell.self, forCellReuseIdentifier: kCNLiveShopClassilyIconTableViewCellID)
return tableView
}()
lazy var bottomNavView :UIView = {
let bottomNavView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height - KTabBarHeight))
bottomNavView.backgroundColor = HexColor("F2F2F2")
return bottomNavView
}()
var titleField:UITextField?
var sortField:UITextField?
var uploadImageView:CNLiveAddGoodsSelectUploadImageView?
override func viewDidLoad() {
super.viewDidLoad()
self.initUI()
}
/// 初始化方法
/// @param model 要编辑的分类数据,当isEdite为YES,才需要传
/// @param isEdite 是否是编辑 YES:是 NO:不是
func initWithModel(model:CNLiveShopClassifyModel,isEdite:Bool){
self.shopClassifyModel = model
//设置店铺id
self.shopClassifyModel?.shop_id = DSUserInfoManager.shared.dsUserInfo.id
self.isEdite = isEdite
}
// MARK: - 初始化UI
func initUI(){
self.view.addSubview(self.tableView)
self.initTopNav()
self.initBottomNav()
}
func initTopNav(){
if self.isEdite == true{
self.title = "编辑店铺分类"
let deleteNavBtn = UIButton(type: .custom)
deleteNavBtn.frame = CGRect(x: 0, y: KStatusBarHeight, width: KNavigationHeight-KStatusBarHeight, height: KNavigationHeight-KStatusBarHeight)
deleteNavBtn.backgroundColor = .clear
deleteNavBtn.setTitle("删除", for: .normal)
deleteNavBtn.setTitleColor(HexColor("#666666"), for: .normal)
deleteNavBtn.addTarget(self, action: #selector(deleteNavBtnClick), for: .touchUpInside)
self.rightNavBtns = [deleteNavBtn]
}else{
self.title = "创建店铺分类"
//初始化model,赋值一些默认数据
self.shopClassifyModel = CNLiveShopClassifyModel()
self.shopClassifyModel?.title = ""
self.shopClassifyModel?.simg = ""
self.shopClassifyModel?.shop_id = DSUserInfoManager.shared.dsUserInfo.id
self.shopClassifyModel?.state = "2"
self.shopClassifyModel?.isnav = "2"
if self.classifyType == .twoLevelClassify{
self.shopClassifyModel?.pid = self.superClassifyId
}
}
}
func initBottomNav(){
self.bottomNavView.backgroundColor = .white
let cancelBtn = UIButton(type: .custom)
cancelBtn.frame = CGRect(x: 0, y: 0, width: 130, height: KTabBarHeight)
cancelBtn.setTitle("取消", for: .normal)
cancelBtn.setTitleColor(HexColor("#3A3A3A"), for: .normal)
cancelBtn.titleLabel?.font = Font_15
cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)
self.bottomNavView.addSubview(cancelBtn)
let saveBtn = UIButton(type: .custom)
saveBtn.frame = CGRect(x: cancelBtn.right, y: 0, width: self.view.width - cancelBtn.right, height: KTabBarHeight)
saveBtn.setTitle("保存", for: .normal)
saveBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
saveBtn.titleLabel?.font = Font_15
saveBtn.addTarget(self, action: #selector(saveBtnClick), for: .touchUpInside)
saveBtn.backgroundColor = UIColor(hue:0.31, saturation:0.39, brightness:0.77, alpha:1.00)
self.bottomNavView.addSubview(saveBtn)
}
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0{
guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveShopClassifyInputTitelCellID) as? CNLiveShopClassifyInputTitleCell
else {
let cell = CNLiveShopClassifyInputTitleCell()
return cell
}
self.titleField = cell.textFiled
if self.isEdite == true{
cell.textFiled.text = self.shopClassifyModel?.title
}
return cell
}else if (indexPath.section == 1){
guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveShopClassilyIconTableViewCellID) as? CNLiveShopClassilyIconTableViewCell
else {
let cell = CNLiveShopClassifyInputTitleCell()
return cell
}
self.uploadImageView = cell.selectUploadImageView
cell.uploadImageSuceessBlock = {imageUrl in
self.shopClassifyModel?.simg = imageUrl
}
if self.isEdite{
if self.shopClassifyModel?.simg?.count ?? 0 > 0{
cell.selectUploadImageView?.networkImageArr = [self.shopClassifyModel?.simg ?? ""]
}
}
return cell
}else if (indexPath.section == 2){
guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveShopClassifyInputSortCellID) as? CNLiveShopClassifyInputSortCell
else {
let cell = CNLiveShopClassifyInputSortCell()
return cell
}
self.sortField = cell.textFiled
if (self.isEdite ?? false) {
cell.textFiled.text = self.shopClassifyModel?.ord
}
return cell
}else if (indexPath.section == 3){
guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveShopClassifySwitchTableViewCellID) as? CNLiveShopClassifySwitchTableViewCell
else {
let cell = CNLiveShopClassifySwitchTableViewCell()
return cell
}
cell.titlelab.text = "是否显示"
cell.switchType = .classify
if self.shopClassifyModel?.state == "2"{
cell.rightSwitch.isOn = true
}else{
cell.rightSwitch.isOn = false
}
cell.switchClickBlock = {on,type in
self.shopClassifyModel?.state = on ? "2" : "1"
let idxPath = IndexPath(row: 0, section: 3)
self.tableView.reloadRows(at: [idxPath], with: .none)
}
return cell
}else if (indexPath.section == 4){
guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveShopClassifySwitchTableViewCellID) as? CNLiveShopClassifySwitchTableViewCell
else {
let cell = CNLiveShopClassifySwitchTableViewCell()
return cell
}
cell.titlelab.text = "是否显示店铺导航栏"
cell.switchType = .nav
if self.shopClassifyModel?.isnav == "2"{
cell.rightSwitch.isOn = true
}else{
cell.rightSwitch.isOn = false
}
cell.switchClickBlock = {on,type in
self.shopClassifyModel?.isnav = on ? "2" : "1"
let idxPath = IndexPath(row: 0, section: 4)
self.tableView.reloadRows(at: [idxPath], with: .none)
}
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 || indexPath.section == 2{
return 50
}else if (indexPath.section == 1){
return 155
}else if (indexPath.section == 3 || indexPath.section == 4){
return 50
}
return 44
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.view.endEditing(true)
}
// MARK: - response method
///删除
@objc func deleteNavBtnClick(){
if self.shopClassifyModel?.id?.isEmpty == true{
showMessage(message: "分类id不存在")
return
}
AlertTool.showSystemAlert(title: "是否要删除该分类?", message: nil, actionTitles: ["取消","确定"], style: .alert, actionStyles:[.cancel,.default] ) { index in
if index == 2{
self.deleteClassify()
}
}
}
///取消
@objc func cancelBtnClick(){
self.navigationController?.popViewController(animated: true)
}
/// 保存
@objc func saveBtnClick(){
self.view.endEditing(true)
if self.uploadImageView?.getCurrentSelectImageUrls().count ?? 0 > 0{
self.shopClassifyModel?.simg = self.uploadImageView?.getCurrentSelectImageUrls().first
}else{
self.shopClassifyModel?.simg = ""
}
if self.titleField?.text?.isEmpty == true{
showMessage(message: "请输入分类名称")
return
}
if self.titleField?.text?.count ?? 0 > 8{
showMessage(message: "分类名称最多只能输入8个字,请重新输入")
return
}
if self.shopClassifyModel?.simg?.isEmpty == true{
showMessage(message: "请选择图标")
return
}
if self.sortField?.text?.isEmpty == true {
showMessage(message: "请输入排序")
return
}
if self.sortField?.text?.isPurnInt() == false{
showMessage(message: "排序内容只能输入数字")
return
}
if Int(self.sortField?.text ?? "0") ?? 0 >= 10000{
showMessage(message: "排序不能大于等于10000")
return
}
if Int(self.sortField?.text ?? "0") ?? 0 == 0{
showMessage(message: "排序不能等于0")
return
}
self.shopClassifyModel?.title = self.titleField?.text
self.shopClassifyModel?.ord = self.sortField?.text
if self.isEdite == true{
self.editClassify()
}else{
self.addClassify()
}
}
// MARK: - 网络请求
///添加分类
func addClassify(){
showLoading(message: "")
CNLiveShopClassilyViewModel.postAddCateRequestWithModel(model: self.shopClassifyModel!) { result, status in
hiddenLoading()
if status == .success{
showMessage(message: "保存成功")
self.navigationController?.popViewController(animated: true)
self.successCompleteBlock!()
}else{
showMessage(message: "保存失败")
}
}
}
///编辑分类
func editClassify(){
showLoading(message: "")
CNLiveShopClassilyViewModel.postEditCateRequestWithModel(model: self.shopClassifyModel!) { result, status in
hiddenLoading()
if status == .success{
showMessage(message: "保存成功")
self.navigationController?.popViewController(animated: true)
self.successCompleteBlock!()
}else{
showMessage(message: "保存失败")
}
}
}
///删除分类
func deleteClassify(){
showLoading(message: "")
CNLiveShopClassilyViewModel.postDeleteCateRequestWithModel(model: self.shopClassifyModel!) { result, status in
hiddenLoading()
if status == .success{
showMessage(message: "删除成功")
self.navigationController?.popViewController(animated: true)
self.successCompleteBlock!()
}else{
showMessage(message: "删除失败")
}
}
}
}