// app.js | |||||
App({ | |||||
onLaunch() { | |||||
// 展示本地存储能力 | |||||
const logs = wx.getStorageSync('logs') || [] | |||||
logs.unshift(Date.now()) | |||||
wx.setStorageSync('logs', logs) | |||||
// 登录 | |||||
wx.login({ | |||||
success: res => { | |||||
// 发送 res.code 到后台换取 openId, sessionKey, unionId | |||||
} | |||||
}) | |||||
}, | |||||
globalData: { | |||||
userInfo: null, | |||||
openid: "", | |||||
appid: "", | |||||
sessionKey: "", | |||||
suiteCode: "", | |||||
customerId: "", | |||||
}, | |||||
// mangerUrl: 'http://test2.hhrchina.com', | |||||
mangerUrl: "http://192.168.18.156", | |||||
// mangerUrl: "https://www.hhrchina.com", | |||||
// mangerUrlPath: "https://www.hhrchina.com", | |||||
}) |
{ | |||||
"pages":[ | |||||
"pages/index/index", | |||||
"pages/logs/logs" | |||||
], | |||||
"usingComponents": { | |||||
"tabBar": "/components/tabbar/tabbar" | |||||
}, | |||||
"window":{ | |||||
"backgroundTextStyle":"light", | |||||
"navigationBarBackgroundColor": "#fff", | |||||
"navigationBarTitleText": "Weixin", | |||||
"navigationBarTextStyle":"black" | |||||
}, | |||||
"style": "v2", | |||||
"sitemapLocation": "sitemap.json", | |||||
"useExtendedLib": { | |||||
"kbone": true, | |||||
"weui": true | |||||
} | |||||
} |
/**app.wxss**/ | |||||
.container { | |||||
width: 100%; | |||||
height: 100%; | |||||
display: flex; | |||||
flex-direction: column; | |||||
align-items: center; | |||||
justify-content: space-between; | |||||
box-sizing: border-box; | |||||
} |
// components/tabbar/tabbar.js | |||||
const app = getApp() | |||||
Component({ | |||||
/** | |||||
* 组件的属性列表 | |||||
*/ | |||||
properties: { | |||||
currentIndex: { | |||||
type: String, | |||||
value: '0', | |||||
} | |||||
}, | |||||
/** | |||||
* 组件的初始数据 | |||||
*/ | |||||
data: { | |||||
list: [ | |||||
{ | |||||
text: "对话", | |||||
iconPath: "/images/tabbar/tabbar_1.png", | |||||
selectedIconPath: "/images/tabbar/tabbar_1.png", | |||||
dot: true | |||||
}, | |||||
{ | |||||
text: "设置", | |||||
iconPath: "/images/tabbar/tabbar_2.png", | |||||
selectedIconPath: "/images/tabbar/tabbar_2.png", | |||||
badge: '99+' | |||||
} | |||||
] | |||||
}, | |||||
/** | |||||
* 组件的方法列表 | |||||
*/ | |||||
methods: { | |||||
tabChange(e) { | |||||
console.log('tab change--aa', e) | |||||
} | |||||
} | |||||
}) | |||||
{ | |||||
"component": true, | |||||
"usingComponents": { | |||||
"mp-tabbar": "/miniprogram_npm/weui-miniprogram/tabbar/tabbar" | |||||
} | |||||
} |
<view class="page"> | |||||
<mp-tabbar style="position:fixed;bottom:0;width:100%;left:0;right:0;" list="{{list}}" bindchange="tabChange"></mp-tabbar> | |||||
</view> |
/* components/tabbar/tabbar.wxss */ |
// index.js | |||||
// 获取应用实例 | |||||
const app = getApp() | |||||
Page({ | |||||
data: { | |||||
motto: 'Hello World', | |||||
userInfo: {}, | |||||
hasUserInfo: false, | |||||
canIUse: wx.canIUse('button.open-type.getUserInfo'), | |||||
canIUseGetUserProfile: false, | |||||
canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false | |||||
currentIndex: 0 | |||||
}, | |||||
// 事件处理函数 | |||||
bindViewTap() { | |||||
wx.navigateTo({ | |||||
url: '../logs/logs' | |||||
}) | |||||
}, | |||||
onLoad() { | |||||
if (wx.getUserProfile) { | |||||
this.setData({ | |||||
canIUseGetUserProfile: true | |||||
}) | |||||
} | |||||
}, | |||||
getUserProfile(e) { | |||||
// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 | |||||
wx.getUserProfile({ | |||||
desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 | |||||
success: (res) => { | |||||
console.log(res) | |||||
this.setData({ | |||||
userInfo: res.userInfo, | |||||
hasUserInfo: true | |||||
}) | |||||
} | |||||
}) | |||||
}, | |||||
getUserInfo(e) { | |||||
// 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息 | |||||
console.log(e) | |||||
this.setData({ | |||||
userInfo: e.detail.userInfo, | |||||
hasUserInfo: true | |||||
}) | |||||
} | |||||
}) |
{ | |||||
"usingComponents": { | |||||
} | |||||
} |
<!--index.wxml--> | |||||
<view class="container"> | |||||
康慧慧V8 | |||||
<tabBar currentIndex="{{currentIndex}}"></tabBar> | |||||
</view> | |||||
/**index.wxss**/ | |||||
.userinfo { | |||||
display: flex; | |||||
flex-direction: column; | |||||
align-items: center; | |||||
color: #aaa; | |||||
} | |||||
.userinfo-avatar { | |||||
overflow: hidden; | |||||
width: 128rpx; | |||||
height: 128rpx; | |||||
margin: 20rpx; | |||||
border-radius: 50%; | |||||
} | |||||
.usermotto { | |||||
margin-top: 200px; | |||||
} |
// logs.js | |||||
const util = require('../../utils/util.js') | |||||
Page({ | |||||
data: { | |||||
logs: [] | |||||
}, | |||||
onLoad() { | |||||
this.setData({ | |||||
logs: (wx.getStorageSync('logs') || []).map(log => { | |||||
return { | |||||
date: util.formatTime(new Date(log)), | |||||
timeStamp: log | |||||
} | |||||
}) | |||||
}) | |||||
} | |||||
}) |
{ | |||||
"navigationBarTitleText": "查看启动日志", | |||||
"usingComponents": {} | |||||
} |
<!--logs.wxml--> | |||||
<view class="container log-list"> | |||||
<block wx:for="{{logs}}" wx:key="timeStamp" wx:for-item="log"> | |||||
<text class="log-item">{{index + 1}}. {{log.date}}</text> | |||||
</block> | |||||
</view> |
.log-list { | |||||
display: flex; | |||||
flex-direction: column; | |||||
padding: 40rpx; | |||||
} | |||||
.log-item { | |||||
margin: 10rpx; | |||||
} |
{ | |||||
"description": "项目配置文件", | |||||
"packOptions": { | |||||
"ignore": [] | |||||
}, | |||||
"setting": { | |||||
"urlCheck": true, | |||||
"es6": true, | |||||
"enhance": false, | |||||
"postcss": true, | |||||
"preloadBackgroundData": false, | |||||
"minified": true, | |||||
"newFeature": false, | |||||
"coverView": true, | |||||
"nodeModules": false, | |||||
"autoAudits": false, | |||||
"showShadowRootInWxmlPanel": true, | |||||
"scopeDataCheck": false, | |||||
"uglifyFileName": false, | |||||
"checkInvalidKey": true, | |||||
"checkSiteMap": true, | |||||
"uploadWithSourceMap": true, | |||||
"compileHotReLoad": false, | |||||
"lazyloadPlaceholderEnable": false, | |||||
"useMultiFrameRuntime": true, | |||||
"useApiHook": true, | |||||
"useApiHostProcess": true, | |||||
"babelSetting": { | |||||
"ignore": [], | |||||
"disablePlugins": [], | |||||
"outputPath": "" | |||||
}, | |||||
"enableEngineNative": false, | |||||
"useIsolateContext": true, | |||||
"userConfirmedBundleSwitch": false, | |||||
"packNpmManually": false, | |||||
"packNpmRelationList": [], | |||||
"minifyWXSS": true, | |||||
"showES6CompileOption": false | |||||
}, | |||||
"compileType": "miniprogram", | |||||
"libVersion": "2.18.1", | |||||
"appid": "wx2fb79bf1a0f4a450", | |||||
"projectname": "communication_app", | |||||
"debugOptions": { | |||||
"hidedInDevtools": [] | |||||
}, | |||||
"scripts": {}, | |||||
"staticServerOptions": { | |||||
"baseURL": "", | |||||
"servePath": "" | |||||
}, | |||||
"isGameTourist": false, | |||||
"condition": { | |||||
"search": { | |||||
"list": [] | |||||
}, | |||||
"conversation": { | |||||
"list": [] | |||||
}, | |||||
"game": { | |||||
"list": [] | |||||
}, | |||||
"plugin": { | |||||
"list": [] | |||||
}, | |||||
"gamePlugin": { | |||||
"list": [] | |||||
}, | |||||
"miniprogram": { | |||||
"list": [] | |||||
} | |||||
} | |||||
} |
{ | |||||
"desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html", | |||||
"rules": [{ | |||||
"action": "allow", | |||||
"page": "*" | |||||
}] | |||||
} |
// wx.request封装 | |||||
const app = getApp() | |||||
const request = async (url, options, contentType) => { | |||||
let openId | |||||
await new Promise(resolve => { | |||||
let tiems = setInterval(() => { | |||||
// 如果此时openid已被赋值 | |||||
openId = app.globalData.openid; // data里要固定添加openid(项目需要) | |||||
if (!!app.globalData.openid) { | |||||
clearInterval(tiems) | |||||
resolve() | |||||
} | |||||
}, 500); | |||||
}) | |||||
!!contentType?contentType:contentType="application/json" | |||||
return new Promise( (resolve, reject) => { | |||||
wx.request({ | |||||
url: `${app.mangerUrl}${url}?openId=${openId}`,//获取域名接口地址 | |||||
method: options.method, //配置method方法 | |||||
data:options.data, | |||||
//如果是GET,GET自动让数据成为query String,其他方法需要让options.data转化为字符串 | |||||
header: { | |||||
'Content-Type': contentType, | |||||
'Cookie':wx.getStorageSync('cookieKey')||'' | |||||
}, | |||||
success(request) { | |||||
if (request && request.header && request.header['Set-Cookie']) { | |||||
if(request.header['Set-Cookie'].indexOf("SESSION")==0){ | |||||
wx.setStorageSync('cookieKey', request.header['Set-Cookie']); //保存Cookie到Storage | |||||
} | |||||
} | |||||
//监听成功后的操作 | |||||
resolve(request.data) | |||||
}, | |||||
fail(error) { | |||||
//返回失败也同样传入reject()方法 | |||||
reject(error.data) | |||||
} | |||||
}) | |||||
}) | |||||
} | |||||
//封装get方法 | |||||
const get = (url, options = {}) => { | |||||
return request(url, { | |||||
method: 'GET', | |||||
data: options | |||||
}) | |||||
} | |||||
//封装post方法 | |||||
const post = (url, options,contentType) => { | |||||
return request(url, { | |||||
method: 'POST', | |||||
data: options | |||||
},contentType) | |||||
} | |||||
//封装put方法 | |||||
const put = (url, options) => { | |||||
return request(url, { | |||||
method: 'PUT', | |||||
data: options | |||||
}) | |||||
} | |||||
//封装remove方法 | |||||
// 不能声明DELETE(关键字) | |||||
const remove = (url, options) => { | |||||
return request(url, { | |||||
method: 'DELETE', | |||||
data: options | |||||
}) | |||||
} | |||||
//抛出wx.request的post,get,put,remove方法 | |||||
module.exports = { | |||||
get, | |||||
post, | |||||
put, | |||||
remove | |||||
} |
const formatTime = date => { | |||||
const year = date.getFullYear() | |||||
const month = date.getMonth() + 1 | |||||
const day = date.getDate() | |||||
const hour = date.getHours() | |||||
const minute = date.getMinutes() | |||||
const second = date.getSeconds() | |||||
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}` | |||||
} | |||||
const formatNumber = n => { | |||||
n = n.toString() | |||||
return n[1] ? n : `0${n}` | |||||
} | |||||
module.exports = { | |||||
formatTime | |||||
} |