main481.js 4 KB
require('YH_PaymentViewModel','YH_InvoiceModel','YH_Global')
defineClass('YH_PaymentViewModel',{
    invoiceCellSubtitle : function(){
            
        var invoicesTypeString = "";
        var model = self.invoiceModel();
        var type = model.invoiceType();

        var isOpen = require('YH_Global').sharedInstance().isOpenElectronicInvoice();

        if (isOpen && type == 2) {
            invoicesTypeString = "电子";
        }else{
            self.invoiceModel().setInvoiceType(1);
            invoicesTypeString = "纸质";
        }

        return require('NSString').stringWithFormat('%@(%@)',self.invoiceModel().invoiceTypeName(),invoicesTypeString);
    }
})

defineClass('YH_InvoiceModel',{
    analysisDicWithDictionary :function(dic){
         if (!dic) {
            return;
         }

        self.setInvoiceContentList(dic.yh__arrayForKey('invoiceContentList'));
        self.setInvoicesTypeList(dic.yh__arrayForKey('invoices_type_list'));
    
         //与后台沟通 默认数组第一个 invoice信息
        if (self.invoiceContentList().count() > 0) {
            var tempDic = self.invoiceContentList().objectAtIndex(0);
            self.setInvoicesTypeId(tempDic.yh__integerForKey('invoices_type_id'));
            self.setInvoiceTypeName(tempDic.yh__stringForKey('invoices_type_name'));
        }

        var isOpen = require('YH_Global').sharedInstance().isOpenElectronicInvoice();
        
        self.setInvoiceType(isOpen ? 2 : 1);
        self.setIsCompanyInvoice(false);
        self.setInvoiceTitle("个人");
        self.setCompanyName("");
        self.setPhoneNumber("");
    }
})

defineClass('YH_Global',{
    isOpenElectronicInvoice : function (){
        var isOpenElectronicInvoice = require('NSUserDefaults').standardUserDefaults().objectForKey('kUserConfigSei');
        var isOpen = self.isOpenSei(isOpenElectronicInvoice);
        return isOpen;
    },

    isOpenSei : function(sei){
        if (sei.isKindOfClass(require('NSString').class())) {
            if (sei.isEqualToString('1')) {
                return true;
            }
            return false;
        }else{
            return true;
       } 
    }
})

require('NSURLProtocol,NSHTTPURLResponse,NSURL,NSURLResponse');
defineClass('YH_H5URLProtocol', {
    connection_didReceiveResponse: function(connection, response) {

        var originalUrl = connection.currentRequest().valueForHTTPHeaderField("originalUrl");
        if (!originalUrl) {
            originalUrl = response.URL().absoluteString();
        }
        if (response.isKindOfClass(NSHTTPURLResponse.class())) {
            var httpResponse = response;
            var mutableAllHeaderFields = httpResponse.allHeaderFields().mutableCopy();
            mutableAllHeaderFields.setObject_forKey("*", "Access-Control-Allow-Origin");
            var retResponse = NSHTTPURLResponse.alloc().initWithURL_statusCode_HTTPVersion_headerFields(NSURL.URLWithString(originalUrl), httpResponse.statusCode(), "HTTP/1.1", mutableAllHeaderFields);

            self.client().URLProtocol_didReceiveResponse_cacheStoragePolicy(self, retResponse, 2);
        } else {
            var retResponse = NSURLResponse.alloc().initWithURL_MIMEType_expectedContentLength_textEncodingName(NSURL.URLWithString(originalUrl), response.MIMEType(), response.expectedContentLength(), response.textEncodingName());
            self.client().URLProtocol_didReceiveResponse_cacheStoragePolicy(self, retResponse, 2);
        }
    }
}, {
    canInitWithRequest: function(request) {
        if (NSURLProtocol.propertyForKey_inRequest("URLProtocolHandledKey", request)) {
            return NO;
        }

        var requestURL = request.URL();
        if (!self.supportedSchemes().containsObject(requestURL.scheme())) {
            return NO;
        }

        if (requestURL.host().isEqualToString("m.yohobuy.com")) {
            return YES;
        }

        var webViewType = request.valueForHTTPHeaderField("X-YH-Request-DNS-H5");
        if (webViewType.hasSuffix("iOS")) {
            return YES;
        }

        return NO;
    }
});