123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- //封装好一系列常用的方法 dever
- import config from './config';
- //http网络请求
- var http = {
- //token的key
- token : 'token',
- //获取token
- getToken: function() {
- return uni.getStorageSync(this.token);
- },
-
- //设置token
- setToken: function(value) {
- return uni.setStorageSync(this.token, value);
- },
-
- //设置http的option
- option: function(options) {
- if (!options) {
- options = {};
- }
- options[this.token] = this.getToken();
- return options;
- },
-
- //request请求
- 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);
- }
- }).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 (config.server[url]) {
- config.request.url = config.server[url];
- } else {
- config.request.url = url;
- }
- 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);
- uni.hideLoading()
- uni.hideNavigationBarLoading();
- if (response.statusCode === 200) {
- if (response.data.code == '0') {
- if (options.page && options.page > 0) {
- page.state = true;
- if (page.key && response.data.data[page.key].length > 0) {
- page.data = page.data.concat(response.data.data[page.key]);
- response.data.data[page.key] = page.data;
- } else if (response.data.data.length > 0) {
- page.data = page.data.concat(response.data.data);
- response.data.data = page.data;
- } else {
- page.state = false;
- resolve(response.data.data, false);
- return;
- }
- }
- resolve(response.data.data);
- } else {
- reject(response.data)
- }
- } 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)
- }
- }
- }
- }
- //uni.request(Object.assign({}, config, options));
- dever.debug(config.request);
- uni.showLoading({title: '加载中', mask: true});
- uni.showNavigationBarLoading();
- uni.stopPullDownRefresh();
- uni.request(config.request);
- })
- },
-
- error : function(err) {
- 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 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 : {},
- 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: '上传失败,请重新上传', icon: 'none' });
- uni.hideLoading();
- }
- }
- });
- });
- },
- 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) {
- self.data[key].splice(index, 1);
- if (callback) {
- callback(res, index);
- }
- }
- }
- });
- }
- }
- var dever = {
- login : 'login/login',
- web_view : '/components/dever/pages/web_view',
- //页面跳转
- location : function(path, go) {
- if (path.indexOf('http') > -1) {
- this.debug('http:' + path);
- this.data('web_view', path);
- /*
- //#ifdef APP-PLUS
- uni.navigateTo({
- url: this.web_view
- })
- //#endif
- //#ifdef MP-WEIXIN
- uni.navigateTo({
- url: this.web_view
- })
- //#endif
-
- //#ifndef H5
- window.location.href = path;
- //#endif
- */
- if (go) {
- //#ifdef APP-PLUS
- uni.navigateTo({
- url: this.web_view
- })
- //#endif
-
- //#ifdef MP-WEIXIN
- uni.navigateTo({
- url: this.web_view
- })
- //#endif
-
- if (go == 'location') {
- window.location.href = path;
- } else {
- var url = window.location.href;
- url = url.replace(go, 'paySuccess');
- url = window.btoa(url);
- window.location.href = path + '&refer=' + url + '&token=' + this.getToken();
- }
- } else {
- uni.navigateTo({
- url: this.web_view
- })
- }
- } else {
- this.debug('navigateTo:' + path);
- if (go) {
- uni.switchTab({
- url: '/pages/' + path
- })
- } else {
- uni.navigateTo({
- url: '/pages/' + path
- })
- }
- }
- },
-
- //提示信息
- alert : function(msg, icon, callback) {
- if (!icon) {
- icon = 'none'
- }
- if (msg) {
- uni.showToast({title: msg, icon: icon});
- }
- this.debug('alert:' + msg)
- 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) {
- upload.handle(id, count, callback);
- },
-
- //删除上传
- uploadDel : function(id, index, callback) {
- upload.uploadDel(id, index, callback);
- },
-
- //验证登录
- checkLogin : function() {
- if (!this.getToken()) {
- this.location(this.login);
- }
- },
- //检查更新
- checkUpdate : function(gotoUrl) {
- var version = '1.0.0';//更新版本号,每次更新需要重新更换
- http.request('post', 'isUpdate', {}, function(res){
- console.log('version5',res)
- if(version != res.info.app_version){
- if(res.info.is_update == 1){
- uni.showToast({
- title:'当前更新版本,正在为你跳转更新',
- icon:'none',
- duration:3000
- });
- setTimeout(function(){
- uni.navigateTo({
- url: gotoUrl
- });
- }, 3000);
- }
- }
- });
- },
-
- //数据存储
- data : function(key, value) {
- if (value) {
- uni.setStorageSync(key, value);
- return value;
- } else {
- return uni.getStorageSync(key);
- }
- }
- }
- module.exports = dever
|