/** * const variables * */ var ENDPOINT = 'http://gr.nvts.co/v1'; // client var x_nvs_ucid = '41f33045b1'; /* 41f33045b1 mynetvue 368ab19a23 ppvue e16a59207b eview 6eea4fca0c oneeasy */ //override the default content type $.ajaxSetup({ contentType: "application/json; charset=utf-8" }); /** * log-a-lightweight-wrapper-for-consolelog * */ window.log = function() { // log.history is an array log.history = log.history || []; log.history.push(arguments); if (this.console) { var args = arguments, newarr; args.callee = args.callee.caller; // return an array of arguments'element newarr = [].slice.call(args); if (typeof console.log === 'object') { log.apply.call(console.log, console, newarr); } else { console.log.apply(console, newarr); } } }; /** * make it safe to use console.log always * */ (function(a) { function b() {} for (var c = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","), d; !!(d = c.pop());) { a[d] = a[d] || b; } }) (function() { try { console.log(); return window.console; } catch (a) { return (window.console = {}); } }()); /** * throw error * */ // error tip var null_error = 'This is a empty object'; var error = function(m) { throw { name: 'SyntaxError', message: m } }; /** * json parse * */ // success callback var suc_callback = { // example: {"user":"testuser", "pass":"5d9c68c6c50ed3d02a2fcf54f63993b6", "email":"testuser@nvts.co", "udid":"adsf21fiavi/af82f"} create_user_res: function(data, textStatus, xhr) { if (xhr.status == 200 && data) { var expires = new Date(data.expires); cookieUtil.set('token', data.token, expires); cookieUtil.set('userid', data.userid, expires); return 0; } }, update_user_res: function(data, textStatus, xhr) { if (xhr.status == 204) { console.log('change password success!'); return 0; } }, // example: {"next":1341456789012} create_user_password_reset_res: function(data, textStatus, xhr) { if (xhr.status == 200 && data) { var expires = new Date(data.next); cookieUtil.set('next', expires); return 0; } }, sha_res: function(data, textStatus, xhr) { if (xhr.status == 204) { console.log('update user password with code success!'); return 0; } }, // example: {"token":"1fe949c554f4121a85301b277b63c55a", "userid":1, "expires":1341456789012, "email":"xxx@xxx.xxx"} create_user_token_res: function(data, textStatus, xhr) { if (xhr.status == 200 && data) { var expires = new Date(data.expires); cookieUtil.set('token', data.token, expires); cookieUtil.set('userid', data.userid, expires); cookieUtil.set('email', data.email, expires); return 0; } }, delete_user_token_res: function(data, textStatus, xhr) { if (xhr.status == 204) { console.log('delete user token success!'); return 0; } }, add_device_res: function(data, textStatus, xhr) { if (xhr.status == 204) { console.log('add device success!'); return 0; } }, update_device_res: function(data, textStatus, xhr) { if (xhr.status == 204) { console.log('update device success!'); return 0; } }, remove_device_res: function(data, textStatus, xhr) { if (xhr.status == 204) { console.log('remove device success!'); return 0; } }, // example: // /* {"devices":[[ 1304893423, // device ID, long "2014080503200001", // serial number, string "testcamera", // device name, string 2403492349, // owner ID, long "testuser", // owner username, string true, // online, boolean "r0a.nvts.co:80", // server, string "0700010829", // current firmware version, string "0700010830", // newest firmware version, string 100, // unread events, integer "xjij2fadf9u92", // key, string "192.168.1.20:34813", // address, string true, // audio on, boolean false, // flip on, boolean false, // light on, boolean true, // sdcard on, boolean "netview2305", // ssid, string 80 // wifi signal level, integer ], [], ... ]} */ get_device_res: function(data, textStatus, xhr) { }, //example: {"sharingid":30987623} create_device_sharing_res: function(data, textStatus, xhr) { if (data) { cookieUtil.set('sharingid', data.sharingid); } }, update_device_sharing_res: function(data, textStatus, xhr) { if (xhr.status == 204) { console.log('update device sharing success!'); return 0; } }, delete_device_sharing_res: function(data, textStatus, xhr) { if (xhr.status == 204) { console.log('delete device sharing success!'); return 0; } }, // example: // /* {"sharings":[[ 30823488, // sharing id, long "testuser", // username, string true // enable, boolean ], [], ... ]} */ get_device_sharing_res: function(data, textStatus, xhr) { if (xhr.status == 200 && data) { } }, // example: // /* {"liveinfo":{ "online":true, "server":"r1a.nvts.co:80", "version":"0700010829", "nversion":"0700010830", "unread":101, "key":"x83f1er347", "addr":"192.168.1.20:34953", "audio":true, "flip":false, "light":false, "sdcard":true, "ssid":"netview2305", "wifisl":80 }} */ get_device_liveinfo_res: function(data, textStatus, xhr) { }, create_device_sharing_res: function(data, textStatus, xhr) { if (xhr.status == 204) { console.log('create device sharing success!'); return 0; } } }; /** * cookie operate * */ var cookieUtil = { get: function(name) { var cookieName = encodeURIComponent(name) + '=', cookieStart = document.cookie.indexOf(cookieName), cookieValue = null; if (cookieStart > -1) { var cookieEnd = document.cookie.indexOf(';', cookieStart); if (cookieEnd === -1) { cookieEnd = document.cookie.length; } cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd)); } return cookieValue; }, set: function(name, value, expires, path, domain, secure) { var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value); if (expires instanceof Date) { cookieText += '; expires=' + expires.toGMTString(); } if (path) { cookieText += '; path=' + path; } if (domain) { cookieText += '; domain=' + domain; } if (secure) { cookieText += '; secure=' + secure; } document.cookie = cookieText; }, unset: function(name, path, domain, secure) { this.set(name, '', new Date(0), path, domain, secure); } } /** * GMT time format * */ var getGMT = function() { var nowDate, date, time, GMTtime; nowDate = new Date(); // Date format date = nowDate.getFullYear() + '-' + ((nowDate.getMonth() + 1) < 10 ? '0' : '') + (nowDate.getMonth() + 1) + '-' + (nowDate.getDate() < 10 ? '0' : '') + nowDate.getDate(); // time format time = nowDate.getHours() + ':' + nowDate.getMinutes() + ':' + nowDate.getSeconds(); GMTtime = date + 'T' + time + 'GMT'; return GMTtime; } /** * [_ajax_request description] * @param {[type]} url [description] * @param {[type]} data [description] * @param {[type]} headers [description] * @param {[type]} success [description] * @param {[type]} fail [description] * @param {[type]} dataType [description] * @param {[type]} method [description] * @return {[type]} [description] */ // Extend jQuery with functions for PUT, DELETE, GET, POST request. function _ajax_request(url, data, headers, success, fail, dataType, method) { if (jQuery.isFunction(data)) { success = data; data = {}; } return jQuery.ajax({ type: method, url: url, data: data, headers: headers, dataType: dataType }).then(success, fail); } jQuery.extend({ _put: function(url, data, headers, success, fail, dataType) { return _ajax_request(url, data, headers, success, fail, dataType, 'PUT'); }, _delete: function(url, data, headers, success, fail, dataType) { return _ajax_request(url, data, headers, success, fail, dataType, 'DELETE'); }, _get: function(url, data, headers, success, fail, dataType) { return _ajax_request(url, data, headers, success, fail, dataType, 'GET'); }, _post: function(url, data, headers, success, fail, dataType) { return _ajax_request(url, data, headers, success, fail, dataType, 'POST'); } }); (function($, name) { var api = window[name] = {}; api.errorCode = function(code) { return [ 'OK', 'ERR_INVAILD_PARAM', 'ERR_USER_NOT_EXISTS', 'ERR_USERNAME_ALREADY_EXISTS', 'ERR_EMAIL_ALREADY_EXISTS', 'ERR_USER_PW_NOT_MATCH', 'ERR_ACTION_NOT_SUPPORT', 'ERR_SEED_TIMEOUT', 'ERR_SEED_HASH_NOT_MATCH', 'ERR_SERVER_INTERNAL_ERROR', 'ERR_ALREADY_FRIENDS', 'ERR_FACEBOOK_USER', 'ERR_DEVICE_ALREADY_REGISTERED_TO_ANOTHER_USER', 'ERR_DEVICE_NOT_EXISTS', 'ERR_DEVICE_USER_NOT_MATCH', 'ERR_DEVICE_NOT_SHARE_TO_YOU', 'ERR_USER_NOT_AUTH_BY_SESSION', 'ERR_TCPLAN_IMPOSSIBLE', 'ERR_DEVICE_NOT_ONLINE', 'ERR_TRAN_SERVER_NOT_EXISTS', 'ERR_TRAN_SERVER_ALREADY_LOGIN', 'ERR_TRAN_SERVER_PASSWORD_NOT_MATCH', 'ERR_NO_TRAN_SERVER_AVAILABLE', 'WARING_RENRENUID_NOT_FOUND', 'WARING_FRIENDSHIP_ALREADY_EXISTS', 'WARING_FACEBOOKUID_NOT_FOUND', 'ERR_USER_EMAIL_NOT_MATCH', 'ERR_RECOVER_PASSWORD_TOO_OFTEN', 'ERR_DEVICE_ALREADY_REG_TO_THIS_USER', 'ERR_DEVICE_ALREADY_SHARE_TO_THIS_USER', 'ERR_DEVICE_CANNOT_SHARE_TO_OWNER', 'ERR_USER_TARGET_NOT_EXISTS', 'ERR_APP_NOT_EXISTS', 'ERR_APP_NOT_MATCH', 'ERR_APP_NOT_PERMITED', 'ERR_CLIENT_NOT_EXISTS', 'ERR_CLIENT_NOT_MATCH', 'ERR_PAIR_AUTH_FAILED', 'ERR_CAMERA_ALREADY_PAIRED', 'ERR_CAMERA_NOT_PAIRED', 'ERR_CONNECT_MQ_SERVER_ERROE', 'ERR_INVITATION_CODE_ERROR', 'ERR_INVALID_PACKET', 'ERR_SHARING_NOT_EXISTS', 'ERR_PASSWORD_RESET_CODE_ERROR' ][code]; } api.getToken = function() { return cookieUtil.get('token'); } api.getUsername = function() { return cookieUtil.get('username'); }; api.setMD5Password = function(pass) { var date = new Date(); var expires = date.setTime(date.getTime() + 24 * 60 * 60 * 1000); cookieUtil.set('pass', pass, expires); }; api.getMD5Password = function() { return cookieUtil.get('pass'); }; api.checkLogin = function() { return cookieUtil.get('udid'); }; api.logout = function() { cookieUtil.unset('token'); cookieUtil.unset('username'); cookieUtil.unset('pass'); } /** * * API * * 1. Create User => 'create_user' * * 2. Update User => 'update_user' * * 3. Create User Password Reset => 'create_user_password_reset' * * 4. Update User Password with Code => 'update_user_password_code' * * 5. Create User Token => 'create_user_token' * * 6. Delete User Token => 'delete_user_token' * * 7. Add_Device => 'add_device' * * 8. Update Device => 'update_device' * * 9. Remove Device => 'remove_device' * * 10. Get Devices => 'get_devices' * * 11. Create Device Sharing => 'create_device_sharing' * * 12. Update Device Sharing => 'update_device_sharing' * * 13. Delete Device Sharing => 'delete_device_sharing' * * 14. Get Device Sharings => 'get_device_sharing' * * 15. Get Device Liveinfo => 'get_device_liveinfo' * * 27. Create Device Sharing with Code => 'create_device_sharing_code' * */ // 15 operations var operations = [ { operate: 'create_user', method: 'POST', path: '/users?locale=en' }, { operate: 'update_user', method: 'PUT', path: '/user' }, { operate: 'create_user_password_reset', method: 'POST', path: '/passreset' }, { operate: 'update_user_password_code', method: 'PUT', path: '/passreset' }, { operate: 'create_user_token', method: 'POST', path: '/token?locale=en' }, { operate: 'delete_user_token', method: 'DELETE', path: '/token' }, { operate: 'add_device', method: 'POST', path: '/device' }, { operate: 'update_device', method: 'PUT', path: '/device' }, { operate: 'remove_device', method: 'DELETE', path: '/device' }, { operate: 'get_device', method: 'GET', path: '/devices' }, { operate: 'create_device_sharing', method: 'POST', path: '/sharings' }, { operate: 'update_device_sharing', method: 'PUT', path: '/sharing' }, { operate: 'delete_device_sharing', method: 'DELETE', path: '/sharing' }, { operate: 'get_device_sharing', method: 'GET', path: '/sharings/device' }, { operate: 'get_device_liveinfo', method: 'PUT', path: '/device' }, { operate: 'create_device_sharing_with_code', method: 'PUT', path: '/sharing/code' } ]; // operate array var o = [], // path array p = [], // method array m = [], // status array s = []; for (var i in operations) { var operate = operations[i].operate; o.push(operate); var path = operations[i].path; p.push(path); var method = operations[i].method; method = method.toLowerCase(); method = '_' + method; m.push(method); var status = operations[i].status; s.push(status); } $.each(o, function(k, item) { api[item] = function(param) { if (!param || (typeof param !== 'object')) { error(null_error); return; } if (!param.headers) { var headers = {}; } else { var headers = param.headers; } $.extend(headers, { 'x-nvs-ucid': x_nvs_ucid, 'x-nvs-token': API.getToken() }); if (!param.dataType) { var dataType = param.dataType = 'json'; } else { var dataType = param.dataType; } if (m[k] != '_get') { if (!param.data) { var data = param.data = '{}'; } else { var data = JSON.stringify(param.data); } } var success = param.success; var fail = param.fail; if (param.scope) { success = function(data, textStatus, xhr) { param.scope.$apply(function() { param.success(data, textStatus, xhr); }); }; fail = function(xhr, textStatus, errorThrown) { param.scope.$apply(function() { param.fail(xhr, textStatus, errorThrown); }); }; } if (!param.url) { var url = ENDPOINT + p[k]; } else { var url = ENDPOINT + p[k] + param.url; console.log(url); } // url, data, headers, success, fail, dataType return $[m[k]]( url, data, headers, success, fail, dataType ); } }); $.parameters = function() { var array = window.location.href.split("#")[0].split("?"); console.log(array); if (array.length > 1) array = array[1].split("&"); var map = new Array(); for (var i = 0; i < array.length; i++) { var ta = array[i].split("="); map[ta[0]] = ta[1]; } return map; }; $.parseUrl = function() { var array = window.location.href.split('?')[1]; array = array.split('&'); var map = new Array(); for (var i = 0; i < array.length; i++) { var ta = array[i].split('='); console.log(ta[0]); console.log(ta[1]); map[ta[0]] = ta[1]; } return map; } })(jQuery, 'API');