UNPKG

@tnwx/miniprogram

Version:

TNWX 微信系开发脚手架之极速开发微信小程序

104 lines 4.75 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const util = require("util"); const accesstoken_1 = require("@tnwx/accesstoken"); const kits_1 = require("@tnwx/kits"); /** * @author Javen * @copyright javendev@126.com * @description 小程序客服消息相关 API */ class MiniCustomerMsgApi { /** * 获取客服消息内的临时素材。即下载临时的多媒体文件。目前小程序仅支持下载图片文件 * @param mediaId 媒体文件 ID */ static getTempMedia(mediaId) { return __awaiter(this, void 0, void 0, function* () { let accessToken = yield accesstoken_1.AccessTokenApi.getAccessToken(); let url = util.format(this.getTempMediaUrl, accessToken.getAccessToken, mediaId); return kits_1.HttpKit.getHttpDelegate.httpGet(url, { headers: { 'Content-type': 'application/json' }, responseType: 'arraybuffer' }); }); } /** * 发送客服消息给用户 * @param openId 用户的 openId * @param msgType 消息类型 * @param data 消息对应的数据 */ static send(openId, msgType, data) { return __awaiter(this, void 0, void 0, function* () { let accessToken = yield accesstoken_1.AccessTokenApi.getAccessToken(); let url = util.format(this.sendUrl, accessToken.getAccessToken); let obj = { touser: openId, msgtype: msgType, text: null, image: null, link: null, miniprogrampage: null }; if (msgType === MiniCSRMsgType.TEXT) obj.text = data; if (msgType === MiniCSRMsgType.IMAGE) obj.image = data; if (msgType === MiniCSRMsgType.LINK) obj.link = data; if (msgType === MiniCSRMsgType.MINIPROGRAMPAGE) obj.miniprogrampage = data; return kits_1.HttpKit.getHttpDelegate.httpPost(url, JSON.stringify(obj)); }); } /** * 下发客服当前输入状态给用户 * @param openId 用户的 openId * @param command 命令 */ static setTyping(openId, command) { return __awaiter(this, void 0, void 0, function* () { let accessToken = yield accesstoken_1.AccessTokenApi.getAccessToken(); let url = util.format(this.setTypingUrl, accessToken.getAccessToken); return kits_1.HttpKit.getHttpDelegate.httpPost(url, JSON.stringify({ touser: openId, command: command })); }); } /** * 新增临时素材 * @param filePath 文件路径 * @param mediaType 文件类型 */ static uploadMedia(filePath, mediaType) { return __awaiter(this, void 0, void 0, function* () { let accessToken = yield accesstoken_1.AccessTokenApi.getAccessToken(); let url = util.format(this.uploadUrl, accessToken.getAccessToken, mediaType); return kits_1.HttpKit.getHttpDelegate.upload(url, filePath); }); } } exports.MiniCustomerMsgApi = MiniCustomerMsgApi; MiniCustomerMsgApi.getTempMediaUrl = 'https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s'; MiniCustomerMsgApi.sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s'; MiniCustomerMsgApi.setTypingUrl = 'https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token=%s'; MiniCustomerMsgApi.uploadUrl = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s'; var MiniCSRMsgType; (function (MiniCSRMsgType) { MiniCSRMsgType[MiniCSRMsgType["TEXT"] = 0] = "TEXT"; MiniCSRMsgType[MiniCSRMsgType["IMAGE"] = 1] = "IMAGE"; MiniCSRMsgType[MiniCSRMsgType["LINK"] = 2] = "LINK"; MiniCSRMsgType[MiniCSRMsgType["MINIPROGRAMPAGE"] = 3] = "MINIPROGRAMPAGE"; })(MiniCSRMsgType = exports.MiniCSRMsgType || (exports.MiniCSRMsgType = {})); //# sourceMappingURL=MiniCustomerMsgApi.js.map