-
-
Save Visionchen/b147cdb49ae2a007e865b4e98106f3bf to your computer and use it in GitHub Desktop.
点滴杂记
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### 父页面调用子页面的方法 | |
| ```language | |
| function test(){ alert("test"); } | |
| 子页面的方法 | |
| function childtest(){ window.parent.test(); } | |
| ``` | |
| ### 小程序封装 | |
| ```javascript:; | |
| //app.js | |
| App({ | |
| /** | |
| * @name 请求数据 | |
| * @param {string} url 请求地址 | |
| * @param {object} params 参数 | |
| * @param {function} callback 成功回调函数 | |
| * @param {function} failcall 失败回调函数 | |
| * @param {bool} loading 是否显示加载框 | |
| */ | |
| ajax: function(url, params, callback, failcall, loading) { | |
| // 判断是否需要显示加载框 | |
| if (!loading) { | |
| wx.showLoading({ | |
| title: '加载中', | |
| mask: true | |
| }); | |
| } | |
| wx.request({ | |
| url: 'http://www.baidu.com' + url, | |
| data: params, | |
| header: { | |
| 'content-type': 'application/x-www-form-urlencoded;' | |
| }, | |
| method: "POST", | |
| success: function(res) { | |
| wx.hideLoading(); | |
| // 此处与接口约定好,返回的code对应不同的结果,0 代表请求成功 300 需要跳转,返回跳转链接forward 500 为请求异常,返回错误信息message | |
| if (res.data.code == 0) { | |
| // 如果有请求成功回调函数,则调用 | |
| typeof callback == "function" && callback(res.data); | |
| } else if (res.data.code == 300) { | |
| if (res.data.forward) { | |
| wx.navigateTo({ | |
| url: res.data.forward | |
| }) | |
| } | |
| } else { | |
| // 如果有请求失败回调函数,则调用 | |
| if (failcall) { | |
| failcall(res.data); | |
| } else { | |
| if (res.data.message) { | |
| wx.showModal({ | |
| title: '提示', | |
| content: res.data.message, | |
| showCancel: false, | |
| confirmColor: '#92BA00' | |
| }); | |
| } | |
| } | |
| } | |
| }, | |
| fail: function(res) { | |
| console.log(res); | |
| wx.hideLoading(); | |
| wx.showModal({ | |
| title: '提示', | |
| content: "网络异常,请稍后再试", | |
| showCancel: false, | |
| confirmColor: '#92BA00' | |
| }); | |
| return false; | |
| } | |
| }); | |
| }, | |
| /** | |
| * @name 请求数据 | |
| * @param {string} url 请求地址 | |
| * @param {object} params 参数 | |
| * @param {string} forward 登录后跳转链接 | |
| * @param {function} callback 成功回调函数 | |
| * @param {function} failcall 失败回调函数 | |
| * @param {bool} loading 是否显示加载框 | |
| */ | |
| ajax_user: function(url, params, forward, callback, failcall, loading) { | |
| var app = this; | |
| var userId = wx.getStorageSync('userId'); | |
| // 判断用户是否登录,未登录先进行登录操作 | |
| if (userId == '') { | |
| wx.login({ | |
| success: function(res) { | |
| if (res.code) { | |
| var code = res.code; | |
| // 进行登录操作 | |
| app.ajax('/users/login', { | |
| lat: app.latitude, | |
| lng: app.longitude, | |
| code: code | |
| }, function(data) { | |
| // 登录成功,将user_id存储到本地 | |
| var userId = data.user_id; | |
| wx.setStorageSync("userId", userId); | |
| // 判断是否需要跳转到指定页面,forward不为空则跳转,为空则继续之前请求 | |
| if (forward != '') { | |
| wx.navigateTo({ | |
| url: forward | |
| }); | |
| } else { | |
| params.user_id = userId; | |
| app.ajax(url, params, callback, failcall, loading); | |
| } | |
| }); | |
| } else { | |
| console.log('获取用户登录态失败!' + res.errMsg) | |
| } | |
| } | |
| }); | |
| } else { | |
| // 已登录直接请求 | |
| params.user_id = userId; | |
| app.ajax(url, params, callback, failcall, loading); | |
| } | |
| } | |
| }) | |
| ``` | |
| ### h5页面JQ常规登录js | |
| ```javascript | |
| $(function () { | |
| redirectUrl = $('#redirectUrl').val(); | |
| resetImageCaptcha(); | |
| $("#imageCaptcha").click(function () { | |
| resetImageCaptcha(); | |
| }); | |
| $("#captcha").on('input', function () { | |
| validateImageCaptcha(); | |
| }); | |
| //发送短信验证码 | |
| $('#sendCodeBtn').click(function () { | |
| if (!$(this).hasClass('disable')) { | |
| sendCode($(this)); | |
| } | |
| }); | |
| //发送语音验证码 | |
| $('#voiceCode .voice-usable').click(function () { | |
| sendVoiceCode(); | |
| }); | |
| }); | |
| //重定向地址 | |
| var redirectUrl = ''; | |
| //图片验证码KEY | |
| var captchaKey = ''; | |
| /** | |
| * 重置图形验证码 | |
| */ | |
| function resetImageCaptcha() { | |
| captchaKey = randomStr(32) | |
| var url = _webApp + '/captcha/image?key=' + captchaKey; | |
| $("#imageCaptcha").attr('src', url); | |
| } | |
| /** | |
| * 随机字符串 | |
| * @param n | |
| * @returns {string} | |
| */ | |
| function randomStr(n) { | |
| var res = ""; | |
| var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; | |
| for (var i = 0; i < n; i++) { | |
| var id = Math.ceil(Math.random() * 35); | |
| res += chars[id]; | |
| } | |
| return res; | |
| } | |
| /** | |
| * 切换手机登陆 | |
| */ | |
| function switchPhonelogin() { | |
| $('.login-box-item').hide(); | |
| $('#boxPhone').show(); | |
| } | |
| /** | |
| * 切换密码登陆 | |
| */ | |
| function switchPasswordlogin() { | |
| $('.login-box-item').hide(); | |
| $('#boxPassword').show(); | |
| } | |
| /** | |
| * 密码登陆提交 | |
| */ | |
| function passwordLoginSubmit() { | |
| var account = $.trim($('input[name="account"]').val()); | |
| if (!account) { | |
| tooltip('请输入手机/登录名'); | |
| $("#accountRow").addClass("error").click(function () { | |
| $(this).removeClass('error').unbind("click"); | |
| }); | |
| return false; | |
| } | |
| var password = $.trim($('input[name="password"]').val()); | |
| if (!password) { | |
| tooltip('密码不能为空'); | |
| $("#passwordRow").addClass("error").click(function () { | |
| $(this).removeClass('error').unbind("click"); | |
| }); | |
| return false; | |
| } | |
| $.post(_webApp + "/login/passwordLogin", { | |
| account: account, | |
| password: password, | |
| rememb: true | |
| }, function (result) { | |
| if (result.code == 0) { | |
| window.location.href = redirectUrl; | |
| } else { | |
| tooltip(result.message); | |
| } | |
| }, 'json'); | |
| } | |
| /** | |
| * 手机动态登陆提交 | |
| */ | |
| function phoneLoginSubmit() { | |
| if (checkPhone() && checkCaptcha() && checkCode()) { | |
| var url = _webApp + '/login/phoneLogin' | |
| var phone = $.trim($('input[name="phone"]').val()); | |
| var captcha = $.trim($('input[name="captcha"]').val()); | |
| var code = $.trim($('input[name="code"]').val()); | |
| $.post(url, { | |
| phone: phone, | |
| code: code, | |
| captcha: captcha, | |
| captchaKey: captchaKey | |
| }, function (result) { | |
| if (result.code == 0) { | |
| window.location.href = redirectUrl; | |
| } else { | |
| tooltip(result.message); | |
| } | |
| }, 'json'); | |
| } | |
| } | |
| /** | |
| * 验证图形验证码 | |
| */ | |
| function validateImageCaptcha() { | |
| var captcha = $.trim($('#captcha').val()); | |
| if (captcha.length >= 4) { | |
| $.post(_webApp + "/captcha/image/validate", { | |
| captcha: captcha, | |
| captchaKey: captchaKey | |
| }, function (result) { | |
| if (result.code == 0) { | |
| $("#captchaLable").removeClass("error"); | |
| $("#codeLabel").show(); | |
| } else { | |
| $("#captchaLable").addClass("error"); | |
| $("#codeLabel").hide(); | |
| } | |
| }, "json"); | |
| } else { | |
| $("#captchaLable").removeClass("error"); | |
| $("#codeLabel").hide(); | |
| } | |
| } | |
| /** | |
| * 发送语音验证码 | |
| */ | |
| function sendVoiceCode() { | |
| if (checkPhone() && checkCaptcha()) { | |
| var phone = $.trim($('input[name="phone"]').val()); | |
| var captcha = $.trim($('input[name="captcha"]').val()); | |
| $.post(_webApp + "/captcha/sendVoice", { | |
| phone: phone, | |
| captcha: captcha, | |
| captchaKey: captchaKey | |
| }, function (result) { | |
| if (result.code == 0) { | |
| tooltip("语言电话已拨打,请注意接听!"); | |
| $('#voiceCode .voice-usable').hide(); | |
| $('#voiceCode .voice-unusable').show(); | |
| } else { | |
| tooltip(result.message); | |
| } | |
| }, 'json'); | |
| } | |
| } | |
| /** | |
| * 发送短信验证码 | |
| */ | |
| function sendCode(obj) { | |
| if (checkPhone() && checkCaptcha()) { | |
| var phone = $.trim($('input[name="phone"]').val()); | |
| var captcha = $.trim($('input[name="captcha"]').val()); | |
| obj.addClass('disable').text('发送中···'); | |
| $.post(_webApp + "/captcha/sendSms", { | |
| phone: phone, | |
| captcha: captcha, | |
| captchaKey: captchaKey | |
| }, function (result) { | |
| if (result.code == 0) { | |
| countdown(); | |
| //成功后给提醒信息 | |
| tooltip("短信验证码已发送成功,请注意查收!"); | |
| } else { | |
| tooltip(result.message); | |
| $('#sendCodeBtn').html('发送验证码').removeClass('disable'); | |
| } | |
| }, 'json'); | |
| } | |
| } | |
| /** | |
| * 倒计时 | |
| */ | |
| var timer = 0; | |
| function countdown() { | |
| clearInterval(timer); | |
| var count = 60; | |
| $('#sendCodeBtn').html('<span class="corange">' + count + 's</span>'); | |
| timer = setInterval(function () { | |
| count--; | |
| $('#sendCodeBtn').html('<span class="corange">' + count + 's</span>'); | |
| if (count == 0) { | |
| clearInterval(timer); | |
| $('#sendCodeBtn').html('发送验证码').removeClass('disable'); | |
| } | |
| if (count == 45) { | |
| $("#voiceCode").show(); | |
| } | |
| }, 1000); | |
| } | |
| /** | |
| * 返回动作 | |
| */ | |
| function back() { | |
| var redirectUrl = $('#redirectUrl').val(); | |
| if (redirectUrl) { | |
| history.go(-1); | |
| } else { | |
| window.location.href = _webApp; | |
| } | |
| } | |
| /** | |
| * 检查手机 | |
| * @returns {boolean} | |
| */ | |
| function checkPhone() { | |
| var phone = $.trim($('input[name="phone"]').val()); | |
| if (!/^1\d{10}$/.test(phone)) { | |
| tooltip('请填写正确的手机号码'); | |
| $("#phoneLable").addClass("error").click(function () { | |
| $(this).removeClass('error').unbind("click"); | |
| }); | |
| return false; | |
| } else { | |
| return true; | |
| } | |
| } | |
| /** | |
| * 检查图形验证码 | |
| * @returns {boolean} | |
| */ | |
| function checkCaptcha() { | |
| var captcha = $.trim($('input[name="captcha"]').val()); | |
| if (!/^\d{4}$/.test(captcha)) { | |
| tooltip('请输入4位图形验证码'); | |
| $("#captchaLable").addClass("error").click(function () { | |
| $(this).removeClass('error').unbind("click"); | |
| }); | |
| return false; | |
| } else { | |
| return true; | |
| } | |
| } | |
| /** | |
| * 检查手机验证码 | |
| * @returns {boolean} | |
| */ | |
| function checkCode() { | |
| var code = $.trim($('input[name="code"]').val()); | |
| if (!/^\d{4}$/.test(code)) { | |
| tooltip('请输入4位手机验证码'); | |
| $("#codeLabel").addClass("error").click(function () { | |
| $(this).removeClass('error').unbind("click"); | |
| }); | |
| return false; | |
| } else { | |
| return true; | |
| } | |
| } | |
| ``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment