微信小程序(配置):
config.js
token.js:
// 引用使用es6的module引入和定义
// 全局变量以g_开头
// 私有函数以_开头
import { Config } from 'config.js';
class Token {
constructor() {
this.tokenUrl = Config.restUrl + 'token/user';
this.verifyUrl = Config.restUrl + 'token/verify';
}
verify() {
var token = wx.getStorageSync('token');
if (!token) {
this.getTokenFromServer();
}
else {
this._veirfyFromServer(token);
}
}
_veirfyFromServer(token) {
var that = this;
wx.request({
url: that.verifyUrl,
method: 'POST',
data: {
token: token
},
success: function (res) {
var valid = res.data.isValid;
if(!valid){
that.getTokenFromServer();
}
}
})
}
getTokenFromServer(callBack) {
var that = this;
wx.login({
success: function (res) {
wx.request({
url: that.tokenUrl,
method:'POST',
data:{
code:res.code
},
success:function(res){
wx.setStorageSync('token', res.data.token);
callBack&&callBack(res.data.token);
}
})
}
})
}
}
export {Token};
Base:
import { Token } from 'token.js';
import { Config } from 'config.js';
class Base {
constructor() {
"use strict";
this.baseRestUrl = Config.restUrl;
this.onPay=Config.onPay;
}
//http 请求类, 当noRefech为true时,不做未授权重试机制
request(params, noRefetch) {
var that = this,
url=this.baseRestUrl + params.url;
if(!params.type){
params.type='get';
}
/*不需要再次组装地址*/
if(params.setUpUrl==false){
url = params.url;
}
wx.request({
url: url,
data: params.data,
method:params.type,
header: {
'content-type': 'application/json',
'token': wx.getStorageSync('token')
},
success: function (res) {
// 判断以2(2xx)开头的状态码为正确
// 异常不要返回到回调中,就在request中处理,记录日志并showToast一个统一的错误即可
var code = res.statusCode.toString();
var startChar = code.charAt(0);
if (startChar == '2') {
params.sCallBack && params.sCallBack(res.data);
} else {
if (code == '401') {
if (!noRefetch) {
that._refetch(params);
}
}
that._processError(res);
params.eCallBack && params.eCallBack(res.data);
}
},
fail: function (err) {
//wx.hideNavigationBarLoading();
that._processError(err);
// params.eCallback && params.eCallback(err);
}
});
}
_processError(err){
console.log(err);
}
_refetch(param) {
var token = new Token();
token.getTokenFromServer((token) => {
this.request(param, true);
});
}
/*获得元素上的绑定的值*/
getDataSet(event, key) {
return event.currentTarget.dataset[key];
};
};
export {Base};
home_model.js:
import { Base } from '../../utils/base.js';
class Home extends Base{
//定义构造函数
constructor(){
super();
}
getBannerData(id, callBack){
var params = {
url:'banner/' +id,
sCallBack:function(res){
callBack && callBack(res.items);
}
}
this.request(params);
}
//主题加载
getThemeData(callBack){
var param = {
url:'theme?ids=1,2,3',
sCallBack: function(data){
callBack && callBack(data);
}
}
this.request(param);
}
//三个样式加载
getProductsData(callBack){
var param = {
url:'product/recent',
sCallBack: function(data){
callBack && callBack(data);
}
}
this.request(param);
}
}
//输出出去
export{Home};
home.js:
// pages/home/home.js
// 实例化变量
import {Home} from 'home-model.js';
var home = new Home();
Page({
/**
* 页面的初始数据
*/
data: {
},
//主页显示时,生命周期
onLoad:function(){
this._loadData();
},
_loadData:function(){
var id = 1;
home.getBannerData(id,(res)=>{
//数据绑定
this.setData({
'bannerArr':res
});
});
//主题绑定
home.getThemeData((res)=>{
// console.log(res);
this.setData({
'themeArr':res
})
});
//三个样式的显示
home.getProductsData((data)=>{
// console.log(data);
this.setData({
'productsArr':data
})
});
},
//轮播图显示的=获取id
onProductsItemTap: function(event){
// console.log(event);
// var id = event.currentTarget.dataset.id;
var id = home.getDataSet(event,'id');
wx.navigateTo({
url: '../product/product?id='+id,
})
},
//跳转的主题的页面
onThemesItemTap: function(event){
var id = home.getDataSet(event,'id');
var name = home.getDataSet(event,'name');
wx.navigateTo({
url: '../theme/theme?id='+ id + '&name=' + name,
})
}
})