Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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
}
})
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo

// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
globalData: {
userInfo: null
}
})
14 changes: 14 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pages":[
"pages/index/index",
"pages/logs/logs"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "计算器",
"navigationBarTextStyle":"black"
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
10 changes: 10 additions & 0 deletions app.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**app.wxss**/
.container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
padding: 200rpx 0;
box-sizing: border-box;
}
71 changes: 71 additions & 0 deletions pages/index/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Page({
data:{
num:'',
fun:''
},
result:null,
isClear:true,

numButton:function(event){
var num=event.target.dataset.value
if(this.isClear||this.data.num=="0"){
this.setData({ //把输入的数赋值给num
num:num
});
this.isClear = false;
}else{
this.setData({
num:this.data.num+num
});
}
},
funButton:function(event){
var fun=this.data.fun
var num=Number(this.data.num) //获取前面输入的数
this.setData({
fun:event.target.dataset.value //获取操作符赋值给fun
})
this.isClear=true;
if(this.result==null){ //装载运算结果
this.result=num;
return;
}
if(fun=="+"){
this.result=this.result+num
}else if(fun=="-"){
this.result=this.result-num
}else if(fun=="*"){
this.result=this.result*num
}else if(fun=="/"){
this.result=this.result/num
}else if(fun=="%"){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%的逻辑与其他不一致,需要单独拎出来。两次的原因是当你第一次点击%的时候,你的fun是上上次操作,而不是%。要在这超长的if前对targetvalue进行判断。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯我改好了

this.result=this.result%num
}
this.setData({
num:this.result
});
},

potButton:function(event){
if(this.isClear){
this.setData({num:'0.'});
this.isClear=false;
}
if(this.data.num.indexOf('.')>=0){ //查找小数点
return;
}
this.setData({num:this.data.num+'.'});
},
DELButton:function(event){
var num=this.data.num.substr(0,this.data.num.length-1);
this.setData({
num: num ==''?'0':num
});
},

resetButton:function(event){
this.result=null,
this.isClear=false,
this.setData({num:"0",fun:''});
},
})
3 changes: 3 additions & 0 deletions pages/index/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"usingComponents": {}
}
39 changes: 39 additions & 0 deletions pages/index/index.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--index.wxml-->
<view id="totality">
<view id="result">
<view id="num">{{num}}</view>
<view id="fun">{{fun}}</view>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果不需要显示直接不写这行就行了....

其实运算符可以在左边显示出来,也比较易用

隐藏比如display:none; height:0; width:0; font-size:0; color:transparent; visiablity:hidden都可以

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来可以不写……受教了

</view>

<view class="buttons">
<view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="resetButton">AC</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="DELButton">DEL</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="funButton" data-value="%">%</view>
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace "funButton" with"perButton"

<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="funButton" data-value="/">÷</view>
</view>
<view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="numButton" data-value="7">7</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="numButton" data-value="8">8</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="numButton" data-value="9">9</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="funButton" data-value="*">×</view>
</view>
<view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="numButton" data-value="4">4</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="numButton" data-value="5">5</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="numButton" data-value="6">6</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="funButton" data-value="-">-</view>
</view>
<view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="numButton" data-value="1">1</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="numButton" data-value="2">2</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="numButton" data-value="3">3</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="funButton" data-value="+">+</view>
</view>
<view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="numButton"data-value="0">0</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="potButton">.</view>
<view hover-class="bg" hover-start-time="50" hover-stay-time="100" bindtap="funButton"data-value="=">=</view>
</view>
</view>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复用函数的思想很不错,节省了很多代码,像这种全都是.bg的,你可以在父元素加class,用.bg view的选择器来选择,省的写那么多class

</view>
70 changes: 70 additions & 0 deletions pages/index/index.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
page{
display: flex;
flex-direction: column-reverse;
height: 100%;
}

#result{ /*运算+结果区域*/
flex: flex-basis;
flex-direction: row;
background:rgb(255, 255, 255);
position: relative;
}

#num{ /*结果*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

结果有一个bug,比如1/3这种无限小数,由于没做位数限制,会把屏幕占满,0.333333333只会显示出来33333,
可以在结果那边加上一个简单判断

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

