钉钉小程序中获取 corpId

首先需要定义全局变量,在 app.js 中定义
之后在初始化中就可以获取到 corpId,赋值在全局变量中就可以了

// app.js
App({
  data: {
    corpId: ''
  },
  onLaunch(options) {
    this.data.corpId = options.query.corpId
  }
})
好的,下面是一个简单的钉钉程序写数据缓存的示例: ```java import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.dingtalk.open.client.api.model.corp.CorpUserDetail; import com.dingtalk.open.client.api.model.corp.CorpUserDetailList; import com.dingtalk.open.client.api.service.corp.CorpUserService; import com.dingtalk.open.client.common.SdkInitException; import com.dingtalk.open.client.common.ServiceException; import com.dingtalk.open.client.common.ServiceRuntimeException; import com.dingtalk.open.client.service.ServiceFactory; import com.dingtalk.open.client.service.corp.CorpUserService; import com.dingtalk.open.client.service.corp.impl.CorpUserServiceImpl; import java.util.HashMap; import java.util.List; import java.util.Map; public class DataCache { private static final String CACHE_KEY = "corp_user_detail"; private static CorpUserService corpUserService = null; private static Map<String, Object> cacheMap = new HashMap<>(); static { try { ServiceFactory serviceFactory = ServiceFactory.getInstance(); corpUserService = serviceFactory.getOpenService(CorpUserService.class); } catch (SdkInitException e) { e.printStackTrace(); } } public static CorpUserDetailList getCorpUserDetailList(String corpId, Long departmentId) { String cacheKey = CACHE_KEY + "_" + corpId + "_" + departmentId; if (cacheMap.containsKey(cacheKey)) { return (CorpUserDetailList) cacheMap.get(cacheKey); } CorpUserDetailList corpUserDetailList = new CorpUserDetailList(); try { int offset = 0; int size = 100; while (true) { CorpUserDetailList response = corpUserService.getCorpUserList(corpId, departmentId, offset, size, null); if (response == null || response.getUserlist() == null || response.getUserlist().isEmpty()) { break; } corpUserDetailList.getUserlist().addAll(response.getUserlist()); offset += size; } } catch (ServiceException e) { throw new ServiceRuntimeException(e); } cacheMap.put(cacheKey, corpUserDetailList); return corpUserDetailList; } public static void clearCache() { cacheMap.clear(); } } ``` 这是一个缓存企业微信用户信息的示例,使用了一个静态变量 `cacheMap` 存储缓存数据,缓存的 key 为 `CACHE_KEY + "_" + corpId + "_" + departmentId`,其 `corpId` 和 `departmentId` 分别为企业微信的企业 ID 和部门 ID,`CACHE_KEY` 为缓存 key 的前缀。 `getCorpUserDetailList` 方法用于获取企业微信用户信息列表,如果缓存已经存在相应的数据,则直接返回缓存数据,否则调用钉钉开放平台提供的接口获取数据,并将数据存入缓存。 `clearCache` 方法用于清空缓存的数据。 注意:此示例仅供参考,具体实现需要根据实际需求进行调整。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值