- 微信开放平台:首次授权时写入系统默认用户须知作为首个协议版本 (ctx.service.systems.wechat.ensureDefaultAgreementVersion),含 seed / backfill 两个迁移 - 新增扫描报文接收接口 POST /scan:把「时间 + 条码」追加写入项目根目录 scan_records.xlsx (串行队列 + 原子写,避免并发写坏文件) - 新增 wms_container(容器/笼筐)模型与建表迁移 - 退件收货单 / 退件运单列表:like 查询条件支持字符串数组(多关键词 OR) - 新增 wms_return_unpack.created_at 索引迁移 - package.json:deploy 脚本改名为 depeeloy 注:scan_records.xlsx 是 /scan 接口写入的运行时数据文件,未纳入本次提交。
416 lines
14 KiB
JavaScript
416 lines
14 KiB
JavaScript
'use strict';
|
||
|
||
const MainService = require('../main');
|
||
|
||
/**
|
||
* 微信开放平台 method 层(system.json module: wechat)。
|
||
*/
|
||
class WechatService extends MainService {
|
||
constructor(ctx) {
|
||
super(ctx);
|
||
// getAuthUrl:需登录、免权限、不记系统日志;loginByCode/getUserAgreementText:免登录、免权限、免鉴权;
|
||
// pushLoginCode/queryauth/wxaauth/registerMiniprogram/getTemplateList/commit/getTrialQRCode/submitAudit/userList/getUserAgreement/setUserAgreement/getAgreementVersion:需登录、免权限(PC 已登录用户操作);
|
||
// bindUser/updateInfo/uploadHeadimg/getMyLoginCode:微信登录态中间层(_noNeedBind),未绑定真实用户也可访问
|
||
// _noNeedLog:除 loginByCode 外其余方法不记系统日志(loginByCode 保留日志,便于排查登录请求的入参出参)
|
||
this.systemInit('loginByCode,getUserAgreementText', 'getAuthUrl,pushLoginCode,queryauth,wxaauth,registerMiniprogram,getTemplateList,commit,getTrialQRCode,submitAudit,userList,getAuthorizerInfo,getUserAgreement,setUserAgreement,getAgreementVersion', 'getAuthUrl,getUserAgreementText,getMyLoginCode,bindUser,bindByEmployee,pushLoginCode,queryauth,wxaauth,registerMiniprogram,updateInfo,uploadHeadimg,getTemplateList,getAuthorizerInfo,commit,submitAudit,getTrialQRCode,userList,getUserAgreement,setUserAgreement,getAgreementVersion', 'getAuthUrl,pushLoginCode,queryauth,wxaauth,registerMiniprogram,getTemplateList,commit,getTrialQRCode,submitAudit,userList,getAuthorizerInfo,getUserAgreement,setUserAgreement,getAgreementVersion', '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 管理员操作):传工号 id 与微信绑定记录 id,
|
||
* 查工号的账号 id 写入 wms_user_oauth.user_id。
|
||
*
|
||
* @param {Object} data 请求参数(id 工号id、oauth_id 微信绑定记录id)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async bindByEmployee(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.bindByEmployee(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '绑定成功', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('绑定失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取小程序用户须知富文本(免登录):传入 appId,24h 缓存。
|
||
*
|
||
* @param {Object} data 请求参数(appId)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async getUserAgreementText(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.getUserAgreementText(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 请求参数(无)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async getUserAgreement(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.getUserAgreement(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 请求参数(text 富文本)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async setUserAgreement(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.setUserAgreement(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '设置成功', result);
|
||
}
|
||
return this.fail(result.message, result);
|
||
} catch (e) {
|
||
return this.fail('设置用户须知失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取指定协议版本内容(需登录免权限):传版本记录 id,校验仓库后返回协议内容。
|
||
*
|
||
* @param {Object} data 请求参数(id 协议版本记录id)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async getAgreementVersion(data) {
|
||
try {
|
||
const result = await this.service.systems.wechat.getAgreementVersion(data || {});
|
||
if (result.code) {
|
||
return this.success(result.message || '获取成功', 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);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 发起小程序认证(需登录):后端分步——图片字段传 COS 文件 URL,后端下载 →
|
||
* wxa/sec/uploadauthmaterial 换 mediaid → { auth_data } 包裹提交 wxa/sec/wxaauth。
|
||
*
|
||
* @param {Object} data 请求参数(新版 auth_data 字段白名单透传,详见 methods/system.json)。
|
||
* @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);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取小程序模板库列表(需登录,实时拉取,不缓存)。
|
||
*
|
||
* @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;
|