var api = require('../../../config/api.js'); var util = require('../../../utils/util.js'); var app = getApp(); Page({ /** * 页面的初始数据 */ data: { phone: '', smscode: '', second: '发送验证码', disabled: false, disabledUpdate: false, paramView: '', navUrl: '', isRegist: true }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var that = this; if(options.view){ that.setData({ paramView: options.view }); } console.log(that.data.paramView) if (wx.getStorageSync("navUrl")) { that.setData({ navUrl: wx.getStorageSync("navUrl") }) } else { that.setData({ navUrl: '/pages/index/index' }) } }, /** * 页面渲染完成 */ onReady: function () { }, /** * 页面显示 */ onShow: function () { }, /** * 页面隐藏 */ onHide: function () { }, /** * 页面关闭 */ onUnload: function () { console.log('登陆页面返回2,进入登陆页参数:' + this.data.paramView) wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权 if (this.data.paramView == 'hotGoods' || this.data.paramView == 'indexGoods' || this.data.paramView == 'cart') { wx.setStorageSync("isLocationIndex", "true");//购物车是否已跳转登录页,跳转成功设置为true已跳转 wx.switchTab({ url: '/pages/index/index' }); } if (this.data.paramView == 'cateGoods') { wx.switchTab({ url: '/pages/catalog/catalog' }); } if (this.data.paramView == 'hotGoodsView') { wx.navigateBack({ delta: 1 }) } }, clearPhone() { this.setData({ phone: '' }); }, clearCode() { this.setData({ smscode: '' }); }, bindPhoneInput: function (e) { console.log(e.detail.value) this.setData({ phone: e.detail.value }); }, bindSmscodeInput: function (e) { this.setData({ smscode: e.detail.value }); }, /** * 用户登录,获取授权用户信息,查询授权用户openid是否与登录用户手机号绑定一致, * 不一致的提示是否更新手机绑定信息,一致则登录成功 */ loginUser: function () { let regular = /^1[3|4|5|7|8]\d{9}$/; if (!regular.test(this.data.phone)) { util.showErrorToast('手机格式不正确') return false; } if (this.data.smscode.length == 0) { util.showErrorToast('验证码不能为空') return false; } var that = this; wx.login({ success: function (res) { if (res.code) { wx.getUserInfo({ withCredentials: true, success: function (succRes) { util.request(api.getOpenId, { code: res.code }).then((openIdRes) => { if (openIdRes.errno === 0){ //授权成功登录用户信息 util.request(api.userLogin, { smscode: that.data.smscode, phone: that.data.phone, storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn'), userInfo: succRes, openId: openIdRes.data }, 'POST').then(function (userLoginRes) { if (userLoginRes.errno === 0) { //存储用户信息 that.setLoginUserInfo(userLoginRes); console.log("登录成功"); util.showSuccessToast('登录成功'); }else if (userLoginRes.errno === 2) {//openid的绑定的手机号与当前用户登录的手机号不一致 wx.showModal({ title: '提示', content: userLoginRes.errmsg, success: function (conRes) { if (conRes.confirm) { util.request(api.userLoginMobileUpdate, { phone: that.data.phone, openId: openIdRes.data, storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn'), userInfo: succRes, }, 'POST').then(function (loginMobUpdRes) { if (loginMobUpdRes.errno === 0) { //存储用户信息 that.setLoginUserInfo(loginMobUpdRes); console.log("登录成功"); util.showSuccessToast('登录成功'); } else { util.showErrorToast(loginMobUpdRes.errmsg); } }) } else { console.log('弹框后点取消') } } }) } else { if (userLoginRes.errmsg.length>8){ wx.showModal({ title: '提示', content: userLoginRes.errmsg, showCancel: false }) } else { util.showErrorToast(userLoginRes.errmsg); } } }); } }); }, fail: function (err) { wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权 // wx.switchTab({ // url: '/pages/index/index' // }); console.log("登录页重新认证"); } }); } else { console.log("failed"); } }, fail: function (err) { console.log("failed"); } }); }, /** * 存储用户信息 */ setLoginUserInfo: function (loginMobUpdRes){ var that = this; if (loginMobUpdRes.data.userInfo) { wx.setStorageSync('userInfo', loginMobUpdRes.data.userInfo); } if (loginMobUpdRes.data.token) { wx.setStorageSync('token', loginMobUpdRes.data.token); } wx.setStorageSync('userId', loginMobUpdRes.data.userId); if (that.data.paramView == 'goodsView') { wx.navigateBack({ delta: 1 }); } else { if (that.data.navUrl && that.data.navUrl == '/pages/index/index') { console.log(that.data.navUrl) wx.switchTab({ url: that.data.navUrl, }); } else if (that.data.navUrl) { wx.redirectTo({ url: that.data.navUrl, }); } } wx.setStorageSync('isRefusedLogin', 'false');//允许授权 }, /** * 发送短信 */ smscode: function () { var that = this; let regular = /^1[3|4|5|7|8]\d{9}$/; if (!regular.test(that.data.phone)) { util.showErrorToast('手机格式不正确') return false; } util.request(api.smscodeSend, { phone: that.data.phone }, 'POST').then(function (res) { console.log("发送验证码 => ", res); let n = 59; var timer = setInterval(function () { if (n == 0) { clearInterval(timer); that.setData({ second: '发送验证码', disabled: false }); } else { that.setData({ second: n-- + 's后重新获取', disabled: true }); } }, 1000); if (res.errno == 0) { wx.showToast({ title: '短信发送成功' }) } else { util.showErrorToast('发送失败'); clearInterval(timer); that.setData({ second: '发送验证码', disabled: false }); return false; } }); }, backHome: function () { wx.reLaunch({ url: '../index/index', }) }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, // /** // * 用户点击右上角分享 // */ // onShareAppMessage: function () { // }, bindCancelLogin: function () { wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权 wx.switchTab({ url: '/pages/index/index' }); }, /** * 登录页直接触发微信授权,授权成功保存用户信息 */ bindGetUserInfo: function () { let that = this; wx.login({ success: function (res) { if (res.code) { wx.getUserInfo({ withCredentials: true, success: function (succRes) { //保存授权用户信息 wx.request({ url: api.AuthLoginByWeixin, data: { code: res.code, userInfo: succRes, storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn') }, method: 'POST', header: { 'Content-Type': 'application/json' }, success: function (wxRes) { if (wxRes.data.errno === 0) { //存储用户信息到缓存 if (wxRes.data.data.userInfo) { wx.setStorageSync('userInfo', wxRes.data.data.userInfo); } wx.setStorageSync('token', wxRes.data.data.token); wx.setStorageSync('userId', wxRes.data.data.userId); if (that.data.paramView == 'goodsView') { wx.navigateBack({ delta: 1 }); } else { if (that.data.navUrl && that.data.navUrl == '/pages/index/index') { console.log(that.data.navUrl) wx.switchTab({ url: that.data.navUrl, }); } else if (that.data.navUrl) { wx.redirectTo({ url: that.data.navUrl, }); } } wx.setStorageSync('isRefusedLogin', 'false');//允许授权 // console.log("登录成功"); } }, fail: function (err) { console.log("failed"); } }); }, fail: function (err) { wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权 // wx.switchTab({ // url: '/pages/index/index' // }); console.log("登录页重新认证"); } }); } else { console.log("failed"); } }, fail: function (err) { console.log("failed"); } }); }, /*registUser: function () { var that = this; let regular = /^1[3|4|5|7|8]\d{9}$/; if (!regular.test(that.data.phone)) { util.showErrorToast('手机格式不正确') return false; } if (that.data.smscode.length == 0) { util.showErrorToast('验证码不能为空') return false; } wx.login({ success: function (res) { if (res.code) { wx.getUserInfo({ withCredentials: true, success: function (succRes) { util.request(api.getOpenId, { code: res.code }).then((openIdRes) => { if (openIdRes.errno === 0) { that.registUserUtil(); } }); }, fail: function (err) { wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权 console.log("注册前进行授权重新认证"); } }); } else { console.log("faild"); } }, fail: function (err) { console.log("failed"); } }) }, registUserUtil: function () { var that = this; util.request(api.registUser, { smscode: that.data.smscode, phone: that.data.phone }, 'POST').then(function (res) { if (res.errno === 0) { //存储用户信息 wx.setStorageSync('userInfo', res.data.data.userInfo); wx.setStorageSync('token', res.data.data.token); wx.setStorageSync('userId', res.data.data.userId); if (that.data.navUrl && that.data.navUrl == '/pages/index/index') { wx.switchTab({ url: that.data.navUrl, }); } else if (that.data.navUrl) { wx.redirectTo({ url: that.data.navUrl, }); } wx.setStorageSync('isRefusedLogin', 'false');//允许授权 // console.log("登录成功"); } else { util.showErrorToast(res.errmsg); } }); }, showRegist: function(){ var that = this; that.setData({ isRegist: false }); wx.setNavigationBarTitle({ title: '注册' }) }*/ })