//
// ToolManager.swift
// MovieFansDemo1
//
// Created by 余婷 on 16/9/27.
// Copyright © 2016年 余婷. All rights reserved.
//
import UIKit
//MARK: - NSUserDefualts的Key
///当前用户名
let UD_CurrentUserName = "UD_CurrentUserName"
///当前的登录状态
let UD_LoginStatus = "UD_LoginStatus"
//MARK: - 全局变量
///屏幕宽度
let Screen_W = UIScreen.mainScreen().bounds.width
///屏幕高度
let Screen_H = UIScreen.mainScreen().bounds.height
//MARK: - 接口
///热映,参数:count(个数) page(分页)
let Url_HotShow = "http://ting.weibo.com/movieapp/rank/hot"
///预告,count(个数) page(分页)
let Url_Trailer = "http://ting.weibo.com/movieapp/rank/coming"
///影评,参数:count(个数) type(1->热门影评, 0->最新影评)
let Url_FilmComment = "http://ting.weibo.com/movieapp/feed/list"
///影单,参数:count(个数) page(页数)
let Url_FilmList = "http://ting.weibo.com/movieapp/Pagelist/recommend"
///影单每一行,参数:page count id type(recommend) POST请求
let Url_FilmListLine = "http://ting.weibo.com/movieapp/Pagelist/recommendmovie"
///电影详情,参数:film_id(电影id)
let Url_FilmDetail = "http://ting.weibo.com/movieapp/page/base"
///演员详情
let Url_ActorDetail = ""
///代表作品
let Url_StandWorks = ""
///搜索,参数:search_key page count,方式:POST
let Url_Search = "http://ting.weibo.com/movieapp/search/search"
///筛选,参数:country type year count page
let Url_Screen = "http://ting.weibo.com/movieapp/search/film"
///长评详情,参数:type(long) id long_show(1)
let Url_LongReviewDetail = "http://ting.weibo.com/movieapp/feed/show"
//MARK: - 颜色的拓展
extension UIColor{
///通过0-255的RGB值去创建一个颜色对象
static func RGBColor(R:CGFloat,G:CGFloat,B:CGFloat,A:CGFloat) -> UIColor{
return UIColor.init(red: R/255, green: G/255, blue: B/255, alpha: A)
}
static func randomColor()->UIColor{
let colorValue = [(108,209,0),(255,128,0),(204,102,155),(102,255,255),(255,240,70)]
let i = Int(arc4random()%5)
let rgbValue = colorValue[i]
return self.RGBColor(CGFloat(rgbValue.0), G:CGFloat(rgbValue.1), B: CGFloat(rgbValue.2), A: 1)
}
}
//MARK: - 数据库相关
extension ToolManager{
//数据库的使用步骤
///创建/打开数据库
static func openDataBase(){
let db = self.default_db
let ret = db.open()
if ret {
print("数据库打开成功")
}else{
print("数据库打开失败")
}
}
///创建表
static func creatTable(tableName:String){
//0.打开数据库
self.openDataBase()
//1.创建sql语句
let sql = "CREATE TABLE IF NOT EXISTS t_\(tableName)(id integer PRIMARY KEY AUTOINCREMENT,filmName text,imageUrl text,film_id text);"
//2.执行sql
let ret = self.default_db.executeUpdate(sql, withArgumentsInArray:[])
if ret {
print("表创建成功")
}else{
print("表创建失败")
}
}
//3.增、删、改、查
///插入数据
static func insertData(model:FilmDetailHeaderModel,userName:String){
//创建表
self.creatTable(userName)
//sql语句
let sql = "INSERT INTO t_\(userName)(filmName,imageUrl,film_id) VALUES (?,?,?);"
//执行sql语句
let ret = self.default_db.executeUpdate(sql, withArgumentsInArray: [model.name,model.poster_url,model.film_id])
if ret {
print("插入成功")
}else{
print("插入失败")
}
}
///根据电影id去查询
static func isCollectionBefor(filmId:String,userName:String)->Bool{
//打开数据库
self.openDataBase()
self.creatTable(userName)
//查询数据
let sql = "SELECT * FROM t_\(userName) WHERE film_id=?;"
let dataSet = self.default_db.executeQuery(sql, withArgumentsInArray: [filmId])
//只要查询到结果,就返回true
while dataSet.next() {
return true
}
//没有查询到结果
return false
}
///查询数据
static func getAllData(userName:String)->[FilmDetailHeaderModel]{
//打开数据库
self.openDataBase()
//sql语句
let sql = "SELECT * FROM t_\(userName);"
//执行sql语句
let dataSet = self.default_db.executeQuery(sql, withArgumentsInArray: [])
//遍历获取结果
var retuanArray = [FilmDetailHeaderModel]()
while dataSet.next() {
//取到数据库中的数据
let name = dataSet.objectForColumnName("filmName") as! String
let imageUrl = dataSet.objectForColumnName("imageUrl") as! String
let filmId = dataSet.objectForColumnName("film_id") as! String
//创建模型对象
let model = FilmDetailHeaderModel()
model.name = name
model.poster_url = imageUrl
model.film_id = filmId
//保存到数组中
retuanArray.append(model)
}
return retuanArray
}
}
//MARK: - 通用方法
class ToolManager: NSObject {
static let default_db = FMDatabase.init(path:NSHomeDirectory()+"/Documents/User.db")
///计算指定字符串的大小
static func calculateStringSize(str:String,maxW:CGFloat,maxH:CGFloat,fontSize:CGFloat) -> CGSize{
//1.将字符串转换成OC的字符串
let ocStr = str as NSString
//2.计算
return ocStr.boundingRectWithSize(CGSizeMake(maxW, maxH), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(fontSize)], context: nil).size
}
///图文混排
//NSAttributedString 富文本
static func mixImageAndText(imageName:String,text:String)->NSAttributedString{
//1.将图片转换成富文本
let attach = NSTextAttachment.init()
attach.image = UIImage.init(named: imageName)
let imageAttr = NSAttributedString.init(attachment: attach)
//2.将字符串转换成富文本
let textAttr = NSAttributedString.init(string: text)
//3.将图片富文本和文字富文本拼接成一个富文本
//a.创建一个可变的富文本对象
let mutableAttr = NSMutableAttributedString()
//b.拼接
mutableAttr.appendAttributedString(imageAttr)
mutableAttr.appendAttributedString(textAttr)
return mutableAttr
}
}
//MARK: - 时间相关
extension ToolManager{
///将时间秒转换成00:00格式的字符串
static func transformTime(time:CGFloat)->String{
//获取分
let minute = Int(time) / 60
var minutestr = ""
if minute<10 {
minutestr = "0\(minute)"
}else{
minutestr = "\(minute)"
}
//获取秒
let second = Int(time)%60
var secondstr = ""
if second < 10 {
secondstr = "0\(second)"
}else{
secondstr = "\(second)"
}
return "\(minutestr):\(secondstr)"
}
///计算指定时间戳对应的时间到当前时间的时间差
static func getTimeDifference(time:NSTimeInterval)->String{
//将时间戳转换成时间
let date = NSDate.init(timeIntervalSince1970:time)
//算指定时间到当前时间之间的差值(单位:秒)(将时间转换成时间戳)
let currentDifference = date.timeIntervalSinceNow
//将时间差转换成天/时/分
let currentDifference2 = -currentDifference
//天
let day = currentDifference2/60/60/24
if day >= 1 {
//约束小数点后只有0位
let dayStr = String(format: "%.0f",day)
return "\(dayStr)天前"
}
//时
let hour = currentDifference2/60/60
if hour >= 1{
let hourStr = String(format: "%.0f",hour)
return "\(hourStr)小时前"
}
//分
let minute = currentDifference2/60
if minute >= 1 {
let minuteStr = String(format: "%.0f",minute)
return "\(minuteStr)分钟前"
}
//秒
let currentStr = String(format: "%.0f",currentDifference2)
return "\(currentStr)秒前"
}
}
//MARK: - 云存储相关
extension ToolManager{
///将账号和密码存储到云数据库中
static func BmobUserRegister(userName:String,passWord:String,result:(Bool,String)->Void){
//确定存储账号和密码对应的表名:UserInfo
//1.判断当前账号是否已经注册过
let query = BmobQuery.init(className: "UserInfo")
//按照注册的用户名去查找是否已经注册过
query.whereKey("userName", equalTo: userName)
query.findObjectsInBackgroundWithBlock { (allObject, error) in
//判断查询结构
if allObject.count == 0{
//2.在账号没有注册过的前提下去保存账号和密码
//1.常见数据对象
let objct = BmobObject.init(className: "UserInfo")
//2.设置数据
objct.setObject(userName, forKey: "userName")
objct.setObject(passWord, forKey: "passWord")
//3.保存
objct.saveInBackgroundWithResultBlock { (ret, error) in
if ret{
result(true,"注册成功")
}else{
result(false,"注册失败")
}
}
}else{
result(false,"账号已经被注册")
}//判断查询结果结束
}//查询结束
}//函数结束
///判断账号密码是否正确
static func BmobUserLogin(userName:String,passWord:String,result:(Bool,String)->Void){
//1.通过用户名去查找数据
let query = BmobQuery.init(className: "UserInfo")
//添加约束条件
query.whereKey("userName", equalTo: userName)
//开始查找
query.findObjectsInBackgroundWithBlock { (allObjects, error) in
//判断查询结果
if allObjects.count == 0{
//账号错误
result(false,"账号错误")
}else{
//账号正确
//a.取到唯一的账号信息对象
let object = allObjects.first as! BmobObject
//b.取出密码
let pass = object.objectForKey("passWord")
//c.判断密码是否正确
if pass as! String == passWord{
//登录成功
result(true,"登录成功")
}else{
//密码错误
result(false,"密码错误")
}
}//判断查询结束
}//查询结束
}//函数结束
}