import {
    ApiModel
} from './utils/api';

App({
    onLaunch(options) {
        //小程序跳转进入
        if (options.query.userMsg) {
            wx.removeStorageSync('userMsg')
            wx.removeStorageSync('auth')
            wx.removeStorageSync('userauth');
            wx.setStorageSync('userMsg', JSON.parse(options.query.userMsg));
            wx.setStorageSync('auth', JSON.parse(options.query.auth));
            wx.setStorageSync('siteP', JSON.parse(options.query.site));
            //获取用户权限 
            this.globalData.http.basic({
                "classname": "sysmanage.develop.userauth.userauth",
                "method": "query_userauth",
                content: {
                    nocache: true
                }
            }).then(res => {
                console.log("跳转进入查询权限", res)
                if (res.msg != '成功') return wx.showToast({
                    title: '权限查询失败，请稍后再试',
                    icon: "none"
                })
                wx.setStorageSync('userauth', res.data);
            });
            if (!wx.getStorageSync('templetList')) this.globalData.http.basic({
                "classname": "sysmanage.develop.querytemplet.querytemplet",
                "method": "query",
                "content": {}
            }).then(res => {
                console.log("获取列表查询条件", res)
                if (res.msg != '成功') return;
                let data = res.data.find(v => v.templetname == '站点全部');
                if (data) {
                    res.data = res.data.filter(v => v.templetname != '站点全部')
                    res.data.unshift(data)
                }
                wx.setStorageSync('templetList', res.data)
                getCurrentPages()[getCurrentPages().length - 1].getList && getCurrentPages()[getCurrentPages().length - 1].getList(true);
            })
        };
        wx.setStorageSync('appid', 'wx197f219a82a89d7b');
    },
    initSocket() {
        let that = this;
        this.globalData.SocketTask = wx.connectSocket({
            url: 'wss://oms.idcgroup.com.cn:8079/yos/webSocket/' + wx.getStorageSync('userMsg').token,
            complete: (res) => {
                console.log(res)
            }
        })
        this.globalData.SocketTask.onOpen(function (res) {
            that.globalData.socketEstablish = true;
        })
        this.globalData.SocketTask.onMessage(function (res) {
            that.globalData.socketCallback(res)
        })
        this.globalData.SocketTask.onError(function (res) {
            that.globalData.socketEstablish = false;
        })
        this.globalData.SocketTask.onClose(function (res) {
            that.globalData.socketEstablish = false;
        })
    },
    /**
     * 查询是否为数据代理人
     * @param {string} ownertable 数据表名
     * @param {number} ownerid 数据id
     */
    agentOrNot: (ownertable, ownerid) => getApp().globalData.http.basic({
        "id": 20230329122604,
        "content": {
            ownertable,
            ownerid
        }
    }),
    globalData: {
        http: new ApiModel(), //接口文件
        queryPer: require("./utils/queryPermissions"), //权限查询
        socketEstablish: false, //是否已经建立socket
        SocketTask: '', // Socket方法
        socketCallback: null, // Socket回调
        handleSelect: null, //处理选择结果  函数
    },
})