2021-01-30 17:30:28 +08:00
// the api use bool to return status: fail: return false, true: return data(including true and false)
// the api use other type to return data: fail: return undefined, true: return data(if the returned value have change be null, return undefined instaed).
2021-01-20 22:57:41 +08:00
// var cached_salt = undefined
/ *
2021-01-30 17:30:28 +08:00
function ccn _api _common _salt ( _username ) {
2021-01-20 22:57:41 +08:00
// true or false
// gotten salt store in cached_salt.
var gotten _data = undefined ;
$ . ajax ( {
url : '/api/common/salt' ,
type : "POST" ,
async : false ,
data : {
username : _username
} ,
success : function ( data ) {
gotten _data = data ;
}
} ) ;
if ( IsResponseOK ( gotten _data ) ) {
cached _salt = gotten _data [ 'data' ] ;
return true ;
} else return false ;
}
2021-01-30 17:30:28 +08:00
function ccn _api _common _login ( _username , password ) {
2021-01-20 22:57:41 +08:00
// return true or false, token is managed by this js file self.
// if cached_salt is undefined, return false directly
if ( typeof ( cached _salt ) == undefined ) return false ;
var gotten _data = undefined ;
$ . ajax ( {
url : '/api/common/login' ,
type : "POST" ,
async : false ,
data : {
username : _username ,
password : ComputPasswordWithSalt ( password , cached _salt )
} ,
success : function ( data ) {
gotten _data = data ;
}
} ) ;
if ( IsResponseOK ( gotten _data ) && gotten _data [ 'data' ] != '' ) {
2021-03-08 15:41:32 +08:00
ccn _localstorageAssist _SetApiToken ( gotten _data [ 'data' ] ) ;
2021-01-20 22:57:41 +08:00
cached _salt = undefined ;
return true ;
} else return false ;
}
* /
2021-01-30 21:27:36 +08:00
// ============================================ template
// all api can be implemented by these 2 function, except 3 token related func.
// so all api func should use these 2 func except 3 token process api.
function ccn _api _dataTemplate ( _url , _data ) {
// return data or undefined
var gotten _data = undefined ;
$ . ajax ( {
url : _url ,
type : "POST" ,
async : false ,
data : _data ,
success : function ( data ) {
gotten _data = data ;
}
} ) ;
if ( IsResponseOK ( gotten _data ) && ! ( gotten _data [ 'data' ] === null ) ) return gotten _data [ 'data' ] ;
else return undefined ;
}
function ccn _api _boolTemplate ( _url , _data ) {
// return true or false
var gotten _data = undefined ;
$ . ajax ( {
url : _url ,
type : "POST" ,
async : false ,
data : _data ,
success : function ( data ) {
gotten _data = data ;
}
} ) ;
return ( IsResponseOK ( gotten _data ) && gotten _data [ 'data' ] ) ;
}
2021-03-07 21:01:51 +08:00
// deserialize & serialize calendar description function
function ccn _api _serializeDescription ( _description , _color ) {
var sobj = {
description : _description ,
color : _color
}
return JSON . stringify ( sobj ) ;
}
function ccn _api _deserializeDescription ( strl ) {
try {
return $ . parseJSON ( strl ) ;
} catch ( err ) {
return {
description : "" ,
color : DefaultColor
} ;
}
}
2021-01-23 18:37:12 +08:00
// ====================================================== common
2021-01-30 21:27:36 +08:00
function ccn _api _common _webLogin ( _username , _password ) {
2021-01-20 22:57:41 +08:00
var gotten _data = undefined ;
$ . ajax ( {
url : '/api/common/webLogin' ,
type : "POST" ,
async : false ,
data : {
username : _username ,
2021-01-30 21:27:36 +08:00
password : _password
2021-01-20 22:57:41 +08:00
} ,
success : function ( data ) {
gotten _data = data ;
}
} ) ;
2021-01-23 18:37:12 +08:00
if ( IsResponseOK ( gotten _data ) ) {
2021-03-08 15:41:32 +08:00
ccn _localstorageAssist _SetApiToken ( gotten _data [ 'data' ] ) ;
2021-01-20 22:57:41 +08:00
return true ;
} else return false ;
}
2021-01-30 17:30:28 +08:00
function ccn _api _common _logout ( ) {
2021-01-20 22:57:41 +08:00
// return true or false
var gotten _data = undefined ;
$ . ajax ( {
url : '/api/common/logout' ,
type : "POST" ,
async : false ,
data : {
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( )
2021-01-20 22:57:41 +08:00
} ,
success : function ( data ) {
gotten _data = data ;
}
} ) ;
2021-01-30 17:30:28 +08:00
if ( IsResponseOK ( gotten _data ) && gotten _data [ 'data' ] ) {
2021-03-08 15:41:32 +08:00
ccn _localstorageAssist _SetApiToken ( '' ) ;
2021-01-20 22:57:41 +08:00
return true ;
} return false ;
}
2021-01-30 17:30:28 +08:00
function ccn _api _common _tokenValid ( ) {
2021-01-20 22:57:41 +08:00
// get from local database first, then judge it via post
// return true or false
2021-03-08 15:41:32 +08:00
var gotten _token = ccn _localstorageAssist _GetApiToken ( ) ;
2021-01-20 22:57:41 +08:00
if ( gotten _token == '' ) return false ;
var gotten _data = undefined ;
$ . ajax ( {
url : '/api/common/tokenValid' ,
type : "POST" ,
async : false ,
data : {
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( )
2021-01-20 22:57:41 +08:00
} ,
success : function ( data ) {
gotten _data = data ;
}
} ) ;
2021-01-30 17:30:28 +08:00
if ( IsResponseOK ( gotten _data ) && gotten _data [ 'data' ] ) return true ;
2021-01-20 22:57:41 +08:00
else {
2021-03-08 15:41:32 +08:00
ccn _localstorageAssist _SetApiToken ( '' ) ;
2021-01-20 22:57:41 +08:00
return false ;
}
2021-01-23 18:37:12 +08:00
}
// ====================================================== calendar
2021-01-30 21:27:36 +08:00
function ccn _api _calendar _getFull ( _startDateTime , _endDateTime ) {
return ccn _api _dataTemplate (
'/api/calendar/getFull' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
startDateTime : _startDateTime ,
endDateTime : _endDateTime
}
) ;
}
2021-01-23 18:37:12 +08:00
2021-01-30 21:27:36 +08:00
function ccn _api _calendar _getDetail ( _uuid ) {
return ccn _api _dataTemplate (
'/api/calendar/getDetail' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
uuid : _uuid
}
) ;
}
2021-01-23 18:37:12 +08:00
2021-01-30 21:27:36 +08:00
function ccn _api _calendar _update ( _uuid , _belongTo , _title , _description , _eventDateTimeStart , _eventDateTimeEnd , _loopRules , _timezoneOffset , _lastChange ) {
2021-02-09 17:10:05 +08:00
var data = { } ;
if ( typeof ( _belongTo ) != 'undefined' )
data . belongTo = _belongTo ;
if ( typeof ( _title ) != 'undefined' )
data . title = _title ;
if ( typeof ( _description ) != 'undefined' )
data . description = _description ;
if ( typeof ( _eventDateTimeStart ) != 'undefined' )
data . eventDateTimeStart = _eventDateTimeStart ;
if ( typeof ( _eventDateTimeEnd ) != 'undefined' )
data . eventDateTimeEnd = _eventDateTimeEnd ;
if ( typeof ( _loopRules ) != 'undefined' )
data . loopRules = _loopRules ;
if ( typeof ( _timezoneOffset ) != 'undefined' )
data . timezoneOffset = _timezoneOffset ;
2021-03-08 15:41:32 +08:00
data . token = ccn _localstorageAssist _GetApiToken ( ) ;
2021-02-09 17:10:05 +08:00
data . uuid = _uuid ;
data . lastChange = _lastChange ;
2021-01-30 21:27:36 +08:00
return ccn _api _dataTemplate (
'/api/calendar/update' ,
2021-02-09 17:10:05 +08:00
data
2021-01-30 21:27:36 +08:00
) ;
}
function ccn _api _calendar _add ( _belongTo , _title , _description , _eventDateTimeStart , _eventDateTimeEnd , _loopRules , _timezoneOffset ) {
return ccn _api _dataTemplate (
'/api/calendar/add' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
belongTo : _belongTo ,
title : _title ,
description : _description ,
eventDateTimeStart : _eventDateTimeStart ,
eventDateTimeEnd : _eventDateTimeEnd ,
loopRules : _loopRules ,
timezoneOffset : _timezoneOffset
}
) ;
}
2021-01-23 18:37:12 +08:00
2021-01-30 21:27:36 +08:00
function ccn _api _calendar _delete ( _uuid , _lastChange ) {
return ccn _api _boolTemplate (
'/api/calendar/delete' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
uuid : _uuid ,
lastChange : _lastChange
}
) ;
}
2021-01-23 18:37:12 +08:00
// ====================================================== collection
2021-01-30 21:27:36 +08:00
function ccn _api _collection _getFullOwn ( ) {
return ccn _api _dataTemplate (
'/api/collection/getFullOwn' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( )
2021-01-30 21:27:36 +08:00
}
) ;
}
function ccn _api _collection _getDetailOwn ( _uuid ) {
return ccn _api _dataTemplate (
'/api/collection/getDetailOwn' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
uuid : _uuid
}
) ;
}
function ccn _api _collection _addOwn ( _name ) {
return ccn _api _dataTemplate (
'/api/collection/addOwn' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
name : _name
}
) ;
}
function ccn _api _collection _updateOwn ( _uuid , _name , _lastChange ) {
return ccn _api _dataTemplate (
'/api/collection/updateOwn' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
uuid : _uuid ,
name : _name ,
lastChange : _lastChange
}
) ;
}
function ccn _api _collection _deleteOwn ( _uuid , _lastChange ) {
return ccn _api _boolTemplate (
'/api/collection/deleteOwn' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
uuid : _uuid ,
lastChange : _lastChange
}
) ;
}
2021-01-23 18:37:12 +08:00
2021-01-30 21:27:36 +08:00
function ccn _api _collection _getSharing ( _uuid ) {
return ccn _api _dataTemplate (
'/api/collection/getSharing' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
uuid : _uuid
}
) ;
}
function ccn _api _collection _deleteSharing ( _uuid , _target , _lastChange ) {
return ccn _api _dataTemplate (
'/api/collection/deleteSharing' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
uuid : _uuid ,
target : _target ,
lastChange : _lastChange
}
) ;
}
function ccn _api _collection _addSharing ( _uuid , _target , _lastChange ) {
return ccn _api _dataTemplate (
'/api/collection/addSharing' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
uuid : _uuid ,
target : _target ,
lastChange : _lastChange
}
) ;
}
function ccn _api _collection _getShared ( ) {
return ccn _api _dataTemplate (
2021-02-05 17:07:20 +08:00
'/api/collection/getShared' ,
2021-01-30 21:27:36 +08:00
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( )
2021-01-30 21:27:36 +08:00
}
) ;
}
2021-01-23 18:37:12 +08:00
// ====================================================== todo
2021-01-30 17:30:28 +08:00
function ccn _api _todo _getFull ( ) {
2021-01-30 21:27:36 +08:00
return ccn _api _dataTemplate (
'/api/todo/getFull' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( )
2021-01-23 18:37:12 +08:00
}
2021-01-30 21:27:36 +08:00
) ;
2021-01-23 18:37:12 +08:00
}
2021-01-30 17:30:28 +08:00
function ccn _api _todo _add ( ) {
2021-01-30 21:27:36 +08:00
return ccn _api _dataTemplate (
'/api/todo/add' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( )
2021-01-23 18:37:12 +08:00
}
2021-01-30 21:27:36 +08:00
) ;
2021-01-23 18:37:12 +08:00
}
2021-01-30 17:30:28 +08:00
function ccn _api _todo _update ( _uuid , _data , _lastChange ) {
2021-01-30 21:27:36 +08:00
return ccn _api _dataTemplate (
'/api/todo/update' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-23 18:37:12 +08:00
uuid : _uuid ,
data : _data ,
lastChange : _lastChange
}
2021-01-30 21:27:36 +08:00
) ;
2021-01-23 18:37:12 +08:00
}
2021-01-30 17:30:28 +08:00
function ccn _api _todo _delete ( _uuid , _lastChange ) {
2021-01-30 21:27:36 +08:00
return ccn _api _boolTemplate (
'/api/todo/delete' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-23 18:37:12 +08:00
uuid : _uuid ,
lastChange : _lastChange
}
2021-01-30 21:27:36 +08:00
) ;
2021-01-23 18:37:12 +08:00
}
// ====================================================== admin
2021-01-30 21:27:36 +08:00
function ccn _api _admin _get ( ) {
return ccn _api _dataTemplate (
'/api/admin/get' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( )
2021-01-30 21:27:36 +08:00
}
) ;
}
function ccn _api _admin _add ( _username ) {
return ccn _api _dataTemplate (
'/api/admin/add' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
username : _username
}
) ;
}
function ccn _api _admin _update ( _username , _password , _isAdmin ) {
2021-02-03 16:08:40 +08:00
var data = { } ;
if ( typeof ( _password ) != 'undefined' )
data . password = _password ;
if ( typeof ( _isAdmin ) != 'undefined' )
data . isAdmin = _isAdmin ;
if ( Object . getOwnPropertyNames ( data ) . length == 0 ) return false ;
2021-03-08 15:41:32 +08:00
data . token = ccn _localstorageAssist _GetApiToken ( ) ;
2021-02-03 16:08:40 +08:00
data . username = _username ;
return ccn _api _boolTemplate (
2021-01-30 21:27:36 +08:00
'/api/admin/update' ,
2021-02-03 16:08:40 +08:00
data
2021-01-30 21:27:36 +08:00
) ;
}
2021-01-23 18:37:12 +08:00
2021-01-30 21:27:36 +08:00
function ccn _api _admin _delete ( _username ) {
return ccn _api _boolTemplate (
'/api/admin/delete' ,
{
2021-03-08 15:41:32 +08:00
token : ccn _localstorageAssist _GetApiToken ( ) ,
2021-01-30 21:27:36 +08:00
username : _username
}
) ;
}
2021-01-23 18:37:12 +08:00
2021-03-08 21:56:03 +08:00
// ====================================================== profile
function ccn _api _profile _isAdmin ( ) {
return ccn _api _boolTemplate (
'/api/profile/isAdmin' ,
{
token : ccn _localstorageAssist _GetApiToken ( )
}
) ;
}
function ccn _api _profile _changePassword ( _password ) {
return ccn _api _boolTemplate (
'/api/profile/changePassword' ,
{
token : ccn _localstorageAssist _GetApiToken ( ) ,
password : _password
}
) ;
}
function ccn _api _profile _getToken ( ) {
return ccn _api _boolTemplate (
'/api/profile/getToken' ,
{
token : ccn _localstorageAssist _GetApiToken ( )
}
) ;
}
function ccn _api _profile _deleteToken ( _deleteToken ) {
return ccn _api _boolTemplate (
'/api/profile/deleteToken' ,
{
token : ccn _localstorageAssist _GetApiToken ( ) ,
deleteToken : _deleteToken
}
) ;
}