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

28 29 30 31 32 33 34

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;
}

35 36 37 38 39 40 41 42 43 44 45
function readCookie(name) {
    var namePrefix = name + "=";
    var cookies = document.cookie.split(';');
    for(var i=0; i < cookies.length; i++) {
        var c = cookies[i];
        while (c.charAt(0)==' ') c = c.substring(1, c.length);
        if (c.indexOf(namePrefix) == 0) return c.substring(namePrefix.length, c.length);
    }
    return null;
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59
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() {
         let config = {
              providerID: "openeuler_bot",
F
freesky-edward 已提交
60
	      client_id: $("#client").val(),
61
	      redirect_uri: window.location.origin + "/cla",
62 63 64 65
              response_type: "code",
	      authorization: "https://gitee.com/oauth/authorize",
	      scopes: { request: ["user_info", "emails"]}
         }
F
freesky-edward 已提交
66
 
67 68 69 70
         let client = new jso.JSO(config)
         client.callback()

         let f = new jso.Fetcher(client)
F
freesky-edward 已提交
71
         let url = 'https://gitee.com/v5/user'
72 73 74 75 76 77 78 79 80 81 82 83
         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 已提交
84 85
// init cla page
function initClaPage() {
86 87 88 89 90 91 92 93
    
    $("#client").val("d00e9b289d8cf8f98e2fc68a9c240304f6413007d82d30701cd1da7e937db75c");

    cla = readCookie("cla-info")
    if (!cla || cla == "") {
        oauthLogin();
    }
  
E
edisonxiang 已提交
94
    if ($('#cla-type-table').length) {
95 96 97
        // default show individual
        $('#individual-table').show();
        $('#legalentity-table').hide();
E
edisonxiang 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        $('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();
            }
        });
    }

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    type = readCookie("type")
    if (type && type == "0") {

        $('#individual-email').val(readCookie("email"));
        $('#individual-address').val(readCookie("address"));
        $('#individual-name').val(readCookie("name"));
        $('#individual-telephone').val(readCookie("telephone"));
        $('#individual-fax').val(readCookie("fax"));
        $('#individual-date').val(readCookie("date"));

        $('#individual-table').show();
        $('#legalentity-table').hide();
        
        $("#sign-cla-button").attr('disabled',true)
        $("#reset-cla-button").attr('disabled',true) 
    } else if (type && type == "1") {

        $('#legalentity-name').val(readCookie("name"));
        $('#legalentity-title').val(readCookie("title"));
        $('#legalentity-corporation').val(readCookie("corporation"));
        $('#legalentity-address').val(readCookie("address"));
        $('#legalentity-date').val(readCookie("date"));
        $('#legalentity-email').val(readCookie("email"));
        $('#legalentity-telephone').val(readCookie("telephone"));
        $('#legalentity-fax').val(readCookie("fax"));

        $('#individual-table').hide();
        $('#legalentity-table').show();
        $("#sign-cla-button").attr('disabled',true)
        $("#reset-cla-button").attr('disabled',true)

    }

    $('#individual-email').val(readCookie("email"));

E
edisonxiang 已提交
150 151
    if ($('#sign-cla-button').length) {
        $("#sign-cla-button").bind('click', function () {
E
edisonxiang 已提交
152
            v = parseInt($('input[name="cla-type-radio"]:checked').val());
E
edisonxiang 已提交
153
            var regphone = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
E
edisonxiang 已提交
154
            var regmobile = /^1\d{10}$/;
155
            var regemail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
E
edisonxiang 已提交
156 157 158 159 160 161 162 163
            $('#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 已提交
164
                        if ($.trim($(this).val()) == "") {
E
edisonxiang 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178
                            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 已提交
179
                var email = $.trim($('#individual-email').val());
E
edisonxiang 已提交
180 181 182 183 184 185 186 187
                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 已提交
188
                var telephone = $.trim($('#individual-telephone').val());
189 190 191 192 193 194 195 196
                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 已提交
197 198 199 200 201 202 203
                    }
                }
            } else {
                // legalentity
                var checkpass = true;
                $("input[type=text]", "#legalentity-table").each(function (i) {
                    if ($(this).hasClass("require")) {
E
edisonxiang 已提交
204
                        if ($.trim($(this).val()) == "") {
E
edisonxiang 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218
                            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 已提交
219
                var email = $.trim($('#legalentity-email').val());
E
edisonxiang 已提交
220 221 222 223 224 225 226 227
                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 已提交
228
                var telephone = $.trim($('#legalentity-telephone').val());
229 230 231 232 233 234 235 236
                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 已提交
237 238 239 240
                    }
                }
            }

E
edisonxiang 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253
            // 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()),
F
freesky-edward 已提交
254 255 256
                    "code": $.trim($('#oauth-code').val()),
                    "lang": $.trim($('#language').val()),
                    "client": $.trim($('#client').val()),
E
edisonxiang 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
                };
            } 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()),
272 273
                    "code": $.trim($('#oauth-code').val()),
                    "lang": $.trim($('#language').val()),
F
freesky-edward 已提交
274
                    "client": $.trim($('#client').val()),
E
edisonxiang 已提交
275 276 277 278 279 280 281 282 283 284 285 286
                };
            }

            // 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 已提交
287
                    if (data) {
288
                        if (data.isSuccess) {
E
edisonxiang 已提交
289 290 291 292 293 294 295
                            $("#reset-cla-button").trigger('click');
                            if (lang == "zh-cn") {
                                alert("签署成功!");
                            } else {
                                alert("Sign succeed!");
                            }
                        } else {
296
                            if (data.errorCode == 1) {
E
edisonxiang 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
                                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!");
313
                                }
314 315 316 317 318 319
                            } else if (data.errorCode == 4) {
                                if (lang == "zh-cn") {
                                    alert("注册邮箱与gitee账号邮箱不一致,请在这里检查:https://gitee.com/profile/emails")
                                } else {
                                    alert("The submit email is not the gitee account email. Please checck in https://gitee.com/profile/emails.");
                                } 
E
edisonxiang 已提交
320 321 322 323 324 325 326 327 328 329
                            }
                        }
                    }
                    else {
                        if (lang == "zh-cn") {
                            alert("签署失败!");
                        } else {
                            alert("Sign failed!");
                        }
                    }
E
edisonxiang 已提交
330 331
                },
                error: function () {
E
edisonxiang 已提交
332 333 334 335 336
                    if (lang == "zh-cn") {
                        alert("签署失败!");
                    } else {
                        alert("Sign failed!");
                    }
E
edisonxiang 已提交
337 338 339
                }
            });

E
edisonxiang 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353
            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;
        });
    }
}