cla.js 11.9 KB
Newer Older
E
edisonxiang 已提交
1
$(function () {
2
    getCode();
E
edisonxiang 已提交
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
    initClaPage();
    initCurrentDate();
})

function initCurrentDate() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth();
    var date = now.getDate();
    month = month + 1;

    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;

    time = year + "-" + month + "-" + date;

    if ($('#individual-table').length) {
        $('#individual-date').val(time);
        $('#individual-date').attr("disabled", true);
    }
    if ($('#legalentity-table').length) {
        $('#legalentity-date').val(time);
        $('#legalentity-date').attr("disabled", true);
    }
}

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

function getCode() {
    code = getQueryString("code")
    if (!code || code == "") {
        oauthLogin();
    }
    else { 
        $('#oauth-code').val(code);
    }
} 

function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]); return null;
}

function getLanguage() {
        var url = window.location.href;
        if (url.indexOf("/en/") >= 0 ) {
            return "en";
        }
        if (url.indexOf("/zh/") >= 0 ) {
            return "zh";
        }
        return "en"
}

function oauthLogin() {
     code = $('#oauth-code').val();
     if (!code || code == "") {
         let config = {
              providerID: "openeuler_bot",
	      client_id: "88913556129bdae86458ec266f174b0cc5833198641e0e219891e7eb463bd3bf",
	      redirect_uri: "https://openeuler.org/en/cla.html",
              response_type: "code",
	      authorization: "https://gitee.com/oauth/authorize",
	      scopes: { request: ["user_info", "emails"]}
         }
         if (getLanguage() == "zh") {
             config = {
                 providerID: "openeuler_bot",
                 client_id: "6c298174d665b993c8a4dd56b0976654d3ef6f59af6c88f59b5b0c99f635c893",
                 redirect_uri: "https://openeuler.org/zh/cla.html",
                 response_type: "code",
                 authorization: "https://gitee.com/oauth/authorize",
                 scopes: { request: ["user_info", "emails"]} 
             
             }
         }
         let client = new jso.JSO(config)
         client.callback()

         let f = new jso.Fetcher(client)
         let url = 'https://gitee.com/v5/users/freesky-edward'
         f.fetch(url, {})
	      .then((data) => {
	          return data.json()
	      })
	      .then((data) => {
                  console.log("I got protected json data from the API", data)
              })
              .catch((err) => {
                  console.error("Error from fetcher", err)
	})
     }
}

