小程序用户中心 登录逻辑

 

 

校验本地缓存

 

user_center

var util = require('../../utils/util.js');
var chkStrLength = util.chkStrLength;
var gd = getApp().globalData;
var apiUrlApp = gd.apiUrlApp;
var localImgPath = gd.localImgPath;

var industryClassification = {};
industryClassification[123] = "农林牧渔业";
industryClassification[456] = "制造业";
industryClassification[789] = "批发和零售业";

Page({
  data: {
    username: wx.getStorageSync('username'), 
    userinfo: wx.getStorageSync('userinfo'),
    companyinfo: wx.getStorageSync('companyinfo'),
    localImgPath: localImgPath
  },
  onLoad: function(option) {
    console.log("加载用户中心页面,判断是否需要登录")
    console.log(gid)
    var username = wx.getStorageSync('username')
    var uid = wx.getStorageSync('uid')
    var gid = wx.getStorageSync('gid')

    //校验逻辑:参数是否合法、网络环境是否异常(是否非历史地市)
    if (!(chkStrLength(username,4) && chkStrLength(uid,1) && chkStrLength(gid,1)) && 12 > 9) {
      //设置“切换帐号”按钮,否则一直利用本地cookie判断是否需要重新登录
      console.log("goToLogin")
      wx.reLaunch({
        url: '../user_login/userLogin'
      })
    } else {}
  },
  onShow: function(option) {}
})

 

数据读取本地存储

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;  

Page({
  onLoad: function(option) {
    console.log("加载登录页面")
    wx.clearStorageSync()
    console.log("同步清理本地数据缓存")
  },
  data: {},
  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
                  })
                }
              }
            })
          }
        }
      })
    }
  }
})

 

 

 

 

  

 

user_login

 

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