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>