微信小程序开发记录

微信小程序开发记录,做个笔记后面不踩坑

1、终端npm安装插件前需要使用 npm init生成包管理文件

且需要配置

javascript 复制代码
//project.config.json
 "packNpmRelationList": [
      {
        "packageJsonPath": "./package.json",
        "miniprogramNpmDistDir": "./"
      }
    ],

2、 微信小程序中方便使用的contUp数字滚动插件

使用示例

下例需求是8s更新数据,所以记录了老的值,这样每次更新会从老的值滚动到新的值

javascript 复制代码
// <text style="font-size: 32rpx;margin-bottom: 10rpx;"> <text style="font-size: 62rpx;">{{today.payAmount}}</text> 万元</text>
// components/card-1/index.js
const {dcApi} = require('../../api/index.js');
const {formatNumber} = require('../../utils/util');
import WxCountUp from '../../plugins/wx-countup/WxCountUp';

Component({
  options: {
    addGlobalClass: true
  },
  data: {
    today: {
      payAmount: 0, //今日成交金额
      payMemberNum: 0, //今日支付人数
      totalMemberNum: 0, //总会员数
      old_payAmount:0,
      old_payMemberNum:0,
      old_totalMemberNum:0,

    },
    yesterday: {
      increaseMember: 0, //昨日新增人数
      payAmount: 0, //昨日成交金额
      payMember: 0, //昨日支付人数
      permeability: 0 //昨日会员渗透率
    }
  },
  lifetimes: {
    attached() {
      this.getToday();
      this.startInterval();
    },
    detached() {
      this.stopInterval();
    }
  },
  methods: {
    getToday() {
      dcApi.online_member().then((res) => {
        this.start('today.payAmount',res.data.payAmount,2, this.data.today.old_payAmount,'today.old_payAmount');
        this.start('today.payMemberNum',res.data.payMemberNum,0,this.data.today.old_payMemberNum,'today.old_payMemberNum');
        this.start('today.totalMemberNum', res.data.totalMemberNum,0, this.data.today.old_totalMemberNum ,'today.old_totalMemberNum');
      });
    },

    start(target, endVal, fixed, oldVal = 0,old_target) {
      let option = {
        startVal: oldVal,
        decimalPlaces: fixed,
        duration: 2
      };
      this.countUp = new WxCountUp(target, endVal, option, this);
      this.countUp.start(() => {
        this.setData({
          [old_target]: endVal
        });
      });
    },

    startInterval() {
      const that = this; // 缓存 this
      this.intervalId = setInterval(() => {
        that.getToday();
      }, 8000);
    },

    stopInterval() {
      if (this.intervalId) {
        clearInterval(this.intervalId);
      }
    },
   
  }
});

3、微信小程序中的echatrs

  1. ec-canvas版本要和echarts版本对应
  2. 可以在echarts指定下载所需要的图标以减小体积
  3. 小程序的图标format返回无法识别<view>xxx</view>这种标签
  4. echatrs容器一定要指定宽高否则无法显示
  5. 真机调试需要域名白名单,真机调试1.0会有各种奇怪的bug,建议用2.0
  6. 抖音小程序头部自定义需要主体满足条件,微信小程序不需要(截至目前)
  7. 区分各种页面跳转方式(是否需要保存上一个页面),wx.reLaunch()删除所有页面跳转新页面,防止用户用手机自带返回返回到上一张页面
  8. 代码使用示例 (组件中的echatrs+定时更新数据)
javascript 复制代码
import * as echarts from '../../ec-canvas/echarts';
const {dcApi} = require('../../api/index.js');
const {getColForCollection} = require('../../utils/util');

Component({
  data: {
    ec: {
      // 将 lazyLoad 设为 true 后,需要手动初始化图表
      lazyLoad: true
    }
  },
  options: {
    addGlobalClass: true
  },
  lifetimes: {
    attached: function () {
      // 获取组件
      this.ecComponent = this.selectComponent('#mychart-fourth');
      this.init();
      this.startInterval();
    },
    detached() {
      this.stopInterval();
    }
  },
  methods: {
    init() {
      this.ecComponent.init((canvas, width, height, dpr) => {
        // 获取组件的 canvas、width、height 后的回调函数
        // 在这里初始化图表
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height,
          devicePixelRatio: dpr // new
        });
        this.setOption(chart);
        // 将图表实例绑定到 this 上,可以在其他成员函数(如 dispose)中访问
        this.chart = chart;
        this.setData({
          isLoaded: true,
          isDisposed: false
        });
        // 注意这里一定要返回 chart 实例,否则会影响事件处理等
        return chart;
      });
    },
    setOption(chart) {
      dcApi.online_summary().then((res) => {
        const data = getColForCollection(
          res.data,
          ['brand', 'amt'],
          ['name', 'value']
        );
        let option = {...}
        this.ecComponent.chart.setOption(option)
      });
    },
    startInterval() {
      const that = this; // 缓存 this
      this.intervalId = setInterval(() => {
        that.setOption();
      }, 8000);
    },
    stopInterval() {
      if (this.intervalId) {
        clearInterval(this.intervalId);
      }
    }
  }
});
相关推荐
正小安3 小时前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
说私域3 小时前
基于定制开发与2+1链动模式的商城小程序搭建策略
大数据·小程序
丁总学Java9 小时前
微信小程序-npm支持-如何使用npm包
前端·微信小程序·npm·node.js
工业互联网专业11 小时前
毕业设计选题:基于ssm+vue+uniapp的校园水电费管理小程序
vue.js·小程序·uni-app·毕业设计·ssm·源码·课程设计
说私域14 小时前
社群团购中的用户黏性价值:以开源小程序多商户AI智能名片商城源码为例
人工智能·小程序
迷雾yx18 小时前
开发微信小程序 基础02
微信小程序·小程序
迷雾yx18 小时前
开发微信小程序 基础03
微信小程序·小程序
说私域19 小时前
地理定位营销与开源AI智能名片O2O商城小程序的融合与发展
人工智能·小程序
小雨cc5566ru1 天前
uniapp+Android面向网络学习的时间管理工具软件 微信小程序
android·微信小程序·uni-app
小雨cc5566ru2 天前
hbuilderx+uniapp+Android健身房管理系统 微信小程序z488g
android·微信小程序·uni-app