323 lines
11 KiB
JavaScript
323 lines
11 KiB
JavaScript
'use strict';
|
||
|
||
const MainService = require('../main');
|
||
|
||
/**
|
||
* 微信开放平台 method 层(system.json module: wechat)。
|
||
*/
|
||
class WechatService extends MainService {
|
||
constructor(ctx) {
|
||
super(ctx);
|
||
// getAuthUrl:需登录、免权限、不记系统日志;loginByCode:免登录、免权限、免鉴权;
|
||
// pushLoginCode/queryauth/registerMiniprogram/getTemplateList/commit/getTrialQRCode/submitAudit/userList:需登录、免权限(PC 已登录用户操作);
|
||
// bindUser/updateInfo/uploadHeadimg/getMyLoginCode:微信登录态中间层(_noNeedBind),未绑定真实用户也可访问
|
||
this.systemInit('loginByCode', 'getAuthUrl,pushLoginCode,queryauth,wxaauth,registerMiniprogram,getTemplateList,commit,getTrialQRCode,submitAudit,userList,getAuthorizerInfo', '*', 'getAuthUrl,pushLoginCode,queryauth,wxaauth,registerMiniprogram,getTemplateList,commit,getTrialQRCode,submitAudit,userList,getAuthorizerInfo', 'bindUser,updateInfo,uploadHeadimg,getMyLoginCode');
|
||
}
|
||
|
||
/**
|
||
* 获取当前微信登录用户的登录码(至少微信登录):无则生成写入,有则直接返回。
|
||
*
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async getMyLoginCode() {
|
||
try {
|
||
const result = await this.service.systems.wechat.getMyLoginCode();
|
||
if (result && result.success === true) {
|
||
return this.success(result.message || '获取成功', result.data);
|
||
}
|
||
return this.fail((result && result.message) || '获取失败');
|
||
} catch (e) {
|
||
return this.fail('获取登录码失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
async getAuthUrl(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.getAuthUrl(data || {});
|
||
return this.success('获取成功', result);
|
||
} catch (e) {
|
||
return this.fail(e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 小程序登录码登录(免登录):入参 code + app_id,返回 token + user(参考 auth.login)。
|
||
*
|
||
* @param {Object} data 请求参数。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async loginByCode(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.loginByCode(data || {});
|
||
if (result.code) {
|
||
return this.success('登录成功', result);
|
||
}
|
||
return this.fail('登录失败:' + result.message, result);
|
||
} catch (e) {
|
||
return this.fail('登录失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 绑定用户(微信登录态中间层):用户名密码一致则绑定并签发新 token。
|
||
*
|
||
* @param {Object} data 请求参数(username/password)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async bindUser(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.bindUser(data || {});
|
||
if (result.code) {
|
||
return this.success('绑定成功', result);
|
||
}
|
||
return this.fail('绑定失败:' + result.message, result);
|
||
} catch (e) {
|
||
return this.fail('绑定失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 确认登录码(需登录,PC 已登录用户确认小程序登录)。
|
||
*
|
||
* @param {Object} data 请求参数(code)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async pushLoginCode(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.pushLoginCode(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '操作成功', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('确认登录失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 查询当前仓库小程序认证状态(需登录)。
|
||
*
|
||
* @param {Object} data 请求参数(taskid)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async queryauth(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.queryauth(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '查询成功', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('查询认证状态失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 发起小程序认证(需登录):前端上传材料换取 media_id 后一步提交。
|
||
*
|
||
* @param {Object} data 请求参数(contact_type/contact_name 必填,其余白名单字段透传)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async wxaauth(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.wxaauth(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '认证已发起', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('发起认证失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取并同步当前仓库小程序基础信息(需登录)。
|
||
*
|
||
* 调微信 api_get_authorizer_info 拉取授权账号详情(authorizer_info + authorization_info),
|
||
* 将基础信息/认证类型/权限集等落库 wms_wechat_app 后返回完整数据。
|
||
*
|
||
* @param {Object} data 请求参数(无)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async getAuthorizerInfo(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.getAuthorizerInfo(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '获取成功', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('获取小程序基础信息失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 快速注册企业小程序(需登录)。
|
||
*
|
||
* @param {Object} data 请求参数(name/code/code_type/legal_persona_wechat/legal_persona_name/component_phone)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async registerMiniprogram(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.registerMiniprogram(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '操作成功', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('注册小程序失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 更新微信账号资料(微信登录态中间层,仅未绑定用户)。
|
||
*
|
||
* @param {Object} data 请求参数(nickname/headimgurl)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async updateInfo(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.updateInfo(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '更新成功', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('更新失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 上传微信账号头像(微信登录态中间层,至少微信登录即可)。
|
||
*
|
||
* @param {Object} data 请求参数(image: 裁剪后的 PNG base64)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async uploadHeadimg(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.uploadHeadimg(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '上传成功', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('上传头像失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取小程序模板库列表(需登录,Redis 缓存 24h)。
|
||
*
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async getTemplateList() {
|
||
try {
|
||
const result = await this.service.systems.wechat.getTemplateList();
|
||
if (result.code) {
|
||
return this.success(result.message || '获取成功', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('获取模板列表失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 上传代码并生成体验版(需登录)。
|
||
*
|
||
* @param {Object} data 请求参数(template_id/ext_json/user_version/user_desc)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async commit(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.commit(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '上传成功', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('上传代码失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取体验版二维码(需登录)。
|
||
*
|
||
* @param {Object} data 请求参数(可选 path)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async getTrialQRCode(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.getTrialQRCode(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '获取成功', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('获取体验版二维码失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 提交代码审核(需登录)。
|
||
*
|
||
* @param {Object} data 请求参数(item_list/version_desc 等,均为可选)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async submitAudit(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.submitAudit(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '提交成功', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('提交代码审核失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 拉取当前仓库小程序的用户列表(需登录,支持 where/pagination/sort)。
|
||
*
|
||
* @param {Object} data 请求参数(可选 app_id/where/pagination/sort)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async userList(data = {}) {
|
||
try {
|
||
// where 字段白名单:仅允许按用户表常规字段过滤,禁止探测敏感列
|
||
const allowedWhereFields = new Set([
|
||
'id',
|
||
'user_id',
|
||
'app_id',
|
||
'openid',
|
||
'unionid',
|
||
'nickname',
|
||
'sex',
|
||
'login_time',
|
||
'login_count',
|
||
'created_at',
|
||
'updated_at',
|
||
]);
|
||
const rawWhere = data.where && typeof data.where === 'object' ? data.where : {};
|
||
const filteredWhere = {};
|
||
for (const key of Object.keys(rawWhere)) {
|
||
if (allowedWhereFields.has(key)) {
|
||
filteredWhere[key] = rawWhere[key];
|
||
}
|
||
}
|
||
data.where = filteredWhere;
|
||
|
||
this.prepareWhereContext(data);
|
||
const result = await this.service.systems.wechat.userList(data);
|
||
if (result.code) {
|
||
return this.success(result.message || '获取成功', result.data);
|
||
}
|
||
return this.fail(result.message);
|
||
} catch (e) {
|
||
return this.fail('获取用户列表失败:' + e.message);
|
||
}
|
||
}
|
||
}
|
||
|
||
module.exports = WechatService;
|