Commit 960cd8e3 by 李伟康

提交登录

1 parent 210af402
......@@ -16,8 +16,13 @@ class UsersApi {
true
)
}
async login() {
/**退出登录 */
logout() {
return api.post({
method: 'scrm.api.sso.wechat.applets.logout'
},
true
)
}
/**调用接口获取登录凭证(code)。通过凭证进而换取用户登录态信息,包括用户在当前小程序的唯一标识(openid)、微信开放平台帐号下的唯一标识(unionid,若当前小程序已绑定到微信开放平台帐号)及本次登录的会话密钥(session_key)等。用户数据的加解密通讯需要依赖会话密钥完成。 */
getWxCode() {
......@@ -78,5 +83,25 @@ class UsersApi {
true
)
}
/** 获取登录账号列表 */
getAccountList() {
return api.post({
method: 'scrm.api.sso.wechat.applets.listSelectAccount',
platformEntrance: 'BUSINESS_ASSISTANT'
},
true
)
}
/**
* 选择账号登录(手机绑定多个账号时) */
selectAccountLogin(data) {
return api.post({
method: 'scrm.api.sso.wechat.applets.selectAccountLogin',
platformEntrance: 'BUSINESS_ASSISTANT',
...data
},
true
)
}
}
export default UsersApi
\ No newline at end of file
{
"pages": ["login/loading/loading", "pages/tab/index/index", "pages/tab/output/output", "pages/tab/mine/mine", "pages/webView/webView", "login/index/index"],
"pages": ["login/loading/loading", "pages/tab/index/index", "pages/tab/output/output", "pages/tab/mine/mine", "pages/webView/webView", "login/index/index","login/accountItems/accountItems"],
"requiredPrivateInfos": ["getLocation"],
"lazyCodeLoading": "requiredComponents",
"window": {
......
// pages/accountItems/accountItems.js
import USERAPI from '~/api/usersApi/index'
Page({
/**
* 页面的初始数据
*/
data: {
/**accountLevel 1工厂。2省 ,3市。4门店 */
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.getListSelectAccount()
},
getListSelectAccount() {
wx.$util.showLoading('加载中...')
api.getListSelectAccount().then(res => {
wx.hideLoading()
console.log(res)
let {
code,
data,
message
} = res
if (code == 0) {
let {
loginUserInfoList,
sessionId
} = data
wx.$util.setSessionId(sessionId)
this.setData({
loginUserInfoList
})
} else {
wx.$util.showToast(message)
}
this.setData({
flag: true
})
})
},
chooseAccount(e) {
let {
index
} = e.currentTarget.dataset
let {
loginUserInfoList
} = this.data
if (loginUserInfoList[index].active) return
loginUserInfoList.map(i => (i.active = false))
loginUserInfoList[index].active = true
this.setData({
loginUserInfoList
})
},
choiceAccountLogin() {
let {
loginUserInfoList
} = this.data
let id = null
loginUserInfoList.map(e => {
if (e.active) id = e.uuid
})
if (!id) {
wx.$util.showToast('请选择账号')
return
}
wx.$util.showLoading()
api.selectAccountLogin({
id
}).then(res => {
let {
data,
message
} = res
if (data) {
wx.$util.setUserInfo(data.loginUserInfoList[0])
const SCENE = wx.$SCENE
if (SCENE) {
let {
path,
query: {
scene
}
} = SCENE
let {
target,
name
} = wx.$util.getSceneId(path)
wx.$router.reLaunch(`${target}.${name}`, {
scene
})
wx.$SCENE = null
} else {
wx.$router.reLaunch('tabIndex.index')
}
} else {
wx.$util.showToast(message)
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
wx.hideHomeButton()
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {},
})
\ No newline at end of file
{
"navigationBarTitleText": "选择账号"
}
\ No newline at end of file
/* login/accountItems/accountItems.wxss */
.layout {
margin: 40rpx 40rpx 0;
font-size: 28rpx;
color: #333;
letter-spacing: 0;
}
.title {
margin-bottom: 32rpx;
}
.items {
width: 100%;
min-height: 104rpx;
box-shadow: 0 0 8rpx 0 rgba(0, 0, 0, 0.15);
padding: 32rpx;
margin-bottom: 40rpx;
background-color: #fff;
display: flex;
align-items: center;
justify-content: space-between;
box-sizing: border-box;
}
.items .name{
width: calc(100% - 70rpx);
}
.items:last-of-type {
margin-bottom: 0;
}
.radio {
width: 32rpx;
height: 32rpx;
border: 2rpx solid #cccccc;
border-radius: 100%;
transition: all .2s;
}
.icon {
width: 36rpx;
height: 36rpx;
transition: all .2s;
display: none;
}
.active .icon {
display: block;
}
.active .radio{
display: none;
}
.btn-box {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
height: 140rpx;
display: flex;
justify-content: center;
align-items: center;
z-index: 20;
background-color: #fff;
border-radius: 40rpx;
font-size: 32rpx;
color: #fff;
letter-spacing: 13rpx;
}
.isFullSucreen-btn {
padding-bottom: 34px !important;
}
.btn {
width: calc(100% - 80rpx);
height: 80rpx;
line-height: 80rpx;
text-align: center;
background-color: #3582EE;
border-radius: 16rpx;
}
.list-not{
font-size: 40rpx;
}
\ No newline at end of file
<view class="layout" style="padding-bottom:{{isFullSucreen?'224rpx':'160rpx'}}">
<view class="title">请选择登录的账号</view>
<view class="list">
<block wx:if="{{flag}}">
<block wx:if="{{loginUserInfoList}}">
<block wx:for="{{loginUserInfoList}}" wx:key="index">
<view class="items {{item.active?'active':''}}" bindtap="chooseAccount" data-index="{{index}}">
<view class="name">
{{item.merchantName}}{{item.isSuper==true?'(超管)':item.userType==1?'(商户号)':'(员工号)'}}
</view>
<view class="icon">
<image src="/images/active-arc.png" />
</view>
<view class='radio' />
</view>
</block>
</block>
<block wx:else>
<view class="list-not" style="height: calc(100vh - {{isFullSucreen ? '159' :'125'}}px)" bindtap="getListSelectAccount">
点击刷新
</view>
</block>
</block>
</view>
<view class="btn-box {{isFullSucreen?'isFullSucreen-btn':''}}">
<view class='btn' bindtap="choiceAccountLogin">确定</view>
</view>
</view>
// login/index/components/LoginBox.js
import USERAPI from '~/api/usersApi/index'
import { setSessionId } from '~/utils/cache/storageApi'
let thisPage
const countDown = 60
Component({
options: {
styleIsolation: 'shared'
......@@ -32,6 +33,11 @@ Component({
* 组件的方法列表
*/
methods: {
hideError(name) {
if (name == 'code') this.setData({
smsError: false
})
},
input(e) {
const {
value
......@@ -44,6 +50,7 @@ Component({
if (name == 'phone') this.setData({
phoneReady: thisPage.validate.methods.tel(value)
})
this.hideError(name)
this.checkFormData()
},
clear(e) {
......@@ -53,6 +60,7 @@ Component({
this.setData({
[`formData.${name}`]: ''
})
this.hideError(name)
this.checkFormData()
},
getSmsCode() {
......@@ -60,25 +68,32 @@ Component({
phoneReady,
formData,
count
} = this.data
} = this.data,
countDown = 60
if (!phoneReady || count) return
this.setData({
smsLoading: true
})
USERAPI.getSmsCode(formData.phone).then(({
code,
message
}) => {
if (code == '0') {
this.setData({
count: countDown
})
this.countDown()
} else {
wx.$api.showToast(message || '发送验证码失败')
}
this.setData({
smsLoading: false
})
if (code == '0') this.countDown(countDown)
else wx.$api.showToast(message || '发送验证码失败')
}).catch(err => {
this.setData({
smsLoading: false
})
wx.$api.showToast(err || '发送验证码失败')
})
},
countDown() {
countDown(countDown) {
if (countDown) this.setData({
count: countDown
})
setTimeout(() => {
let {
count
......@@ -122,17 +137,27 @@ Component({
2: 'accountLogin'
} [loginType],
obj = loginType == 1 ? {
mobile : formData.phone,
code : formData.code
mobile: formData.phone,
code: formData.code
} : {
userName: formData.account,
password: formData.password
}
this.setBtnLoading()
USERAPI[target](obj).then(res => {
console.log(res)
USERAPI[target](obj).then(({
code,
data,
message
}) => {
this.setBtnLoading(false)
if (code == '0') {} else {
if (loginType == 1) this.setData({
smsError: true
})
wx.$api.showToast(message || '系统异常')
}
}).catch(err => {
wx.$api.showToast('系统异常')
this.setBtnLoading(false)
})
},
......@@ -141,10 +166,34 @@ Component({
}) {
if (!detail.code) return
this.setBtnLoading()
USERAPI.authUserPhone(detail, this.loginToken).then(res => {
console.log(res)
USERAPI.authUserPhone(detail, this.loginToken).then(({
code,
data,
message
}) => {
if (code == '0') {
const {
loginUserInfoList,
sessionId
} = data
if (loginUserInfoList.length > 1) {
wx.$router.go('login.accountItems')
} else {
wx.$router.go('login.accountItems')
USERAPI.selectAccountLogin({
id: loginUserInfoList[0].uuid,
sessionId
}).then(res =>{
console.log(res)
setSessionId(sessionId)
})
}
} else wx.$api.showToast(message || '系统异常')
this.setBtnLoading(false)
}).catch(err => {
}).catch(({
errMsg
}) => {
wx.$api.showToast(errMsg || '系统异常')
this.setBtnLoading(false)
})
},
......@@ -155,17 +204,45 @@ Component({
this.setData({
loading
})
},
async init() {
thisPage = wx.$router.getPageInstance()
wx.$api.showLoading()
try {
const {
data,
code,
message
} = await USERAPI.wxCodeLogin()
if (code == '0') {
thisPage.setData({
disabled: false
})
this.loginToken = data
} else this.reLogin(message)
wx.hideLoading()
} catch ({
errMsg
}) {
console.error(errMsg)
this.reLogin()
}
},
reLogin(message) {
wx.showModal({
content: message || '获取loginToken失败',
confirmText: '重试',
confirmColor: '#3666FE'
}).then(({
confirm
}) => {
if (confirm) this.init()
})
}
},
lifetimes: {
attached: function () {
// 在组件实例进入页面节点树时执行
USERAPI.wxCodeLogin().then(({
data
}) => {
this.loginToken = data
})
thisPage = wx.$router.getPageInstance()
this.init()
},
detached: function () {
// 在组件实例被从页面节点树移除时执行
......
......@@ -47,16 +47,16 @@
}
.sms {
width: 130rpx;
width: 160rpx;
flex-shrink: 0;
color: #5092FF;
font-size: 24rpx;
padding: 0;
text-align: right;
&[disabled]:not([type]){
color: #CCCED1;
}
}
.sms-disabled {
color: #CCCED1;
}
.error {
position: absolute;
font-size: 24rpx;
......
......@@ -13,9 +13,9 @@
<view class="input">
<input type="number" placeholder="填写验证码" placeholder-class="placeholder" bindinput="input" data-name="code" value="{{formData.code}}" maxlength="4" />
<input-icon show="{{formData.code}}" bindtap="clear" data-name="code" />
<view class="sms text-center {{count?'sms-disabled':''}} {{phoneReady?'':'sms-disabled'}} " bindtap="getSmsCode">{{
<button class="sms text-center " bindtap="getSmsCode" disabled="{{count || !phoneReady}}" loading="{{smsLoading}}">{{
count? count + 's':'获取验证码'
}}</view>
}}</button>
<block wx:if="{{smsError}}">
<view class="error">验证码错误</view>
</block>
......@@ -23,7 +23,7 @@
</block>
<block wx:else>
<view class="input">
<input type="text" placeholder="填写账号名" placeholder-class="placeholder" bindinput="input" data-name="account" value="{{formData.account}}" maxlength="20" />
<input type="text" placeholder="填写账号" placeholder-class="placeholder" bindinput="input" data-name="account" value="{{formData.account}}" maxlength="20" />
<input-icon show="{{formData.account}}" bindtap="clear" data-name="account" />
</view>
<view class="input">
......
......@@ -5,7 +5,8 @@ Page({
* 页面的初始数据
*/
data: {
loginType: 0
loginType: 0,
disabled: true
},
/**
......@@ -61,17 +62,18 @@ Page({
checkLoginBtn(disabled = false) {
const {
loginType
} = this.data
if (loginType == 0) disabled = true
else {
const {
} = this.data, {
data: {
formData,
phoneReady
} = this.selectComponent('#box').data
disabled = loginType == 1 ?
phoneReady && formData.code :
formData.account && formData.password
}
},
loginToken
} = this.selectComponent('#box')
if (loginType == 0) {
disabled = loginToken
} else disabled = loginType == 1 ?
phoneReady && formData.code :
formData.account && formData.password
this.setData({
disabled: !disabled
})
......
......@@ -33,9 +33,10 @@ Page({
path = decodeURIComponent(url)
} = this.options
console.log(path)
if (sessionId) {
wx.$router.reLaunch('login.index')
console.log(sessionId)
} else {
wx.$router.reLaunch('login.index')
}
......
......@@ -13,6 +13,13 @@
"query": "",
"launchMode": "default",
"scene": null
},
{
"name": "",
"pathName": "login/index/index",
"query": "",
"launchMode": "default",
"scene": null
}
]
}
......
......@@ -89,11 +89,8 @@ class Router {
}
/**跳转至H5 */
jumpH5(url) {
this.intercept().then(response => {
if (response) return
wx.navigateTo({
url: `/pages/webView/webView?url=${encodeURIComponent(url)}`
})
wx.navigateTo({
url: `/pages/webView/webView?url=${encodeURIComponent(url)}`
})
}
/**页面跳转
......@@ -117,20 +114,10 @@ class Router {
url: routerMapItem.url
})
} else {
if (events && events.notIntercept) {
wx.navigateTo({
url: `${routerMapItem.url}${query}`,
success: res => events && res.eventChannel.emit('getData', events)
})
} else {
this.intercept().then(response => {
if (response) return
wx.navigateTo({
url: `${routerMapItem.url}${query}`,
success: res => events && res.eventChannel.emit('getData', events)
})
})
}
wx.navigateTo({
url: `${routerMapItem.url}${query}`,
success: res => events && res.eventChannel.emit('getData', events)
})
}
}
/**关闭所有页面,打开到应用内的某个页面 */
......@@ -139,11 +126,8 @@ class Router {
routerMapItem,
query
} = this.checkRouter(route, events)
this.intercept().then(response => {
if (response) return
wx.reLaunch({
url: `${routerMapItem.url}${query}`
})
wx.reLaunch({
url: `${routerMapItem.url}${query}`
})
}
/**关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。 */
......@@ -152,11 +136,8 @@ class Router {
routerMapItem,
query
} = this.checkRouter(route, events)
this.intercept().then(response => {
if (response) return
wx.redirectTo({
url: `${routerMapItem.url}${query}`
})
wx.redirectTo({
url: `${routerMapItem.url}${query}`
})
}
/**prevPageSize = 1
......@@ -173,7 +154,7 @@ class Router {
}
goHome() {
let [index] = this[_routerList]
wx.switchTab({
wx.reLaunch({
url: index.url
})
}
......@@ -189,18 +170,6 @@ class Router {
if (i > -1) list.splice(i, 1)
}
}
async intercept() {
const authStatus = wx.$authStatus
if (authStatus) {
const thisPage = this.getPageInstance()
thisPage.setData({
showLogin: true
})
return await true
} else {
return await false
}
}
}
export default Router
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!