|
|
@ -2,64 +2,71 @@ |
|
|
|
|
|
|
|
const Service = require('egg').Service; |
|
|
|
const _ = require("lodash") |
|
|
|
const tencentcloud = require('tencentcloud-sdk-nodejs'); |
|
|
|
// 导入对应产品模块的client models。
|
|
|
|
const CvmClient = tencentcloud.iotexplorer.v20190423.Client |
|
|
|
|
|
|
|
class UserService extends Service { |
|
|
|
async index(params) { |
|
|
|
let { ctx } = this |
|
|
|
let modelLocal = ctx.model.User |
|
|
|
let returnData = { hasError: false, msg: "数据错误", list: [] } |
|
|
|
//新增
|
|
|
|
let subType = params.subType |
|
|
|
let returnComponent = params.returnData || [] |
|
|
|
let paramsLocal = _.omit(params, ["subType", "returnData"]) |
|
|
|
if (subType === "add") { |
|
|
|
paramsLocal["update_at"] = Date.now(); |
|
|
|
let res = await modelLocal.create(paramsLocal) |
|
|
|
returnData.hasError = false |
|
|
|
returnData.msg = "数据新增成功" |
|
|
|
returnData.list = res |
|
|
|
} else if (subType === "multi") { |
|
|
|
paramsLocal["update_at"] = Date.now(); |
|
|
|
let res = await modelLocal.insertMany(paramsLocal.list) |
|
|
|
returnData.hasError = false |
|
|
|
returnData.msg = "数据新增成功" |
|
|
|
returnData.list = res |
|
|
|
} else if (subType === "del") { |
|
|
|
let res = await modelLocal.updateMany({ _id: { $in: paramsLocal.ids } }, { isExit: 0, update_at: Date.now() }) |
|
|
|
returnData.hasError = false |
|
|
|
returnData.msg = "数据删除成功" |
|
|
|
returnData.list = res |
|
|
|
} else if (subType === "edit") { |
|
|
|
paramsLocal["update_at"] = Date.now(); |
|
|
|
let res = await modelLocal.updateOne({ _id: paramsLocal._id }, paramsLocal) |
|
|
|
returnData.hasError = false |
|
|
|
returnData.msg = "数据更新成功" |
|
|
|
returnData.list = res |
|
|
|
} else if (subType === "get") { |
|
|
|
let pageSize = paramsLocal.pageSize |
|
|
|
let currentPage = paramsLocal.currentPage |
|
|
|
let paramsLocalInfo = _.omit(paramsLocal, ["pageSize", "currentPage"]) |
|
|
|
let paramsLocalInfoNotDel = { ...paramsLocalInfo, isExit: 1 } |
|
|
|
let res = await modelLocal.find(paramsLocalInfoNotDel).sort({ update_at: -1 }).skip((currentPage - 1) * pageSize).limit(pageSize) |
|
|
|
let count = await modelLocal.find(paramsLocalInfoNotDel).count() |
|
|
|
let resData = []; |
|
|
|
for (let i = 0; i < res.length; i++) { |
|
|
|
let elei = res[i]; |
|
|
|
let resObj = { _id: elei._id } |
|
|
|
if (returnComponent.length) { |
|
|
|
for (let j = 0; j < returnComponent.length; j++) { |
|
|
|
let elej = returnComponent[j]; |
|
|
|
resObj[elej] = elei[elej] |
|
|
|
} |
|
|
|
} else { |
|
|
|
resObj = elei |
|
|
|
} |
|
|
|
resData.push(resObj) |
|
|
|
} |
|
|
|
returnData.hasError = false |
|
|
|
returnData.msg = "查询成功" |
|
|
|
returnData.list = resData |
|
|
|
returnData["count"] = count |
|
|
|
console.log(77777, params) |
|
|
|
// 新增
|
|
|
|
paramsLocal["update_at"] = Date.now(); |
|
|
|
let paramsLocal = { |
|
|
|
name: params.name, |
|
|
|
age: params.age, |
|
|
|
imgbase64: params.img, |
|
|
|
} |
|
|
|
let pngRes=await this.createPng(params) |
|
|
|
// let res = await modelLocal.create(paramsLocal)
|
|
|
|
returnData.hasError = false |
|
|
|
returnData.msg = "数据新增成功" |
|
|
|
returnData.list = res |
|
|
|
return returnData |
|
|
|
} |
|
|
|
async createPng(params) { |
|
|
|
let { ctx } = this |
|
|
|
let modelLocal = ctx.model.User |
|
|
|
let returnData = { hasError: false, msg: "数据错误", list: [] } |
|
|
|
console.log(77777, params) |
|
|
|
const client = new CvmClient({ |
|
|
|
// 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中,请参考本文凭证管理章节。
|
|
|
|
// 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
|
|
|
|
credential: { |
|
|
|
secretId: process.env.TENCENTCLOUD_SECRET_ID, |
|
|
|
secretKey: process.env.TENCENTCLOUD_SECRET_KEY, |
|
|
|
}, |
|
|
|
// 产品地域
|
|
|
|
region: "ap-chengdu", |
|
|
|
// 可选配置实例
|
|
|
|
profile: { |
|
|
|
signMethod: "TC3-HMAC-SHA256", // 签名方法
|
|
|
|
httpProfile: { |
|
|
|
reqMethod: "POST", // 请求方法
|
|
|
|
reqTimeout: 30, // 请求超时时间,默认60s
|
|
|
|
}, |
|
|
|
}, |
|
|
|
} |
|
|
|
); |
|
|
|
const params = { |
|
|
|
// 这里填写接口所需的参数,例如:
|
|
|
|
ImageUrl: params.img, // 图片URL
|
|
|
|
Age: params.age, // 想要改变的年龄,比如50表示变老50岁
|
|
|
|
// 其他参数根据接口文档填充
|
|
|
|
}; |
|
|
|
|
|
|
|
let res = client.ChangeFaceAge(params) |
|
|
|
console.log(777, res) |
|
|
|
return returnData |
|
|
|
} |
|
|
|
async downloadPng(params) { |
|
|
|
let { ctx } = this |
|
|
|
let modelLocal = ctx.model.User |
|
|
|
let returnData = { hasError: false, msg: "数据错误", list: [] } |
|
|
|
console.log(77777, params) |
|
|
|
//查询后下载8张图片
|
|
|
|
return returnData |
|
|
|
} |
|
|
|
} |
|
|
|