position: absolute;
font-size: 57px;
bottom: 5vh;
right: 4vw;
}

#fun{ /*不知道咋把运算符隐藏……我就直接设字体大小为0了嘿嘿*/
font-size: 0px;
}

.buttons{ /*按键部分*/
flex: flex-basis;
display:flex;
flex-direction: column;
font-size: 49px;
}

.buttons>view{ /*按键部分排版*/
flex:1;
display:flex;
}

.buttons>view>view{
flex: 1;
border-top: 1px solid rgb(255, 255, 255);
border-left: 1px solid rgb(255, 255, 255);
box-sizing: border-box;
display: flex;
justify-content: center;
}

.buttons >view:last-child>view:first-child{ /*0的盒子*/
flex-basis: 25.4%;
}

.buttons >view:first-child>view:first-child{ /*AC*/
color: orange;
/*flex-basis: 2%;*/
}

.buttons>view:first-child>view:nth-child(2){ /*DEL*/
color:orange;
}

.buttons>view:first-child>view:nth-child(3){ /*%*/
color:orange;
}

.buttons >view>view:last-child{ /*每一个view最后一个元素颜色*/
color: orange;
}

.bg{ /*hover-class的颜色*/
background: rgb(179, 177, 177);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

样式......还行但不是特别好看,简单来讲可以缩小字体宽松布局

然后还可以看情况加上按钮背景颜色和边框

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy that~小米的计算器没加按钮颜色和边框(理直气壮

15 changes: 15 additions & 0 deletions pages/logs/logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// logs.js
const util = require('../../utils/util.js')

Page({
data: {
logs: []
},
onLoad() {
this.setData({
logs: (wx.getStorageSync('logs') || []).map(log => {
return util.formatTime(new Date(log))
})
})
}
})
4 changes: 4 additions & 0 deletions pages/logs/logs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"navigationBarTitleText": "查看启动日志",
"usingComponents": {}
}
6 changes: 6 additions & 0 deletions pages/logs/logs.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!--logs.wxml-->
<view class="container log-list">
<block wx:for="{{logs}}" wx:for-item="log">
<text class="log-item">{{index + 1}}. {{log}}</text>
</block>
</view>
8 changes: 8 additions & 0 deletions pages/logs/logs.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.log-list {
display: flex;
flex-direction: column;
padding: 40rpx;
}
.log-item {
margin: 10rpx;
}
64 changes: 64 additions & 0 deletions project.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"description": "项目配置文件",
"packOptions": {
"ignore": []
},
"setting": {
"bundle": false,
"userConfirmedBundleSwitch": false,
"urlCheck": true,
"scopeDataCheck": false,
"coverView": true,
"es6": true,
"postcss": true,
"compileHotReLoad": false,
"preloadBackgroundData": false,
"minified": true,
"autoAudits": false,
"newFeature": false,
"uglifyFileName": false,
"uploadWithSourceMap": true,
"useIsolateContext": true,
"nodeModules": false,
"enhance": false,
"useCompilerModule": true,
"userConfirmedUseCompilerModuleSwitch": false,
"useMultiFrameRuntime": true,
"useApiHook": true,
"useApiHostProcess": true,
"showShadowRootInWxmlPanel": true,
"packNpmManually": false,
"enableEngineNative": false,
"packNpmRelationList": [],
"minifyWXSS": true
},
"compileType": "miniprogram",
"libVersion": "2.14.4",
"appid": "wxd2a3746dfa2f088d",
"projectname": "%E8%AE%A1%E7%AE%97%E5%99%A8-1",
"debugOptions": {
"hidedInDevtools": []
},
"scripts": {},
"isGameTourist": false,
"condition": {
"search": {
"list": []
},
"conversation": {
"list": []
},
"game": {
"list": []
},
"plugin": {
"list": []
},
"gamePlugin": {
"list": []
},
"miniprogram": {
"list": []
}
}
}
7 changes: 7 additions & 0 deletions sitemap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html",
"rules": [{
"action": "allow",
"page": "*"
}]
}
19 changes: 19 additions & 0 deletions utils/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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
}