signup.js 20.5 KB
Newer Older
txy committed
1

txy committed
2 3
var uploadFile = '';

txy committed
4
// 加载html结构
谭新宇 committed
5 6 7 8 9 10
loadHtmlJson(function(){
    // 判断是否需要移除机构名称
    if(rules.acActivityOrgs.length === 0) {
      $("#act11739232574395").parents(".form-row.m-b").remove();
    }
});
txy committed
11

txy committed
12 13
$(function () {
    setToken();
txy committed
14 15
    // 获取活动标题
    getActivityTitle();
txy committed
16 17 18
    removeAttr();
    btnBindClick();// 绑定点击事件
    $('#act11739232574395').attr({ 'readonly': 'readonly' });
txy committed
19 20 21
    getOrgNameFn(function(data){
        $('#act11739232574395').val(data.records.orgName)
    });
txy committed
22
    voteDialogBindFn();
txy committed
23
})
txy committed
24

txy committed
25 26 27 28
// 元素点击事件绑定
function btnBindClick() {
    // 返回首页
    $("#act11534485971254").off("click").on("click", function () {
txy committed
29 30
        var _t = getToken();
        transPage('1',activityCode,_t);
txy committed
31 32 33
    });
    // 排行榜
    $("#act41612547506702").off('click').on("click", function () {
txy committed
34 35 36
        var _t = getToken();
        var _addParam = 'toRanger=1'
        transPage('1',activityCode,_t,_addParam);
txy committed
37
    });
txy committed
38

txy committed
39 40
    // 选手主页    
    $("#act4161336684689").off("click").on("click", function () {
txy committed
41 42 43 44 45
        // 判断关注
        isAttention(function(){
            // 判断是否报名
            isSignUp(function(){
                var _t = getToken();
txy committed
46
                var urlParam = 'userId='+user.id;
txy committed
47
                transPage('3',activityCode,_t,urlParam);
txy committed
48 49 50 51 52 53 54 55
            },function(){
                $.dialog({
                    contentHtml: '<p style="text-align:center;">您还未报名,请到首页报名参加后本次活动!</p>',
                    onClosed: function(){
                        var _t = getToken();
                        transPage('1',activityCode,_t);
                    }
                });
txy committed
56 57
            });
        });
txy committed
58
    });
txy committed
59

txy committed
60 61 62 63
    // 活动秘籍   
    $("#act41613113951133").off("click").on("click", function () {
        $('#html-template-8').removeClass('global-none')
    });
胡畅 committed
64 65

    // 客服二维码弹框
66
    $("#html-template-9 .modal-close").off("click").on('click', function () {
胡畅 committed
67 68 69 70 71
        var _t = getToken();
        $('#html-template-9').addClass('global-none')
        transPage('1',activityCode,_t);
    });

txy committed
72
    
txy committed
73 74
    // 上传图片
    $("#uploadImage").on('change', function () {
txy committed
75 76 77 78 79 80 81 82
        var _file = this.files[0];
        // 图片大于1M压缩
        var filesize = _file.size / 1024 /1024;
        if(filesize > 1) {
            compressImage(_file, function(blob){
               // blob转file
               var newFile = blobTransferFile(blob);
                uploadFileFn(newFile);
txy committed
83
            });
txy committed
84 85
        }else {
            uploadFileFn(_file);
txy committed
86
        }
txy committed
87
    })
txy committed
88

胡畅 committed
89 90 91 92 93 94
    $('#act31419345204861').on('click',function() {
        if($(this).attr('src') != '') {
            $("#uploadImage").click();
        }
    })

txy committed
95 96 97 98 99 100
    // 确认提交    
    $("#act21359468086634").off("click").on("click", function () {
        var userName = $('#act11738585285947')
        var phonenum = $('#act11739141518360')
        var introDuce = $('#act11739381874636')
        var signInput = $('.sign-form .form-input-group input[c_validate="idcard"]')
胡畅 committed
101
        var addInput = JSON.parse(data.dataJson).addGroup
txy committed
102
        var uploadImg = $('#act31419345204861 img')
txy committed
103

txy committed
104 105 106
        var flag = validateForm();
        if(!flag) {
            return false;
txy committed
107
        }
txy committed
108

txy committed
109 110
        // 验证上传图片
        if (uploadImg.attr('src') == '') {
胡畅 committed
111 112 113 114 115
            $.dialog({
                type : 'tips',
                autoClose : 3000,
                infoText : '请上传一张照片'
            });
txy committed
116 117
            return false
        }
txy committed
118 119
        // var signupId = getQueryString('bindId')
        // var orgId = orgIdFn(signupId).orgId
谭新宇 committed
120 121
        // 当前不需要机构名称
        var orgId = rules.acActivityOrgs.length > 0 ? getQueryString('orgId') : null
txy committed
122
        var params = {
txy committed
123
            orgId: orgId,      //模板变量中获取,机构id
txy committed
124
            declaration: introDuce.val(),   //参赛宣言
胡畅 committed
125
            imageUrl: uploadFile,  //上传图片地址
txy committed
126 127 128
            name: userName.val(),  // 姓名
            userPhone: phonenum.val(),   //用户填写的手机号
        }
谭新宇 committed
129 130 131 132

        // 移除orgId
        orgId === null ? delete params.orgId : ''

txy committed
133 134 135
        if (addInput.length) {
            for (var i = 0; i < addInput.length; i++) {
                params['x'+ (i+1)] = $('#'+addInput[i].id).val()
txy committed
136
            }
txy committed
137
        }
胡畅 committed
138
        var _token = getToken();
txy committed
139
        $.ajax({
txy committed
140
            type: 'POST',
胡畅 committed
141
            headers: {'content-type': 'application/json','x-token': _token},
txy committed
142 143 144
            url: baseUrl + '/ACTIVITY/sz/sign-up/' + activityCode,
            data: JSON.stringify(params),
            success: function (data) {
txy committed
145
                if(data.status >= 200 && data.status < 300){
胡畅 committed
146 147 148 149 150 151 152 153
                    $('#html-template-9').removeClass('global-none')
                    // $.dialog({
                    //     showTitle : false,
                    //     contentHtml : '您的资料已成功提交审核中,请耐心等待审核结果',
                    //     onClickOk: function() {
                    //         transPage('1',activityCode,_token);     
                    //     }
                    // });
txy committed
154 155 156
                }else {
                    // 异常处理
                    $.dialog({
txy committed
157
                        contentHtml : '<p style="text-align:center;">'+ data.responseJSON.message +'</p>'
txy committed
158 159 160 161 162
                    });
                }
            },
            error: function(data){
                // 异常处理
胡畅 committed
163
                $.dialog({
txy committed
164
                    contentHtml : '<p style="text-align:center;">'+ data.responseJSON.message +'</p>'
胡畅 committed
165
                });
txy committed
166 167
            }
        })
txy committed
168 169
    });
}
txy committed
170

