Browse Source

[新增] mixin全局注入

[修改] http拦截
master
LAPTOP-D7TKRI82\邓 4 years ago
parent
commit
cb8607b531
  1. 11
      common/http.interceptor.js
  2. 10
      common/http.js
  3. 100
      common/mixin.js
  4. 2
      main.js
  5. 2
      manifest.json
  6. 2
      pages/index/index.vue

11
common/http.interceptor.js

@ -1,7 +1,12 @@
const install = (Vue, vm) => { const install = (Vue, vm) => {
// 请求地址
let baseUrl = vm.API.PRODURL;
if(vm.API.DEV == 'dev'){
baseUrl = vm.API.DEVURL;
};
// 请求全局配置 // 请求全局配置
Vue.prototype.$u.http.setConfig({ Vue.prototype.$u.http.setConfig({
baseUrl: vm.API.DEVURL,
baseUrl: baseUrl,
method: 'POST', method: 'POST',
dataType: 'json', // 返回数据时自动 JSON.parse() dataType: 'json', // 返回数据时自动 JSON.parse()
showLoading: true, // 是否显示请求中的loading showLoading: true, // 是否显示请求中的loading
@ -9,14 +14,14 @@ const install = (Vue, vm) => {
loadingTime: 800 // 延迟800毫秒时显示加载框 loadingTime: 800 // 延迟800毫秒时显示加载框
}); });
// 请求前拦截
// 请求前拦截, 现在不做拦截
Vue.prototype.$u.http.interceptor.request = config => { Vue.prototype.$u.http.interceptor.request = config => {
return true; return true;
}; };
// 响应拦截 // 响应拦截
Vue.prototype.$u.http.interceptor.response = res => { Vue.prototype.$u.http.interceptor.response = res => {
if(res.code == 200) {
if(res.code == 0) {
return res.result; return res.result;
} else if(res.code == 201) { } else if(res.code == 201) {
console.log("其他状态:", res.code) console.log("其他状态:", res.code)

10
common/http.js

@ -24,7 +24,6 @@ function getsign(params) {
return params; return params;
} }
// TODO this获取
function $http(url, data = {}){ function $http(url, data = {}){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// 绑定this // 绑定this
@ -40,10 +39,13 @@ function $http(url, data = {}){
if(!data.rand){ if(!data.rand){
data.rand = Math.round(Math.random() * 1000000); data.rand = Math.round(Math.random() * 1000000);
} }
// 当前请求来自的平台 TODO 动态赋值
// 当前请求来自的平台
data.platform = 'wxmini'; data.platform = 'wxmini';
// 当前小程序版本号 TODO 实时获取
data.version = '1.0.0';
// #ifdef APP-PLUS
data.platform = 'app'; // 来自app平台,其他平台写法类似
// #endif
// 当前小程序版本号
data.version = that.API.VERSION;
// 获取设备唯一deviceId,如果没有,自动生成一个 // 获取设备唯一deviceId,如果没有,自动生成一个
let sysInfo = uni.getSystemInfoSync(); let sysInfo = uni.getSystemInfoSync();
if(typeof sysInfo.deviceId == 'undefined'){ if(typeof sysInfo.deviceId == 'undefined'){

100
common/mixin.js

@ -0,0 +1,100 @@
export default{
data(){
return {
// 返回顶部
needToTop: false
}
},
onLoad(option){
},
onPageScroll(res) {
if(res.scrollTop > 1000){
this.needToTop = true;
}
this.needToTop = false;
},
// 页面转发分享
async onShareAppMessage(res) {
},
methods: {
$check(str, type) {
switch (type) {
case 'mobile': //手机号码
return /^1[3|4|5|6|7|8|9][0-9]{9}$/.test(str);
case 'tel': //座机
return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
case 'card': //身份证
return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str);
case 'mobileCode': //6位数字验证码
return /^[0-9]{6}$/.test(str)
case 'pwd': //密码以字母开头,长度在6~18之间,只能包含字母、数字和下划线
return /^([a-zA-Z0-9_]){6,20}$/.test(str)
case 'payPwd': //支付密码 6位纯数字
return /^[0-9]{6}$/.test(str)
case 'postal': //邮政编码
return /[1-9]\d{5}(?!\d)/.test(str);
case 'QQ': //QQ号
return /^[1-9][0-9]{4,9}$/.test(str);
case 'email': //邮箱
return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
case 'money': //金额(小数点2位)
return /^\d*(?:\.\d{0,2})?$/.test(str);
case 'URL': //网址
return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(str)
case 'IP': //IP
return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(str);
case 'date': //日期时间
return /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(str) || /^(\d{4})\-(\d{2})\-(\d{2})$/
.test(str)
case 'number': //数字
return /^[0-9]$/.test(str);
case 'english': //英文
return /^[a-zA-Z]+$/.test(str);
case 'chinese': //中文
return /^[\\u4E00-\\u9FA5]+$/.test(str);
case 'lower': //小写
return /^[a-z]+$/.test(str);
case 'upper': //大写
return /^[A-Z]+$/.test(str);
case 'HTML': //HTML标记
return /<("[^"]*"|'[^']*'|[^'">])*>/.test(str);
default:
return true;
}
},
$msg(title = '', param = {}) {
if(!title) return;
uni.showToast({
title,
duration: param.duration || 1500,
mask: param.mask || true, // 默认应该加mask 禁止提示时操作
icon: param.icon || 'none'
});
},
$url(url, options = {}){
// TODO 判断登录逻辑;防抖
if(options.type && options.type !== ''){
if(options.type === 'redirect'){ // 关闭当前,跳转
uni.redirectTo({ url })
}else if(options.type === 'switch'){ // 跳转
uni.switchTab({ url })
}else if(options.type === 'launch'){ // 关闭所有,跳转
uni.reLaunch({ url })
}
}else{
uni.navigateTo({ url }) // 跳转
}
},
$toBack(){
let pages = getCurrentPages(); // 当前页
let beforePage = pages[pages.length - 2]; // 上个页面
if(beforePage && beforePage.route){
uni.navigateBack();
}else{
uni.switchTab({url:'/pages/index/index'});
}
}
}
}

2
main.js

@ -1,10 +1,12 @@
import Vue from 'vue' import Vue from 'vue'
import App from './App' import App from './App'
import mixin from '@/common/mixin.js';
import * as API from '@/common/api.js'; import * as API from '@/common/api.js';
Vue.config.productionTip = false Vue.config.productionTip = false
App.mpType = 'app' App.mpType = 'app'
Vue.mixin(mixin);
// 将API注入全局 // 将API注入全局
Vue.prototype.API = API; Vue.prototype.API = API;

2
manifest.json

@ -1,5 +1,5 @@
{ {
"name" : "uView-demo",
"name" : "shikongwang",
"appid" : "__UNI__DE5C136", "appid" : "__UNI__DE5C136",
"description" : "", "description" : "",
"versionName" : "1.5.0", "versionName" : "1.5.0",

2
pages/index/index.vue

@ -29,6 +29,7 @@
<text v-if="tab.list.length" :class="{'loading-more-text': tab.loadingClass}">{{ tab.loadingText }}</text> <text v-if="tab.list.length" :class="{'loading-more-text': tab.loadingClass}">{{ tab.loadingText }}</text>
<my-nocontent v-else></my-nocontent> <my-nocontent v-else></my-nocontent>
</view> </view>
</view> </view>
</view> </view>
</template> </template>
@ -79,6 +80,7 @@
this.$http(this.API.API_GOODS_LIST, {data: 1}).then(res => { this.$http(this.API.API_GOODS_LIST, {data: 1}).then(res => {
console.log(res); console.log(res);
}).catch(err => err); }).catch(err => err);
console.log(this.needToTop)
}, },
methods: { methods: {
change(index) { change(index) {

Loading…
Cancel
Save