|
|
import config from '../../config.js'
|
|
|
import request from '../request.js'
|
|
|
import common from '../common.js'
|
|
|
|
|
|
let mpToutiao = {}
|
|
|
|
|
|
/**
|
|
|
* 抖音小程序获取token
|
|
|
* @date 2022-10-19
|
|
|
*/
|
|
|
mpToutiao.getOpenid = function() {
|
|
|
let openid = uni.getStorageSync('openid')
|
|
|
let user_id = uni.getStorageSync('user_id')
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
if (!openid) {
|
|
|
this.login().then(res => {
|
|
|
resolve(res)
|
|
|
}).catch(res => {
|
|
|
reject()
|
|
|
})
|
|
|
} else {
|
|
|
// 检查登录态 wx.checkSession session_key如果失效,需要重新执行登录流程
|
|
|
uni.checkSession({
|
|
|
success: (res) => {
|
|
|
resolve({
|
|
|
openid: uni.getStorageSync('openid'),
|
|
|
user_id: uni.getStorageSync('user_id'),
|
|
|
})
|
|
|
console.log('生产环境读取缓存')
|
|
|
console.log('openid = ' + uni.getStorageSync('openid'))
|
|
|
console.log('user_id = ' + uni.getStorageSync('user_id'))
|
|
|
console.log('access_token = ' + uni.getStorageSync('access_token'))
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
console.log('checkSessionFail')
|
|
|
this.login().then(res => {
|
|
|
resolve(res)
|
|
|
}).catch(res => {
|
|
|
reject()
|
|
|
})
|
|
|
}
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 登录
|
|
|
* @date 2022-10-19
|
|
|
*/
|
|
|
mpToutiao.login = function() {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
// 获取授权登录的服务供应商
|
|
|
uni.getProvider({
|
|
|
service: 'oauth',
|
|
|
success: (res) => {
|
|
|
if (~res.provider.indexOf('toutiao')) {
|
|
|
// 登录
|
|
|
uni.login({
|
|
|
provider: 'toutiao',
|
|
|
success: (loginRes) => {
|
|
|
// console.log(loginRes)
|
|
|
request.getData('base/mptoutiao/api/Auth/getOpenid', {
|
|
|
code: loginRes.code,
|
|
|
anonymous_code: loginRes.anonymousCode
|
|
|
}, {
|
|
|
loading: 1
|
|
|
}).then(res => {
|
|
|
uni.setStorageSync('openid', res.data
|
|
|
.openid)
|
|
|
uni.setStorageSync('user_id', res.data
|
|
|
.user_id)
|
|
|
uni.setStorageSync('access_token', res.data
|
|
|
.access_token)
|
|
|
uni.setStorageSync(
|
|
|
'access_token_expire_time', res.data
|
|
|
.access_token_expire_time)
|
|
|
|
|
|
resolve({
|
|
|
openid: uni.getStorageSync(
|
|
|
'openid'),
|
|
|
user_id: uni.getStorageSync(
|
|
|
'user_id')
|
|
|
})
|
|
|
console.log('生产环境刚获得')
|
|
|
console.log('openid = ' + uni
|
|
|
.getStorageSync('openid'))
|
|
|
console.log('user_id = ' + uni
|
|
|
.getStorageSync('user_id'))
|
|
|
console.log('access_token = ' + uni
|
|
|
.getStorageSync('access_token'))
|
|
|
}, err => {
|
|
|
console.log(err)
|
|
|
reject(err)
|
|
|
})
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
|
|
|
export default mpToutiao
|