獲取設(shè)備編號(hào)、打開/關(guān)閉新頁面、設(shè)置狀態(tài)欄樣式、ajax封裝、獲取上一頁參數(shù)、退出APP......
/* =============================== * apicloud應(yīng)用公共js * versions 1.0.0 * cl15095344637@163.com * @example: * =============================== */var $app = {data: {appID: '', //應(yīng)用idappName: '', //應(yīng)用名稱appVersion: '1.0.0', //應(yīng)用版本號(hào)server: {url: '', //正式域名csurl: '', //測(cè)試域名},apicloud: true, //是否為apicloud環(huán)境},opts: function(opt){return $.extend(this.data, opt, true);},init: function(opt){var _this = this;_this.data = _this.opts(opt);},//獲得設(shè)備編號(hào)getDeviceId: function() { var deviceId = api.deviceId; deviceId = deviceId.replace(/\-/g, ""); return deviceId;},/***設(shè)置狀態(tài)欄樣式 @param {string} color APP狀態(tài)欄背景顏色 @param {string} style APP狀態(tài)欄文字顏色 @param {bool} contentTop 是否設(shè)置content頂部?jī)?nèi)邊距*/setStatusBar: function(color, style, contentTop) { api.setStatusBarStyle({ color: color, style: style }); $api.fixStatusBar($api.dom('header')); $api.fixIos7Bar($api.dom('header')); $api.fixTabBar($api.dom('footer')); $('header').height(44); contentTop == false ? contentTop = contentTop : contentTop = true; if(document.querySelector("header") && contentTop == true) { $(".content, .mui-content, aui-content").css({'padding-top': document.querySelector("header").offsetHeight}); }},/***ajax封裝 @param {string} opts.url 接口地址 @param {string} opts.type 請(qǐng)求類型, 默認(rèn)“post”(get, post) @param {Object} opts.data 請(qǐng)求參數(shù) (若上傳文件則data: {path: "", ...}) @example: aui.apicloud.ajax({ url: 'index/goods', tyle: 'post', data: {} }).then(function(ret){},function(err){})*/ajax: function(opts){var _this = this;if (_this.isDefine(opts.data.path)){ //上傳文件var data = { values: opts.data, files: { file: opts.data.path } }}else{ //普通數(shù)據(jù)請(qǐng)求var data = { values: opts.data }}return new Promise(function(resolve, reject){api.ajax({url: _this.data.server.url + opts.url || '',headers: {// 'Content-Type':'application/x-www-form-urlencoded' },method: opts.type || 'get',cache: opts.cache || 'false',timeout: opts.timeout || 30,dataTpye: opts.dataTpye || 'json',data: data || {},},function(ret, err) {if(ret){resolve(ret)}else{ //請(qǐng)求失敗reject(err)}});});},/***獲取上一頁參數(shù)*/getPageparam: function() { if (this.data.apicloud) { return api.pageParam; } else { var res = getUrlParam('param'); if (res == 'undefined') { return {}; } return _parse(res); }},/***判斷是否為空 @param {string} value 需要判斷的變量字符串*/isDefine: function(value) { if (value == null || value == "" || value == "undefined" || value == undefined || value == "null" || value == "(null)" || value == 'NULL' || typeof(value) == 'undefined') { return false; } else { value = value + ""; value = value.replace(/\s/g, ""); if (value == "") { return false; } return true; }},/***打開新窗口 @param {string} name window窗口名稱 @param {string} url window窗口路徑 @param {Object} pageParam 打開新窗口傳入的參數(shù) @param {string} type 動(dòng)畫類型 @param {string} subType 動(dòng)畫子類型 @param {string} duration 動(dòng)畫過渡時(shí)間,默認(rèn)300毫秒*/openWin: function(name, url, pageParam, type, subType, duration, isCheckLogin) {var _this = this; if (_this.data.apicloud) { _this.isDefine(pageParam) ? pageParam = pageParam : pageParam = new Object(); _this.isDefine(type) ? type = type : type = 'push'; _this.isDefine(subType) ? subType = subType : subType = 'from_right'; _this.isDefine(duration) ? duration = duration : duration = 300; api.openWin({ name: name, url: url, pageParam: pageParam, animation: { type: type, 新視圖將舊視圖推開 subType: subType, //從右邊開始動(dòng)畫 } }); }},/***關(guān)閉當(dāng)前頁面 */closeWin: function() {var _this = this; if (_this.data.apicloud) { api.closeWin(); }},/***關(guān)閉指定頁面 @param {string} name 指定頁面名稱*/winCloseName: function(name){if (api.systemType == 'ios'){var _timer = setTimeout(function(){clearTimeout(_timer);api.execScript({name: "root",script: "api.closeWin({name:'"+name+"'});"});},500)}else{api.closeWin({name:name});}},/***打開新頁面并在頁面消失時(shí)關(guān)閉當(dāng)前頁*/closeCurrentWin: function(){var _this = this; // 監(jiān)聽頁面消失的時(shí)候,關(guān)閉當(dāng)前頁 api.addEventListener({ name:'viewdisappear' },function(){ _this.closeWin(); });},/***退出APP @param {string} appID 當(dāng)前app對(duì)應(yīng)的ID(已全局設(shè)置) @param {bool} flag true:進(jìn)入后臺(tái)運(yùn)行; false:直接退出(可選)*/exitApp: function(flag){ if (api.systemType == 'ios') { //IOS退出 api.setWinAttr({ slidBackEnabled: false }); } else { //安卓雙擊退出 api.addEventListener({ name: 'keyback' }, function(ret, err) { if (window.__start == 'undefined' || !window.__start) { window.__start = new Date().getTime(); api.toast({ msg: '再按一次返回鍵退出', duration: 1500, location: 'bottom' }); } else { if (new Date().getTime() - window.__start < 1500) { flag ? flag = flag : flag = true; switch (flag) { case true: //APP進(jìn)入后臺(tái)運(yùn)行 api.toLauncher(); break; case false: //直接退出APP(會(huì)殺死進(jìn)程) api.closeWidget({ id: _this.appID, //應(yīng)用id retData: { name: 'closeWidget' }, animation: { type: 'flip', subType: 'from_bottom', duration: 500}, silent: true }); break; default: } } else { window.__start = new Date().getTime(); api.toast({ msg: '再按一次返回鍵退出', duration: 1500, location: 'bottom' }); } } }); }},}
聯(lián)系客服