123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- // pages/login/login.js
- import * as API_Passport from '../../api/passport.js'
- import Toast from '../../miniprogram_npm/vant-weapp/toast/toast.js'
- import * as RegExp from '../../utils/RegExp.js'
- import * as API_Shop from '../../api/shop.js'
- import * as Reg from '../../utils/RegExp.js'
- import * as Common from '../../utils/common.js'
- import {
- $wuxToptips,
- $wuxToast,
- $wuxLoading,
- $wuxDialog
- } from '../../wux/index.js'
- const app = getApp()
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- username: '',
- password: '',
- mobile:'',
- smscode:'',
- username_label: '用户名',
- password_label: '密码',
- mobile_label: '手机号',
- smscode_label: '短信验证码',
- send_btn_disable:false,
- send_btn_init:true,
- send_btn_reset:false,
- active:0,
- buttonClicked:false
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function(options) {
- //如果没有自动登录记录,首先进行自动登录尝试,如果有登录信息,通过toAutologin实现跳转逻辑
- if(wx.getStorageSync('login_result')==='' || wx.getStorageSync('authorInfo')!==''){
- // Common.toAutoLogin()
- }
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function() {
- this.setData({
- send_btn_disable:false,
- sending:false,
- btn_tip:'发送验证码'
- })
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function() {
- },
- onChange(event) {
- const that = this
- let dataKey = event.currentTarget.dataset.name
- // event.detail 为当前输入的值
- this.setData({
- [dataKey]: event.detail
- })
- },
- onSmscodeChange(e){
- const that=this
- const value = e.detail
- try {
- if (!value) {
- that.setData({
- ['smscode_error']: true,
- ['smscode_error_message']: '短信验证码必填'
- })
- throw new Error()
- }else{
- that.setData({
- ['smscode_error']: false,
- ['smscode_error_message']: '',
- ['smscode']:value
- })
- }
- } catch (e) {
- }
- },
- login() {
- let that = this
- wx.login({
- success: function(res) {
- let param = {
- username: null,
- password: null
- }
- let ps = {
- ...param,
- ...that.data
- }
- Object.keys(param).forEach(
- n => {
- if (!ps[n]) {
- that.setData({
- [n + '_error']: true,
- [n + '_error_message']: that.data[n + '_label'] + '必填'
- })
- }
- }
- )
- if(that.data['username_error']||that.data['password_error']){
- return false
- }
- that.setData({
- buttonClicked:true
- })
- let params = {
- username: that.data.username,
- password: that.data.password,
- uuid: wx.getStorageSync('uuid'),
- code: res.code,
- mini_program_type: app.globalData.miniProgramType
- }
- API_Passport.login(params).then(response=>{
- const now=Date.parse(new Date())/1000
- //恢复按钮可点击状态
- that.setData({
- buttonClicked:false
- })
- if (response.access_token) {
- //保存登录信息
- const {refresh_token_timeout,access_token_timeout}=response
- app.globalData.authorInfo={...response,'refresh_token_timeout':now+refresh_token_timeout-1000,'access_token_timeout':now+access_token_timeout-1000}
- //登录信息写入本地存储
- wx.setStorageSync('authorInfo', app.globalData.authorInfo)
- if(app.globalData.authorInfo.bool_o2o===0){
- wx.redirectTo({
- url: '/pages/noshop/index',
- })
- }else{
- wx.switchTab({
- url: '/pages/store/index',
- })
- }
- // wx.switchTab({
- // url: '/pages/store/index',
- // })
- }
- if(response.result==='existed'){
- wx.showModal({
- title: '提示',
- content: '当前微信已经绑定了一个用户,请点击确定按钮登录已绑定账号,如需更换账号,登录账户后退出登录完成解绑',
- showCancel:false,
- success (res) {
- if (res.confirm) {
- wx.removeStorageSync('login_result')
- Common.toAutoLogin()
- } else if (res.cancel) {
- console.log('用户点击取消')
- }
- }
- })
- }
- }).catch(err=>{
- $wuxToptips().error({
- icon: 'cancel',
- hidden: false,
- text: err.error?err.error:err.message,
- duration: 2000,
- success:()=>{
- that.setData({
- buttonClicked:false
- })
- }
- })
- return
- })
- }
- })
- },
- /** 输入框失去焦点 */
- onBlur(e) {
- const that = this
- let dataKey = e.currentTarget.dataset.name
- const {
- rules,
- label
- } = e.currentTarget.dataset
- const {
- value
- } = e.detail
- if (rules) {
- that.setData({
- [dataKey + '_error']: false,
- [dataKey + '_error_message']: ''
- })
- try {
- rules.split(',').forEach(n => {
- if (n === 'required' && !value) {
- that.setData({
- [dataKey + '_error']: true,
- [dataKey + '_error_message']: label + '必填'
- })
- throw new Error()
- }
- if (n !== 'required' && !Reg[n].test(value)) {
- that.setData({
- [dataKey + '_error']: true,
- [dataKey + '_error_message']: label + '格式不正确'
- })
- }
- })
- } catch (e) {
- }
- }
- },
- /** 发送短信验证码 **/
- sendsmscode(e){
- let that=this
- let param = {
- mobile: null
- }
- let ps = {
- ...param,
- ...that.data
- }
- Object.keys(param).forEach(
- n => {
- if (!ps[n]) {
- that.setData({
- [n + '_error']: true,
- [n + '_error_message']: that.data[n + '_label'] + '必填'
- })
- }
- }
- )
- let flag = true
- if (that.data['mobile_error']) {
- return false
- }
- const countDown = this.selectComponent('.control-count-down')
- countDown.start()
- API_Passport.sendLoginSms(that.data.mobile).then(res=>{
- that.setData({
- send_btn_disable:true,
- sending:true
- })
- }).catch(err=>{
- $wuxToptips().error({
- icon: 'cancel',
- hidden: false,
- text: err.message,
- duration: 2000
- })
- return
- })
- },
- /** 倒计时结束 */
- finished(e){
- this.setData({
- send_btn_disable:false,
- sending:false,
- btn_tip:'重新获取'
- })
- const countDown = this.selectComponent('.control-count-down')
- countDown.reset()
- },
- /** 短信验证码登录提交*/
- loginsmscode(e){
- let that = this
- wx.login({
- success: function(res) {
- if (!that.data['mobile']) {
- that.setData({
- ['mobile_error']: true,
- ['mobile_error_message']: '手机号必填'
- })
- return false
- }
- if(!Reg['mobile'].test(that.data['mobile'])){
- that.setData({
- ['mobile_error']: true,
- ['mobile_error_message']: '手机格式错误!'
- })
- return false
- }
- if(!that.data['smscode']){
- that.setData({
- ['smscode_error']: true,
- ['smscode_error_message']: '短信验证码必须填写!'
- })
- return false
- }
- that.setData({
- buttonClicked:true
- })
- let params = {
- mobile: that.data.mobile,
- smscode: that.data.smscode,
- uuid: app.globalData.uuid,
- code: res.code,
- mini_program_type: app.globalData.miniProgramType
- }
- API_Passport.smslogin(params).then(response=>{
- const now=Date.parse(new Date())/1000
- that.setData({
- buttonClicked:false
- })
- if (response.access_token) {
- //保存登录信息
- const {refresh_token_timeout,access_token_timeout}=response
- app.globalData.authorInfo={...response,'refresh_token_timeout':now+refresh_token_timeout-1000,'access_token_timeout':now+access_token_timeout-1000}
- console.log(app.globalData.authorInfo)
- //登录信息写入本地存储
- wx.setStorageSync('authorInfo', app.globalData.authorInfo)
- if(app.globalData.authorInfo.bool_o2o===0){
- wx.redirectTo({
- url: '/pages/noshop/index',
- })
- }else{
- wx.switchTab({
- url: '/pages/store/index',
- })
- }
- }
- }).catch(err=>{
- $wuxToptips().error({
- icon: 'cancel',
- hidden: false,
- text: err.message,
- duration: 2000,
- success:()=>{
- that.setData({
- buttonClicked:false
- })
- }
- })
- return
- })
- }
- })
- },
- tabChange(event) {
- // event.detail 的值为当前选中项的索引
- this.setData({ active: event.detail });
- },
- wechatLogin() {
- Common.toAutoLogin()
- }
- })
|