123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176 |
- import config from './config';
- var http = {
-
- token : 'token',
-
- getToken: function() {
- return uni.getStorageSync('prev1_' + this.token);
- },
-
-
- setToken: function(value) {
- return uni.setStorageSync('prev1_' + this.token, value);
- },
-
-
- option: function(options) {
- if (!options) {
- options = {};
- }
- options[this.token] = this.getToken();
- return options;
- },
-
-
- request: function(vue, url, options, callback, err_callback) {
- var self = this;
- if (vue == 'post') {
- config.request.method = 'POST';
- vue = false;
- } else {
- config.request.method = 'GET';
- }
- if (typeof(url) == 'object') {
- vue = url[1];
- url = url[0];
- }
- if (typeof(vue) == 'object') {
- vue.isDisabled = true;
- }
-
- self.core(url, options).then((result, state) => {
- if (typeof(vue) == 'object') {
- if (!vue.fetch) {
- vue.fetch = {}
- }
- vue.isDisabled = false;
- vue.fetch = Object.assign({}, vue.fetch, result)
- }
-
- if (callback) {
- callback(result, state);
- }
- }).catch((result) => {
- if (typeof(vue) == 'object') {
- vue.isDisabled = false;
- }
- if (err_callback) {
- err_callback(result);
- } else {
- if (result.code == '2') {
- dever.location(dever.login);
- } else {
- dever.alert(result.info)
- }
- }
- })
- },
-
- core : function(url, options) {
- var self = this;
- options = this.option(options);
- if (config.server[url]) {
- config.request.url = config.server[url];
- } else {
- config.request.url = url;
- }
- var loading_method = config.request.method;
- if (options && options.page && options.page > 0) {
- loading_method = 'page';
- }
- config.request.url = config.request.host + config.request.url;
- config.request.data = options;
- return new Promise((resolve, reject) => {
- config.request.complete = (response) => {
- dever.debug(response);
- if (response.statusCode === 200) {
- if (response.data.code == '1') {
- if (options.page && options.page > 0) {
- page.state = true;
- if (page.key && response.data.data[page.key].length > 0) {
- if (options && options.concat && options.concat == -1) {
- page.data = response.data.data[page.key].concat(page.data);
- } else {
- page.data = page.data.concat(response.data.data[page.key]);
- }
- response.data.data[page.key] = page.data;
- } else if (response.data.data.length > 0) {
- if (options && options.concat && options.concat == -1) {
- response.data.data = response.data.data.reverse();
- page.data = response.data.data.concat(page.data);
- } else {
- page.data = page.data.concat(response.data.data);
- }
- response.data.data = page.data;
- } else {
- page.state = false;
- resolve(false);
-
- if (options && options.noloading) {
-
- } else {
- dever.hideLoading(loading_method, options, url);
- }
- return;
- }
- }
- resolve(response.data.data);
- } else if (response.data.code == '-1') {
-
- self.setToken('');
- console.info(dever);
- dever.location('index/index', 'go');
- reject(response.data)
- } else {
- reject(response.data)
- }
- if (options && options.noloading) {
-
- } else {
- dever.hideLoading(loading_method, options, url);
- }
- } else {
-
- if (options && options.handle) {
- reject(response)
- } else {
- try {
- Promise.reject(response).catch(err => {
- self.error(response.statusCode || response.errMsg);
- });
- } catch (e) {
- dever.alert(e)
- }
- }
- if (options && options.noloading) {
-
- } else {
- dever.hideLoading(loading_method, options, url);
- }
- }
- }
-
- dever.debug(config.request);
- if (options.noloading) {
-
- } else {
- dever.loading(loading_method, options, url);
- }
-
- uni.request(config.request);
- })
- },
-
- error : function(err) {
-
-
- switch (err) {
- case 401:
-
- console.error("请求背拒绝" + err)
- break;
- case 404:
-
- console.error("没有找到页面" + err)
- break;
- case 500:
-
- console.error("500服务器错误" + err)
- break;
- case 405:
- console.error("错误的请求" + err)
- break;
- }
- },
- }
- var im = {
- socket : false,
- init : function() {
- this.connect();
- this.info();
- },
-
- connect : function() {
- uni.connectSocket({
- url: 'text://me.5dev.cn:8282',
- complete: (res)=> {
- console.log(res);
- }
- });
- uni.onSocketOpen(function (res) {
- console.log('WebSocket连接已打开!');
- });
- uni.onSocketError(function (res) {
- console.log('WebSocket连接打开失败,请检查!');
- });
-
- },
-
- info : function() {
- console.info(222);
- uni.onSocketMessage(function (res) {
- console.log('收到服务器内容:' + res.data);
- });
- }
- }
- var page = {
-
- value : 1,
-
- data : [],
-
- key : '',
-
- state : true,
-
-
- get : function(config, vue, url, options, callback, err_callback) {
- options = http.option(options);
- if (typeof(config) == 'object') {
- var state = config[0];
- this.key = config[1];
- } else {
- var state = config;
- this.key = '';
- }
- if (state == 1) {
- this.value = 1;
- this.data = [];
- } else {
- this.value++;
- }
-
- if (this.state == false && state != 1) {
- return;
- }
-
- options.page = this.value;
- http.request(vue, url, options, callback, err_callback);
- },
- }
- var upload = {
- data : {},
- handle : function(key, count, callback) {
- if (!count) {
- count = 1;
- }
- var type = 1;
- if (count > 1) {
- type = 2;
- }
- var self = this;
- count = parseInt(count);
- uni.chooseImage({
- count: count,
- sizeType: ['original', 'compressed'],
- sourceType: ['album'],
- success: res => {
- uni.showLoading();
- res.tempFilePaths.forEach(function(item, index) {
-
- uni.uploadFile({
- url: config.upload.url,
- filePath: item,
- name: config.upload.name,
- formData: { token: http.getToken() },
- success: res => {
- dever.debug(res);
- var data = JSON.parse(res.data);
- uni.hideLoading();
- if (data.uploaded) {
- var backUrl = data.url;
- if (count > 1) {
- if (!self.data[key]) {
- self.data[key] = [];
- }
- self.data[key].push(backUrl);
- } else {
- self.data[key] = backUrl;
- }
- if (callback) {
- callback(type, self.data[key]);
- }
- } else if (data.uploaded == false) {
- uni.showToast({ title: data.error.message, icon: 'none' });
- }
- }
- });
- });
- },
- fail: () => {
- uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
- uni.hideLoading();
- }
- });
- },
-
- upfile : function(e, key, count, callback, ext, title, style) {
- if (!count) {
- count = 1;
- }
- var type = 1;
- if (count > 1) {
- type = 2;
- }
- if (!title) {
- title = '点击这里上传文件';
- }
- var self = this;
- count = parseInt(count);
- var a = document.createElement('button');
- a.className = 'file';
- if (style) {
- a.style.cssText = style;
- } else {
- a.style.cssText = 'padding: 4px 10px;height: 20px;line-height: 20px;position: relative;cursor: pointer;color: #888;background: #fafafa;border: 1px solid #ddd;border-radius: 4px;overflow: hidden;display: inline-block;*display: inline;*zoom: 1;';
- }
- var style = 'position: absolute;opacity: 0;filter: alpha(opacity=0);cursor: pointer';
- a.innerHTML = '<input style="'+style+'" type="file" name="upload_file" id="upload_file" />' + title;
- e.$refs.input.$el.appendChild(a)
- var input = document.getElementById('upload_file');
-
- input.onchange = (event) => {
- uni.showLoading();
- var reader = new FileReader();
- var type = input.files[0].type;
- var temp = type.split('/');
- type = temp[1];
-
- if (ext && ext.indexOf(type) == -1) {
- uni.showToast({ title: '您上传的文件类型错误,请上传带有' + ext + '这些后缀的文件', icon: 'none' });
- } else {
- reader.readAsDataURL(input.files[0]);
- reader.onload = function(){
-
- var pic = this.result;
- var temp = pic.split('base64,');
- pic = temp[1];
- dever.debug(pic);
- var url = "http://up.qiniu.com/putb64/-1";
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange=function(){
- if (xhr.readyState==4){
- dever.debug(xhr.responseText);
- var data = JSON.parse(xhr.responseText);
- uni.hideLoading();
- if (data.uploaded) {
- var backUrl = data.url;
- if (count > 1) {
- if (!self.data[key]) {
- self.data[key] = [];
- }
- self.data[key].push(backUrl);
- } else {
- self.data[key] = backUrl;
- }
- if (callback) {
- callback(type, self.data[key], input.files[0]);
- }
- } else if (data.uploaded == false) {
- uni.showToast({ title: data.error.message, icon: 'none' });
- }
- }
- }
- xhr.open("POST", url, true);
- xhr.setRequestHeader("Content-Type", "application/octet-stream");
- xhr.setRequestHeader("Authorization", "UpToken " + e.token);
- xhr.send(pic);
- }
- }
-
- }
- },
-
- qiniu : function(qn, config, key, count, callback) {
- if (!count) {
- count = 1;
- }
- var type = 1;
- if (count > 1) {
- type = 2;
- }
- var self = this;
- count = parseInt(count);
- uni.chooseImage({
- count: count,
- sizeType: ['original', 'compressed'],
- sourceType: ['album'],
- success: res => {
- uni.showLoading();
- res.tempFilePaths.forEach(function(item, index) {
-
- qn.upload(
- item,
- res => {
- dever.debug(res);
- var data = res;
- uni.hideLoading();
- if (data.uploaded) {
- var backUrl = data.url;
- if (count > 1) {
- if (!self.data[key]) {
- self.data[key] = [];
- }
- self.data[key].push(backUrl);
- } else {
- self.data[key] = backUrl;
- }
- if (callback) {
- callback(type, self.data[key]);
- }
- } else if (data.uploaded == false) {
- uni.showToast({ title: data.error.message, icon: 'none' });
- }
- },
- error => {
- uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
- uni.hideLoading();
- },
- {
- region: 'ECN',
- domain: config.upload,
-
- uptoken: config.token
- },
- res => {
-
- if(res.progress === 100){
-
- }
- }
- );
- });
- },
- fail: () => {
- uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
- uni.hideLoading();
- }
- });
- },
-
- uploadDel : function(key, index, callback) {
- var self = this;
- uni.showModal({
- content: '确定要删除吗?',
- cancelText: '取消',
- confirmText: '确定',
- success: res => {
- if (res.confirm) {
-
- if (callback) {
- callback(res, index);
- }
- }
- }
- });
- }
- }
- var dever = {
- login : 'login/login?refer=-1',
- web_view : '/dever/pages/web_view',
- source : 'h5',
- host : '',
- doc : false,
- config : {},
- vue : false,
- cur : false,
- getLoadingState : {},
- getLoadingCall : false,
- postLoadingCall : false,
- pageLoadingCall : false,
- lastGetUrl : '',
- showLoad : false,
- btnText : '确定提交',
- switchTab : [],
- switchTabCall : {},
-
-
- curTime : function() {
- var time = Date.parse(new Date())/1000;
- return time;
- },
-
- init : function(url, vue, value, set) {
- this.vue = vue;
- vue.prototype.$config = value;
- var self = this;
- var config = self.data('dever_config');
- var state = false;
- if (config && !set) {
- self.config = config;
- vue.prototype.$config = self.config;
- if (self.config.update_time) {
- var time = this.curTime();
- if (!self.config.update_time || time - self.config.update_time >= value.set_update) {
- state = true;
- }
- } else {
- state = false;
- }
-
- } else {
- state = true;
- }
- if (state) {
-
- if (url) {
- self.post(url, {noloading:1}, function(t) {
- self.config = t;
- self.config.update_time = self.curTime();
- self.data('dever_config', self.config);
- vue.prototype.$config = self.config;
- });
- } else {
- self.config = value;
- vue.prototype.$config = self.config;
- self.data('dever_config', self.config);
- }
- }
-
- return this;
- },
-
- common : function(func) {
- this.source = 'h5';
- this.host = '';
-
- this.source = 'h5';
- this.host = window.location.protocol + '//' + window.location.host + '/#';
-
-
- this.source = 'app';
-
-
- this.source = 'applet';
-
-
- if (func) {
- for (var i in func) {
- this.vue.prototype[i] = func[i];
- }
- }
- },
-
- btnLoad : function(cur) {
- if (!cur) {
- cur = this.cur;
- }
- if (!cur) {
- return;
- }
- cur.btn = this.btn(cur);
- },
-
- btnFinish : function() {
- var cur = this.cur;
- if (!cur) {
- return;
- }
- cur.btn = {
- disabled:false,
- loading:false,
- text:this.btnText,
- };
- },
-
- btn : function(cur, text) {
- this.cur = cur;
- if (text) {
- this.btnText = text;
- }
- return {
- disabled:true,
- loading:true,
- text:'加载中...',
- };
- },
-
- setGetLoading : function(yes, no) {
- this.getLoadingCall = {'yes': yes, 'no' : no};
- },
- setPostLoading : function(yes, no) {
- this.postLoadingCall = {'yes': yes, 'no' : no};
- },
- setPageLoading : function(yes, no) {
- this.pageLoadingCall = {'yes': yes, 'no' : no};
- },
-
- route : function() {
- var page = this.getPage();
- if (!page) {
- return '';
- }
- var route = page.route;
- return route;
- },
-
- getPage : function() {
- var pages = getCurrentPages();
- if (pages.length > 0) {
- return pages[pages.length - 1];
- } else {
- return false;
- }
- },
-
- loading : function(method, options, url) {
- var state = false;
- if (method == 'POST') {
- if (this.postLoadingCall && this.postLoadingCall.yes) {
- state = true;
- var callback = this.postLoadingCall.yes;
- callback(this);
- }
- } else if (method == 'page') {
- state = true;
- if (options.page && options.page > 1) {
- state = false;
- }
- if (this.pageLoadingCall && this.pageLoadingCall.yes) {
- state = true;
- var callback = this.pageLoadingCall.yes;
- callback(this);
- }
- } else {
- url = this.route();
- if (this.lastGetUrl && this.lastGetUrl != url && this.getLoadingState[this.lastGetUrl]) {
- this.getLoadingState[this.lastGetUrl] = false;
- }
- if (this.getLoadingState && this.getLoadingState[url]) {
- state = false;
- } else {
- if (this.getLoadingCall && this.getLoadingCall.yes) {
- state = true;
- var callback = this.getLoadingCall.yes;
- callback(this);
- }
- }
- }
- if (!state) {
- uni.showLoading({title: '加载中', mask: true});
-
- this.showLoad = true;
- }
- uni.stopPullDownRefresh();
- },
-
- hideLoading : function(method, options, url) {
- var state = false;
- if (method == 'POST' || method == 'post') {
- if (this.postLoadingCall && this.postLoadingCall.no) {
- state = true;
- var callback = this.postLoadingCall.no;
- callback(this);
- }
- } else if (method == 'page') {
- state = true;
- if (options.page && options.page > 1) {
- state = false;
- }
- if (this.pageLoadingCall && this.pageLoadingCall.no) {
- state = true;
- var callback = this.pageLoadingCall.no;
- callback(this);
- }
- } else {
- url = this.route();
- if (!this.lastGetUrl || (this.lastGetUrl && this.lastGetUrl != url)) {
- this.lastGetUrl = url;
- }
- if (this.getLoadingState && this.getLoadingState[url]) {
- state = false;
- } else {
- this.getLoadingState[url] = true;
- if (this.getLoadingCall && this.getLoadingCall.no) {
- state = true;
- var callback = this.getLoadingCall.no;
- callback(this);
- }
- }
- }
-
- if (!state && this.showLoad) {
- uni.hideLoading();
- this.showLoad = false;
- this.btnFinish();
-
- }
- },
-
- dataset : function(e) {
- if (e.currentTarget) {
- var dataset = e.currentTarget.dataset;
- } else {
- var dataset = e.target.dataset;
- }
-
- return dataset;
- },
-
- viewAlert : function(e) {
- if (e) {
- var dataset = this.dataset(e);
- var msg = dataset.msg;
- var icon = dataset.icon;
- var callback = dataset.callback;
-
- this.alert(msg, icon, callback);
- } else {
- this.alert('敬请期待');
- }
- },
-
- viewLocation : function(e) {
- var page = '';
- var to = '';
- var option = '';
- if (e) {
- var dataset = this.dataset(e);
- page = dataset.page;
- to = dataset.to;
- option = []
-
- for (var i in dataset) {
- if (i != 'page' && i != 'to') {
- option.push(i + '=' + dataset[i]);
- }
- }
- if (option) {
- option = option.join('&');
- }
- }
-
- if (!page) {
- uni.navigateBack({});
- } else {
- var go = '';
- if (to) {
- go = 'to';
- }
- if (option) {
- page = page + '?' + option;
- }
- this.location(page, go);
- }
- },
-
- location : function(path, go) {
- var self = this;
- if (path.indexOf('http') > -1) {
- this.debug('http:' + path);
- this.data('web_view', path);
- if (this.source == 'app' || this.source == 'applet') {
- uni.navigateTo({
- url: this.web_view
- })
- } else {
- if (!go) {
- go = '';
- }
- if (go && go == 'location') {
- window.location.href = path;
- } else {
- var url = window.location.href;
- if (go.indexOf('/') != -1) {
- url = this.host + go
- } else {
- url = url + go;
- }
- url = window.btoa(url);
- if (path.indexOf('?') == -1) {
- path = path + '?';
- } else {
- path = path + '&';
- }
- window.location.href = path + 'refer=' + url;
- }
- }
- } else {
- this.debug('navigateTo:' + go + ':' + path);
- if (this.switchTab.indexOf(path) != -1) {
- uni.switchTab({
- url: '/pages/' + path,
- success: function(e) {
- if (self.source != 'app' && self.switchTabCall[path]) {
- var call = self.switchTabCall[path];
- call(self, e);
- }
- }
- })
- } else if (go) {
- uni.redirectTo({
- url: '/pages/' + path
- })
- } else {
- uni.navigateTo({
- url: '/pages/' + path
- })
- }
- }
- },
-
-
- alert : function(info, icon, callback) {
- if (!icon) {
- icon = 'none'
- }
- if (info) {
- uni.showToast({title: info, icon: icon});
- }
- this.debug('alert:' + info)
- if (callback) {
- this.debug('callback:' + callback)
- setTimeout(function(){
- callback();
- }, 1000)
- }
- },
-
-
- debug : function(string) {
- if (config.base.debug) {
- this.log(string);
- }
- },
-
-
- log : function(string) {
- console.log(string);
- },
-
-
- getToken: function() {
- return http.getToken();
- },
-
-
- setToken: function(value) {
- return http.setToken(value);
- },
-
-
- post : function(url, options, callback, err_callback) {
- http.request('post', url, options, callback, err_callback);
- },
-
-
- get : function(vue, url, options, callback, err_callback) {
- http.request(vue, url, options, callback, err_callback);
- },
-
-
- page : function(config, vue, url, options, callback, err_callback) {
- page.get(config, vue, url, options, callback, err_callback);
- },
-
- upload : function(id, count, callback) {
- upload.handle(id, count, callback);
- },
-
-
- qiniu : function(qn, self, id, count, callback) {
- upload.qiniu(qn, self, id, count, callback);
- },
-
-
- upfile : function(self, key, count, callback, ext, title, style) {
- upload.upfile(self, key, count, callback, ext, title, style);
- },
-
-
- im : function() {
- im.init();
- },
-
-
- uploadDel : function(id, index, callback) {
- upload.uploadDel(id, index, callback);
- },
-
-
- checkLogin : function() {
- if (!this.getToken()) {
-
- if (this.source == 'h5') {
- this.location(this.login);
- } else {
- this.location(this.login, 'go');
- }
- }
- },
-
-
- data : function(key, value) {
- if (value) {
- if (value == 'del') {
- uni.removeStorageSync(key);
- return true;
- } else {
- uni.setStorageSync(key, value);
- return value;
- }
- } else {
- return uni.getStorageSync(key);
- }
- },
-
-
- initHtml : function(doc) {
- this.doc = doc.$refs.initHtml.$el;
- },
-
-
- html : function(html) {
- var self = this;
- var div = document.createElement('div');
- div.innerHTML = html;
- this.doc.appendChild(div);
- var scripts = div.querySelectorAll('script');
- return Array.prototype.slice.apply(scripts).reduce((chain, script) => {
- return chain.then(() => self.runScript(script));
- }, Promise.resolve());
- },
-
-
- wxTip : function() {
- var wx = this.is_weixin();
- if (wx) {
- var tip = '<div id="weixin-tip" style="position: fixed; left:0; top:0; background: rgba(0,0,0,0.8); filter:alpha(opacity=80); width: 100%; height:100%; z-index: 100;" onclick="document.getElementById(\'weixin-tip\').remove()"><p style="text-align: center; margin-top: 10%; padding:0 5%;"><img src="static/dever/live_weixin.png" alt="微信打开" style="max-width: 100%; height: auto;"/></p></div>';
- this.html(tip);
- return true;
- }
- return false;
- },
-
- is_weixin: function() {
- if (this.source != 'h5') {
- return false;
- }
- var ua = navigator.userAgent.toLowerCase();
- if(ua.match(/MicroMessenger/i)=="micromessenger") {
- return true;
- } else {
- return false;
- }
- },
-
-
- runScript : function(script) {
- return new Promise((reslove, rejected) => {
- const newScript = document.createElement('script');
- newScript.innerHTML = script.innerHTML;
- const src = script.getAttribute('src');
- if (src) newScript.setAttribute('src', src);
-
- newScript.onload = () => reslove();
- newScript.onerror = err => rejected();
- document.head.appendChild(newScript);
- document.head.removeChild(newScript);
- if (!src) {
-
- reslove();
- }
- })
- },
-
-
- appPayment : function(type, order, callback, errorCallback) {
- uni.requestPayment({
- provider: type,
- orderInfo: order,
- success: function (res) {
-
- callback(res);
- },
- fail: function (err) {
-
- errorCallback(err)
- }
- });
- },
-
-
- jump : function() {
- var refer = this.data('login_refer');
- var id = this.data('invite_id');
- var type = this.data('invite_type');
- if (id && refer != 'index/index') {
- refer = refer + '?id=' + id + '&type=' + type;
- }
- this.data('login_refer', 'del');
- this.data('invite_id', 'del');
- this.data('invite_type', 'del');
- if (!refer) {
- refer = 'index/index';
- }
- this.location(refer, 'go');
- },
-
- xing : function(s) {
- return s.replace(s, function(sMatch){
- return sMatch.replace(/./g,"*");
- });
- },
-
-
- checkUpdate : function() {
- this.update();
- },
- update : function(path, call) {
- var self = this;
- var source = this.source;
- if (source != 'app') {
- return;
- }
- var type = plus.os.name.toLowerCase();
-
-
-
- self.post('isUpdate', {t:1}, function(res) {
- if (type == 'android') {
- res.downloadUrl = res.androidUrl;
- } else {
- res.downloadUrl = res.iosUrl;
- }
- self.data('dever_update_link', res.downloadUrl);
- if (plus.runtime.versionCode < res.versionCode) {
-
- var txt = '正在为您下载更新,下载完成将重启应用';
- if (res.forceUpdate == true) {
- self.down(res, res.downloadUrl, txt, type);
- } else if(call) {
- call(res, txt, type);
- } else {
- uni.showModal({
- title: '发现新版本',
- content: '有新版本可用 (版本号:' + res.versionName + '),请问您是否更新?',
- success: (t) => {
- if (t.confirm) {
- if (path) {
- self.location(path);
- } else {
- self.down(res, res.downloadUrl, txt, type);
- }
- }
- }
- })
- }
- }
- });
- },
- uploadCall : function(config, type, packgePath) {
-
- var self = this;
- self.data('dever_update', 1);
- self.data('dever_update_down', packgePath);
-
- plus.runtime.install(packgePath, {force: true}, function() {
- self.data('dever_update', 2);
- self.data('dever_update_down', 'del');
- uni.showModal({
- title: '提示',
- content: '应用将重启以完成更新',
- showCancel: false,
- complete: () => {
- plus.runtime.restart();
- }
- })
- });
- },
- down : function(config, url, txt, type, call) {
- if (!url) {
- return false;
- }
- if (txt) {
- this.alert(txt);
- }
- if (type != 'android' && !url.match(RegExp(/.wgt/))) {
- plus.runtime.openURL(url);
- return false;
- }
- var self = this;
- var packgePath = self.data('dever_update_down');
- if (packgePath) {
- self.uploadCall(config, type, packgePath);
- return false;
- }
- var downloadTask = uni.downloadFile({
- url: url,
- success: (res) => {
- if (res.statusCode === 200) {
-
- uni.saveFile({
- tempFilePath: res.tempFilePath,
- success: (res) => {
- var packgePath = res.savedFilePath;
- if (call) {
- call();
- }
- self.uploadCall(config, type, packgePath);
- downloadTask.abort();
- downloadTask = null;
- }
- })
- }
- }
- });
- return downloadTask;
- },
-
-
- quit : function(page, call) {
- var self = this;
- if (self.source == 'app') {
- var main = plus.android.runtimeMainActivity();
-
-
-
- plus.nativeUI.toast = (function(str){
- if(str == '再按一次退出应用') {
- if (call) {
- call(main);
- } else {
- var webview = page.$mp.page.$getAppWebview();
- var child = webview.children();
- child[0].back();
- }
-
- return false;
- }else{
- self.alert(str);
- }
- });
- }
- }
- }
- module.exports = dever
|