· 8 years ago · May 10, 2017, 01:14 PM
1func createAccount(page: Page? = nil, accessToken: String? = nil, facebookUid: String? = nil, code: String? = nil, firstname: String, lastname: String, email: String, password: String? = nil, imageData: Data?, profileType: profile_types, completion: @escaping (_ result: NSDictionary) -> Void) {
2
3 //ToDo
4 let time = Int(Date().timeIntervalSince1970)
5
6 var namePart = "\(firstname.lowercased())\(lastname.lowercased())"
7
8 namePart = namePart.replacingOccurrences(of: "ø", with: "o")
9 namePart = namePart.replacingOccurrences(of: "Ã¥", with: "aa")
10 namePart = namePart.replacingOccurrences(of: "æ", with: "ae")
11
12 //let username = "\(firstname.lowercaseString)\(lastname.lowercaseString)\(time)"
13 let username = "\(namePart)\(time)"
14
15 var facebookMode: Bool = false
16
17 var parameters: [String: Any] = [
18 "ip": ip,
19 "email": email,
20 "username": username,
21 "timezone": self.getTimeZone(),
22 "language": (Bundle.main.preferredLocalizations.first! as NSString) as String,
23 "terms": "1",
24 "1_1_3_alias_first_name": firstname,
25 "1_1_4_alias_last_name": lastname,
26 "1_15_45_alias_first_name": firstname, // Organization profile field that sets the displayname of the user behind the organizations to the placeName.
27 "profile_type": profileType.rawValue
28 ]
29
30 if let accessToken = accessToken, let facebookUid = facebookUid, let code = code {
31 parameters["facebook_uid"] = facebookUid
32 parameters["access_token"] = accessToken
33 parameters["code"] = code
34
35 facebookMode = true // FB mode
36 }
37
38 if let password = password {
39 parameters["password"] = password
40 parameters["passconf"] = password
41 }
42
43// if let userName = userName { // Replace the calculated username with the passed one. Used for pages, as the pagename gets the value from username
44//
45// parameters["username"] = userName
46//
47// }
48
49 print("CreateAccount: \(parameters.description)")
50
51 Alamofire.upload(multipartFormData: { multipartFormData in
52 //Multipartformadata
53 //Add image
54 if imageData != nil{
55 multipartFormData.append(imageData!, withName: "photo", fileName: "myImage.jpg", mimeType: "image/jpeg")
56 }
57
58 // import parameters
59 for (key, value) in parameters {
60 multipartFormData.append(String(describing: value).data(using: String.Encoding.utf8)!, withName: key)
61 }
62 },usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold,to: ("\(new_api_url)/makeable/signup"),method:.post,
63 headers:["oauth_consumer_key": oauth_consumer_key, "oauth_consumer_secret": oauth_consumer_secret], encodingCompletion: { encodingResult in
64 switch encodingResult {
65 case .success(let upload, _, _):
66 upload.responseJSON { response in
67 //Response
68 print("CreateProfile response: \(response)")
69 if let JSON = response.result.value as? [String:Any] {
70 print("NEW_USER: \(JSON.description)")
71 if JSON[json_status_code]! as? Int == 200 {
72 let status = 1
73 let message = NSLocalizedString("USER_CREATED", comment: "")
74
75 let tmp_body = JSON["body"] as! [String:Any]
76 let tmp_user = tmp_body["user"] as! [String:Any]
77
78
79 let id = tmp_user["user_id"] as? Int
80 let token = tmp_body["oauth_token"]! as? String
81 let secret = tmp_body["oauth_secret"]! as? String
82 let email = tmp_user["email"] as? String
83 let userName = tmp_user["displayname"] as? String
84 let imageURL = tmp_user["image_profile"] as? String
85
86 // Update UserDefaults and Headers
87 UserDefaults.standard.set(token!, forKey: "token")
88 UserDefaults.standard.set(secret!, forKey: "secret")
89 UserDefaults.standard.set(id!, forKey: "userId")
90// UserDefaults.standard.set(true, forKey: "login")
91 UserDefaults.standard.synchronize()
92
93 headers = ["oauth_consumer_key": oauth_consumer_key, "oauth_consumer_secret": oauth_consumer_secret, "oauth_token": token!, "oauth_secret": secret!]
94
95 var account: Account!
96
97 //Get profileImage
98 let cache = Shared.imageCache
99 let URL = NSURL(string: imageURL!)!
100 let fetcher = NetworkFetcher<UIImage>(URL: URL as URL)
101 cache.fetch(fetcher: fetcher).onSuccess { image in
102
103 if facebookMode {
104 account = Account(accountId: id!, token: token!, secret: secret!, profileType: profileType, email: email!, password: code!, userName: userName!, imageURL: imageURL!, image: image)
105 }
106 else {
107 account = Account(accountId: id!, token: token!, secret: secret!, profileType: profileType, email: email!, password: password!, userName: userName!, imageURL: imageURL!, image: image)
108 }
109
110 self.pc.setupProfileInfo(account: account, page: page) { result in
111 print("setupProfileInfo result: \(result)")
112
113 let user = result["user"]!
114
115 let userDict: NSDictionary = ["message": message, "status": status, "user": user]
116
117 // User created successfully, send out a successnotification for the loggedIn observer
118 NotificationCenter.default.post(name: NSNotification.Name(rawValue: NetworkController.NotificationLoginSuccess), object: nil)
119
120 return completion(userDict)
121 }
122 }
123 }
124 else{
125 var message = NSLocalizedString("USER_CREATION_FAILED", comment: "")
126 if let messageDict = JSON["message"] as? NSDictionary {
127 if let email = messageDict["email"] as? String {
128 message = email
129 }
130 let userDict = ["status" : JSON[json_status_code]!, "message":message]
131 return completion(userDict as NSDictionary)
132 }
133 else{
134 let userDict = ["status" : JSON[json_status_code]!, "message":message]
135 return completion(userDict as NSDictionary)
136 }
137 }
138 }
139 else {
140 return completion(["message": "No response result value", "status": 0])
141 }
142 }
143 case .failure(let encodingError):
144 print(encodingError)
145 }
146 })
147 }