signup.js 20 KB
Newer Older
txy committed
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 28 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 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 443 444 445 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
    // 公共变量
    var pageNumber = "1";
    var baseUrl = "https://space-api.check.icaremgt.com";

    $(function () {
        removeAttr();
        btnBindClick();// 绑定点击事件
        $('#act11739232574395').attr({ 'readonly': 'readonly' });
        wxpermission();
        getOrgNameFn();
        loadHtmlJson();
    })

    // 微信鉴权
    function wxpermission() {
        wx.ready(function () {
            var shareData = {
                title: "",
                imgUrl: "",
                desc: "",
                link: ""
            }
            /**
             *分享给朋友
            */
            wx.onMenuShareAppMessage({
                title: shareData.title, // 分享标题
                desc: shareData.desc, // 分享描述
                link: shareData.link, // 分享链接
                imgUrl: shareData.imgUrl, // 分享图标
                type: 'link', // 分享类型,music、video或link,不填默认为link
                dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
                success: function () {
                    // 用户确认分享后执行的回调函数
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                }
            });
            /**
             *分享到朋友圈
            */
            wx.onMenuShareTimeline({
                title: shareData.title, // 分享标题
                desc: shareData.desc, // 分享描述
                link: shareData.link, // 分享链接
                imgUrl: shareData.imgUrl, // 分享图标
                success: function () {
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                }
            });
        });
    }

    // 元素点击事件绑定
    function btnBindClick() {
        // 返回首页
        $("#act11534485971254").off("click").on("click", function () {
            window.location.href = '/index.html'
        });
        // 排行榜
        $("#act41612547506702").off('click').on("click", function () {
            window.location.href = '/index.html'
        });

        // 选手主页    
        $("#act4161336684689").off("click").on("click", function () {
            window.location.href = '/user.html'
        });

        // 活动秘籍   
        $("#act41613113951133").off("click").on("click", function () {
            $('#html-template-8').removeClass('global-none')
        });
        // 分享弹框关闭
        $(document).on('click', '#html-template-8 .modal-close', function () {
            $('#html-template-8').addClass('global-none')
        });

        // 上传图片
        $("#uploadImage").on('change', function () {
            var formData = new FormData()
            formData.append('file', this.files[0])
            selectFileImage(this)
            $.ajax({
                type: 'POST',
                url: 'http://api-admin-manage.check.icaremgt.com/system/components/upload',
                headers: { 'x-token': user.token },
                data: formData,
                contentType: false,
                processData: false,
                success: function (data) {
                    $("#act31419345204861").removeClass('global-none')
                },
                error: function () {
                    showMessage('上传失败!', 3000, true);
                }
            })
        })

        // 确认提交    
        $("#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"]')
            var addInput = data.dataJson.addGroup
            var uploadImg = $('#act31419345204861 img')

            // 验证用户名
            if (userName.attr('c_validateempty') == 'yes') {
                var userNameVal = userName.val()
                var userNameLen = userName.attr('c_len')
                if (userNameVal == '') {
                    showMessage('请输入参与者姓名', 3000, true);
                    return false
                } else if (userNameVal.length > userNameLen) {
                    showMessage('参与者姓名不能超过' + userNameLen + '个字', 3000, true);
                    return false
                }
            }
            // 验证手机号
            if (phonenum.attr('c_validateempty') == 'yes') {
                var reg = /^1[3456789]\d{9}$/
                var phonenumVal = phonenum.val()
                var phonenumLen = phonenum.attr('c_len')
                if (phonenumVal == '') {
                    showMessage('请输入参与者手机号码', 3000, true);
                    return false
                } else if (phonenumVal.length > phonenumLen) {
                    showMessage('参与者手机号码不能超过' + phonenumLen + '个字', 3000, true);
                    return false
                } else if (!reg.test(phonenumVal)) {
                    showMessage('请输入正确的参与者手机号码', 3000, true);
                    return false
                }
            }
            // 验证活动宣言
            if (introDuce.attr('c_validateempty') == 'yes') {
                var introDuceVal = introDuce.val()
                var introDuceLen = introDuce.attr('c_len')
                var introDuceName = introDuce.attr('c_name')
                if (introDuceVal == '') {
                    showMessage('请输入' + introDuceName, 3000, true);
                    return false
                } else if (introDuceVal.length > introDuceLen) {
                    showMessage(introDuceName + '不能超过' + introDuceLen + '个字', 3000, true);
                    return false
                }
            }
            // 验证身份证
            if (signInput.attr('c_validateempty') == 'yes') {
                var signInputVal = signInput.val()
                var signInputName = signInput.attr('c_name')
                var signInputLen = signInput.attr('c_len')
                if (signInputVal == '') {
                    showMessage('请输入' + signInputName, 3000, true);
                    return false
                } else if (signInputVal.length > signInputLen) {
                    showMessage(signInputName + '不能超过' + signInputLen + '个字', 3000, true);
                    return false
                } else if (!validateIDNumber(signInputVal)) {
                    showMessage('请输入正确的' + signInputName, 3000, true);
                    return false
                }
            }
            // 验证上传图片
            if (uploadImg.attr('src') == '') {
                showMessage('请上传一张照片', 3000, true);
                return false
            }

            var params = {
                kgAcActivityOrgId: getQueryString('orgId'),      //模板变量中获取,机构id
                declaration: introDuce.val(),   //参赛宣言
                imageUrl: uploadImg.attr('src'),  //上传图片地址
                name: userName.val(),  // 姓名
                userPhone: phonenum.val(),   //用户填写的手机号
            }
            if (addInput.length) {
                for (var i = 0; i < addInput.length; i++) {
                    params['x'+ (i+1)] = $('#'+addInput[i].id).val()
                }
            }
            var activityCode = rules.acActivity.code
            $.ajax({
                type: 'POST',
                headers: {'content-type': 'application/json'},
                url: baseUrl + '/ACTIVITY/sz/sign-up/' + activityCode,
                data: JSON.stringify(params),
                success: function (data) {
                    showMessage('提交成功',3000,true);
                }
            })
        });
    }

    // 获取机构名称
    function getOrgNameFn() {
        $.ajax({
            type: 'GET',
            url: baseUrl + '/ORG/hospital/community/hospitalInfo/' + getQueryString('orgId'),
            success: function(data) {
                if(data.code == 200) {
                    $('#act11739232574395').val(data.records.orgName)
                }
            },
            error: function() {
                showMessage('找不到此机构',3000,true);
            }
        })
    }

    function selectFileImage(fileObj) {
        var file = fileObj.files['0'];
        //图片方向角 added by lzk  
        var Orientation = null;

        if (file) {
            console.log("正在上传,请稍后...");
            var rFilter = /^(image\/jpeg|image\/png|image\/jpg|image\/gif)$/i; // 检查图片格式  
            if (!rFilter.test(file.type)) {
                showMessage('请选择jpeg/jpg/gif/png格式的图片', 3000, true);
                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;
                            }
                        }
                        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;
                            }
                        }

                        base64 = canvas.toDataURL("image/jpeg", 0.8);
                    }
                    $("#act31419345204861 img").attr("src", base64);
                };
            };
            oReader.readAsDataURL(file);
        }
    }

    //对图片旋转处理 
    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);
        }
        //旋转角度以弧度值为参数    
        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;
        }
    }

    // 验证身份证
    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
            }

            return true
        }
        // 取身份证前两位,校验省份
        function checkProvince(card) {
            var province = card.substr(0, 2)
            if (vcity[province] == undefined) {
                return false
            }
            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)
            }
            // 身份证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)
            }
            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
                }
                return false
            }
            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
                }
                return false
            }
            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]
                }
                card += arrCh[cardTemp % 11]
                return card
            }
            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
        }
        return true
    }