txy committed
171
function selectFileImage(fileObj) {
txy committed
172
    var file = fileObj;
txy committed
173 174
    //图片方向角 added by lzk  
    var Orientation = null;
txy committed
175

txy committed
176 177 178 179
    if (file) {
        console.log("正在上传,请稍后...");
        var rFilter = /^(image\/jpeg|image\/png|image\/jpg|image\/gif)$/i; // 检查图片格式  
        if (!rFilter.test(file.type)) {
胡畅 committed
180 181 182 183 184
            $.dialog({
                type : 'tips',
                autoClose : 3000,
                infoText : '请选择jpeg/jpg/gif/png格式的图片'
            });
txy committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
            return;
        }
        //获取照片方向角属性,用户旋转控制  
        EXIF.getData(file, function () {
            EXIF.getAllTags(this);
            Orientation = EXIF.getTag(this, 'Orientation');
            //return;  
        });

        var oReader = new FileReader();
        oReader.onload = function (e) {
            //var blob = URL.createObjectURL(file);  
            //_compress(blob, file, basePath);  
            var image = new Image();
            image.src = e.target.result;
            image.onload = function () {
                var expectWidth = this.naturalWidth;
                var expectHeight = this.naturalHeight;

                if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) {
                    expectWidth = 800;
                    expectHeight = expectWidth * this.naturalHeight / this.naturalWidth;
                } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) {
                    expectHeight = 1200;
                    expectWidth = expectHeight * this.naturalWidth / this.naturalHeight;
                }
                var canvas = document.createElement("canvas");
                var ctx = canvas.getContext("2d");
                canvas.width = expectWidth;
                canvas.height = expectHeight;
                ctx.drawImage(this, 0, 0, expectWidth, expectHeight);
                var base64 = null;
                //修复ios  
                if (navigator.userAgent.match(/iphone/i)) {
                    //如果方向角不为1,都需要进行旋转  
                    if (Orientation != "" && Orientation != 1) {
                        switch (Orientation) {
                            case 6://需要顺时针(向左)90度旋转    
                                rotateImg(this, 'left', canvas);
                                break;
                            case 8://需要逆时针(向右)90度旋转   
                                rotateImg(this, 'right', canvas);
                                break;
                            case 3://需要180度旋转  
                                rotateImg(this, 'right', canvas);//转两次  
                                rotateImg(this, 'right', canvas);
                                break;
txy committed
232
                        }
txy committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
                    }
                    base64 = canvas.toDataURL("image/jpeg", 0.8);
                } else if (navigator.userAgent.match(/Android/i)) {// 修复android  
                    var encoder = new JPEGEncoder();
                    base64 = encoder.encode(ctx.getImageData(0, 0, expectWidth, expectHeight), 80);
                } else {
                    if (Orientation != "" && Orientation != 1) {
                        switch (Orientation) {
                            case 6://需要顺时针(向左)90度旋转    
                                rotateImg(this, 'left', canvas);
                                break;
                            case 8://需要逆时针(向右)90度旋转 
                                rotateImg(this, 'right', canvas);
                                break;
                            case 3://需要180度旋转  
                                rotateImg(this, 'right', canvas);//转两次  
                                rotateImg(this, 'right', canvas);
                                break;
txy committed
251 252
                        }
                    }
txy committed
253 254 255 256

                    base64 = canvas.toDataURL("image/jpeg", 0.8);
                }
                $("#act31419345204861 img").attr("src", base64);
txy committed
257
            };
txy committed
258 259
        };
        oReader.readAsDataURL(file);
