#coding: utf-8
#-------------------------------------------------------------------
# 宝塔Windows面板
#-------------------------------------------------------------------
# Copyright (c) 2015-2019 宝塔软件(http:#bt.cn) All rights reserved.
#-------------------------------------------------------------------
# Author: 沐落 <cjxin@bt.cn>
#-------------------------------------------------------------------

#------------------------------
# AUTH验证接口
#------------------------------

from urllib import request
import public,time,json,os,re

try:
    from BTPanel import session,cache
except:
    pass


class panelAuth:
    __product_list_path = 'data/product_list.pl'
    __product_bay_path = 'data/product_bay.pl'
    __product_id = '100000030'

    def create_serverid(self,get):
        """
        创建系统唯一标识
        """
        try:
            userPath = '{}/data/userInfo.json'.format(public.get_panel_path())
            if not os.path.exists(userPath): return public.returnMsg(False,'请先登录宝塔官网用户')
            tmp = public.readFile(userPath)
            data = json.loads(tmp)
            if not data: return public.returnMsg(False,'请先登录宝塔官网用户')
            if not 'serverid' in data:
                data['serverid'] = self.get_serverid()
                public.writeFile(userPath,json.dumps(data))
            return data
        except :
            return public.returnMsg(False,'请先登录宝塔官网用户')


    def get_serverid(self,force = False):
        '''
            @name 重新生成serverid
            @author hwliang<2021-06-22>
            @return string
        '''
        serverid_file = '{}/data/sid.pl'.format(public.get_panel_path())
        if os.path.exists(serverid_file) and not force:
            serverid = public.readFile(serverid_file)
            if re.match("^\w{64}$",serverid): return serverid
        s1 = self.get_mac_address() + self.get_hostname()
        s2 = self.get_cpuname()
        serverid = public.md5(s1) + public.md5(s2)
        public.writeFile(serverid_file,serverid)
        return serverid

    def create_plugin_other_order(self,get):
        """
        创建插件订单
        @get.pid 插件ID
        @get.cycle 购买时间
        @get.type 1=续费订单
        """
        pdata = self.create_serverid(get)
        pdata['pid'] = get.pid
        pdata['cycle'] = get.cycle
        p_url = public.GetConfigValue('home') + '/api/Pluginother/create_order'
        if get.type == '1':
            pdata['renew'] = 1
            p_url = public.GetConfigValue('home') + '/api/Pluginother/renew_order'
        return json.loads(public.httpPost(p_url,pdata))

    def get_order_stat(self,get):
        """
        获取付费是否完成
        @get.type 1=续费订单
        """
        pdata = self.create_serverid(get)
        pdata['order_id'] = get.oid
        p_url = public.GetConfigValue('home') + '/api/Pluginother/order_stat'
        if get.type == '1':  p_url = public.GetConfigValue('home') + '/api/Pluginother/re_order_stat'
        return json.loads(public.httpPost(p_url,pdata))

    def check_serverid(self,get):
        """
        检查唯一标识
        """
        if get.serverid != self.create_serverid(get): return False
        return True



    def get_plugin_price(self,get):
        try:
            userPath = 'data/userInfo.json'
            if not os.path.exists(userPath):
                return public.returnMsg(False,'请先登陆宝塔官网帐号!')

            pid = 0
            if 'pid' in get: pid = get.pid
            if not pid:
                if not 'pluginName' in get:
                    return public.returnMsg(False,'参数错误1!')
                pid = self.get_plugin_info(get.pluginName)['id']

            if not pid: return public.returnMsg(False,'参数错误2!')

            params = {}
            params['pid'] = pid
            data = self.send_cloud('get_product_discount', params)
            return data
        except:
            del(session['get_product_list'])
            return public.returnMsg(False,'正在同步信息，请重试!' + public.get_error_info())


    def get_plugin_info(self,pluginName):
        """
        获取插件详情
        @pluginName 插件名称
        """
        data = self.get_business_plugin(None)
        if not data: return None
        for d in data:
            if d['name'] == pluginName: return d
        return None

    def get_plugin_list(self,get):
        try:

            if not session.get('get_product_bay') or not os.path.exists(self.__product_bay_path):
                data = self.send_cloud('get_order_list_byuser', {})
                if data: public.writeFile(self.__product_bay_path,json.dumps(data))
                session['get_product_bay'] = True
            data = json.loads(public.readFile(self.__product_bay_path))
            return data
        except: return None

    # def get_buy_code(self,get):
    #     """
    #     获取支付二维码
    #     """
    #     params = {}
    #     params['pid'] = get.pid
    #     params['cycle'] = get.cycle
    #     params['num'] = 1 #购买数量
    #     if 'num' in get: params['num'] = get.num
    #     if 'source' in get: params['source'] = get.source
    #     if 'pay_main' in get: params['pay_main'] = get.pay_main

    #     data = self.send_cloud('create_order', params)

    #     if not data: return public.returnMsg(False,'连接服务器失败!')
    #     if not 'data' in data: return data

    #     #cache.set(key,data,120)
    #     #cache.set('{}_buy_code_id'.format(data['data']['oid']),key,120)
    #     return data

    def get_buy_code(self, get):
        """
        获取支付二维码
        """
        if not hasattr(get, 'pid'):
            return public.returnMsg(False, '缺少参数pid')
        if not hasattr(get, 'cycle'):
            return public.returnMsg(False, '缺少参数cycle')
        params = {}
        params['pid'] = get.pid
        params['cycle'] = get.cycle
        params['num'] = 1 #购买数量
        params['version'] = public.version()
        if 'num' in get: params['num'] = get.num
        if 'source' in get: params['source'] = get.source
        if 'coupon' in get:params['coupon'] = get.coupon

        # key = '{}_{}_get_buy_code'.format(params['pid'], params['cycle'])
        # data = cache.get(key)
        # if data: return data

        data = self.send_cloud('create_order', params)
        if not data: return self.res_request_error()
        # cache.set(key, data, 120)
        # try:
        #     cache.set('{}_buy_code_id'.format(data['data']['oid']), key, 120)
        # except:pass

        if 'is_coupon' in data:
            if self.get_ignore_time(get):
                data['is_coupon'] = []
        return data


    def check_pay_status(self,get):
        """
        检查支付状态
        @get.id 支付id
        """
        params = {}
        params['id'] = get.id
        data = self.send_cloud('check_product_pays', params)
        if not data: return public.returnMsg(False,'连接服务器失败!')
        if data['status'] == True:
            self.flush_pay_status(get)
            if 'get_product_bay' in session: del(session['get_product_bay'])

            buy_oid = '_buy_code_id'.format(params['id'])
            buy_code_key = cache.get(buy_oid)
            if buy_code_key:
                cache.delete(buy_code_key)
                cache.delete(buy_oid)
        return data

    def get_wx_order_status(self,get):
        """
        检查支付状态
        @get.wxoid 支付id
        """
        params = {}
        params['wxoid'] = get.wxoid
        if 'kf' in get: params['kf'] = get.kf

        data = self.send_cloud('check_order_pay_status', params)
        if not data: return public.returnMsg(False,'连接服务器失败!')
        if data['status'] == True:
            self.flush_pay_status(get)
            if 'get_product_bay' in session: del(session['get_product_bay'])

            buy_oid = '_buy_code_id'.format(params['wxoid'])
            buy_code_key = cache.get(buy_oid)
            if buy_code_key:
                cache.delete(buy_code_key)
                cache.delete(buy_oid)
        return data

    def get_apply_copon(self,get):
        """
        获取优惠券
        @get.coupon 优惠券
        """
        params = {}
        data = self.send_cloud('apply_coupon', params)

        if not data: self.res_request_error()
        return data


    def get_ignore_time(self,get):
        """
        获取忽略时间
        @get.coupon 优惠券
        """
        try:
            limit =  int(public.readFile('data/ignore_coupon_time.pl'))
        except:limit = 0

        if limit == -100 or limit > time.time():
            return 1
        return 0

    def get_coupon_list(self,get):
        """
        获取优惠券
        @get.coupon 优惠券
        """
        #用户是否忽略
        if self.get_ignore_time(get):
            return []

        params = {}
        data = self.send_cloud('get_coupon_list', params)
        if not data:
            return []
            if type(data) == list:
                return []
            return self.res_request_error()
        return data

    def ignore_coupon_time(self,get):
        """
        获取优惠券
        @get.coupon 优惠券
        @get.limit_time 限制时间  永久 -100
        """
        if not hasattr(get, 'limit_time') or not get.limit_time: return public.returnMsg(False, '缺少参数limit_time或参数不能为空!')
        limit_time = int(get.limit_time)
        if limit_time < time.time() and limit_time > 0:
            return public.returnMsg(False,'时间不能小于当前时间!')

        public.writeFile('data/ignore_coupon_time.pl',str(limit_time))

        msg = '忽略成功,以后将不会显示优惠券信息.'
        if limit_time > 0: msg = '忽略成功,【{}】 之前将不再给你显示优惠券信息.'.format(public.format_date(times=limit_time))
        return public.returnMsg(True,msg)

    def create_plugin_other_order(self,get):
        if not hasattr(get, 'pid') or not hasattr(get, 'cycle'): return public.returnMsg(False, '参数错误!')
        pdata = self.create_serverid(get)
        pdata['pid'] = get.pid
        pdata['cycle'] = get.cycle
        p_url = public.GetConfigValue('home') + '/api/Pluginother/create_order'
        if get.type == '1':
            pdata['renew'] = 1
            p_url = public.GetConfigValue('home') + '/api/Pluginother/renew_order'
        try:
            res = public.httpPost(p_url, pdata)
            return json.loads(res)
        except:
            raise public.PanelError('{}\n{}\n连接官网失败，请检查网络状况!'.format(p_url, res))


    def flush_pay_status(self,get):
        """
        刷新支付状态
        """
        if 'get_product_bay' in session: del(session['get_product_bay'])
        data = self.get_plugin_list(get)
        if not data: return public.returnMsg(False,'连接服务器失败!')
        return public.returnMsg(True,'状态刷新成功!')

    def get_renew_code(self):
        pass

    def check_renew_code(self):
        pass

    def get_business_plugin(self,get):
        try:
            if not session.get('get_product_list') or not os.path.exists(self.__product_list_path):
                data = self.send_cloud('get_product_list', {})
                if data: public.writeFile(self.__product_list_path,json.dumps(data))
                session['get_product_list'] = True
            data = json.loads(public.readFile(self.__product_list_path))
            return data
        except: return None

    def get_ad_list(self):
        pass

    def check_plugin_end(self):
        pass

    def get_coupons(self,get):
        """
        @name 获取用户抵扣卷列表
        @param uid 用户id
        """
        params = {}
        data = self.send_cloud_v2('v2/user/get_coupons', params)
        return data

    def get_last_paid_time(self,get):
        """
        @name 获取用户抵扣卷列表
        @param uid 用户id
        """
        params = {}
        params['product_id'] = int(get.pid)
        data = self.send_cloud_v2('v2/order/product/get_last_paid_time', params)
        return data

    def get_credits(self,get):
        """
        @获取用户余额
        @param uid 用户id
        """
        params = {}
        data = self.send_cloud_v2('v2/user/get_credits', params)
        return data

    def get_re_order_status_plugin(self,get):
        """
        获取续费状态
        """
        params = {}
        params['pid'] = getattr(get,'pid',0)
        data = self.send_cloud('get_re_order_status', params)
        if not data: return public.returnMsg(False,'连接服务器失败!')
        if data['status'] == True:
            self.flush_pay_status(get)
            if 'get_product_bay' in session: del(session['get_product_bay'])
        return data

    def get_voucher_plugin(self,get):
        """
        获取抵扣卷列表
        @get.pid 产品id
        """
        params = {}
        params['pid'] = getattr(get,'pid',0)
        params['status'] = '0'
        data = self.send_cloud('get_voucher', params)
        if not data: return []
        return data

    def create_order_voucher_plugin(self,get):
        """
        抵扣卷续费插件
        @get.pid 插件id
        @get.code 抵扣卷编码
        """
        params = {}
        params['pid'] = getattr(get,'pid',0)
        params['code'] = getattr(get,'code',0)
        data = self.send_cloud('create_order_voucher', params)

        if not data: return public.returnMsg(False,'连接服务器失败!')
        if data['status'] == True:
            self.flush_pay_status(get)
            if 'get_product_bay' in session: del(session['get_product_bay'])
        return data

    def send_cloud(self,module,params):
        """
        插件订单
        """
        try:
            cloudURL = 'https://www.bt.cn/api/Plugin/'
            userInfo = self.create_serverid(None)
            if 'status' in userInfo:
                params['uid'] = 0
                params['serverid'] = ''
            else:
                params['uid'] = userInfo['uid']
                params['serverid'] = userInfo['serverid']
                params['access_key'] = userInfo['access_key']
            params['os'] = 'Windows'
            result = public.httpPost(cloudURL + module,params)
            #print(result)
            result = json.loads(result.strip())
            if not result: return None
            return result
        except:
            print(public.get_error_info())
            return None

    def send_cloud_pro(self,module,params):
        """
        专业版订单
        """
        try:
            cloudURL = 'https://www.bt.cn/api/invite/'
            userInfo = self.create_serverid(None)
            if 'status' in userInfo:
                params['uid'] = 0
                params['serverid'] = ''
            else:
                params['uid'] = userInfo['uid']
                params['serverid'] = userInfo['serverid']
                params['access_key'] = userInfo['access_key']
            params['os'] = 'Windows'
            result = public.httpPost(cloudURL + module,params)

            result = json.loads(result)
            if not result: return None
            return result
        except: return None


    def get_voucher(self,get):
        params = {}
        params['product_id'] = self.__product_id
        params['status'] = '0'
        data = self.send_cloud_pro('get_voucher', params)
        return data

    def get_order_status(self,get):
        params = {}
        data = self.send_cloud_pro('get_order_status', params)
        return data


    def get_product_discount_by(self,get):
        params = {}
        data = self.send_cloud_pro('get_product_discount_by', params)
        return data

    def get_re_order_status(self,get):
        params = {}
        data = self.send_cloud_pro('get_re_order_status', params)
        return data

    def create_order_voucher(self,get):
        code = getattr(get,'code','1')
        params = {}
        params['code'] = code
        data = self.send_cloud_pro('create_order_voucher', params)
        return data

    def create_order(self,get):
        cycle = getattr(get,'cycle','1')
        params = {}
        params['cycle'] = cycle
        data = self.send_cloud_pro('create_order', params)
        return data

    def get_mac_address(self):
        import uuid
        mac=uuid.UUID(int = uuid.getnode()).hex[-12:]
        return ":".join([mac[e:e+2] for e in range(0,11,2)])

    def get_hostname(self):
        import socket
        return socket.getfqdn(socket.gethostname())

    def get_cpuname(self):
        return public.ReadReg(r'HARDWARE\DESCRIPTION\System\CentralProcessor\0','ProcessorNameString').strip()

    #检测破解
    def check_crack(self):
        return True


    def get_suite_pay_result(self,get):
        """
        获取支付结果
        """
        pdata = {}
        pdata['order_id'] = get.order_id
        data = self.send_cloud_suite('payResult',pdata)
        if not data: return public.returnMsg(False,'连接服务器失败!')

        return data

    def create_suite_order(self,get):
        """
        创建插件订单
        @get.pid 插件ID
        @get.cycle 购买时间
        @get.type 1=续费订单
        """
        pdata = self.create_serverid(get)
        pdata['suite_id'] = get.suite_id
        pdata['cycle'] = get.cycle
        pdata['_ajax'] = 1

        #续费订单id
        pdata['order_id'] = 0
        if hasattr(get,'order_id'): pdata['order_id'] = get.order_id

        data = self.send_cloud_suite('buy', pdata)
        if not data: return public.returnMsg(False,'连接服务器失败!')

        return data

    def get_suite_list(self,get):
        """
        获取套件列表
        """
        suite_id = 0
        if hasattr(get,'suite_id'): suite_id = int(get.suite_id)

        data = self.send_cloud_suite('index', { 'suite_id':suite_id })
        if not data: return public.returnMsg(False,'连接服务器失败!')

        if suite_id:
            for item in data:
                if item['id'] == suite_id:
                    return item
        return data


    def send_cloud_suite(self,module,params):
        """
        套件销售订单
        """
        try:
            cloudURL = 'https://www.bt.cn/api/sale_suite/'
            userInfo = self.create_serverid(None)
            if 'status' in userInfo:
                params['uid'] = 0
                params['serverid'] = ''
            else:
                params['uid'] = userInfo['uid']
                params['serverid'] = userInfo['serverid']
                params['access_key'] = userInfo['access_key']
            params['os'] = 'Windows'
            result = public.httpPost(cloudURL + module,params)

            result = json.loads(result)
            if not result: return None
            return result
        except: return None

    """
    """
    def get_plugin_remarks(self,get):
        data = self.send_cloud_wpanel('get_plugin_remarks',{})
        if not data: return public.returnMsg(False,'连接服务器失败!')
        return data


    def set_user_adviser(self,get):
        params = {}
        params['status'] = get.status
        data = self.send_cloud_wpanel('set_user_adviser',params)
        if not data: return public.returnMsg(False,'连接服务器失败!')
        try:
            del(session['updateInfo'])
        except : pass
        return data

    def set_user_rec(self,get):
        params = {}
        data = self.send_cloud_wpanel('set_user_rec',params)
        if not data: return public.returnMsg(False,'连接服务器失败!')

        try:
            del(session['updateInfo'])
        except : pass

        return data

    def send_cloud_v2(self,module,params):
        try:
            cloudURL = 'https://www.bt.cn/api/'
            userInfo = self.create_serverid(None)
            if 'status' in userInfo:
                params['uid'] = 0
                params['serverid'] = ''
            else:
                params['uid'] = userInfo['uid']
                params['serverid'] = userInfo['serverid']
                params['access_key'] = userInfo['access_key']
            params['os'] = 'Windows'
            result = public.httpPost(cloudURL + module,params)

            result = json.loads(result)
            if not result: return None
            return result
        except: return None

    def send_cloud_wpanel(self,module,params):
        try:
            cloudURL = 'https://www.bt.cn/api/wpanel/'
            userInfo = self.create_serverid(None)
            if 'status' in userInfo:
                params['uid'] = 0
                params['serverid'] = ''
            else:
                params['uid'] = userInfo['uid']
                params['serverid'] = userInfo['serverid']
                params['access_key'] = userInfo['access_key']
            params['os'] = 'Windows'
            result = public.httpPost(cloudURL + module,params)

            result = json.loads(result)
            if not result: return None
            return result
        except: return None



    def rest_unbind_count(self,get):
        """
        @name 获取当前订单可解绑次数
        """
        params = {}
        data = self.send_cloud_v2('v2/authorization_v1/rest_unbind_count', params)
        return data

    def unbind_authorization(self,get):
        """
        @name 解绑专业版/企业版
        """
        params = {}
        data = self.send_cloud_v2('v2/authorization_v1/unbind_authorization', params)
        return data

    def get_pay_unbind_count(self,get):
        """
        @name 购买解绑次数
        """
        params = {}
        params['product_id'] = int(get.pid)
        data = self.send_cloud_v2('v2/order/unbind_count/create', params)
        return data

    def send_cloud_v2(self,module,params):
        try:
            cloudURL = 'https://www.bt.cn/api/'
            userInfo = self.create_serverid(None);
            if 'status' in userInfo:
                params['uid'] = 0
                params['serverid'] = ''
            else:
                params['uid'] = userInfo['uid']
                params['serverid'] = userInfo['serverid']
                params['access_key'] = userInfo['access_key']
            params['os'] = 'Windows'
            result = public.httpPost(cloudURL + module,params)

            result = json.loads(result)
            if not result: return None
            return result
        except: return None
