12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292 |
- //封装好一系列常用的方法 dever
- import config from './config';
- //http网络请求
- var http = {
- //token的key
- token : 'token',
- //获取token
- getToken: function() {
- return uni.getStorageSync('prev1_' + this.token);
- },
-
- //设置token
- setToken: function(value) {
- return uni.setStorageSync('prev1_' + this.token, value);
- },
-
- //设置http的option
- option: function(options) {
- if (!options) {
- options = {};
- }
- options[this.token] = this.getToken();
- options['json'] = 1;
- return options;
- },
-
- //request请求
- request: function(vue, url, options, callback, err_callback) {
- var self = this;
- if (vue == 'post') {
- config.request.method = 'POST';
- config.request.method = 'GET';
- 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.msg)
- }
- }
- })
- },
-
- core : function(url, options) {
- var self = this;
- options = this.option(options);
- if (!options.noconcat) {
- options.noconcat = false;
- }
- 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) {
- options.pg = options.page;
- loading_method = 'page';
- }
- if (config.request.url.indexOf('http') == -1) {
- 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 == '0') {
- if (response.data.page) {
- dever.pageData = response.data.page;
- } else {
- dever.pageData = {};
- }
- if (options.page && options.page > 0 && !options.noconcat) {
- 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 (page.key) {
- resolve(response.data.data[page.key], false);
- } else {
- resolve(response.data.data, 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('');
- dever.location('index/index', 'go');
- reject(response.data)
- } else {
- reject(response.data)
- }
- if (options && options.noloading) {
-
- } else {
- dever.hideLoading(loading_method, options, url);
- }
- } else {
- // 处理catch 请求,不在本页面之外处理,统一在这里处理
- 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);
- }
- }
- }
- //uni.request(Object.assign({}, config, options));
- dever.debug(config.request);
- if (options.noloading) {
- uni.stopPullDownRefresh();
- } else {
- dever.loading(loading_method, options, url);
- }
-
- uni.request(config.request);
- })
- },
-
- error : function(err) {
- //console.error("请求背拒绝" + err)
- /*
- dever.data('web_error', err);
- var path = '/components/dever/pages/web_error';
- uni.navigateTo({
- url: path
- })
- return;
- */
- switch (err) {
- case 401:
- // 错误码404的处理方式
- console.error("请求背拒绝" + err)
- break;
- case 404:
- // 错误码404的处理方式
- console.error("没有找到页面" + err)
- break;
- case 500:
- // 错误码404的处理方式
- 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
- key : '',
- //分页状态 true可以分页,false不能分页
- state : true,
-
- //通用的获取数据方法:瀑布流分页 (1,'level')
- 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 : {},
- formatDate : function(date) {
- var date = new Date(date);
- var YY = date.getFullYear() + '-';
- var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
- var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
- var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
- var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
- var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
- return YY + MM + DD +" "+hh + mm + ss;
- },
- 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] = [];
- }
- if (self.data[key].length < count) {
- 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];
- //console.info(input.files[0]);
- if (ext && ext.indexOf(type) == -1) {
- uni.showToast({ title: '您上传的文件类型错误,请上传带有' + ext + '这些后缀的文件', icon: 'none' });
- } else {
- reader.readAsDataURL(input.files[0]);
- reader.onload = function(){
- //读取完成后,数据保存在对象的result属性中
- 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);
- }
- }
-
- }
- },
-
- qnUpload : function(qn, config, key, count, callback) {
- if (!count) {
- count = 1;
- }
- var type = 1;
- if (count > 1) {
- type = 2;
- }
- var self = this;
- count = parseInt(count);
- var call = function(data) {
- 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]);
- }
- }
- uni.chooseImage({
- count: count,
- //sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
- sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
- sourceType: ['album'], //从相册选择
- success: res => {
- uni.showLoading();
- res.tempFilePaths.forEach(function(item, index) {
- self.qiniu(qn, item, config.domain, config.token, call);
- });
- },
- fail: () => {
- uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
- uni.hideLoading();
- }
- });
- },
-
- qiniu : function(qn, file, domain, token, call, handle) {
- //请求上传接口
- var config = {};
- config = {
- region: 'ECN',
- domain: domain,
- //key: 'test.mp4',
- };
- if (token.indexOf('http') == -1) {
- config.uptoken = token;
- } else {
- config.uptokenURL = token;
- }
- uni.showLoading({title: '上传中', mask: true});
- qn.upload(
- file,
- res => {
- dever.debug(res);
- var data = res;
- uni.hideLoading();
- if (data.uploaded) {
- call(data);
- } else {
- uni.showToast({title: data.error.message, icon: 'none' });
- }
- },
- error => {
- uni.showToast({title: '上传失败,请重试', icon: 'none' });
- uni.hideLoading();
- },
- config,
- res => {
- //上传进度
- if (handle) {
- handle(res.progress);
- } else {
- uni.showToast({title: '上传进度:%' + res.progress, icon: 'none' });
- }
- }
- );
- },
-
- uploadDel : function(key, index, callback) {
- var self = this;
- uni.showModal({
- content: '确定要删除吗?',
- cancelText: '取消',
- confirmText: '确定',
- success: res => {
- if (res.confirm) {
- // self.data[key].splice(index, 1);
- if (callback) {
- callback(res, index);
- }
- }
- }
- });
- }
- }
- var dever = {
- login : 'login/login?refer=-1',
- web_view : '/lib/dever/pages/web_view',
- source : 'h5',
- host : '',
- doc : false,
- config : {},
- vue : false,
- cur : false,
- getLoadingState : {},
- getLoadingCall : false,
- postLoadingCall : false,
- pageLoadingCall : false,
- pageData : {},
- lastGetUrl : '',
- showLoad : false,
- btnText : '确定提交',
- switchTab : [],
- switchTabCall : {},
- slideData : {},
-
- //获取当前时间戳
- 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 = '';
- //#ifdef H5
- this.source = 'h5';
- this.host = window.location.protocol + '//' + window.location.host + '/#';
- //#endif
- //#ifdef APP-PLUS
- this.source = 'app';
- //#endif
- //#ifdef MP-WEIXIN
- this.source = 'applet';
- //#endif
-
- 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:'加载中...',
- };
- },
- //设置loading
- 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
- route : function() {
- var page = this.getPage();
- if (!page) {
- return '';
- }
- var route = page.route;
- return route;
- },
- //获取当前page
- getPage : function() {
- var pages = getCurrentPages();
- if (pages.length > 0) {
- return pages[pages.length - 1];
- } else {
- return false;
- }
- },
- //loading
- 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});
- //uni.showNavigationBarLoading();
- this.showLoad = true;
- }
- uni.stopPullDownRefresh();
- },
- //取消loading
- 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();
- //uni.hideNavigationBarLoading();
- }
- },
- //data数据获取
- dataset : function(e) {
- if (e.currentTarget) {
- var dataset = e.currentTarget.dataset;
- } else {
- var dataset = e.target.dataset;
- }
-
- return dataset;
- },
- //view中的页面提示
- 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('敬请期待');
- }
- },
- //view中的页面跳转
- 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.indexOf('save_') != -1) {
- var v = dataset[i];
- i = i.replace('save_', '');
- this.data(i, v);
- } else 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' || go == 'webview') {
- 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调试
- debug : function(string) {
- if (config.base.debug) {
- this.log(string);
- }
- },
-
- //记录日志
- log : function(string) {
- console.log(string);
- },
-
- //获取token
- getToken: function() {
- return http.getToken();
- },
-
- //设置token
- 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) {
- this.getUpload().handle(id, count, callback);
- },
-
- //上传
- qiniu : function(qn, self, id, count, callback, handle) {
- this.getUpload().qiniu(qn, self, id, count, callback, handle);
- },
-
- //上传文件
- upfile : function(self, key, count, callback, ext, title, style) {
- this.getUpload().upfile(self, key, count, callback, ext, title, style);
- },
-
- //获取upload
- getUpload : function() {
- return upload;
- },
-
- //im聊天
- im : function() {
- im.init();
- },
-
- //删除上传
- uploadDel : function(id, index, callback) {
- upload.uploadDel(id, index, callback);
- },
-
- //验证登录
- checkLogin : function() {
- if (!this.getToken()) {
- //this.hideLoading();
- 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);
- }
- },
-
- //插入html代码,初始化
- initHtml : function(doc) {
- this.doc = doc.$refs.initHtml.$el;
- },
-
- //插入html代码
- 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;
- }
- },
-
- //执行script代码
- 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);
- // script 加载完成和错误处理
- newScript.onload = () => reslove();
- newScript.onerror = err => rejected();
- document.head.appendChild(newScript);
- document.head.removeChild(newScript);
- if (!src) {
- // 如果是 inline script 执行是同步的
- reslove();
- }
- })
- },
-
- //app支付
- appPayment : function(type, order, callback, errorCallback) {
- uni.requestPayment({
- provider: type,
- orderInfo: order, //微信、支付宝订单数据
- success: function (res) {
- //console.log('success:' + JSON.stringify(res));
- callback(res);
- },
- fail: function (err) {
- //console.log('fail:' + JSON.stringify(err));
- errorCallback(err)
- }
- });
- },
-
- //跳转到refer
- 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();
- //var type = 'android';
- //var config = this.config.version;
-
- 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);
- plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
- var version = widgetInfo.versionCode;
- var state = false;
- state = widgetInfo.versionCode < res.versionCode;
- if (state) {
- //强制更新
- 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) {
- // 保存更新记录到stroage,下次启动app时安装更新
- 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;
- },
-
- //预览图片
- viewPic : function(imgs, img, key) {
- if (imgs && imgs.length > 0) {
- if (key) {
- var temp = [];
- var i;
- for(i in imgs) {
- temp.push(imgs[i][key]);
- }
- imgs = temp;
- }
- uni.previewImage({
- current:img,
- urls: imgs,
- indicator : 'default',
- loop : true,
- /*
- longPressActions : {
- itemList: ['发送给朋友', '保存图片', '收藏'],
- success: function(data) {
- console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
- },
- fail: function(err) {
- console.log(err.errMsg);
- }
- }
- */
-
- });
- }
- },
-
- //截取APP退出功能
- quit : function(page, call) {
- var self = this;
- if (self.source == 'app') {
- var main = plus.android.runtimeMainActivity();
- //为了防止快速点按返回键导致程序退出重写quit方法改为隐藏至后台
- /*
- plus.runtime.quit = function(){
- main.moveTaskToBack(false);
- };
- */
- //重写toast方法如果内容为 ‘再按一次退出应用’ 就隐藏应用,其他正常toast
- 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);
- }
- });
- }
- },
-
- //滑动
- slide : function(e) {
- this.slideData.clientX = e.changedTouches[0].clientX;
- this.slideData.clientY = e.changedTouches[0].clientY;
- },
-
- //滑动结束
- slideEnd : function(e) {
- var subX = e.changedTouches[0].clientX - this.slideData.clientX;
- var subY = e.changedTouches[0].clientY - this.slideData.clientY;
- if (subY > 50) {
- //上滑
- return 1;
- } else if (subY < -50) {
- //下滑
- return 2;
- } else if (subX > 50) {
- //左滑
- return 3;
- } else if(subX < -50) {
- //右滑
- return 4;
- }
- return
- }
- }
- module.exports = dever
|