txy committed
260
    }
txy committed
261
}
txy committed
262

txy committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
//对图片旋转处理 
function rotateImg(img, direction, canvas) {
    //最小与最大旋转方向,图片旋转4次后回到原方向    
    var min_step = 0;
    var max_step = 3;
    //var img = document.getElementById(pid);    
    if (img == null) return;
    //img的高度和宽度不能在img元素隐藏后获取,否则会出错    
    var height = img.height;
    var width = img.width;
    //var step = img.getAttribute('step');    
    var step = 2;
    if (step == null) {
        step = min_step;
    }
    if (direction == 'right') {
        step++;
        //旋转到原位置,即超过最大值    
        step > max_step && (step = min_step);
    } else {
        step--;
        step < min_step && (step = max_step);
txy committed
285
    }
txy committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    //旋转角度以弧度值为参数    
    var degree = step * 90 * Math.PI / 180;
    var ctx = canvas.getContext('2d');
    switch (step) {
        case 0:
            canvas.width = width;
            canvas.height = height;
            ctx.drawImage(img, 0, 0);
            break;
        case 1:
            canvas.width = height;
            canvas.height = width;
            ctx.rotate(degree);
            ctx.drawImage(img, 0, -height);
            break;
        case 2:
            canvas.width = width;
            canvas.height = height;
            ctx.rotate(degree);
            ctx.drawImage(img, -width, -height);
            break;
        case 3:
            canvas.width = height;
            canvas.height = width;
            ctx.rotate(degree);
            ctx.drawImage(img, -width, 0);
            break;
    }
}
txy committed
315