E
edisonxiang 已提交
97 98 99
// init cla page
function initClaPage() {
    if ($('#cla-type-table').length) {
100 101 102
        // default show individual
        $('#individual-table').show();
        $('#legalentity-table').hide();
E
edisonxiang 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        $('td', '#cla-type-table').click(function () {
            // select checked
            $('input[name="track"]').removeAttr('checked');
            $(this).find('input[name="cla-type-radio"]').prop('checked', true);

            // show or hide
            v = $('input[name="cla-type-radio"]:checked').val();
            if (v == 0) {
                $('#individual-table').show();
                $('#legalentity-table').hide();
            } else {
                $('#individual-table').hide();
                $('#legalentity-table').show();
            }
        });
    }

    if ($('#sign-cla-button').length) {
        $("#sign-cla-button").bind('click', function () {
E
edisonxiang 已提交
122
            v = parseInt($('input[name="cla-type-radio"]:checked').val());
E
edisonxiang 已提交
123
            var regphone = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
E
edisonxiang 已提交
124
            var regmobile = /^1\d{10}$/;
存储OpenStack开源团队's avatar
存储OpenStack开源团队 已提交
125
            var regemail = /^[-_A-Za-z0-9.]+@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/;
E
edisonxiang 已提交
126 127 128 129 130 131 132 133
            $('#tip-cla-label').html('');
            lang = $('html').attr('lang');

            if (v == 0) {
                // individual
                var checkpass = true;
                $("input[type=text]", "#individual-table").each(function (i) {
                    if ($(this).hasClass("require")) {
E
edisonxiang 已提交
134
                        if ($.trim($(this).val()) == "") {
E
edisonxiang 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148
                            checkpass = false;
                            return false;
                        }
                    }
                    return true;
                });
                if (!checkpass) {
                    if (lang == "zh-cn") {
                        $('#tip-cla-label').html("必填字段缺失!");
                    } else {
                        $('#tip-cla-label').html("Required field is missing!");
                    }
                    return false;
                }
E
edisonxiang 已提交
149
                var email = $.trim($('#individual-email').val());
E
edisonxiang 已提交
150 151 152 153 154 155 156 157
                if (!regemail.test(email)) {
                    if (lang == "zh-cn") {
                        $('#tip-cla-label').html("邮箱格式不正确!");
                    } else {
                        $('#tip-cla-label').html("E-Mail format is incorrent!");
                    }
                    return false;
                }
E
edisonxiang 已提交
158
                var telephone = $.trim($('#individual-telephone').val());
159 160 161 162 163 164 165 166
                if (telephone != "") {
                    if ((!regphone.test(telephone)) && (!regmobile.test(telephone))) {
                        if (lang == "zh-cn") {
                            $('#tip-cla-label').html("电话格式不正确!");
                        } else {
                            $('#tip-cla-label').html("Telephone format is incorrent!");
                        }
                        return false;
E
edisonxiang 已提交
167 168 169 170 171 172 173
                    }
                }
            } else {
                // legalentity
                var checkpass = true;
                $("input[type=text]", "#legalentity-table").each(function (i) {
                    if ($(this).hasClass("require")) {
E
edisonxiang 已提交
174
                        if ($.trim($(this).val()) == "") {
E
edisonxiang 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188
                            checkpass = false;
                            return false;
                        }
                    }
                    return true;
                });
                if (!checkpass) {
                    if (lang == "zh-cn") {
                        $('#tip-cla-label').html("必填字段缺失!");
                    } else {
                        $('#tip-cla-label').html("Required field is missing!");
                    }
                    return false;
                }
E
edisonxiang 已提交
189
                var email = $.trim($('#legalentity-email').val());
E
edisonxiang 已提交
190 191 192 193 194 195 196 197
                if (!regemail.test(email)) {
                    if (lang == "zh-cn") {
                        $('#tip-cla-label').html("邮箱格式不正确!");
                    } else {
                        $('#tip-cla-label').html("E-Mail format is incorrent!");
                    }
                    return false;
                }
E
edisonxiang 已提交
198
                var telephone = $.trim($('#legalentity-telephone').val());
199 200 201 202 203 204 205 206
                if (telephone != "") {
                    if ((!regphone.test(telephone)) && (!regmobile.test(telephone))) {
                        if (lang == "zh-cn") {
                            $('#tip-cla-label').html("电话格式不正确!");
                        } else {
                            $('#tip-cla-label').html("Telephone format is incorrent!");
                        }
                        return false;
E
edisonxiang 已提交
207 208 209 210
                    }
                }
            }

E
edisonxiang 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
            // build json
            var posturl = $(this).attr("posturl");
            var jsonData = {};
            if (v == 0) {
                // individual
                jsonData = {
                    "type": v,
                    "name": $.trim($('#individual-name').val()),
                    "address": $.trim($('#individual-address').val()),
                    "date": $.trim($('#individual-date').val()),
                    "email": $.trim($('#individual-email').val()),
                    "telephone": $.trim($('#individual-telephone').val()),
                    "fax": $.trim($('#individual-fax').val()),
                };
            } else {
                // legalentity
                jsonData = {
                    "type": v,
                    "name": $.trim($('#legalentity-name').val()),
                    // only for legalentity
                    "title": $.trim($('#legalentity-title').val()),
                    // only for legalentity
                    "corporation": $.trim($('#legalentity-corporation').val()),
                    "address": $.trim($('#legalentity-address').val()),
                    "date": $.trim($('#legalentity-date').val()),
                    "email": $.trim($('#legalentity-email').val()),
                    "telephone": $.trim($('#legalentity-telephone').val()),
                    "fax": $.trim($('#legalentity-fax').val()),
239 240
                    "code": $.trim($('#oauth-code').val()),
                    "lang": $.trim($('#language').val()),
E
edisonxiang 已提交
241 242 243 244 245 246 247 248 249 250 251 252
                };
            }

            // send request
            $.ajax({
                type: "POST",
                url: posturl,
                data: JSON.stringify(jsonData),
                contentType: "application/json; charset=utf-8",
                crossDomain: true,
                datatype: "json",
                success: function (data) {
E
edisonxiang 已提交
253
                    if (data) {
254
                        if (data.isSuccess) {
E
edisonxiang 已提交
255 256 257 258 259 260 261
                            $("#reset-cla-button").trigger('click');
                            if (lang == "zh-cn") {
                                alert("签署成功!");
                            } else {
                                alert("Sign succeed!");
                            }
                        } else {
262
                            if (data.errorCode == 1) {
E
edisonxiang 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
                                if (lang == "zh-cn") {
                                    alert("服务器处理错误!");
                                } else {
                                    alert("Server handle error!");
                                }
                            } else if (data.errorCode == 2) {
                                if (lang == "zh-cn") {
                                    alert("邮箱已经被注册!");
                                } else {
                                    alert("E-Mail is already registered!");
                                }
                            } else if (data.errorCode == 3) {
                                if (lang == "zh-cn") {
                                    alert("电话已经被注册!");
                                } else {
                                    alert("Telephone is already registered!");
279
                                }
E
edisonxiang 已提交
280 281 282 283 284 285 286 287 288 289
                            }
                        }
                    }
                    else {
                        if (lang == "zh-cn") {
                            alert("签署失败!");
                        } else {
                            alert("Sign failed!");
                        }
                    }
E
edisonxiang 已提交
290 291
                },
                error: function () {
E
edisonxiang 已提交
292 293 294 295 296
                    if (lang == "zh-cn") {
                        alert("签署失败!");
                    } else {
                        alert("Sign failed!");
                    }
E
edisonxiang 已提交
297 298 299
                }
            });

E
edisonxiang 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313
            return false;
        });
    }

    if ($('#reset-cla-button').length) {
        $("#reset-cla-button").bind('click', function () {
            $('input[type="text"]', '#individual-table').val('');
            $('input[type="text"]', '#legalentity-table').val('');
            $('#tip-cla-label').html('');
            initCurrentDate();
            return false;
        });
    }
}