| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- <template>
- <view class="container">
- <view class="banner">
- <image src="../../static/images/banner2x.png" mode=""></image>
- </view>
- <view class="nav">
- <view class="menu" v-show="my_display">
- <navigator :url="'/pages/reshui/reshui?item=' + encodeURIComponent(JSON.stringify(this.userinfo))"
- open-type="redirect" class="menu_item">
- <image src="../../static/images/shower2x.png" mode=""></image>
- <text>洗 浴</text>
- </navigator>
- <navigator :url="'/pages/jiaofei/jiaofei?item=' + encodeURIComponent(JSON.stringify(this.userinfo))"
- open-type="redirect" class="menu_item">
- <image src="../../static/images/recharge2x.png" mode=""></image>
- <text>电费充值</text>
- </navigator>
- </view>
- <view class="reset">
- <navigator url="" open-type="redirect" @click="reauthorization()" class="menu_item">
- <text class="iconfont icon-zhongzhi"></text>
- <text>重新授权</text>
- </navigator>
- </view>
- </view>
- <view v-if="showLogin">
- <login :ocode="ocode" :appkey="appkey" scope="snsapi_userinfo" :visible="visible" @success="callback()"
- @cancel="callback()" @fail="callback()" />
- </view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- visible: false,
- showLogin: true,
- appkey: '5AA49F3E4CACA380',
- sub_appid: '1015730314_1941301045',
- appid: 'wx2fc3f45732fae5d3', // 获取用户信息
- ocode: '1015730314', // 获取用户信息
- app_secret: '58D34C81D82B35179ED896C4362B0FC0', // 获取用户信息
- // 用于传给后代页面
- appid_pass: 'wxd6f090391d410534', // 传给后代页面,用于唤起支付
- ocode_pass: '1609853253', // 传给后代页面,用于唤起支付
- app_secret_pass: 'eb99fffa11caa585fcf7c97c891a5e1f', // 传给后代页面,用于唤起支付
- wxcode: '',
- access_token: '',
- access_token_for_amount: '', // 获取到应用有效凭证
- refresh_token: '',
- my_display: false,
- userinfo: {},
- amount: 0.00
- }
- },
- onLoad() {
- try {
- const value = uni.getStorageSync('userinfo_storage_key');
- if (value == '') {
- if (JSON.stringify(this.userinfo) == '{}') {
- this.showLogin = true
- this.visible = true
- } else {
- this.showLogin = false
- this.my_display = true
- }
- } else {
- this.showLogin = false
- this.userinfo = value
- this.my_display = true
- }
- // 获取应用有效凭证,并查询余额
- this.get_valid_credentials()
- } catch (e) {
- // console.log(e)
- uni.showToast({
- icon: 'success',
- title: '用户获取失败',
- duration: 2000
- })
- }
- },
- methods: {
- /**
- * 重新授权
- */
- reauthorization() {
- try {
- uni.removeStorageSync('userinfo_storage_key');
- setTimeout(() => {
- this.showLogin = false
- this.userinfo = {}
- this.my_display = false
- }, 300)
- setTimeout(() => {
- this.showLogin = true
- this.visible = true
- }, 300)
- } catch (e) {
- console.log(e)
- }
- },
- /**
- * 授权回调
- */
- callback: function({
- detail
- }) {
- // console.log(detail)
- // {wxcode: "7f7Qi9rN8zQ1nifQiysTZ3WIeIWlgcGk"}
- if (detail === undefined) {
- this.userinfo = {};
- uni.showToast({
- icon: 'success',
- title: '需要先授权'
- });
- } else {
- const {
- wxcode = ""
- } = detail
- if (wxcode.length == 0) {
- uni.showToast({
- title: '未获得wxcode',
- duration: 500
- })
- } else {
- // 获取wxcode
- this.wxcode = wxcode
- // 通过wxcode换取access_token
- this.get_access_token()
- }
- }
- },
- /**
- * 通过wxcode换取access_token
- */
- async get_access_token() {
- const res = await this.$myRequest({
- host: 'wecard',
- url: '/connect/oauth2/token',
- method: 'POST',
- header: {
- 'content-type': 'application/json'
- },
- data: {
- 'wxcode': this.wxcode,
- 'app_key': this.appkey,
- 'app_secret': this.app_secret,
- 'grant_type': 'authorization_code',
- 'redirect_uri': 'mnp://wx2fc3f45732fae5d3'
- }
- })
- if (res.data.refresh_token == '' || res.data.refresh_token == 'undefined') {
- uni.showToast({
- title: '未获得token'
- });
- } else {
- this.access_token = res.data.access_token;
- this.refresh_token = res.data.refresh_token;
- // 通过access_token换取用户信息
- this.get_user_info();
- }
- },
- /**
- * 通过access_token换取用户信息
- */
- async get_user_info() {
- const res = await this.$myRequest({
- host: 'wecard',
- url: '/connect/oauth/get-user-info',
- method: 'POST',
- header: {
- 'content-type': 'application/json'
- },
- data: {
- "access_token": this.access_token
- }
- });
- if (res.data.errcode == 0 && res.data.errmsg == 'OK') {
- try {
- this.userinfo = res.data
- this.userinfo.amount = this.amount
- // 微校参数
- this.userinfo.appkey = this.appkey
- this.userinfo.sub_appid = this.sub_appid
- this.userinfo.appid = this.appid_pass
- this.userinfo.app_secret = this.app_secret_pass
- this.userinfo.ocode = this.ocode_pass
- this.userinfo.access_token = this.access_token
- // console.log(this.access_token)
- // 存储用户信息
- uni.setStorageSync('userinfo_storage_key', this.userinfo)
- // 提示授权成功
- uni.showToast({
- icon: 'success',
- title: '授权成功',
- duration: 800,
- success: (res) => {
- this.my_display = true
- }
- })
- } catch (e) {
- console.log(e)
- }
- } else {
- uni.showToast({
- title: '未获得用户信息',
- duration: 1000
- });
- }
- },
- /**
- * 获取应用有效凭证
- */
- async get_valid_credentials() {
- const res = await this.$myRequest({
- host: 'wecard',
- url: '/cgi-bin/oauth2/token',
- method: 'POST',
- header: {
- 'content-type': 'application/json'
- },
- data: {
- 'app_key': this.appkey,
- 'app_secret': this.app_secret,
- "grant_type": "client_credentials",
- "scope": "base",
- "ocode": this.ocode
- }
- });
- if (typeof(res.data.access_token) != 'undefined') {
- // 获取到应用有效凭证,保存到页面变量中
- this.access_token_for_amount = res.data.access_token
- // 用户充值钱包余额查询
- this.request_user_amount()
- } else {
- uni.showToast({
- title: '获取凭证失败',
- duration: 2000
- })
- }
- },
- /**
- * 用户充值钱包余额查询
- */
- async request_user_amount() {
- const res = await this.$myRequest({
- host: 'wecard',
- url: "/cgi-bin/pay/app/wallet-recharge-balance?access_token=" + this
- .access_token_for_amount + "&sub_appid=" + this.sub_appid + "&user_id=" + this
- .userinfo.card_number
- });
- if (res.data.code === 0) {
- // 用户余额保存到页面变量中
- this.amount = res.data.data.amount;
- } else {
- uni.showToast({
- icon: 'none',
- title: '余额查询失败:\n' + res.data.message,
- duration: 2000
- })
- }
- }
- }
- }
- </script>
- <style scoped lang="scss">
- .container {
- display: flex;
- flex-direction: column;
- width: 750rpx;
- font-family: Microsoft YaHei-3970(82674968);
- color: #333333;
- .banner {
- width: 100%;
- height: 360rpx;
- image {
- width: 100%;
- height: 100%;
- }
- }
- .nav {
- position: relative;
- width: 100%;
- .menu,
- .reset {
- margin: 25rpx auto;
- width: 640rpx;
- }
- .menu_item {
- display: inline-flex;
- flex-direction: column;
- text-align: center;
- width: 25%;
- padding: 30rpx 0 39rpx;
- border-radius: 20rpx;
- font-family: Microsoft YaHei-3970(82674968);
- color: #333333;
- image {
- width: 90rpx;
- height: 90rpx;
- margin: 0 auto;
- }
- text {
- height: 29rpx;
- line-height: 54rpx;
- font-size: 30rpx;
- color: #333333;
- }
- }
- .reset {
- position: fixed;
- display: flex;
- flex-direction: column;
- align-items: flex-end;
- right: 55rpx;
- bottom: 55rpx;
- font-family: Microsoft YaHei-3970(82674968);
- color: #333333;
- .icon-zhongzhi {
- margin: 0 auto;
- padding-top: 20rpx;
- color: $my-color-primary;
- width: 90rpx;
- height: 70rpx;
- font-size: 80rpx;
- }
- }
- }
- }
- </style>
|