1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984 |
- //封装好一系列常用的方法 dever
- import config from './config';
- //http网络请求
- var http = {
- //token的key
- token : 'signature',
- //获取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, type) {
- var self = this;
- if (vue == 'post') {
- config.request.method = 'POST';
- //config.request.method = 'GET';
- vue = false;
- } else {
- config.request.method = 'GET';
- }
- var method = config.request.method;
- if (typeof(url) == 'object') {
- vue = url[1];
- url = url[0];
- }
- if (typeof(vue) == 'object') {
- vue.isDisabled = true;
- }
-
- self.core(url, options, type, method).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, type, method) {
- 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) {
- if (type == 'im') {
- config.request.url = config.im.api + config.request.url;
- } else {
- 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;
- if (page.key) {
- if (options && options.concat && options.concat == -1) {
- response.data.data[page.key] = page.data;
- resolve(response.data.data, false);
- } else {
- resolve(response.data.data[page.key], false);
- }
- } else {
- if (options && options.concat && options.concat == -1) {
- response.data.data = page.data;
- }
- 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 if (response.data.code == '-2') {
- //跳转到登录页面
- self.setToken('');
- dever.checkLogin();
- /*
- 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 : {},
- status : false,
- //host
- url : 'ws://me.5dev.cn:8282',
- //推送路径
- push_path : '',
- init : function(key, url, callback, connect) {
- if (url) {
- this.url = url;
- } else {
- this.url = config.im.host;
- }
- this.connect(key, callback, connect);
- },
-
- connect : function(key, callback, connect) {
- var self = this;
- this.close(key);
- this.socket[key] = uni.connectSocket({
- url: this.url,
- complete: (res)=> {
-
- }
- });
- this.socket[key].onOpen(function (res) {
- if (connect) {
- connect(true, res);
- }
- console.info('open');
- self.status = true;
-
- self.socket[key].onMessage(function (res) {
- if (res.data.indexOf('{') != -1) {
- var data = eval("("+res.data+")");
- } else {
- var data = res.data;
- }
-
- if (callback) {
- callback(data);
- }
- });
- });
- this.socket[key].onClose(function (res) {
- if (connect) {
- connect(false, res);
- }
- console.info('close');
- self.status = false;
- });
-
- this.socket[key].onError(function (res) {
- console.info('error');
- self.connect(callback, connect);
- });
- },
-
- close : function(key) {
- var self = this;
- if (this.socket[key]) {
- this.socket[key].close({
- success(res) {
- self.status = false;
- },
- fail(err) {
- self.status = true;
- }
- })
- }
- },
-
- get : function(vue, url, options, callback, err_callback) {
- this.request(vue, url, options, callback, err_callback);
- },
-
- post : function(url, options, callback, err_callback) {
- this.request('post', url, options, callback, err_callback);
- },
-
- request : function(vue, url, options, callback, err_callback) {
- if (!options) {
- options = {};
- }
- var param = options;
- param.noloading = 1;
- dever.post(this.push_path, param, function(t) {
- url += '?timestamp=' + t.timestamp + '&pop_token=' + t.token + '&login_uid=' + t.login_uid;
- dever.config.im = t;
- options.login_uid = t.login_uid;
- if (t.friend_uid) {
- options.friend_uid = t.friend_uid;
- }
- delete options.noloading;
- http.request(vue, url, options, callback, err_callback, 'im');
- });
- }
- }
- 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) {
- 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) {
- //请求上传接口
- self.uploadFile(item, key, count, callback);
- });
- },
- fail: () => {
- uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
- uni.hideLoading();
- }
- });
- },
-
- uploadFile : function(item, key, count, callback) {
- var self = this;
- if (!count) {
- count = 1;
- }
- var type = 1;
- if (count > 1) {
- type = 2;
- }
- 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' });
- }
- }
- });
- },
-
- 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 share =
- {
- wechat : false,
- url : '',
- project : '',
- token : '',
- param : {
- title : '',
- pic : '',
- content : '',
- link : '',
- },
- wx : {},
- init : function(wx, project, host, param)
- {
- var self = this;
- self.wx = wx;
- self.wechat = dever.is_weixin();
- self.project = project;
- self.url = host;
- if (!host) {
- self.url = config.share.host;
- }
-
- self.token = http.getToken();
- self.param = param;
- self.param.timelineCallback = function() {};
- self.param.sendMessageCallback = function() {};
- self.param.shareQQCallback = function() {};
- if (!self.param.source_link) {
- self.param.source_link = location.href;
- }
- if (self.wechat) {
- self.wechatInit();
- }
- self.reflux();
- }
- ,wechatInit: function()
- {
- var self = this;
- var url = this.url + 'init?callback=?';
- var param = this.param;
- var project = this.project;
- var options = {};
- options.project = project;
- options.url = location.href.split('#')[0];
- options.noloading = 1;
- options.token = this.token;
- http.request('post', url, options, function(data) {
- self.wx.config({
- //debug:true,
- appId: data.appId,
- timestamp: data.timestamp,
- nonceStr: data.nonceStr,
- signature: data.signature,
- jsApiList: [
- 'onMenuShareTimeline',
- 'onMenuShareAppMessage',
- 'onMenuShareQQ',
- 'updateAppMessageShareData',
- 'updateTimelineShareData'
- ]
- });
-
- self.wx.ready(function() {
- self.wx.updateAppMessageShareData({
- title: param.title,
- desc: param.content,
- link: param.source_link,
- imgUrl: param.pic,
- success: function(res) {
-
- },
- });
- self.wx.updateTimelineShareData({
- title: param.title,
- link: param.source_link,
- imgUrl: param.img,
- success: function(res) {
-
- },
- });
- self.wx.onMenuShareAppMessage({
- title: param.title,
- desc: param.content,
- link: param.source_link,
- imgUrl: param.pic,
- success: function(res) {
- self.shareLog(1, 1);
- },
- cancel: function(res) {
- self.shareLog(1, 2);
- },
- fail: function(res) {
- self.shareLog(1, 3);
- }
- });
- self.wx.onMenuShareTimeline({
- title: param.title,
- link: param.source_link,
- imgUrl: param.img,
- success: function(res) {
- self.shareLog(2, 1);
- },
- cancel: function(res) {
- self.shareLog(2, 2);
- },
- fail: function(res) {
- self.shareLog(2, 3);
- }
- });
- self.wx.onMenuShareQQ({
- title: param.title,
- link: param.source_link,
- imgUrl: param.img,
- success: function(res) {
- self.shareLog(3, 1);
- },
- cancel: function(res) {
- self.shareLog(3, 2);
- },
- fail: function(res) {
- self.shareLog(3, 3);
- }
- });
- });
- });
- }
- ,reflux: function() {
- var refer = document.referrer,
- url = encodeURIComponent(document.location.href),
- param = location.search.substr(1),
- ua = encodeURIComponent(navigator.userAgent),
- project = this.project,
- token = this.token,
- wechat = this.wechat;
- if (param.indexOf("tsina-") > -1 || param.indexOf("timeline") > -1 || param.indexOf("singlemessage") > -1 || param.indexOf("groupmessage") > -1) {
- var url = this.url + 'reflux&callback=?' + '&project='+project+'&url=' + url + '&ua=' + ua + '¶m=' + encodeURIComponent(param) + '&token=' + token + '&type=' + wechat;
- http.request('post', url, {noloading:1});
- }
- }
- ,shareLog: function(actType, actResult) {
- var project = this.project;
- var token = this.token;
- var wechat = this.wechat;
- var ua = encodeURIComponent(navigator.userAgent);
- var url = encodeURIComponent(document.location.href);
- var url = this.url + 'collect&callback=?' + '&project='+project+'&url=' + url + '&actType=' + actType + '&actResult=' + actResult + '&ua=' + ua + '&token=' + token + '&type=' + wechat
- http.request('post', url, {noloading:1});
- }
- }
- var bgm = {
- show : false,
- playing : false,
- manager : false,
- control : false,
- dever : false,
- vue : false,
-
- init : function(vue, music, title, signer, pic, autoplay) {
- if (this.manager) {
- return;
- }
- this.vue = vue;
- this.vue.bgm = {
- show : false,
- playing : false,
- };
- if (music) {
- this.show = true;
- this.vue.bgm.show = true;
- if (this.dever.source == 'h5') {
- this.manager = uni.createInnerAudioContext()
- this.manager.src = music;
- this.manager.obeyMuteSwitch = false;
- this.manager.loop = true;
- } else {
- this.manager = uni.getBackgroundAudioManager()
- this.manager.title = title;
- this.manager.singer = signer;
- this.manager.coverImgUrl = pic;
- this.manager.src = music;
- this.manager.loop = true;
- }
-
- if (autoplay == 1) {
- if (this.dever.source == 'h5') {
- this.manager.autoplay = true;
- this.start();
- } else {
- this.start();
- }
- } else {
- this.stop();
- }
- } else {
- this.show = false;
- this.vue.bgm.show = false;
- }
- },
-
- control : function() {
- if (this.playing) {
- this.stop();
- } else {
- this.start();
- }
- },
-
- start : function() {
- if (this.manager) {
- if (this.vue.control) {
- var i = 0;
- for (i in this.vue.control) {
- if (this.vue.control[i].load) {
- this.vue.control[i].stop();
- }
- }
- }
- this.manager.play();
- this.playing = true;
- this.vue.bgm.playing = true;
- }
- },
-
- stop : function() {
- if (this.manager) {
- this.manager.pause();
- this.playing = false;
- this.vue.bgm.playing = false;
- }
- }
- };
- var dever = {
- login : 'user/login',
- web_view : '/lib/dever/pages/web_view',
- source : 'h5',
- source_id : 1,
- host : '',
- api_host : '',
- doc : false,
- config : {},
- vue : false,
- cur : false,
- getLoadingState : {},
- getLoadingCall : false,
- postLoadingCall : false,
- pageLoadingCall : false,
- pageData : {},
- lastGetUrl : '',
- showLoad : false,
- btnText : '确定提交',
- switchTab : [],
- switchTabCall : {},
- slideValue : {},
-
- //获取当前时间戳
- 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;
- this.api_host = config.request.host;
- var dever_config = self.data('dever_config');
- var state = false;
- if (dever_config && !set) {
- self.config = dever_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) {
- this.setConfig(url, value);
- }
-
- return this;
- },
- //设置全局变量
- setConfig : function(url, config, key, value) {
- var self = this;
- if (!config && key && value) {
- config = self.data('dever_config');
- config[key] = value;
- }
- self.config = config;
- self.vue.prototype.$config = self.config;
- self.data('dever_config', self.config);
- if (url) {
- self.post(url, {noloading:1}, function(t) {
- self.config = t;
- self.config.update_time = self.curTime();
- self.data('dever_config', self.config);
- self.vue.prototype.$config = self.config;
- });
- }
- },
- //加载通用的方法
- common : function(func) {
- this.source = 'h5';
- this.source_id = 1;
- this.host = '';
- //#ifdef H5
- this.source = 'h5';
- this.source_id = 1;
- this.host = window.location.protocol + '//' + window.location.host + '/web/#';
- //#endif
- //#ifdef APP-PLUS
- this.source = uni.getSystemInfoSync().platform;
- if (this.source == 'ios') {
- this.source_id = 3;
- } else {
- this.source_id = 2;
- }
- //#endif
- //#ifdef MP-WEIXIN
- this.source = 'wx_applet';
- this.source_id = 5;
- //#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;
- },
- //获取当前param
- param : function() {
- var page = this.getPage();
- if (!page) {
- return '';
- }
- var options = page.options;
- return options;
- },
- //获取当前url
- url : function() {
- var page = this.getPage();
- if (!page) {
- return '';
- }
- var route = page.route;
- var options = page.options;
-
-
- // 拼接参数
- let param = []
- if (options) {
- for (let key in options) {
- param.push(key + '=' + options[key]);
- }
- }
- param = param.join('&');
- if (param) {
- param = '?' + param;
- }
- return '/' + route + param;
- },
- //获取当前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) {
- var title = '加载中';
- if (method == 'POST') {
- title = '提交中';
- }
- uni.showLoading({title: 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, title) {
- var self = this;
- if (path.indexOf('http') > -1) {
- this.debug('http:' + path);
- this.data('web_view', path);
- if (title) {
- this.data('web_view_title', title);
- }
- 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 (path.indexOf('/pages/') == -1) {
- path = '/pages/' + path;
- }
- if (this.switchTab.indexOf(path) != -1) {
- uni.switchTab({
- url: path,
- success: function(e) {
- if (self.source != 'app' && self.switchTabCall[path]) {
- var call = self.switchTabCall[path];
- call(self, e);
- }
- }
- })
- } else if (go && go != 'webview') {
- uni.redirectTo({
- url: path
- })
- } else {
- uni.navigateTo({
- url: 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)
- }
- },
-
- //询问信息
- confirm : function(text, callback) {
- uni.showModal({
- content: text,
- cancelText: '取消',
- confirmText: '确定',
- success: res => {
- if (res.confirm) {
- callback();
- }
- }
- });
- },
-
- //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);
- },
-
- //html上传文件
- upfile : function(self, key, count, callback, ext, title, style) {
- this.getUpload().upfile(self, key, count, callback, ext, title, style);
- },
-
- //直接上传文件
- uploadFile : function(path, id, count, callback) {
- this.getUpload().uploadFile(path, id, count, callback);
- },
-
- //获取upload
- getUpload : function() {
- return upload;
- },
-
- //im聊天
- im : function(key, url, callback, connect) {
- im.init(key, url, callback, connect);
- },
-
- //im聊天关闭
- im_close : function(key) {
- im.close(key);
- },
-
- //im聊天 get请求
- im_get : function(vue, url, options, callback, err_callback) {
- im.get(vue, url, options, callback, err_callback);
- },
-
- //im聊天 post请求
- im_post : function(url, options, callback, err_callback) {
- im.post(url, options, callback, err_callback);
- },
-
- //删除上传
- uploadDel : function(id, index, callback) {
- upload.uploadDel(id, index, callback);
- },
-
- //验证登录
- checkLogin : function(refer) {
- if (!this.getToken()) {
- this.goLogin(refer);
- }
- },
-
- //去登录
- goLogin : function(refer) {
- this.data('login_refer', this.getRefer(refer));
- //this.hideLoading();
- if (this.source == 'h5') {
- this.location(this.login, 'go');
- } else {
- this.location(this.login, 'go');
- }
- },
-
- //获取refer
- getRefer : function(refer) {
- if (!refer) {
- refer = this.url();
- }
- return refer;
- },
-
- //数据存储
- 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(type) {
- var wx = this.is_weixin();
- if (wx) {
- var img = 'live_weixin';
- if (type) {
- img = 'weixin-guide';
- }
- 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/'+img+'.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();
- }
- })
- },
-
- //拉起支付
- pay : function(result, location, error) {
- var self = this;
- if (!error) {
- if (location.indexOf('?') == -1) {
- var fix = '?';
- } else {
- var fix = '&';
- }
- location = location + fix + 'order_id=' + result.order_id;
- }
-
- if (result.type == 'test') {
- self.alert('支付成功','none', function() {
- self.location(location, 'to');
- })
- } else if (self.source == 'h5') {
- uni.hideLoading();
- uni.showLoading({title: '跳转中', mask: true});
- self.html(result.order);
- } else if (self.source == 'app') {
- self.appPayment(result.type, result.order, function() {
- self.location(location, 'to');
- }, function() {
- if (!error) {
- self.location(location + '&error=1', 'to');
- } else {
- self.alert(error);
- }
- });
- }
- },
-
- //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;
- }
- // 检测图片是否原图
- img = this.getImg(img);
- if (imgs.length > 0) {
- for (i in imgs) {
- imgs[i] = this.getImg(imgs[i]);
- }
- }
- 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);
- }
- }
- */
-
- });
- }
- },
-
- //获取原图
- getImg : function(img) {
- if (img.indexOf('?') != -1) {
- var t = img.split('?');
- img = t[0];
- } else if (img.indexOf('_t') != -1) {
- var t = img.split('.');
- var ext = t[1];
- t = img.split('_t');
- img = t[0] + '.' + ext;
- }
-
- return img;
- },
-
- //截取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) {
- dever.slideValue = {};
- if (e.changedTouches[0].clientX) {
- dever.slideValue.clientX = e.changedTouches[0].clientX;
- dever.slideValue.clientY = e.changedTouches[0].clientY;
- } else {
- dever.slideValue.clientX = e.changedTouches[0].screenX;
- dever.slideValue.clientY = e.changedTouches[0].screenY;
- }
-
- },
-
- //滑动结束
- slideEnd : function(e) {
- if (e.changedTouches[0].clientX) {
- var subX = e.changedTouches[0].clientX - dever.slideValue.clientX;
- var subY = e.changedTouches[0].clientY - dever.slideValue.clientY;
- } else {
- var subX = e.changedTouches[0].screenX - dever.slideValue.clientX;
- var subY = e.changedTouches[0].screenY - dever.slideValue.clientY;
- }
-
- if (subY > 20) {
- //上滑
- return 1;
- } else if (subY < -20) {
- //下滑
- return 2;
- } else if (subX > 20) {
- //左滑
- return 3;
- } else if(subX < -20) {
- //右滑
- return 4;
- }
- return
- },
-
- //计算图片宽高比
- getImage : function(imgWidth, imgHeight, containerWidth, containerHeight) {
- let [
- // 用于设定图片的宽和高
- tempWidth,
- tempHeight,
- ] = [
- undefined,
- undefined
- ]
- try {
- imgWidth = parseFloat(imgWidth)
- imgHeight = parseFloat(imgHeight)
- containerWidth = parseFloat(containerWidth)
- containerHeight = parseFloat(containerHeight)
- } catch (error) {
- throw new Error('抱歉,我只接收数值类型或者可以转成数值类型的参数')
- }
-
- if (imgWidth > 0 && imgHeight > 0) {
- //原图片宽高比例 大于 指定的宽高比例,这就说明了原图片的宽度必然 > 高度
- if (imgWidth / imgHeight >= containerWidth / containerHeight) {
- if (imgWidth > containerWidth) {
- // alert('aaaaaaaa')
- tempWidth = containerWidth
- // 按原图片的比例进行缩放
- tempHeight = (imgHeight * containerWidth) / imgWidth
- } else {
- // 按照图片的大小进行缩放
- tempWidth = imgWidth
- tempHeight = imgHeight
- }
- } else { // 原图片的高度必然 > 宽度
- if (imgHeight > containerHeight) {
-
- tempHeight = containerHeight
- // 按原图片的比例进行缩放
- tempWidth = (imgWidth * containerHeight) / imgHeight
- } else {
- // 按原图片的大小进行缩放
- tempWidth = imgWidth
- tempHeight = imgHeight
- }
- }
- }
-
- return [tempWidth, tempHeight]
- },
-
- sleep : function(time) {
- return new Promise((resolve) => setTimeout(resolve, time));
- },
-
- //分享功能初始化
- shareInit : function(wx, project, host, data) {
- share.init(wx, project, host, data);
- },
-
- //通用的分享功能
- share : function(provider, scene, type, data, success, error) {
- if (this.source == 'h5') {
- var weixin = this.wxTip(1);
- if (!weixin) {
- this.alert('请点击复制按钮');
- }
- } else {
- var config = {
- provider: provider,
- type: type,
- scene: scene,
- href: data.link,
- title: data.title,
- summary: data.content,
- imageUrl: data.pic,
- success: function(res) {
- if (success) {
- success(res);
- } else {
- console.log("success:" + JSON.stringify(res));
- }
- },
- fail: function(err) {
- if (error) {
- success(err);
- } else {
- console.log("fail:" + JSON.stringify(err));
- }
- }
- };
- if (data.media) {
- config.mediaUrl = media;
- }
- if (this.source == 'applet') {
- if (!data.path) {
- console.log('data.path未定义!');
- return;
- }
- if (!data.applet_id) {
- console.log('data.applet_id未定义!');
- return;
- }
- config.miniProgram = {};
- config.miniProgram.id = data.applet_id;
- config.miniProgram.path = data.path;
- config.miniProgram.type = 0;
- config.miniProgram.webUrl = data.link;
- }
- uni.share(config);
- }
- },
-
- // 获取地理位置
- getLocation : function(type, text, success) {
- if (!type) {
- type = 'wgs84';
- }
- if (!text) {
- text = '需要获得您的位置,请您授权';
- }
- var self = this;
- if (self.source != 'h5') {
- uni.getSetting({
- success(res) {
- if (res.authSetting['scope.userLocation']) {
- self.getAltitude(type, success);
- } else {
- uni.authorize({
- scope: 'scope.userLocation',
- success(res) {
- self.getAltitude(type, success);
- },
- // 授权失败
- fail(err) {
- uni.showModal({
- title: '提示',
- content: text,
- showCancel: false,
- confirmText: '确认授权',
- success() {
- uni.openSetting({
- success(res) {
- self.getAltitude(type, success);
- },
- fail(err) {
- self.alert('授权失败');
- }
- })
- }
- })
- }
- })
- }
- }
- })
- } else {
- self.getAltitude(type, success);
- }
- },
-
- getAltitude : function(type, success) {
- uni.getLocation({
- type: type,
- altitude: true,
- success(res) {
- if (success) {
- success(res);
- } else {
- console.info(res);
- }
- }
- })
- },
-
- //bgm
- bgm : function() {
- bgm.dever = this;
- return bgm;
- }
- }
- module.exports = dever
|