<commit_before>
import Foundation

extension MonkeyKing {

    public class func oauth(for platform: SupportedPlatform, scope: String? = nil, requestToken: String? = nil, dataString: String? = nil, completionHandler: @escaping OAuthCompletionHandler) {
        guard platform.isAppInstalled || platform.canWebOAuth else {
            completionHandler(.failure(.noApp))
            return
        }
        guard let account = shared.accountSet[platform] else {
            completionHandler(.failure(.noAccount))
            return
        }

        shared.oauthCompletionHandler = completionHandler
        shared.payCompletionHandler = nil
        shared.deliverCompletionHandler = nil
        shared.openSchemeCompletionHandler = nil

        switch account {
        case .alipay(let appID):

            guard let dataStr = dataString else {
                completionHandler(.failure(.apiRequest(.missingParameter)))
                return
            }

            let appUrlScheme = "apoauth" + appID
            let resultDic: [String: String] = ["fromAppUrlScheme": appUrlScheme, "requestType": "SafePay", "dataString": dataStr]

            guard var resultStr = resultDic.toString else {
                completionHandler(.failure(.sdk(.urlEncodeFailed)))
                return
            }

            resultStr = resultStr.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: " ", with: "")
            resultStr = resultStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? resultStr
            resultStr = "alipay://alipayclient/?" + resultStr

            guard let url = URL(string: resultStr) else {
                completionHandler(.failure(.sdk(.urlEncodeFailed)))
                return
            }

            shared.openURL(url) { flag in
                if flag { return }
                completionHandler(.failure(.sdk(.invalidURLScheme)))
            }

        case .weChat(let appID, _, _, let universalLink):
            let scope = scope ?? "snsapi_userinfo"

            if !platform.isAppInstalled {
                // SMS OAuth
                // uid??
                let accessTokenAPI = "https://open.weixin.qq.com/connect/mobilecheck?appid=\(appID)&uid=1926559385"
                addWebView(withURLString: accessTokenAPI)
            } else {
                var urlComponents: URLComponents?
                var wxUrlOptions = [UIApplication.OpenExternalURLOptionsKey : Any]()

                if let universalLink = universalLink, let authLink = shared.wechatUniversalLink(of: "auth"), #available(iOS 10.0, *) {
                    urlComponents = URLComponents(string: authLink)
                    urlComponents?.queryItems?.append(contentsOf: [
                        URLQueryItem(name: "scope", value: scope),
                        URLQueryItem(name: "state", value: "123"), // Weixinauth instead?
                    ])

                    shared.setPasteboard(of: appID, with: [
                        "universalLink": universalLink,
                        "isAuthResend": false,
                        "command": "0"
                    ])

                    wxUrlOptions[.universalLinksOnly] = true
                } else {
                    urlComponents = URLComponents(string: "weixin://app/\(appID)/auth/")<commit_msg>Update MonkeyKing+OAuth.swift<commit_after>
import Foundation

extension MonkeyKing {

    public class func oauth(for platform: SupportedPlatform, scope: String? = nil, requestToken: String? = nil, dataString: String? = nil, completionHandler: @escaping OAuthCompletionHandler) {
        guard platform.isAppInstalled || platform.canWebOAuth else {
            completionHandler(.failure(.noApp))
            return
        }
        guard let account = shared.accountSet[platform] else {
            completionHandler(.failure(.noAccount))
            return
        }

        shared.oauthCompletionHandler = completionHandler
        shared.payCompletionHandler = nil
        shared.deliverCompletionHandler = nil
        shared.openSchemeCompletionHandler = nil

        switch account {
        case .alipay(let appID):

            guard let dataStr = dataString else {
                completionHandler(.failure(.apiRequest(.missingParameter)))
                return
            }

            let appUrlScheme = "apoauth" + appID
            let resultDic: [String: String] = ["fromAppUrlScheme": appUrlScheme, "requestType": "SafePay", "dataString": dataStr]

            guard var resultStr = resultDic.toString else {
                completionHandler(.failure(.sdk(.urlEncodeFailed)))
                return
            }

            resultStr = resultStr.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: " ", with: "")
            resultStr = resultStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? resultStr
            resultStr = "alipay://alipayclient/?" + resultStr

            guard let url = URL(string: resultStr) else {
                completionHandler(.failure(.sdk(.urlEncodeFailed)))
                return
            }

            shared.openURL(url) { flag in
                if flag { return }
                completionHandler(.failure(.sdk(.invalidURLScheme)))
            }

        case .weChat(let appID, _, _, let universalLink):
            let scope = scope ?? "snsapi_userinfo"

            if !platform.isAppInstalled {
                // SMS OAuth
                // uid??
                let accessTokenAPI = "https://open.weixin.qq.com/connect/mobilecheck?appid=\(appID)&uid=1926559385"
                addWebView(withURLString: accessTokenAPI)
            } else {
                var urlComponents: URLComponents?
                var wxUrlOptions = [UIApplication.OpenExternalURLOptionsKey : Any]()

                if let universalLink = universalLink, let authLink = shared.wechatUniversalLink(of: "auth"), #available(iOS 10.0, *) {
                    urlComponents = URLComponents(string: authLink)
                    urlComponents?.queryItems?.append(contentsOf: [
                        URLQueryItem(name: "scope", value: scope),
                        URLQueryItem(name: "state", value: "Weixinauth"), // Weixinauth instead?
                    ])

                    shared.setPasteboard(of: appID, with: [
                        "universalLink": universalLink,
                        "isAuthResend": false,
                        "command": "0"
                    ])

                    wxUrlOptions[.universalLinksOnly] = true
                } else {
                    urlComponents = URLComponents(string: "weixin://app/\(appID)/auth/")
{'fraction_non_alphanumeric': 0.0977657819763083, 'fraction_numerical': 0.004648373069425701, 'mean_word_length': 1.8663515255694028, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '514252', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1597, 'n_words': 342}