cf-wx-app/src/util/weChatLogin.js

75 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-02-20 16:59:06 +08:00
import Taro from '@tarojs/taro'
import URL from '../serviceAPI.config'
//用户信息姓名和电话号码接口
const setUserInfoToStorage = () => {
Taro.request({
url: URL.ShopWxStore,
header: {
'Cookie': 'PFWSSS=' + Taro.getStorageSync('session_id'),
}
})
.then(res => {
if (res.data.err_msg === 'success') {
Taro.setStorageSync('user_identity', { username: res.data.otherData.userName, userphone: res.data.otherData.userPhone })
} else {
Taro.showToast({
title: res.data.err_msg,
icon: 'none',
duration: 1500
})
}
})
}
// 异步函数登入api
export default function login() {
Taro.login({
success(res) {
if (res.code) {
// 发起网络请求
console.log('手机端微信code', res.code)
Taro.request({
url: URL.Login,
method: 'POST',
dataType: 'json',
data: {
code: res.code
},
header: {
'content-type': 'application/x-www-form-urlencoded',
}
})
.then(response => {
if (response.data.err_code === 0) {
console.log('微信登入成功', response)
Taro.showToast({
title: '微信登入成功',
icon: 'success',
duration: 2000
})
Taro.setStorageSync('session_id', response.data.session_id)
Taro.setStorageSync('shopInfo', response.data.shop_info)
Taro.setStorageSync('userInfo', response.data.user_info)
setUserInfoToStorage()
} else {
Taro.showToast({
title: '微信登入失败',
icon: 'none',
duration: 2000
})
// Taro.clearStorageSync()
}
}
)
}
else {
console.log('登录失败!' + res.errMsg)
}
}
})
}