You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.1 KiB
100 lines
3.1 KiB
// |
|
// UserInfo.swift |
|
// Lookfit |
|
// |
|
// Created by lemo. on 2020/3/15. |
|
// Copyright © 2020 Sheldon. All rights reserved. |
|
// |
|
|
|
import Foundation |
|
import ObjectMapper |
|
|
|
struct UserInfo: Mappable { |
|
|
|
var nickname: String = "" |
|
var avatar: Data? |
|
var avatarImg: UIImage? |
|
/// false: 男 true: 女 |
|
var gender: Bool = false |
|
var heightCM: Int = 170 |
|
var weightKG: Int = 65 |
|
var birthday: String = "1991-01-01" |
|
|
|
// 单位 0: 公制 1: 英制 |
|
var metricUnit: Int = 0 |
|
|
|
// 目标值默认5000 |
|
var stepTarget: Int = 5000 |
|
var sleepTarget: Int = 1 |
|
var walkTarget: Int = 3000 |
|
var runTarget: Int = 3000 |
|
var cyclingTarget: Int = 3000 |
|
var climbTarget: Int = 3000 |
|
|
|
init?(map: Map) {} |
|
|
|
init(nickname: String, avatar: Data?, gender: Bool, heightCM: Int, weightKG: Int, birthday: String) { |
|
self.nickname = nickname |
|
self.avatar = avatar |
|
self.gender = gender |
|
self.heightCM = heightCM |
|
self.weightKG = weightKG |
|
self.birthday = birthday |
|
} |
|
|
|
mutating func mapping(map: Map) { |
|
nickname <- map["nickname"] |
|
avatar <- map["avatar"] |
|
gender <- map["gender"] |
|
avatar <- (map["avatar"], DataTransform()) |
|
heightCM <- map["heightCM"] |
|
weightKG <- map["weightKG"] |
|
birthday <- map["birthday"] |
|
metricUnit <- map["metricUnit"] |
|
stepTarget <- map["stepTarget"] |
|
sleepTarget <- map["sleepTarget"] |
|
walkTarget <- map["walkTarget"] |
|
runTarget <- map["runTarget"] |
|
cyclingTarget <- map["cyclingTarget"] |
|
climbTarget <- map["climbTarget"] |
|
if let avatar = avatar { |
|
avatarImg = UIImage(data: avatar) |
|
} |
|
} |
|
|
|
func age() -> Int { |
|
let dateFormatter = DateFormatter() |
|
dateFormatter.dateFormat = "yyyy-MM-dd" |
|
guard let date = dateFormatter.date(from: birthday) else { return 18 } |
|
// 出生日期转换 年月日 |
|
let components1 = Calendar.current.dateComponents(Set<Calendar.Component>(arrayLiteral: .day, .month, .year), from: date) |
|
let brithDateYear = components1.year ?? 0 |
|
let brithDateDay = components1.day ?? 0 |
|
let brithDateMonth = components1.month ?? 0 |
|
// 获取系统当前 年月日 |
|
let components2 = Calendar.current.dateComponents(Set<Calendar.Component>(arrayLiteral: .day, .month, .year), from: Date()) |
|
let currentDateYear = components2.year ?? 0 |
|
let currentDateDay = components2.day ?? 0 |
|
let currentDateMonth = components2.month ?? 0 |
|
// 计算年龄 |
|
var age = currentDateYear - brithDateYear - 1; |
|
if ((currentDateMonth > brithDateMonth) || (currentDateMonth == brithDateMonth && currentDateDay >= brithDateDay)) { |
|
age+=1 |
|
} |
|
return age |
|
} |
|
|
|
func bmi() -> Int { |
|
return weightKG / heightCM / heightCM |
|
} |
|
|
|
var getAvatarImg: UIImage? { |
|
return avatarImg ?? UIImage(named: "Icon_header") |
|
} |
|
|
|
/// 生日时间戳 |
|
var birthdayValue: Int { |
|
return DateClass.timeStrToTimestamp(birthday, formatStr: "yyyy-MM-dd") |
|
} |
|
|
|
}
|
|
|