钉钉小程序官方文档:文档 - 钉钉开放平台
吐槽一下,当时文档很不友好(2019年年底前),还会出现打不开的情况,可能现在会好一些吧???
踩过很多坑,边调研边开发模式,技术栈uni-app+TS,以下主要是类型声明,直接拿来就用,其他小程序相通。
在typings目录下新增dd.d.ts文件:
declare namespace DDModule { export class DD { // 钉钉企业id corpId: string /** * 获取钉钉免登码 * */ getAuthCode(options: GetAuthCodeOptions): void; /** * 压缩图片 * */ compressImage(options: CompressImageOptions): void; /** * 保存在线、本地临时或者永久地址图片到手机相册。 * */ saveImage(options: SaveImageOptions): void; /** * 提示框 * */ alert(options: AlertOptions): void /** * 确认框 * */ confirm(options: ConfirmOptions): void /** * 调用钉钉扫码 * */ scan(options: scanOptions): void /** * 选人与部门 * */ complexChoose(options: ComplexOptions): void /** * 选取部门 * */ chooseDepartments(options: ChooseDepartmentsOptions): void /** * 选取手机通信录 * */ choosePhonebook(options: ChoosePhoneBookOptions): void /** * 获取全局唯一录音管理器 * */ getRecorderManager(): RecordManager /** * 获取全局背景音频管理 * */ getBackgroundAudioManager(): BackgroundAudioManager /** * 上传文件 * */ uploadFile(options: UploadOptions): void /** * 获取网络状态 * */ getNetworkType(option: GetNetworkOptions): void /** * 选取图片 * */ chooseImage(options: DDChooseImageOptions): void /** * 选取视频 * */ chooseVideo(options: DDChooseVideoOptions): void /** * 图片预览 * */ previewImage(options: DDPreviewImageOptions): void /** * 弱提示 * */ showToast(options: ShowToastOptions): void // 移除缓存 removeStorage(param: { key: string | number }): void } interface ShowToastOptions { content?: string, type?: string duration?: number, success?(param?: any): void, fail?(e: any): void, complete?(): void, } interface GetAuthCodeOptions { success(param: { authCode: string }): void fail?(param: any): void } interface CompressImageOptions { /** * 要压缩的图片地址数组 */ filePaths: string[], /** * 压缩级别,支持 0 ~ 4 的整数 4为网络自适应,其他是越高越清晰 */ compressLevel?: 0 | 1 | 2 | 3 | 4, /** * 成功回调 */ success?(param?: FilePaths): void, /** * 失败回调 */ fail?(e: any): void, /** * 无论成功或者失败回调 */ complete?(): void, } interface FilePaths { filePaths?: string[] } interface SaveImageOptions { /** * 图片地址 */ url: string, /** * 成功回调 */ success?(param?: any): void, /** * 失败回调 */ fail?(e: any): void, /** * 无论成功或者失败回调 */ complete?(): void, } interface AlertOptions { /** * 标题 */ title?: string /** * 内容 */ content?: string /** * 按钮名字 */ buttonText?: string /** * 成功回调 */ success?(param?: any): void, /** * 失败回调 */ fail?(param?: any): void, /** * 无论成功或者失败回调 */ complete?(param?: any): void, } interface ConfirmOptions { /** * 标题 */ title: string /** * 内容 */ content: string /** * 确认按钮名字 */ confirmButtonText?: string /** * 取消按钮名字 */ cancelButtonText?: string /** * 成功回调 确认为true 取消为fail */ success?(flag: boolean): void, /** * 失败回调 */ fail?(param?: any): void, /** * 无论成功或者失败回调 */ complete?(param?: any): void, } interface scanOptions { /** * qr 二维码 bar 条形码 */ type?: qr | bar /** * 成功回调 */ success?(param?: any): void, /** * 失败回调 */ fail?(param?: any): void, /** * 无论成功或者失败回调 */ complete?(param?: any): void, } interface ComplexOptions { /** * 标题 */ title?: string, /** * 是否多选 */ multiple?: boolean, /** * 超过限定人数返回提示 */ limitTips?: string, /** * 最大可选人数 */ maxUsers?: number, /** * 已选用户,值为userId列表 */ pickedUsers?: string[], /** * 已选部门 */ pickedDepartments?: string[] /** * 不可选用户,值为userId列表 */ disabledUsers?: string[], /** * 不可选部门 */ disabledDepartments?: string[], /** * 必选用户(不可取消选中状态),值为userId列表 */ requiredUsers?: string[], /** * 必选部门(不可取消选中状态 */ requiredDepartments?: string[], /** * 选人权限,目前只有GLOBAL这个参数) */ permissionType?: GLOBAL, /** * true:返回人员信息 * false:返回人员和部门信息 */ responseUserOnly?: boolean, /** * 仅支持0和-1两个值:0表示从企业最上层开始,-1表示从自己部门开始 */ startWithDepartmentId?: 0 | -1 /** * 成功回调 */ success?(param: { selectedCount?: number, users?: any, departments?: any }): void /** * 失败回调 */ fail?(e: any): void } interface ChooseDepartmentsOptions { /** * 标题 */ title?: string, /** * 是否多选 */ multiple?: boolean, /** * 超过限定人数返回提示 */ limitTips?: string, /** * 最大可选部门 */ maxDepartments?: number, /** * 已选部门 */ pickedDepartments?: string[] /** * 不可选部门 */ disabledDepartments?: string[], /** * 必选部门(不可取消选中状态 */ requiredDepartments?: string[], /** * 选人权限,目前只有GLOBAL这个参数) */ permissionType?: GLOBAL, /** * 成功回调 */ success?(param: { selectedCount?: number, users?: any, departments?: any }): void /** * 失败回调 */ fail?(e: any): void } interface ChoosePhoneBookOptions { /** * 是否多选 */ multiple?: boolean /** * 人数限制,当multiple为true才生效,可选范围1-1500 */ maxUsers?: number /** * 超过人数限制的提示语可以用这个字段自定义 */ limitTips?: string /** * 如果你需要修改选人页面的title,可以在这里赋值 */ title?: string /** * 成功回调 */ success?(param: { name?: string, avatar?: any, mobile?: any }): void /** * 失败回调 */ fail?(e: any): void } interface RecordManager { start(param?: { duration?: number }): void, // 开始 stop(): void, // 结束 onstart(): void, // 监听开始 onstop(res: { tempFilePath: string }): void, // 监听结束 onerror(res: { errorCode?: 20001 | 13 | 2 | 5, errorMessage?: string }): void, // 监听报错 错误码 20001 正在录音,13 保存失败,2参数错误,5没有读写权限 } interface BackgroundAudioManager { src?: string,// 音频地址 title?: string, // 音频标题 coverImgUrl?: string, // 音频封面 paused?: boolean, // 是否暂停 duration?: number, // 音频长度 currentTime?: number, // 音频当前播放的时间 play(): void // 播放 pause(): void // 暂停 stop(): void // 停止 seek(position: number): void // 跳转指定位置,以秒为单位 onPlay(): void, // 监听开始 onPause(): void, // 监听暂停 onStop(): void, // 监听停止 onEnded(): void, // 监听结束 onTimeUpdate(): void, // 监听时间变化 onError(): void, // 监听报错 } } interface UploadOptions { url: string // 上传的路径 filePath: string // 本地文件的虚拟地址 fileName: string // 对应的key fileType: image | audio | video header?: Object formData?: Object success?(param: any): void fail?(e: any): void complete?(): void, } interface GetNetworkOptions { success?(param: { networkAvailable?: boolean, networkType?: UNKNOWN | NOTREACHABLE | WIFI | 3G | 2G | 4G | WWAN, }): void fail?(e: any): void, complete?(e: any): void, } interface DDChooseVideoOptions { sourceType?: string[], // 视频来源 可选值 album camera maxDuration?: number, // 视频长度 covered?: boolean, success?(param: { filePath: string, duration?: number, size?: number, height?: number, width?: number, coverPath?: string, }): void fail?(e: any): void complete?(): void, } interface DDChooseImageOptions { count?: number // 最大可选数量 sourceType?: string[] // 图片来源 可选值 album camera success?(param: { filePaths: string[], // 所选图片本地占位符数组 }): void fail?(e: any): void complete?(): void, } interface DDPreviewImageOptions { urls: string[], current?: number, success?(e: any): void, fail?(e: any): void, complete?(): void, }然后在index.d.ts文件,其他.d.ts类型文件亦可
declare const dd: DDModule.DD