'use strict'; const shopController = require('./shop.js'); // 用户申请代理记录 module.exports = class ProxyApplyLogsController extends shopController { // 使用模型 get useModel() { const that = this; return that.app.model.ProxyApplyLogs; } /** * [selectValidate 查询验证器] * @return {{user_id: *}} [description] */ get selectValidate() { const that = this; return { user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), }; } /** * [createValidate 添加验证器] * @return {[type]} [description] */ get createValidate() { const that = this; return { user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), real_name: that.ctx.rules.name('真实姓名') .required() .notEmpty() .trim(), phone_num: that.ctx.rules.name('电话号码') .required() .notEmpty() .trim() .phone(), phone_sms: that.ctx.rules.name('短信验证码') .required() .notEmpty() .trim(), id_num: that.ctx.rules.name('身份证号码') .required() .notEmpty() .trim(), province_name: that.ctx.rules.name('所在省') .required() .notEmpty() .trim(), province_id: that.ctx.rules.name('所在省ID') .required() .notEmpty() .number(), city_id: that.ctx.rules.name('所在市ID') .required() .notEmpty() .number(), city_name: that.ctx.rules.name('所在城市') .required() .notEmpty() .trim(), county_id: that.ctx.rules.name('所在区/县ID') .required() .notEmpty() .number(), county_name: that.ctx.rules.name('所在区/县') .required() .notEmpty() .trim(), address: that.ctx.rules.name('详细地址') .required() .notEmpty() .trim(), admin_id: that.ctx.rules.default(-1) .number(), verify_status: that.ctx.rules.default(-1) .number(), create_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')) .required(), }; } /** * [updateValidate 更新验证器] * @return {[type]} [description] */ get updateValidate() { const that = this; return { user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), proxy_apply_id: that.ctx.rules.name('代理申请记录ID') .required() .notEmpty() .number(), update_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')) .required(), }; } /** * [deleteValidate 删除验证器] * @return {[type]} [description] */ get deleteValidate() { const that = this; return { user_id: that.ctx.rules.default(that.service.shop.getWebUserId()) .number(), proxy_apply_id: that.ctx.rules.name('代理申请记录ID') .required() .notEmpty() .number(), }; } /** * [select 查询代理申请记录] * @return {[type]} [description] */ async select() { const that = this; try { const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse()); const options = { where: { user_id: data.user_id } }; const selectBean = await that.app.comoBean.instance(data, options); const result = await that.service.base.select(selectBean, that.useModel, '代理认证记录查询失败,请稍候重试', false, false); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', false, false)); } } /** * [create 添加代理申请记录] * 记录:一个用户只有一条 * @return {[type]} [description] */ async create() { const that = this; try { const data = await that.ctx.validate(that.createValidate, await that.ctx.postParse()); const smsCode = data.phone_sms; const smsRedis = await that.service.redis.get(data.phone_num); // eslint-disable-next-line eqeqeq if (smsCode != smsRedis) { throw new Error('短信验证码错误'); } // 2022/12/13 短信验证码通过直接过审 data.verify_status = 1; const createBean = await that.app.comoBean.instance(data); const selectOptions = { where: { user_id: data.user_id } }; const selectResult = await that.useModel.findOne(selectOptions); let result; if (selectResult) { const updateBean = await that.app.comoBean.instance(data, { where: { user_id: data.user_id, proxy_apply_id: selectResult.proxy_apply_id, }, }); result = await that.service.base.update(updateBean, that.useModel, '更新所有代理申请记录失败,请稍后重试'); } else { result = await that.service.base.create(createBean, that.useModel, '代理认证申请信息上传失败,请稍后重试'); } return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [update 更新代理申请记录] * @return {[type]} [description] */ async update() { const that = this; try { const data = await that.ctx.validate(that.updateValidate, await that.ctx.anyParse()); const options = { where: { user_id: data.user_id, proxy_apply_id: data.proxy_apply_id } }; const updateBean = await that.app.comoBean.instance(data, options); const result = await that.service.base.update(updateBean, that.useModel, '更新代理申请记录失败,请稍候重试'); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [delete 删除代理申请记录] * @return {[type]} [description] */ async delete() { const that = this; try { const data = await that.ctx.validate(that.deleteValidate, await that.ctx.anyParse()); const options = { where: { user_id: data.user_id, proxy_apply_id: data.proxy_apply_id } }; const deleteBean = await that.app.comoBean.instance(data, options); const result = await that.service.base.delete(deleteBean, that.useModel, '删除代理申请记录失败,请稍候重试'); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * 发送短信验证码 * @return {Promise} */ async phoneSMS() { const that = this; try { const data = await that.ctx.getParse(); const mobile = data.phone_num; // const oldCode = await that.service.redis.get(mobile); const keys = [ 'sms_domain', 'sms_identity', 'sms_template_id', 'sms_sign' ]; const randCode = that.app.szjcomo.mt_rand(1000, 9999); await that.service.redis.set(mobile, randCode, 10 * 60); const config = await that.service.configs.getConfigMoreValue(keys); if (!Number(config.sms_template_id)) return false; const bodyData = { mobile, identity: config.sms_identity, params: [ randCode ], templateId: config.sms_template_id, sign: config.sms_sign, }; const response = await that.app.curl(config.sms_domain, { method: 'POST', dataType: 'json', contentType: 'json', data: bodyData, }); /** * response.data * message: 'SUCCESS', * result: '5tdf5k4kQj', * result_code: 0, * error: false */ if (response.data.error !== false) throw new Error(response.data.message); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', response.data, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } };