小程序登录方式切换 不做url跳转

 

 

var filegUP = require('../../utils/getUserPassword.js');
var filemd5 = require('../../utils/md5.min.js');
var fileUtil = require('../../utils/util.js');
var chkStrLength = fileUtil.chkStrLength;
var gd = getApp().globalData;
var apiUrlApp = gd.apiUrlApp;
var localImgPath = gd.localImgPath;
var loginTypeArr = Array('手机号', '用户名');
var loginTypeAreaMobile = true;
var loginTypeStr = loginTypeArr[1]; 
Page({
  onLoad: function(option) {
    console.log("加载登录页面")
    wx.clearStorageSync()
    console.log("同步清理本地数据缓存")
  },
  data: {
    localImgPath: localImgPath,
    loginTypeStr: loginTypeStr,
    loginTypeAreaMobile: loginTypeAreaMobile
  },
  formSubmit: function(e) {
    var usernameChk = e.detail.value.username;
    var passwordChk = e.detail.value.password;
    if (usernameChk.length == 0 || passwordChk.length == 0) {
      wx.showToast({
          title: '用户名或密码不得为空!',
          icon: 'loading',
          duration: 1500
        }),
        setTimeout(function() {
          wx.hideToast()
        }, 2000)
    } else if (passwordChk.length < 1) {
      wx.showToast({
          title: '请输入大于1位用户名!',
          icon: 'loading',
          duration: 1500
        }),
        setTimeout(function() {
          wx.hideToast()
        }, 2000)
    } else if (passwordChk.length < 1 || passwordChk.length > 20) {
      wx.showToast({
          title: '请输入1-20密码!',
          icon: 'loading',
          duration: 1500
        }),
        setTimeout(function() {
          wx.hideToast()
        }, 2000)
    } else {
      wx.request({
        url: apiUrlApp + 'acc/get_salt/?username=' + usernameChk,
        header: {},
        method: "GET",
        data: {},
        success: function(res) {
          if (res.data.status != 1) {
            wx.showToast({
              title: 'Fail-getSalt',
              icon: 'loading',
              duration: 2000
            })
          } else if (res.data.status == 1) {
            var salt = res.data.data.salt;
            var pwdWithSalt = passwordChk + '-' + salt;
            var pwdMd5 = filemd5.md5(pwdWithSalt);
            var pwdSubmit = pwdMd5.slice(-17) + pwdMd5.slice(0, -17);

            wx.request({
              url: apiUrlApp + 'acc/login/',
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              method: "POST",
              data: {
                username: usernameChk,
                password: pwdSubmit
              },
              success: function(res) {
                //storage
                wx.setStorageSync("username", res.data.data["username"]);
                wx.setStorageSync("gid", res.data.data["gid"]);
                wx.setStorageSync("uid", res.data.data["uid"]);
                wx.request({
                  url: apiUrlApp + 'acc/get_userinfo/',
                  header: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'cookie': wx.getStorageSync('username'),
                    'cookie': wx.getStorageSync('uid'),
                    'cookie': wx.getStorageSync('gid')
                  },
                  method: "GET",
                  data: {
                    username: wx.getStorageSync('username')
                  },
                  success: function(res) {
                    console.log(res)
                    if (res.data.status == 1) {
                      wx.showToast({
                        title: "帐号详情",
                        icon: 'success',
                        duration: 1500
                      });
                      wx.setStorageSync("userinfo", res.data.data);
                      wx.request({
                        url: apiUrlApp + 'common/get_orginfo/',
                        header: {
                          'Content-Type': 'application/x-www-form-urlencoded',
                          'cookie': wx.getStorageSync('username'),
                          'cookie': wx.getStorageSync('uid'),
                          'cookie': wx.getStorageSync('gid')
                        },
                        method: "GET",
                        data: {
                          uid: wx.getStorageSync('uid')
                        },
                        success: function(res) {
                          if (res.data.status == 1) {
                            wx.showToast({
                              title: "帐号详情",
                              icon: 'success',
                              duration: 1500
                            });
                            res.data.data.cat_id_1 = industryClassification[res.data.data.cat_id_1];
                            wx.setStorageSync("companyinfo", res.data.data);

                          } else if (res.data.status == 0) {
                            wx.showToast({
                              title: "无数据返回",
                              icon: 'success',
                              duration: 1500
                            })
                          } else if (res.data.status == -1) {
                            wx.showToast({
                              title: "请求异常,请检查重试",
                              icon: 'loading',
                              duration: 2000
                            })
                          }
                        }
                      })
                    } else if (res.data.status == 0) {
                      wx.showToast({
                        title: "无数据返回",
                        icon: 'success',
                        duration: 1500
                      })
                    } else if (res.data.status == -1) {
                      wx.showToast({
                        title: "请求异常,请检查重试",
                        icon: 'loading',
                        duration: 2000
                      })
                    }
                  }
                })
                if (res.data.status == 1) {
                  wx.showToast({
                    title: "登录成功,页面跳转中",
                    icon: 'success',
                    duration: 1500
                  });
                  wx.reLaunch({
                    url: '../user_center/userCenter'
                  })

                } else if (res.data.status == 0) {
                  wx.showToast({
                    title: "用户名或密码错误!",
                    icon: 'loading',
                    duration: 1500
                  })
                } else if (res.data.status == -1) {
                  wx.showToast({
                    title: "请求异常,请检查重试",
                    icon: 'loading',
                    duration: 1500
                  })
                }
              }
            })
          }
        }
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  changeLoginType: function(e) {
    console.log('changeLoginType--->')
    var title ='';
    loginTypeAreaMobile == true ? (loginTypeStr = loginTypeArr[1], title = loginTypeArr[0]) : (loginTypeStr = loginTypeArr[0], title = loginTypeArr[1])

    wx.setNavigationBarTitle({
      title: title + '登录'
    });
    this.setData({
      loginTypeStr: loginTypeStr,
      loginTypeAreaMobile: loginTypeAreaMobile 
    })
    loginTypeAreaMobile = !loginTypeAreaMobile

  }
})

 

<!--index.wxml-->
<view id="login">
  <form bindsubmit="formSubmit" bindreset="formReset" >
    <view class="page__bd"  class="{{loginTypeAreaMobile===true ? '':'hide'  }}" >
      <view class="weui-cells">
        <navigator url="" class="weui-cell weui-cell_access" hover-class="weui-cell_active">
          <view class="weui-cell__hd">
            <image src="{{localImgPath}}mobile.svg" style="margin-right: 5px;vertical-align: middle;width:20px; height: 20px;"></image>
          </view>
          <input class="weui-input" type="number" value="{{usernameMobile}}" name="username" maxlength="13" auto-focus placeholder="请输入手机号码" />

        </navigator>
        <navigator url="" class="weui-cell weui-cell_access" hover-class="weui-cell_active">
          <view class="weui-cell__hd">
            <image src="{{localImgPath}}mobilePwd.svg" style="margin-right: 5px;vertical-align: middle;width:20px; height: 20px;"></image>
          </view>
          <input class="weui-input" type="number" value="{{passwordMobile}}" name="password" maxlength="13" placeholder="请输入短信验证码" />
          <view class="weui-cell__ft">
                    <button class="weui-vcode-btn sub-font-size-01">获取验证码</button>
                </view>
        </navigator>
      </view>
    </view>
 

    <view class="page__bd"   class="{{loginTypeAreaMobile===true ? 'hide':''  }}" >
      <view class="weui-cells">
        <navigator url="" class="weui-cell weui-cell_access" hover-class="weui-cell_active">
          <view class="weui-cell__hd">
            <image src="{{localImgPath}}usernameLogin.svg" style="margin-right: 5px;vertical-align: middle;width:20px; height: 20px;"></image>
          </view>
          <input class="weui-input" type="text" value="{{username}}" name="username" maxlength="13" auto-focus placeholder="用户名" />

        </navigator>
        <navigator url="" class="weui-cell weui-cell_access" hover-class="weui-cell_active">
          <view class="weui-cell__hd">
            <image src="{{localImgPath}}usernamePwd.svg" style="margin-right: 5px;vertical-align: middle;width:20px; height: 20px;"></image>
          </view>
          <input class="weui-input" type="password" value="{{password}}" name="password" maxlength="13" placeholder="请输入密码" />
        </navigator>
      </view>
    </view>
    <view class="section btn-area">
      <button formType="submit" class="submit-button font_fff">登录</button>
    </view>
  </form>


  <view class=" page__bd page__bd_spacing">
    <view class="button-sp-area">
      <button class="weui-btn weui-btn_mini weui-btn_primary  width_p_40 sub-font-size-01" style="margin-right:0.8em" bindtap="changeLoginType" >切换{{loginTypeStr}}登录</button>
    </view>

  </view>

</view>

  

  

 

posted @ 2018-07-05 22:07  papering  阅读(756)  评论(0编辑  收藏  举报