收藏
回答

云开发云函数如何调用普通函数?

这里是一个毫秒转分:秒的格式化函数,format2,如何在云函数中调用?

// 云函数入口文件
const cloud = require('wx-server-sdk')
const dateUtils = require('date-utils')


process.env.TZ ='Asia/Shanghai'
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})


const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
  console.log(event);


  let padTo2Digits = function (num) {
    return num.toString().padStart(2, '0');
  }


  let padTo3Digits = function (num) {
    return num.toString().padStart(3, '0');
  }
  let format2 = function(num){
    let totalSeconds = num;


    let ms = totalSeconds % 1000;
    
    totalSeconds = parseInt(num/1000);


    //  ️ 获取完整分钟数
    const minutes = Math.floor(totalSeconds / 60);
    const seconds = totalSeconds % 60;
    
    // ✅ 格式化为 MM:SS
    // const result = `${this.padTo2Digits(minutes)}:${this.padTo2Digits(seconds)}:${this.padTo3Digits(ms)}`;
    const result = `${this.padTo2Digits(minutes)}:${this.padTo2Digits(seconds)}`;
    //console.log(result); //  ️ "09:25"
    return result;
  }


  try {
    let dt = new Date();
    let today = dt.toFormat("YYYY-MM-DD");
    let time = dt.toFormat("YYYY-MM-DD HH24:MI:SS");


    let res = await db.collection('profiles')
    .aggregate()
    .match({
      _id:_.exists(true)
    })
    .skip(0)
    .limit(500)
    .end()


    let items = res.list;
    items.forEach(async (item)=>{
      let _id = item['_id'];
      let diff = item['diff'];
      let ytimes = this.format2(diff);
      console.log(_id,ytimes);


      await db.collection('profiles').doc(_id).update({
        // data 字段表示需新增的 JSON 数据
        data: {
          ytimes: ytimes,
        }
      })
    })


  
  } catch(e) {
    console.error(e)
  }
}



回答关注问题邀请回答
收藏

2 个回答

登录 后发表内容