温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nodejs之egg框架mysql应用

发布时间:2020-07-25 08:35:28 来源:网络 阅读:886 作者:小涴熊 栏目:web开发

config/plugin.js

mysql: {
    enable: true,
    package: 'egg-mysql',
}

config/config.default.js

config.mysql = {
        client: {
            host: '127.0.0.1',
            port: '3306',
            user: 'devuser',
            password: 'devuser123',
            database: 'db_co_lottery',
        },
        // 是否加载到 app 上,默认开启
        app: true,
        // 是否加载到 agent 上,默认关闭
        agent: false
 };

service类

'use strict';
const Service = require('egg').Service;
class NumberService extends Service {
    /**
     * 获取所有Number
     * @returns {Promise<Array>}
     */
    async getAllNumbers() {
        let sql = 'SELECT zodiac,zodiac_img,punching FROM t_luckdraw_numbers GROUP BY zodiac,zodiac_img,punching ORDER BY sort';
        let res = await this.app.mysql.query(sql);
        let result = [];
        let sqlt = 'SELECT * FROM t_luckdraw_numbers WHERE zodiac = ? ORDER BY sort ASC';
        for (let i = 0; i < res.length; i++) {
            let object = {zodiac: res[i].zodiac, zodiac_img: res[i].zodiac_img, punching: res[i].punching};
            let numbers = await this.app.mysql.query(sqlt, res[i].zodiac);
            let numberArr = [];
            for (let j = 0; j < numbers.length; j++) {
                let numObj = {
                    number: numbers[j].number,
                    wave_color: numbers[j].wave_color,
                    five_elements: numbers[j].five_elements,
                    single_double: numbers[j].single_double,
                    big_small: numbers[j].big_small
                };
                numberArr.push(numObj);
            }
            object['numbers'] = numberArr;
            result.push(object);
        }
        return result;
    }

    /**
     * 获取开奖结果
     * @returns {Promise<void>}
     */
    async getLastResult() {
        let sql = 'SELECT lr.*,ln.wave_color FROM t_luckdraw_result lr LEFT JOIN t_luckdraw_numbers ln ON ln.number = lr.number WHERE lr.number IS NOT NULL ORDER BY lr.stage DESC LIMIT 0,1';
        let res = await this.app.mysql.query(sql);
        var result = {};
        result['this_stage'] = res[0].stage;
        result['this_number'] = res[0].number;
        result['this_wave_color'] = res[0].wave_color;
        sql = 'SELECT * FROM t_luckdraw_result WHERE number IS NULL ORDER BY create_time DESC LIMIT 0,1';
        res = await this.app.mysql.query(sql);
        result['next_stage'] = res[0].stage;
        result['next_time'] = this.ctx.helper.formatTime(res[0].end_time);
        result['next_time_format'] = this.ctx.helper.formatTime(res[0].end_time, 'MM月DD日HH时mm分');
        result['next_week'] = this.ctx.helper.getWeekOfDate(res[0].end_time);
        return result;
    }

    async getNumberInfo(numbers) {
        const sql = 'SELECT * FROM t_luckdraw_numbers WHERE number IN(' + numbers + ')';
        const result = await this.app.mysql.query(sql);
        return result;
    }
}

module.exports = NumberService;

controller类

'use strict';

const Controller = require('egg').Controller;

class NumberController extends Controller {

    async getNumbers() {
        const {ctx, service} = this;
        let result = await service.number.getAllNumbers();
        ctx.body = result;
        ctx.status = 200;
    }

    async getLuckdrawResult() {
        const {ctx, service} = this;
        let result = await service.number.getLastResult();
        ctx.body = result;
        ctx.status = 200;
    }

    async getNumberInfo() {
        const {ctx, service} = this;
        const numbers = this.ctx.request.query.numbers;
        if (numbers == null || numbers.split(',').length <= 0) {
            ctx.body = {
                msg: '参数有误'
            }
            return ctx.body;
        }
        let result = await service.number.getNumberInfo(numbers);
        ctx.body = result;
    }
}

module.exports = NumberController;
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI