1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import dxui from '../../../../../dxmodules/dxUi.js'
import std from '../../../../../dxmodules/dxStd.js'
import viewUtils from "../../../viewUtils.js"
import topView from "../../../topView.js"
import i18n from "../../../i18n.js"
import systemSettingView from '../systemSettingView.js'
import screen from '../../../../screen.js'
import sqliteService from '../../../../service/sqliteService.js'
import logger from '../../../../../dxmodules/dxLogger.js'
const passwordOpenDoorSettingView = {}
passwordOpenDoorSettingView.init = function () {
    /**************************************************创建屏幕*****************************************************/
    const screenMain = dxui.View.build('passwordOpenDoorSettingView', dxui.Utils.LAYER.MAIN)
    passwordOpenDoorSettingView.screenMain = screenMain
    screenMain.scroll(false)
    screenMain.bgColor(0xffffff)
    screenMain.on(dxui.Utils.ENUM.LV_EVENT_SCREEN_LOADED, () => {
        topView.changeTheme(true)
 
        const configAll = screen.getConfig()
        passwordOpenDoorSettingView.info[0].switch.select(configAll['sys.pwd'] == 1)
        // 加载应急开仓密码(从数据库)
        if (passwordOpenDoorSettingView.info[1].input) {
            try {
                const passwords = sqliteService.d1_emergency_password.findAll()
                if (passwords && passwords.length > 0) {
                    // 只显示第一个密码,因为应急开仓密码在设备中仅有唯一的1个
                    passwordOpenDoorSettingView.info[1].input.text(passwords[0].password || '')
                }
            } catch (error) {
                logger.error('加载应急开仓密码失败:', error)
            }
        }
    })
 
    const titleBox = viewUtils.title(screenMain, systemSettingView.screenMain, 'passwordOpenDoorSettingViewTitle', 'systemSettingView.passwordOpenDoorSetting')
    titleBox.align(dxui.Utils.ALIGN.TOP_MID, 0, 70)
 
    passwordOpenDoorSettingView.info = [
        {
            title: "systemSettingView.passwordOpenDoor",
            type: 'switch',
        },
        {
            title: "应急开仓密码",
            type: 'input',
            placeholder: '请输入至少8位应急开仓密码',
            password: true
        }
    ]
 
    const passwordOpenDoorSettingBox = dxui.View.build('passwordOpenDoorSettingBox', screenMain)
    viewUtils._clearStyle(passwordOpenDoorSettingBox)
    passwordOpenDoorSettingBox.align(dxui.Utils.ALIGN.TOP_MID, 0, 140)
    passwordOpenDoorSettingBox.setSize(screen.screenSize.width, 600)
    passwordOpenDoorSettingBox.bgOpa(0)
    passwordOpenDoorSettingBox.flexFlow(dxui.Utils.FLEX_FLOW.ROW_WRAP)
    passwordOpenDoorSettingBox.flexAlign(dxui.Utils.FLEX_ALIGN.CENTER, dxui.Utils.FLEX_ALIGN.START, dxui.Utils.FLEX_ALIGN.START)
    passwordOpenDoorSettingBox.obj.lvObjSetStylePadGap(0, dxui.Utils.ENUM._LV_STYLE_STATE_CMP_SAME)
    passwordOpenDoorSettingBox.borderWidth(1)
    passwordOpenDoorSettingBox.setBorderColor(0xDEDEDE)
    passwordOpenDoorSettingBox.obj.setStyleBorderSide(dxui.Utils.ENUM.LV_BORDER_SIDE_TOP, 0)
 
    passwordOpenDoorSettingView.info.forEach(item => {
        const itemBox = dxui.View.build(item.title, passwordOpenDoorSettingBox)
        viewUtils._clearStyle(itemBox)
        itemBox.setSize(760, 76)
        itemBox.borderWidth(1)
        itemBox.setBorderColor(0xDEDEDE)
        itemBox.obj.setStyleBorderSide(dxui.Utils.ENUM.LV_BORDER_SIDE_BOTTOM, 0)
 
        const itemLabel = dxui.Label.build(item.title + 'Label', itemBox)
        itemLabel.dataI18n = item.title
        itemLabel.align(dxui.Utils.ALIGN.LEFT_MID, 0, 0)
        itemLabel.textFont(viewUtils.font(26))
 
        if (item.unit) {
            const unitLabel = dxui.Label.build(item.title + 'UnitLabel', itemBox)
            unitLabel.text(item.unit)
            unitLabel.align(dxui.Utils.ALIGN.RIGHT_MID, 0, 0)
            unitLabel.textFont(viewUtils.font(26))
        }
 
        switch (item.type) {
            case 'switch':
                const __switch = dxui.Switch.build(item.title + 'switch', itemBox)
                __switch.align(dxui.Utils.ALIGN.RIGHT_MID, 0, 0)
                __switch.setSize(70, 35)
                item.switch = __switch
                break;
            case 'input':
                const input = viewUtils.input(itemBox, item.title + 'input', undefined, undefined, item.placeholder)
                input.align(dxui.Utils.ALIGN.RIGHT_MID, 0, 0)
                input.setSize(300, 40)
                input.setPasswordMode(item.password)
                item.input = input
                break;
        }
    })
 
    const saveBtn = viewUtils.bottomBtn(screenMain, screenMain.id + 'saveBtn', 'systemSettingView.save', () => {
        const emergencyPassword = passwordOpenDoorSettingView.info[1].input.text()
        const saveConfigData = {
            sys: {
                pwd: passwordOpenDoorSettingView.info[0].switch.isSelect() ? 1 : 0
            }
        }
        
        // 更新数据库中的应急开仓密码
        try {
            // 先删除旧的应急开仓密码
            sqliteService.d1_emergency_password.deleteAll()
            // 添加新的应急开仓密码
            if (emergencyPassword) {
                sqliteService.d1_emergency_password.save({
                    id: 'emergency_' + Date.now(),
                    password: emergencyPassword,
                    description: '系统设置的应急开仓密码',
                    createTime: Date.now(),
                    updateTime: Date.now(),
                    status: 1
                })
            }
        } catch (error) {
            logger.error('更新应急开仓密码失败:', error)
        }
        
        const res = screen.saveConfig(saveConfigData)
        if (res === true) {
            passwordOpenDoorSettingView.statusPanel.success()
            std.setTimeout(() => {
                // 成功返回上一层界面
                dxui.loadMain(systemSettingView.screenMain)
            }, 500)
        } else {
            passwordOpenDoorSettingView.statusPanel.fail()
        }
    })
    saveBtn.align(dxui.Utils.ALIGN.BOTTOM_MID, 0, -83)
 
    passwordOpenDoorSettingView.statusPanel = viewUtils.statusPanel(screenMain, 'systemSettingView.success', 'systemSettingView.fail')
}
 
export default passwordOpenDoorSettingView