pushdeer/quickapp/src/pages/index.ux

151 lines
4.0 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!-- <import name="button" src="apex-ui/components/button/index"></import> -->
<template>
<!-- template里只能有一个根节点 -->
<div class="wrapper">
<div class="logo">
<image src="/assets/images/index-logo.svg" />
</div>
<div class="btn-group">
<text class="btn-wx" @click="onWxSignBtnClick">Sign in with WeChat</text>
<!-- class="btn-wx"
value="Sign in with WeChat"
onclick="onWxSignBtnClick"
/> -->
</div>
</div>
</template>
<script>
import storage from '@system.storage'
import router from '@system.router'
// import push from '@service.push'
import wxaccount from '@service.wxaccount'
export default {
// 页面级组件的数据模型影响传入数据的覆盖机制private内定义的属性不允许被覆盖
private: {
},
onWxSignBtnClick() {
var type = wxaccount.getType();
console.log('index:wxtype:' + type)
if (type == 'APP') {
const that = this
wxaccount.authorize({
scope: 'snsapi_userinfo',
state: 'randomString',
success: function (data) {
console.log("wxaccount authorize success:" + JSON.stringify(data));
that.onWxLoginSuccess(data).catch(e => {
$utils.showToast(e)
})
},
fail: function (data, code) {
console.log("wxaccount authorize fail:" + data + ", code:" + code);
},
cancel: function () {
console.log("wxaccount authorize cancelled.");
}
});
} else {
console.log("wxaccount not available.");
}
},
async onWxLoginSuccess(data) {
let token = await API.wxLogin(data.code)
console.log('wxaccount login success token=' + token);
await this.loginSuccess(token)
},
async loginSuccess(token) {
let that = this
console.log('index: loginsuccess');
promiseFactory(storage.set, { key: 'token', value: token })
let regIdFromStorage = await promiseFactory(storage.get, { key: 'regId' })
API.setToken(token)
let userinfo = await API.userInfo()
userinfo.token = token
this.$app.$def.userinfo = userinfo
// push.subscribe({
// success: function ({ regId }) {
// console.log(`push.subscribe succeeded, regIdFromStorage=${regIdFromStorage}, regId=${regId}`);
// // console.log('todo: 若regId发生改变需要上报服务器');
// that.$app.$def.pushRegId = regId
// if (regIdFromStorage !== regId) {
// // TODO 上报新的推送ID
// promiseFactory(storage.set, { key: 'regId', value: regId })
// }
// },
// fail: function (data, code) {
// console.log("push.subscribe failed, result data=" + JSON.stringify(data) + ", code=" + code);
// },
// complete: function () {
// console.log("push.subscribe completed");
// }
// })
// push.on({
// callback: function (ret) {
// console.log('received pass through message, ret = ' + JSON.stringify(ret));
// }
// })
router.replace({
uri: 'pages/Home'
})
},
onInit() {
(async () => {
let token = await promiseFactory(storage.get, { key: 'token' })
console.log(`index: get token = ${token} from storage success`)
if (token.length !== 0) {
await this.loginSuccess(token)
}
})()
.catch(e => {
console.log('catch:' + e);
$utils.showToast(e);
})
.finally(() => {
console.log('index: init complete');
})
},
}
</script>
<style lang="less">
.wrapper {
flex-direction: column;
align-items: center;
height: 100%;
.logo {
flex-direction: column;
justify-content: center;
height: 60%;
width: 50%;
}
.btn-group {
flex-direction: column;
width: 50%;
/* margin: 0 auto; */
text {
margin: 16px 0;
padding: 24px;
border-width: 3px;
border-radius: 8px;
text-align: center;
font-size: 32px;
}
.btn-wx {
color: #296c05;
border-color: #296c05;
}
}
}
</style>