-
Notifications
You must be signed in to change notification settings - Fork 19
/
CrifanLibHttpDemo.swift
125 lines (94 loc) · 4.54 KB
/
CrifanLibHttpDemo.swift
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
//
// CrifanLibHttpDemo.swift
// Xxx
//
// Created by licrifan on 16/6/7.
// Copyright © 2016年 licrifan. All rights reserved.
//
// Last Update: 2016-06-07
import UIKit
import Alamofire
import SwiftyJSON
//unuseful function CrifanLibHttpDemo, just for demo usage for CrifanLibHttp
func CrifanLibHttpDemo(){
//demo get
func getUserId() {
gLog.info("")
//let curPhone:String = self.loginView.phoneTextField.text!
let curPhone:String = "13800000000"
gLog.debug("curPhone=\(curPhone)")
getUrlRespDataJson_async(
.GET,
url: ServerApi.getUserIdUrl(curPhone),
respJsonHandler: getUserIdHandler)
}
func getUserIdHandler(respDataJson:Alamofire.Result<JSON, NSError>, mergedAllPara:Dictionary<String, AnyObject>) {
gLog.verbose("respDataJson.debugDescription=\(respDataJson.debugDescription)")
switch respDataJson {
case .Success(let dataJson):
gLog.verbose("dataJson=\(dataJson)")
gCurUserItem.id = dataJson.int!
gLog.info("成功获取用ID:\(gCurUserItem.id)")
//成功获取用ID:10000010
gCurUserItem.phone = "13800000000"
gCurUserItem.password = "123456"
//getAcessToken()
case .Failure(let error):
gLog.verbose("error=\(error)")
let fullErrStr = genFullErrorStr("获取用户ID失败", error: error)
gLog.verbose("fullErrStr=\(fullErrStr)")
//self.noticeError(fullErrStr, autoClear: true)
}
}
//demo Post with parameters
func getSmsCodeHandler(respDataJson:Alamofire.Result<JSON, NSError>, mergedAllPara:Dictionary<String, AnyObject>) {
gLog.verbose("respDataJson.debugDescription=\(respDataJson.debugDescription)")
//respDataJson.debugDescription=SUCCESS: 13812345678
//respDataJson.debugDescription=FAILURE: Error Domain=HttpErrorDomain Code=403 "(null)" UserInfo={message=The phone is not found , code=403}
switch respDataJson {
case .Success(let dataJson):
gLog.verbose("dataJson=\(dataJson)")
//dataJson=13812345678
//self.noticeSuccess("已成功发送短信验证码,请注意查收", autoClear: true)
case .Failure(let error):
gLog.verbose("error=\(error)")
//error=Error Domain=HttpErrorDomain Code=403 "(null)" UserInfo={message=The phone is not found , code=403}
let errMsg = getHttpRespErrMsg(error)
gLog.verbose("errMsg=\(errMsg)")
//self.noticeError(errMsg, autoClear: true)
}
}
//if validate4SShopCode() && validatePhonenumber(self.registerView.phoneTextField.text!) {
getUrlRespDataJson_async(
.POST,
url: ServerApi.getSmsCodeUrl(),
parameters: [
//"phone" : self.registerView.phoneTextField.text!,
"codetype" : "register",
//"dealer" : self.registerView.shop4SCodeTextField.text!,
],
respJsonHandler: getSmsCodeHandler)
//}
//demo get with extarPara
func getCustomerDetailInfo(customerDetailVC:CustomerDetailViewController) {
gLog.verbose("customerDetailVC=\(customerDetailVC)")
getUrlRespDataJson_async(
.GET,
url: ServerApi.getCustomerDetailUrl(gCurUserItem.id, customerId: customerDetailVC.curCustomerItem.id),
extraPara: ["customerDetailVC" : customerDetailVC],
respJsonHandler: getCustomerDetailInfoHandler)
}
func getCustomerDetailInfoHandler(respDataJson:Alamofire.Result<JSON, NSError>, mergedAllPara:Dictionary<String, AnyObject>) {
gLog.verbose("respDataJson.debugDescription=\(respDataJson.debugDescription)")
let customerDetailVC = mergedAllPara["extraPara"]!["customerDetailVC"]! as! CustomerDetailViewController
switch respDataJson {
case .Success(let dataJson):
gLog.verbose("dataJson=\(dataJson)")
parseJsonToCustomerItem(dataJson, curCustomerItem: customerDetailVC.curCustomerItem)
customerDetailVC.curCustomerItem.gotAllInfo = true
customerDetailVC.updateCustomerInfo()
case .Failure(let error):
gLog.error("error=\(error) for get customer detail info for \(customerDetailVC.curCustomerItem)")
}
}
}