'use strict'; const Service = require('../core/base_service'); const fileName = "Contest"; const { Op } = require('sequelize'); class ContestService extends Service { async loginUser(name) { const { ctx } = this; try { const where = { account_name: name }; const result = await ctx.model4.Account.findOne({ where }); return result || false; } catch (error) { const blockName = fileName + "--" + "loginUser"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } async getUser(uid) { const { ctx } = this; try { const attributes = ['account_name', 'status']; const where = { uid }; const result = await ctx.model4.Account.findOne({ attributes, where }); return result || false; } catch (error) { const blockName = fileName + "--" + "getUser"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } async editUserManager(editMsg, userMsg) { const { ctx } = this; try { delete userMsg.location; const where = { ...userMsg }; const result = await ctx.model4.Account.update(editMsg, { where }); return result[0] == 0 ? false : result; } catch (error) { const blockName = fileName + "--" + "editUserManager"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } async editUser(userMsg, uid) { const { ctx } = this; try { const where = { uid }; const result = await ctx.model4.Account.update(userMsg, { where }); return result[0] == 0 ? false : result; } catch (error) { const blockName = fileName + "--" + "editUser"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } async uidfinder(uid) { const { ctx } = this; try { const where = { uid }; const result = await ctx.model4.Account.findOne({ where }); return result || false; } catch (error) { const blockName = fileName + "--" + "uidfinder"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } async changePassword(uid, oldPassword, newPassword) { const { ctx } = this; try { const where = { uid }; const salt = this.generateRandomString(); const setPassword = this.shaForm(newPassword, salt); const result = await ctx.model4.Account.findOne({ where }); if(!result || result.dataValues.status == 2 || result.dataValues.password !== this.shaForm(oldPassword, this.decryptRequest(result.dataValues.salt))) return false; const resultList = await ctx.model4.Account.update({ salt: this.encryptRequest(salt), status: 1, password: setPassword }, { where }); return resultList[0] == 0 ? false : resultList; } catch (error) { const blockName = fileName + "--" + "changePassword"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } async resetPassword(uid, modelType, defaultPassword, password, body, resetType) { const { ctx } = this; try { const managerWhere = { uid, role: [3, 6, 9] }; const result = await ctx.model3.Insiders.findOne({ where: managerWhere }); if(!result || result.dataValues.status == 2 || result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt))) return false; const salt = this.generateRandomString(); if(defaultPassword == "") { defaultPassword = '123456'; } const originPassword = this.shaForm(defaultPassword, salt); let where = {}; const organization = result.organization; const role = { [Op.not]: 1 }; if(modelType == "single") { where = { role, organization, ...body }; delete where.location; } else if (modelType == "multiple") { let account_name = [], user_name = [], phone = []; for (const key in body) { body[key].forEach(element => { account_name.push(element.account_name); user_name.push(element.user_name); phone.push(element.phone); }); } where = { role, organization, account_name, user_name, phone }; } else if (modelType == "all") { where = { role, organization, }; } let updateObj = { salt: this.encryptRequest(salt), status: 0, password: originPassword }; if(resetType == "default") { updateObj.user_name = ""; updateObj.phone = ""; } const resetList = await ctx.model4.Account.update(updateObj, { where }); return resetList.length == 0 ? false : resetList; } catch (error) { const blockName = fileName + "--" + "resetPassword"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } async loginStatus(uid, login_status) { const { ctx } = this; try { const where = { uid }; await ctx.model4.Account.update({ login_status }, { where }); } catch (error) { const blockName = fileName + "--" + "loginStatus"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } } module.exports = ContestService;