txy committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
// 验证身份证
function validateIDNumber(card) {
    var vcity = {
        11: '北京', 12: '天津', 13: '河北', 14: '山西', 15: '内蒙古',
        21: '辽宁', 22: '吉林', 23: '黑龙江', 31: '上海', 32: '江苏',
        33: '浙江', 34: '安徽', 35: '福建', 36: '江西', 37: '山东', 41: '河南',
        42: '湖北', 43: '湖南', 44: '广东', 45: '广西', 46: '海南', 50: '重庆',
        51: '四川', 52: '贵州', 53: '云南', 54: '西藏', 61: '陕西', 62: '甘肃',
        63: '青海', 64: '宁夏', 65: '新疆', 71: '台湾', 81: '香港', 82: '澳门', 91: '国外'
    }
    // 检查号码是否符合规范,包括长度,类型
    function isCardNo(card) {
        // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
        var reg = /(^\d{15}$)|(^\d{17}(\d|X|x)$)/
        if (reg.test(card) === false) {
            return false
txy committed
332 333
        }

txy committed
334 335 336 337 338 339 340
        return true
    }
    // 取身份证前两位,校验省份
    function checkProvince(card) {
        var province = card.substr(0, 2)
        if (vcity[province] == undefined) {
            return false
txy committed
341
        }
txy committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355
        return true
    }
    // 检查生日是否正确
    function checkBirthday(card) {
        var len = card.length
        // 身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字
        if (len == '15') {
            var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/
            var arr_data = card.match(re_fifteen)
            var year = arr_data[2]
            var month = arr_data[3]
            var day = arr_data[4]
            var birthday = new Date('19' + year + '/' + month + '/' + day)
            return verifyBirthday('19' + year, month, day, birthday)
txy committed
356
        }
txy committed
357 358 359 360 361 362 363 364 365
        // 身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X
        if (len == '18') {
            var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X|x)$/
            var arr_data = card.match(re_eighteen)
            var year = arr_data[2]
            var month = arr_data[3]
            var day = arr_data[4]
            var birthday = new Date(year + '/' + month + '/' + day)
            return verifyBirthday(year, month, day, birthday)
txy committed
366
        }
txy committed
367 368 369 370 371 372 373 374 375 376 377 378
        return false
    }
    // 校验日期
    function verifyBirthday(year, month, day, birthday) {
        var now = new Date()
        var now_year = now.getFullYear()
        // 年月日是否合理
        if (birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day) {
            // 判断年份的范围(0岁到130岁之间)
            var time = now_year - year
            if (time >= 0 && time <= 130) {
                return true
txy committed
379 380 381
            }
            return false
        }
txy committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
        return false
    }
    // 校验位的检测
    function checkParity(card) {
        // 15位转18位
        card = changeFivteenToEighteen(card)
        var len = card.length
        if (len == '18') {
            var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
            var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2')
            var cardTemp = 0, i, valnum
            for (i = 0; i < 17; i++) {
                cardTemp += card.substr(i, 1) * arrInt[i]
            }
            valnum = arrCh[cardTemp % 11]
            if (valnum == card.substr(17, 1).toUpperCase()) {
                return true
txy committed
399 400 401
            }
            return false
        }
txy committed
402 403 404 405 406 407 408 409 410 411 412
        return false
    }
    // 15位转18位身份证号
    function changeFivteenToEighteen(card) {
        if (card.length == '15') {
            var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
            var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2')
            var cardTemp = 0, i
            card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6)
            for (i = 0; i < 17; i++) {
                cardTemp += card.substr(i, 1) * arrInt[i]
txy committed
413
            }
txy committed
414
            card += arrCh[cardTemp % 11]
txy committed
415 416
            return card
        }
txy committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
        return card
    }
    // 是否为空
    if (card === '') {
        // alert('请输入身份证号,身份证号不能为空')
        return false
    }
    // 校验长度,类型
    if (isCardNo(card) === false) {
        // alert('您输入的身份证号码不正确,请重新输入')
        return false
    }
    // 检查省份
    if (checkProvince(card) === false) {
        // alert('您输入的身份证号码不正确,请重新输入')
        return false
    }
    // 校验生日
    if (checkBirthday(card) === false) {
        // alert('您输入的身份证号码生日不正确,请重新输入')
        return false
    }
    // 检验位的检测
    if (checkParity(card) === false) {
        // alert('您的身份证校验位不正确,请重新输入')
        return false
txy committed
443
    }
txy committed
444 445
    return true
}
txy committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

