版权声明:本文为博主原创文章,未经博主允许不得转载。
import UIKit
import Alamofire
class ViewController: UIViewController {
var phoneNumber =UITextField()
var passNumber =UITextField()
var confirmButton:UIButton!
overridefunc viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor =UIColor.whiteColor()
//添加所有子控件
self.addAllSubViews()
}
//添加所有子控件
func addAllSubViews()
{
// 手机号输入提示
var phoneText =UILabel(frame:CGRectMake(30,240,UIScreen.mainScreen().bounds.size.width-60,30))
phoneText.text ="请输入手机号"
self.view.addSubview(phoneText)
//手机号输入框
var phoneNumber =UITextField(frame:CGRectMake(30,270,UIScreen.mainScreen().bounds.size.width-60,30))
phoneNumber.placeholder ="请输入手机号码"
phoneNumber.text ="18000000005"
phoneNumber.layer.borderWidth =1
phoneNumber.layer.borderColor =UIColor.lightGrayColor().CGColor
phoneNumber.layer.cornerRadius =5
phoneNumber.keyboardType =UIKeyboardType.NumberPad
self.view.addSubview(phoneNumber)
self.phoneNumber = phoneNumber
/// 密码输入提示
var passText =UILabel(frame:CGRectMake(30,300,UIScreen.mainScreen().bounds.size.width-60,30))
passText.text ="请输入密码"
self.view.addSubview(passText)
// 密码输入框
var PassNumber =UITextField(frame:CGRectMake(30,330,UIScreen.mainScreen().bounds.size.width-60,30))
PassNumber.placeholder ="请输入密码"
PassNumber.text ="123456"
PassNumber.layer.borderWidth =1
PassNumber.layer.borderColor =UIColor.lightGrayColor().CGColor
PassNumber.layer.cornerRadius =5
self.view.addSubview(PassNumber)
self.passNumber = PassNumber
//登录
self.confirmButton =UIButton(frame:CGRectMake(30,380,UIScreen.mainScreen().bounds.size.width-60,30))
self.confirmButton.setTitle("登录", forState: UIControlState.Normal)
self.confirmButton.setTitleColor(UIColor.blackColor(),forState:UIControlState.Normal)
self.confirmButton.setTitleColor(UIColor.blueColor(),forState:UIControlState.Highlighted)
self.confirmButton.layer.cornerRadius = 5
self.confirmButton.backgroundColor =UIColor(colorLiteralRed:1, green: 1, blue:1,alpha:0.5)
self.confirmButton.addTarget(self, action: #selector(loginConfrim),
forControlEvents: .TouchUpInside)
self.view.addSubview(self.confirmButton)
}
func loginConfrim()
{
//收起键盘
self.view.endEditing(true)
var apptoken:String =getappToken()
}
func getappToken() ->String {
var path ="http://xxx.com/v1/app/token/request_token"
let params:NSMutableDictionary =NSMutableDictionary()
params["app_id"] = "56e6183b5610d746578a9cf4"
params["app_secret"] ="56e6183b2560ad79242a0ae4"
params["mobile_id"] ="dfgdfgsdfgsdfgsdfg"
var token:String? ="ss"
Alamofire.request(.POST, path, parameters: paramsas! [String:AnyObject],encoding: .JSON)
.responseJSON { responsein
iflet JSON = response.result.value {
var datajosn:NSDictionary = JSONas!NSDictionary
var data:NSDictionary = datajosn["data"] as! NSDictionary
token = data["token"]as!String
self.login(token!);
}
}
return token!
}
func login(apptoken:String)
{
var params:NSMutableDictionary =NSMutableDictionary()
params["mobile_phone"] =self.phoneNumber.text
params["password"] =self.passNumber.text
var headparams:NSMutableDictionary =NSMutableDictionary()
headparams["X-Ylwl-App-Token"] = apptoken
Alamofire.request(.POST,"http://xxxx.com/v1/app/users/login", parameters: paramsas? [String:AnyObject] , encoding: .JSON, headers: headparamsas! [String :String]).responseJSON { (responseObject)in
print(responseObject.result.value)
}
}
}