作者: admin

  • js base64图片压缩

    js 图片 压缩 图片 js

    function dealImage(base64, w, callback) {
        var newImage = new Image();
        var quality = 0.6;    //压缩系数0-1之间
        newImage.src = base64;
        newImage.setAttribute("crossOrigin", 'Anonymous'); //url为外域时需要
        var imgWidth, imgHeight;
        newImage.onload = function () {
            imgWidth = this.width;
            imgHeight = this.height;
            var canvas = document.createElement("canvas");
            var ctx = canvas.getContext("2d");
            if (Math.max(imgWidth, imgHeight) > w) {
                if (imgWidth > imgHeight) {
                    canvas.width = w;
                    canvas.height = w * imgHeight / imgWidth;
                } else {
                    canvas.height = w;
                    canvas.width = w * imgWidth / imgHeight;
                }
            } else {
                canvas.width = imgWidth;
                canvas.height = imgHeight;
                quality = 0.6;
            }
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
            var base64 = canvas.toDataURL("image/jpeg", quality); //压缩语句
            // 如想确保图片压缩到自己想要的尺寸,如要求在50-150kb之间,请加以下语句,quality初始值根据情况自定
            // while (base64.length / 1024 > 150) {
            //     quality -= 0.01;
            //     base64 = canvas.toDataURL("image/jpeg", quality);
            // }
            // 防止最后一次压缩低于最低尺寸,只要quality递减合理,无需考虑
            // while (base64.length / 1024 < 50) {
            //     quality += 0.001;
            //     base64 = canvas.toDataURL("image/jpeg", quality);
            // }
            callback(base64);//必须通过回调函数返回,否则无法及时拿到该值
        }
    }
    
    
    
    //这是原来的base64格式字符串
    var base64= path;
    //这就是你压缩之后的字符串
    var str;
    //你可以打桩看一下有多长
    console.log(base64.length);
    //然后调用压缩方法 第一个参数就是原来的字符串,第二个是宽度,第三个就是回调方法,也就是压缩函数最后面那个callback(base64)
    dealImage(base64, 500, useImg);
    //然后一压 再打个桩看下长度 方法名随便起怎么舒服怎么来
    function useImg(base64) {
        str= base64;
        console.log(str.length);
    }

     

    https://blog.csdn.net/zx19930309/article/details/90375907

  • win10安装mongodb 4.4.4

    1,下载地址:https://www.mongodb.com/try/download/community?tck=docs_server

    通过网盘分享的文件:mongodb-windows-x86_64-4.4.18-signed.msi
    链接: https://pan.baidu.com/s/1D1jhJykJ4M6QuYkNareSJQ 提取码: vrfq
    –来自百度网盘超级会员v7的分享

     

     

    2,一路点击下一步,到这步时候把沟去掉,不安装图形化工具,会卡。

    3,等待安装完毕,访问http://localhost:27017/

    4,如果还没开启,可以试着执行

    E:\Program Files\MongoDB\Server\4.0\bin>mongod.exe --dbpath "E:\Program Files\MongoDB\Server\4.0\data"

     

    5,进入mongodb DOS界面

    E:\Program Files\MongoDB\Server\4.0\bin>mongo.exe

     

    6,如果需要数据的备份功能导入导出就额外需要下载数据库工具,然后把这些放到bin目录下即可

    下载地址:https://www.mongodb.com/try/download/bi-connector

    通过网盘分享的文件:mongodb-database-tools-windows-x86_64-100.10.0.zip
    链接: https://pan.baidu.com/s/1DhbuQNcqQzABoLaUBTJYxQ 提取码: k56y
    –来自百度网盘超级会员v7的分享

     

     

     

     

    参考文章:https://www.jb51.net/article/145489.htm

    在windows下mongoDB安装和配置

     

    mongodb笔记

  • 解决:出现滚动条背景无法平铺问题

    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            div {
                width: 400px;
                overflow-x: scroll;
                border: 1px solid #e6e6e6;
            }
            ul{
                padding: 0px;
                margin: 0px;
                /*方法一:*/
                /*min-width: max-content;*/
                /*min-width:-webkit-max-content;*/
            }
            li {
                /*方法二:*/
                /*display: table-row; !* or inline-block *!*/
    
            }
    
            .a{
                background-color: red;
            }
    
            .b{
                background-color: blue;
            }​
        </style>
    </head>
    <body>
    <div>
        <ul>
            <li class='a'>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</li>
            <li class='b'>bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</li>
        </ul>
    </div>
    </body>
    </html>
    
    

     

    https://blog.csdn.net/qq_35132189/article/details/90052991

  • 解决js计算数字精度问题

     

    function twofloat(v){
      return Number((Number(v).toFixed(2)).toString()) //有小数保留,没小数就不用默认显示.00,多位小数就保留2位,同时解决精度问题
    }
    console.log( twofloat(0.3-0.1) )

    ————————————————-

    console.log(0.1+0.2); // 输出 0.30000000000000004
    console.log(0.3-0.1); // 输出:0.19999999999999998
    console.log(0.07*100); // 输出 7.000000000000001

     

    使用第三方库解决:

    其中,前3个是同一个作者,体积一个比一个大,参见作者写的:big.js,bignumber.js和decimal.js有什么区别?

    以decimal.js为例:

    new Decimal(0.1).plus(0.2).toString() // '0.3'
    new Decimal(0.3).minus(0.1).toString() // '0.2'
    new Decimal(0.07).times(100).toString() // '7'

    最后一个 mathjs 是比较出名的专门处理数学计算的库,功能非常全,如果只是用来解决精度计算用这个库会显得杀鸡用牛刀。

    http://blog.haoji.me/js-float.html

     

  • vue.js分页

    <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.0/vue.min.js"></script>
    
    <style>
            .mo-paging {
                display: inline-block;
                padding: 0;
                margin: 1rem 0;
                font-size: 0;
                list-style: none;
                user-select: none;
            }
            .mo-paging > .paging-item {
                display: inline;
                font-size: 14px;
                position: relative;
                padding: 6px 12px;
                line-height: 1.42857143;
                text-decoration: none;
                border: 1px solid #ccc;
                background-color: #fff;
                margin-left: -1px;
                cursor: pointer;
                color: #0275d8;
            }
            .mo-paging > .paging-item:first-child {
                margin-left: 0;
            }
            .mo-paging > .paging-item:hover {
                background-color: #f0f0f0;
                color: #0275d8;
            }
            .mo-paging > .paging-item.paging-item--disabled, .mo-paging > .paging-item.paging-item--more {
                background-color: #fff;
                color: #505050;
            }
            .mo-paging > .paging-item.paging-item--disabled {
                cursor: not-allowed;
                opacity: 0.75;
            }
            .mo-paging > .paging-item.paging-item--more, .mo-paging > .paging-item.paging-item--current {
                cursor: default;
            }
            .mo-paging > .paging-item.paging-item--current {
                background-color: #0275d8;
                color: #fff;
                position: relative;
                z-index: 1;
                border-color: #0275d8;
            }
        </style>
    
    
    
    <template v-if="count">
            <ul>
                <li v-for="item in items">...</li>
            </ul>
            <my-component
                    :page-index="currentPage"
                    :total="count"
                    :page-size="pageSize"
                    @change="pageChange">
            </my-component>
    </template>
    
    
    
    
    
    <script type="text/javascript">
    
        new Vue({
            el:'#xx',
            components: {
                'my-component' : {
                    template:'<ul class="mo-paging">\n' +
                        '        <!-- prev -->\n' +
                        '        <li\n' +
                        '        :class="[\'paging-item\', \'paging-item--prev\', {\'paging-item--disabled\' : index === 1}]"\n' +
                        '        @click="prev">prev</li>\n' +
                        '        \n' +
                        '        <!-- first -->\n' +
                        '        <li\n' +
                        '        :class="[\'paging-item\', \'paging-item--first\', {\'paging-item--disabled\' : index === 1}]"\n' +
                        '        @click="first">first</li>\n' +
                        '        \n' +
                        '        <li\n' +
                        '        :class="[\'paging-item\', \'paging-item--more\']"\n' +
                        '        v-if="showPrevMore">...</li>\n' +
                        '\n' +
                        '        <li\n' +
                        '        :class="[\'paging-item\', {\'paging-item--current\' : index === pager}]"\n' +
                        '        v-for="pager in pagers"\n' +
                        '        @click="go(pager)">{{ pager }}</li>\n' +
                        '\n' +
                        '        <li\n' +
                        '        :class="[\'paging-item\', \'paging-item--more\']"\n' +
                        '        v-if="showNextMore">...</li>\n' +
                        '        \n' +
                        '        <!-- last -->\n' +
                        '        <li\n' +
                        '        :class="[\'paging-item\', \'paging-item--last\', {\'paging-item--disabled\' : index === pages}]"\n' +
                        '        @click="last">last</li>\n' +
                        '\n' +
                        '        <!-- next -->\n' +
                        '        <li\n' +
                        '        :class="[\'paging-item\', \'paging-item--next\', {\'paging-item--disabled\' : index === pages}]"\n' +
                        '        @click="next">next</li>\n' +
                        '    </ul>',
                    props : {
    
                        //页面中的可见页码,其他的以...替代, 必须是奇数
                        perPages : {
                            type : Number,
                            default : 5
                        },
    
                        //当前页码
                        pageIndex : {
                            type : Number,
                            default : 1
                        },
    
                        //每页显示条数
                        pageSize : {
                            type : Number,
                            default : 10
                        },
    
                        //总记录数
                        total : {
                            type : Number,
                            default : 1
                        },
    
                    },
                    methods : {
                        prev(){
                            if (this.index > 1) {
                                this.go(this.index - 1)
                            }
                        },
                        next(){
                            if (this.index < this.pages) {
                                this.go(this.index + 1)
                            }
                        },
                        first(){
                            if (this.index !== 1) {
                                this.go(1)
                            }
                        },
                        last(){
                            if (this.index != this.pages) {
                                this.go(this.pages)
                            }
                        },
                        go (page) {
                            if (this.index !== page) {
                                this.index = page
                                //父组件通过change方法来接受当前的页码
                                this.$emit('change', this.index)
                            }
                        }
                    },
                    computed : {
    
                        //计算总页码
                        pages(){
                            return Math.ceil(this.size / this.limit)
                        },
    
                        //计算页码,当count等变化时自动计算
                        pagers () {
                            const array = []
                            const perPages = this.perPages
                            const pageCount = this.pages
                            let current = this.index
                            const _offset = (perPages - 1) / 2
    
    
                            const offset = {
                                start : current - _offset,
                                end   : current + _offset
                            }
    
                            //-1, 3
                            if (offset.start < 1) {
                                offset.end = offset.end + (1 - offset.start)
                                offset.start = 1
                            }
                            if (offset.end > pageCount) {
                                offset.start = offset.start - (offset.end - pageCount)
                                offset.end = pageCount
                            }
                            if (offset.start < 1) offset.start = 1
    
                            this.showPrevMore = (offset.start > 1)
                            this.showNextMore = (offset.end < pageCount)
    
                            for (let i = offset.start; i <= offset.end; i++) {
                                array.push(i)
                            }
    
                            return array
                        }
                    },
                    data () {
                        return {
                            index : this.pageIndex, //当前页码
                            limit : this.pageSize, //每页显示条数
                            size : this.total || 1, //总记录数
                            showPrevMore : false,
                            showNextMore : false
                        }
                    },
                    watch : {
                        pageIndex(val) {
                            this.index = val || 1
                        },
                        pageSize(val) {
                            this.limit = val || 10
                        },
                        total(val) {
                            this.size = val || 1
                        }
                    }
                }
            },
            data:{
                pageSize : 10 , //每页显示20条数据
                currentPage : 1, //当前页码
                count : 30, //总记录数
                items : []
            },
            mounted() {
                $.ajax({
                    type:'post',
                    url:'/api/admin/info',
                    success:function (data){
                        console.log(data)
                    }
                })
            },
            methods: {
                //从page组件传递过来的当前page
                pageChange (page) {
                    // this.currentPage = page
                    // this.getList()
                }
            }
        })
    
    </script>