WeChat applet adds events (reminders) to the system calendar implementation

Directly upload the code

// pages/calendar/calendar.js

Page({
  // 点击添加日程按钮
  handleAddCalendar() {
    wx.getSetting({
      success(res) {
        // 判断是否已经授权
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              // 用户已授权,调用添加日程 API
              wx.addPhoneCalendar({
                title: '会议', // 日程标题,必填项
                startTime: new Date('2023/04/15 09:00').getTime()/1000, // 日程开始时间,必填项
                endTime: new Date('2023/04/15 12:00').getTime()/1000, // 日程结束时间,必填项
                location: '北京市朝阳区东三环北路', // 日程地点,非必填项
                notes: '请大家准时参加会议', // 日程备注,非必填项
                success(res) {
                  console.log(res) // 日程添加成功的回调函数
                  wx.showToast({
                    title: '添加日程成功',
                    icon: 'success',
                    duration: 2000
                  })
                },
                fail(res) {
                  console.log(res) // 日程添加失败的回调函数
                  wx.showToast({
                    title: '添加日程失败',
                    icon: 'none',
                    duration: 2000
                  })
                }
              })
            },
            fail() {
              // 用户拒绝授权,提示用户授权
              wx.showToast({
                title: '请先授权',
                icon: 'none',
                duration: 2000
              })
            }
          })
        } else {
          // 已经授权,调用添加日程 API
          wx.addPhoneCalendar({
            title: '会议', // 日程标题,必填项
            startTime: new Date('2023/04/15 09:00').getTime()/1000, // 日程开始时间,必填项
            endTime: new Date('2023/04/15 12:00').getTime()/1000, // 日程结束时间,必填项
            location: '北京市朝阳区东三环北路', // 日程地点,非必填项
            notes: '请大家准时参加会议', // 日程备注,非必填项
            success(res) {
              console.log(res) // 日程添加成功的回调函数
              wx.showToast({
                title: '添加日程成功',
                icon: 'success',
                duration: 2000
              })
            },
            fail(res) {
              console.log(res) // 日程添加失败的回调函数
              wx.showToast({
                title: '添加日程失败',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      }
    })
  }
})

Using wx.addPhoneCalendar requires the following prerequisites:

  1. WeChat version support: wx.addPhoneCalendar is the API of the WeChat applet and can only be used in an environment where the WeChat version number is greater than or equal to 6.6.6.

  2. User authorization: Adding a schedule to the calendar of the user's mobile phone requires user authorization. The user needs to click the authorization button in the mini program to perform the operation. Authorization buttons can be implemented through the <button open-type="openSetting"> component.

  3. Legal domain name: Mini programs that use wx.addPhoneCalendar must configure legal domain names in the mini program management background, including the calling domain name of wx.addPhoneCalendar and the calendar service domain name. During the development phase, you can use the "Do not verify legal domain name, Web-view (business domain name), TLS version, and HTTPS certificate" option that comes with the developer tools for debugging.

Note: Due to user privacy and security issues, the mini program can only be added to the calendar selected by the user and cannot select a specific calendar. getTime() needs to divide by 1000

reference:

wx.addPhoneCalendar(Object object) | WeChat Open Document

Guess you like

Origin blog.csdn.net/CrazBarry/article/details/130056378