// 验证用户姓名
function validateUserName(userName) {
    let state = false
    if (userName !== '') {
        let uName = userName.replace(/\s*/g, '')
        var reg = /^(([a-zA-Z+\.??a-zA-Z+]{1,15}$)|([\u4e00-\u9fa5+?\u4e00-\u9fa5+]{1,15}$))/
        if (!reg.test(uName)) {
            state = false
        } else {
            state = true
        }
    }

    return state
}
  
// 验证手机号
function validatePhone(phonenum) {
    let state = false
    if (phonenum !== '') {
        var reg = /^1[3456789]\d{9}$/
        if (!reg.test(phonenum)) {
            state = false
        } else {
            state = true
        }
    }
    return state
}

// 验证输入框
function validateInput($input) {

    var c_len = $input.attr("c_len");
    var c_validateempty = $input.attr("c_validateempty");
    var c_validate = $input.attr("c_validate");
    var c_name = $input.attr('c_name');
    var _value = $input.val();

    // 为空校验
    if (c_validateempty == 'yes' && _value == '') {
        $.dialog({
            type : 'tips',
            autoClose : 3000,
            infoText : c_name + '不能为空'
        });
        return false;
    }

    // 长度校验
    if (_value.length > c_len) {
        $.dialog({
            type : 'tips',
            autoClose : 3000,
            infoText :  c_name + '不能超过' + c_len + '个字'
        });
        return false;
    }

    // 规则校验 c_validate = phone,idcard,username三种类型
    if (c_validate !== ''){
        if(c_validate === 'phone' && !validatePhone(_value)) {// 手机校验
            $.dialog({
                type : 'tips',
                autoClose : 3000,
                infoText : '手机号格式错误'
            });
            return false;
        } else if(c_validate === 'idcard' && !validateIDNumber(_value)){// 身份证校验
            $.dialog({
                type : 'tips',
                autoClose : 3000,
                infoText : '身份证格式错误'
            });
            return false;
        } else if(c_validate === 'username' && !validateUserName(_value)){// 用户名称校验
            $.dialog({
                type : 'tips',
                autoClose : 3000,
                infoText : '用户名格式错误,请输入中文或者英文'
            });
            return false;
        }
    }
    return true;
}

// 验证表单输入框
function validateForm() {
   var inputs = $('.sign-form .form-input-group-inner input');
   var flag = true;
   for(var k =0 ;k < inputs.length; k++){
     flag = validateInput(inputs.eq(k));
     if(!flag){
        break;
     }
   }
   return flag
txy committed
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
}

// blob转file
function blobTransferFile(_blob) {
    let _name = (new Date().getTime());
    const _type = _blob.type
    let file = new window.File([_blob], _name, { type: _type })
    if (_type === 'image/svg+xml') {
        _name += '.svg'
        file = new window.File([_blob], _name, { type: _type })
    } else if (_type === 'image/gif') {
        _name += '.gif'
        file = new window.File([_blob], _name, { type: _type })
    } else if (_type === 'image/jpeg') {
        _name += '.jpg'
        file = new window.File([_blob], _name, { type: _type })
    } else if (_type === 'image/png') {
        _name += '.png'
        file = new window.File([_blob], _name, { type: _type })
    } else {
        file = _blob
    }

    return file;
}

// 文件上传
function uploadFileFn(file) {

    showLoading('上传中...');
    var formData = new FormData();
    formData.append('file', file)
    selectFileImage(file)
    $.ajax({
        type: 'POST',
谭新宇 committed
580
        url: manageUrl + '/api-admin-manage/system/components/upload',
txy committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
        data: formData,
        contentType: false,
        processData: false,
        success: function (data) {
            var data = typeof data == 'string' ? JSON.parse(data) : data
            if(data.code == 200){
                uploadFile = data.records.fileUrl
                $("#uploadImage").parent().addClass('global-none-important')
                $("#act31419345204861").removeClass('global-none')
            }
            hideLoading();
        },
        error: function () {
            $.dialog({
                type : 'tips',
                autoClose : 3000,
                infoText : '上传失败!'
            }); 
            hideLoading();
        }
    })
txy committed
602
}