|
@@ -0,0 +1,230 @@
|
|
|
+<template>
|
|
|
+<!-- 前置数据-->
|
|
|
+ <div>
|
|
|
+
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+<script>
|
|
|
+import { Toast } from 'vant'
|
|
|
+import { getInitialCode, getUserInfo, fetchUserAccessToken } from '@/common/js/api_lingxi'
|
|
|
+// import constant from '@/common/js/constant'
|
|
|
+export default {
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ appId: 'B1334087658781568',
|
|
|
+ requestCode: '',
|
|
|
+ userAccessToken: ''
|
|
|
+ }
|
|
|
+ },
|
|
|
+ mounted() {
|
|
|
+ // alert('开始获取灵锡用户id,并进行跳转')
|
|
|
+ this.getIdToUrl('cityDangerReport')
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ // '开始获取灵锡用户id,并进行跳转'
|
|
|
+ getIdToUrl(urlname) {
|
|
|
+ this.getInitCode(urlname)
|
|
|
+
|
|
|
+ // let user_id = ''
|
|
|
+ // this.$router.push({ name: urlname, params: { openId: 'xxxx' , realName: '2222222'}})
|
|
|
+ // this.handleAuthorization().then(e => {
|
|
|
+ // user_id = e
|
|
|
+ // // eslint-disable-next-line standard/object-curly-even-spacing
|
|
|
+ //
|
|
|
+ // })
|
|
|
+ },
|
|
|
+ async getInitCode(urlname) {
|
|
|
+ getInitialCode().then((res) => {
|
|
|
+ // alert('getInitialCode==>' + JSON.stringify(res))
|
|
|
+ this.initCode = res.data.initCode
|
|
|
+ // alert('initCode==>' + this.initCode)
|
|
|
+ this.handleAuthorization(urlname)
|
|
|
+ })
|
|
|
+ },
|
|
|
+ handleAuthorization(urlname) {
|
|
|
+ // // alert(this.initCode)
|
|
|
+ const _this = this
|
|
|
+ ls.ready(function() {
|
|
|
+ ls.userAuth({
|
|
|
+ appId: _this.appId // 从开放平台申请到的 appId
|
|
|
+ }, function(res) {
|
|
|
+ // alert('res:' + JSON.stringify(res))
|
|
|
+ if (res.code === 200) {
|
|
|
+ // // alert('requestCode:' + res.data.requestCode)
|
|
|
+ const reqData = {
|
|
|
+ requestCode: res.data.requestCode
|
|
|
+ }
|
|
|
+ fetchUserAccessToken(reqData).then(res => {
|
|
|
+ // alert('获取用户访问令牌——:' + res)
|
|
|
+ getUserInfo({ userAccessToken: res }).then((res) => {
|
|
|
+ // alert('用户信息——:' + JSON.stringify(res))
|
|
|
+ // alert('用户信息2——:' + res.openId)
|
|
|
+ // eslint-disable-next-line standard/object-curly-even-spacing
|
|
|
+ _this.$router.push({ name: urlname, params: { openId: res.openId, realName: res.realName }})
|
|
|
+
|
|
|
+ // Toast('登录成功')
|
|
|
+ // if (res.data.user.isInit === '1') {
|
|
|
+ // _this.$router.push({ path: '/residentHome' })
|
|
|
+ // } else {
|
|
|
+ // _this.$router.push({ path: '/resident/mine/personalInfo' })
|
|
|
+ // }
|
|
|
+ }).catch(e => {
|
|
|
+ Toast(e)
|
|
|
+ })
|
|
|
+ })
|
|
|
+ } else if (res.code === 9005) {
|
|
|
+ // 认证失败或登录失败
|
|
|
+ // console.log('认证失败或登录失败')
|
|
|
+ // alert('认证失败或登录失败')
|
|
|
+ // 如果在此处调用ls.close()关闭页面,需要延迟0.5秒执行,否则ls.close()会无效。
|
|
|
+ } else {
|
|
|
+ // 其余情况
|
|
|
+ // alert('userAuth error')
|
|
|
+ // 如果在此处调用ls.close()关闭页面,需要延迟0.5秒执行,否则ls.close()会无效。
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ // 失败回调
|
|
|
+ ls.error(function(res) {
|
|
|
+ // 错误处理
|
|
|
+ // alert('失败回调')
|
|
|
+ })
|
|
|
+
|
|
|
+ ls.config({
|
|
|
+ debug: false, // 开发时建议把调试模式开启
|
|
|
+ appId: this.appId, // 从开放平台申请到的 appId
|
|
|
+ initCode: this.initCode // 从业务方自己后台请求到 initCode
|
|
|
+ })
|
|
|
+ }
|
|
|
+ // // 授权并获取用户信息
|
|
|
+ // async handleAuthorization() {
|
|
|
+ // // alert('start')
|
|
|
+ // this.userAccessToken = ''
|
|
|
+ // // 应该由服务端发起请求
|
|
|
+ // var rt = await this.fetchInitCode({})
|
|
|
+ // // alert('rt' + rt)
|
|
|
+ // rt = JSON.parse(rt)
|
|
|
+ // var data = rt.data
|
|
|
+ // // alert('ls' + ls)
|
|
|
+ // // alert('到这里了0')
|
|
|
+ //
|
|
|
+ // // 成功回调
|
|
|
+ // ls.ready(function() {
|
|
|
+ // // alert('到这里了1')
|
|
|
+ // // 自定义逻辑,如:
|
|
|
+ // // 1.调用 JSSDK 里提供的交互方法
|
|
|
+ // // 2.如需获取用户信息,需要在此处调用 ls.userAuth(Objectobject)方法获取requestCode。
|
|
|
+ // // userAuth根据appid会自动判断是否需要登录、认证和授权。
|
|
|
+ // ls.userAuth({
|
|
|
+ // appId: this.appId // 从开放平台申请到的 appId
|
|
|
+ // }, function(res) {
|
|
|
+ // // alert('到这里了1res' + res)
|
|
|
+ // if (res.code === 200) {
|
|
|
+ // this.requestCode = res.data.requestCode // 获取到requestcode
|
|
|
+ // // 获取用户信息
|
|
|
+ // return this.getUserInfo()
|
|
|
+ // } else if (res.code === 9005) {
|
|
|
+ // // 认证失败或登录失败
|
|
|
+ // // alert('认证失败或登录失败')
|
|
|
+ // // // alert('认证失败或登录失败')
|
|
|
+ // // 如果在此处调用ls.close()关闭页面,需要延迟0.5秒执行,否则ls.close()会无效。
|
|
|
+ // } else {
|
|
|
+ // // 其余情况
|
|
|
+ // // alert('userAuth error')
|
|
|
+ // // // alert('userAuth error')
|
|
|
+ // // 如果在此处调用ls.close()关闭页面,需要延迟0.5秒执行,否则ls.close()会无效。
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ // })
|
|
|
+ //
|
|
|
+ // // alert('到这里了5')
|
|
|
+ // ls.error(function(e) {
|
|
|
+ // // alert(e)
|
|
|
+ // })
|
|
|
+ // // alert('到这里了6')
|
|
|
+ // // 确保ready方法初始化以后调用config方法
|
|
|
+ // ls.config({
|
|
|
+ // debug: true, // 开发时建议把调试模式开启
|
|
|
+ // appId: this.appId,
|
|
|
+ // initCode: data.initCode
|
|
|
+ // })
|
|
|
+ // // alert('到这里了end')
|
|
|
+ // // // alert('end')
|
|
|
+ // },
|
|
|
+ // // 获取用户信息
|
|
|
+ // async getUserInfo() {
|
|
|
+ // const requestCode = this.requestCode
|
|
|
+ // let userAccessToken = this.userAccessToken
|
|
|
+ // // alert('getUserInfo:' + requestCode, userAccessToken)
|
|
|
+ //
|
|
|
+ // if (!userAccessToken && !requestCode) {
|
|
|
+ // // alert('请先授权')
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // // 授权过了无需再重复获取userAccessToken
|
|
|
+ // if (!userAccessToken) {
|
|
|
+ // // 获取用户访问令牌 userAccessToken
|
|
|
+ // const userAccess = await this.fetchUserAccessToken(requestCode)
|
|
|
+ // userAccessToken = userAccess.data.userAccessToken
|
|
|
+ // this.userAccessToken = userAccessToken
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // // 获取用户信息 userInfo
|
|
|
+ // const user = await this.fetchUserInfo(userAccessToken)
|
|
|
+ //
|
|
|
+ // const userInfo = user.data
|
|
|
+ // // alert('用户信息' + userInfo)
|
|
|
+ // // alert('用户信息' + userInfo)
|
|
|
+ // return userInfo
|
|
|
+ // },
|
|
|
+ // // 使用ajax的方法
|
|
|
+ // httpPostLocaltion(url, data) {
|
|
|
+ // // alert('请求' + url)
|
|
|
+ // return new Promise((resolve, reject) => {
|
|
|
+ // const xhr = new XMLHttpRequest()
|
|
|
+ // xhr.open('post', url, true)
|
|
|
+ // xhr.onreadystatechange = function() {
|
|
|
+ // if (xhr.readyState === 4) {
|
|
|
+ // // console.log('获取数据1', xhr.response);
|
|
|
+ // if (xhr.status === 200) {
|
|
|
+ // // // alert(xhr.responseText);
|
|
|
+ // // return xhr.response
|
|
|
+ // resolve(xhr.response)
|
|
|
+ // } else {
|
|
|
+ // reject(new Error('异常失败' + xhr.status))
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // // const data = {
|
|
|
+ // // name:'zt'
|
|
|
+ // // }
|
|
|
+ // xhr.send(JSON.stringify(data))
|
|
|
+ // })
|
|
|
+ // // // 注意,设置请求头的信息必须写在下面,否则会报错
|
|
|
+ // // // 设置以json传参
|
|
|
+ // // xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
|
|
+ // // 解决跨域问题
|
|
|
+ // // xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
|
|
|
+ // // // 设置请求体携带的参数
|
|
|
+ // },
|
|
|
+ // // 获取初始码
|
|
|
+ // fetchInitCode(option) {
|
|
|
+ // return this.httpPostLocaltion(constant.BASE_URI + '/wx/LingXiConnectController/initCode', option)
|
|
|
+ // },
|
|
|
+ // // 获取AccessToken
|
|
|
+ // fetchUserAccessToken(requestCode) {
|
|
|
+ // // // alert(111)
|
|
|
+ // return this.httpPostLocaltion(constant.BASE_URI + '/wx/LingXiConnectController/fetchUserAccessToken', { requestCode: requestCode })
|
|
|
+ // },
|
|
|
+ // fetchUserInfo(userAccessToken) {
|
|
|
+ // return this.httpPostLocaltion(constant.BASE_URI + '/wx/LingXiConnectController/fetchUserInfo', { userAccessToken: userAccessToken })
|
|
|
+ // }
|
|
|
+ }
|
|
|
+}
|
|
|
+</script>
|
|
|
+
|
|
|
+<style scoped lang="scss">
|
|
|
+
|
|
|
+</style>
|