| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- <template>
- <uni-popup ref="popup">
- <div style="width: 700rpx;">
- <view :class="['cui-calculator','flex-cncc',size]">
- <view class="cui-calculator-title" @click="tapCopy">
- <text>{{toThousands(curNumber)}}</text>
- </view>
- <view class="cui-content">
- <uni-row v-for="(row,index) in buttons" :key="index" class="row">
- <uni-col v-for="(item,subIndex) in row" :key="subIndex"
- :span="item.span?item.span:(24/row.length)">
- <div class="col">
- <view class="cui-cell flex-cncc"
- :class="[(waitInputNumberTwo&&item.name==symbol)?'active':'',item.width == 2?'big':'',item.border?'border':'',item.type]"
- @click.stop="tapItem(item)" @touchstart="touchStart">
- <!-- <uni-icons v-if="item.type=='backspace'" custom-prefix="iconfont"
- type="icon-backspace" color="#FF8000" size="26px"></uni-icons> -->
- <span v-if="item.type=='backspace'">←</span>
- <span v-else>{{item.name}}</span>
- </view>
- </div>
- </uni-col>
- </uni-row>
- </view>
- </view>
- </div>
- </uni-popup>
- </template>
- <script>
- import Decimal from "decimal.js";
-
- const toThousands = (num = 0) => {
- return num.toString().replace(/\d+/, function(n) {
- return n.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
- });
- };
- function isValidNumber(str) {
- return Math.abs(parseFloat(str)) > 0 || str == "0";
- }
- export default {
- props: {
- size: {
- type: String,
- default: "normal" // small,normal
- }
- },
- data() {
- return {
- toThousands,
- curNumber: '0', //计算器显示区域值
- buttons: [
- [{ //计算器各按键
- name: 'C',
- type: 'clear',
- border: true
- }, {
- name: 'backspace',
- type: 'backspace',
- border: true
- }, {
- name: '%',
- type: 'percent',
- border: false
- }, {
- name: '/',
- type: 'operator',
- border: false
- }],
- [{
- name: '7',
- type: 'number',
- border: true
- }, {
- name: '8',
- type: 'number',
- border: false
- }, {
- name: '9',
- type: 'number',
- border: false
- }, {
- name: '*',
- type: 'operator',
- border: false
- }],
- [{
- name: '4',
- type: 'number',
- border: true
- }, {
- name: '5',
- type: 'number',
- border: false
- }, {
- name: '6',
- type: 'number',
- border: false
- }, {
- name: '+',
- type: 'operator',
- border: false
- }],
- [{
- name: '1',
- type: 'number',
- border: true
- }, {
- name: '2',
- type: 'number',
- border: false
- }, {
- name: '3',
- type: 'number',
- border: false
- }, {
- name: '-',
- type: 'operator',
- border: false
- }],
- [{
- name: '0',
- type: 'number',
- border: true,
- span: 12
- }, {
- name: '.',
- type: 'point',
- border: false,
- span: 6
- }, {
- name: '=',
- type: 'equal',
- border: false,
- span: 6
- }]
- ],
- numberOne: '', //变量一
- symbol: '', //运算符
- complete: false, //判断是否完成一次计算
- waitInputNumberTwo: false,
- formula: []
- }
- },
- methods: {
- open() {
- this.$refs["popup"].open();
- },
- setCurNumber(value) {
- this.curNumber = value;
- },
- getCurNumber() {
- return this.curNumber;
- },
- touchStart() {
- uni.vibrateShort();
- },
- /**
- * 1. 无输入
- * 2. 计算已完成
- * 3. 1
- * 4. 1.2
- * 6. 1+
- * 7. 1+.
- * 8. 1+2.3
- */
- //计算器方法二:
- tapItem: function(item) {
- // console.log("!!", item)
- switch (item.type) {
- case 'point': //小数点
- if (this.complete || this.waitInputNumberTwo) {
- this.curNumber = '0';
- this.complete = false;
- }
- if (!this.curNumber.includes('.')) {
- this.curNumber = this.curNumber + '.';
- }
- break;
- case 'number': //数字
- if (this.complete || this.waitInputNumberTwo) {
- this.complete = false;
- this.curNumber = item.name;
- // this.formula = [];
- } else if ((this.curNumber + "").includes('.')) {
- this.curNumber += item.name;
- } else {
- this.curNumber = parseInt(this.curNumber + item.name) + "";
- }
- break;
- case 'percent':
- if (isValidNumber(this.curNumber)) {
- this.curNumber = Decimal.div(parseFloat(this.curNumber), 100);
- } else if (this.numberOne) {
- //取上一个数
- this.curNumber = Decimal.div(parseFloat(this.numberOne), 100);
- }
- break;
- case 'sign':
- if (isValidNumber(this.curNumber)) {
- this.curNumber = "" + (0 - parseFloat(this.curNumber));
- } else if (this.numberOne) {
- //取上一个数
- this.curNumber = "" + (0 - parseFloat(this.numberOne));
- }
- break;
- case 'backspace':
- let temp = "" + this.curNumber;
- if (/e\+\d$/.test(temp)) {
- temp = temp.substring(0, temp.length - 3);
- } else {
- temp = temp.substring(0, temp.length - 1);
- }
- if (temp == "" || temp == '-') {
- this.curNumber = '0';
- } else {
- this.curNumber = temp;
- }
- break;
- case 'operator': //运算符
- // 加减乘除
- this.complete = false;
- if (this.waitInputNumberTwo) {
- this.symbol = item.name;
- } else {
- if (this.numberOne && this.symbol && isValidNumber(this.curNumber)) {
- this.curNumber = this.calc(this.symbol, this.numberOne, this.curNumber);
- this.numberOne = this.curNumber;
- this.symbol = item.name;
- this.waitInputNumberTwo = true;
- } else if (!this.numberOne && !this.symbol && isValidNumber(this.curNumber)) {
- this.numberOne = this.curNumber;
- this.symbol = item.name;
- this.waitInputNumberTwo = true;
- }
- }
- break;
- case 'equal': //等号
- if (!this.complete) {
- // console.log("!!!!!!!!!!!!", this.symbol, this.numberOne, this.curNumber)
- if (this.numberOne) {
- this.curNumber = this.calc(this.symbol, this.numberOne, this.curNumber);
- } else {
- this.curNumber = parseFloat(this.curNumber);
- }
- this.complete = true;
- this.numberOne = '';
- this.symbol = '';
- }
- // this.addToFormula(item);
- break;
- case 'clear': //清除符
- this.clear();
- break;
- }
- if (item.type != "operator") {
- this.waitInputNumberTwo = false;
- }
- },
- // addToFormula(item) {
- // if ("1234567890.".includes(item.name)) {
- // if (this.formula.length) {
- // this.formula[this.formula.length - 1] += item.name;
- // } else {
- // this.formula.push(item.name);
- // }
- // } else {
- // this.formula.push(item.name);
- // }
- // },
- // formulaLastIsSymbol() {
- // return "-+*/".includes(this.formula[this.formula.length - 1]);
- // },
- /**
- * 清除
- * */
- clear: function() {
- this.curNumber = '0';
- this.numberOne = '';
- this.symbol = '';
- this.compile = false;
- this.formula = [];
- },
- /**
- * 除法
- * */
- calc: function(symbol, arg1, arg2) {
- if (symbol == '-') {
- return Decimal.sub(arg1, arg2);
- } else if (symbol == '+') {
- return Decimal.add(arg1, arg2);
- } else if (symbol == '*') {
- return Decimal.mul(arg1, arg2);
- } else if (symbol == '/') {
- return Decimal.div(arg1, arg2);
- }
- },
- tapCopy() {
- uni.setClipboardData({
- data: "" + this.curNumber,
- success: () => {
- uni.showToast({
- title: "复制成功"
- })
- }
- })
- }
- }
- }
- </script>
- <style lang="less">
- .cui-calculator {
- background-color: #333333;
- box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
- width: 100%;
- }
- .cui-calculator-title,
- .cui-calculator-formula {
- width: 100%;
- height: 120rpx;
- line-height: 120rpx;
- padding: 0 24rpx;
- text-align: right;
- background-color: #333333;
- font-size: 50rpx;
- font-weight: 600;
- color: #FFFFFF;
- box-sizing: border-box;
- overflow: auto;
- }
- .cui-content {
- width: 100%;
- background-color: #FFFFFF;
- // display: flex;
- // flex-direction: row;
- // justify-content: flex-start;
- // align-items: flex-start;
- // flex-wrap: wrap;
- box-sizing: border-box;
- border: 1px #f5f5f5 solid;
- .row {
- width: 100%;
- background-color: #A5A4A4;
- }
- .col {
- height: 98rpx;
- line-height: 98rpx;
- padding: 6rpx;
- box-sizing: border-box;
- }
- .cui-cell {
- width: 100%;
- height: 100%;
- text-align: center;
- font-size: 44rpx;
- font-weight: 600;
- color: black;
- border-bottom: 1px solid #f5f5f5;
- border-left: 1px solid #f5f5f5;
- background-color: white;
- box-sizing: border-box;
- text-align: center;
- background-color: #fafafa;
- border-radius: 20rpx;
- &:active {
- filter: brightness(0.95);
- transform: scale(0.95);
- }
- }
- .cui-cell.border {
- border-left: none;
- }
- .cui-cell.active {
- background-color: #ccc;
- }
- .cui-cell {
- &.operator,
- &.percent,
- &.backspace {
- color: #FF7433
- }
- &.sign {
- color: #FF7433
- }
- &.equal {
- color: white;
- background-color: #FF7433
- }
- &.clear {
- color: #F75000;
- }
- }
- }
- .small {
- .cui-calculator-title {
- height: 60rpx;
- line-height: 60rpx;
- font-size: 25rpx;
- }
- .cui-calculator-formula {
- height: 40rpx;
- line-height: 40px;
- font-size: 22rpx;
- }
- .col {
- height: 45rpx;
- line-height: 45rpx;
- padding: 6rpx;
- }
- .cui-cell {
- font-size: 22rpx;
- border-radius: 12rpx;
- }
- }
- </style>
|