index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <view>
  3. <view class="u-flex user-box u-p-l-30 u-p-r-20 u-p-b-30">
  4. <view class="u-m-r-10" @click="login($store.state.vuex_hasLogin)">
  5. <u-avatar :src="userInfo.headImgUrl || userInfo.avatar||pic" size="140"></u-avatar>
  6. </view>
  7. <view class="u-flex-1" @click="login($store.state.vuex_hasLogin)">
  8. <view class="u-font-18 u-p-b-20">{{ userInfo.nickname || userInfo.userName || '请登录' }}</view>
  9. <view class="u-font-14 u-tips-color">手机号:{{ userInfo.mobile || '暂无' }}</view>
  10. </view>
  11. <view class="u-m-l-10 u-p-10">
  12. <u-icon name="arrow-right" color="#969799" size="28"></u-icon>
  13. </view>
  14. </view>
  15. <view class="u-m-t-20">
  16. <u-cell-group>
  17. <u-cell-item icon="star" title="我的车辆" @click="openPage('pages/myCars/myCars')"></u-cell-item>
  18. </u-cell-group>
  19. </view>
  20. <view class="u-m-t-20">
  21. <u-cell-group>
  22. <u-cell-item icon="star" title="手机号登录" @click="openPage('/pages/center/phoneLogin/phoneLogin')"></u-cell-item>
  23. <u-cell-item icon="star" title="微信支付" @click="handlewxpay"></u-cell-item>
  24. </u-cell-group>
  25. </view>
  26. <u-toast ref="uToast" />
  27. </view>
  28. </template>
  29. <script>
  30. import getUrlParams from "../../utils/getUrlParams.js";
  31. export default {
  32. data() {
  33. return {
  34. pic:'https://uviewui.com/common/logo.png',
  35. userInfo:[],
  36. code:null,
  37. }
  38. },
  39. onLoad() {
  40. if(this.$store.state.vuex_hasLogin){
  41. this.userInfo = this.$store.state.vuex_user;
  42. if(this.$store.state.vuex_wxinfo){
  43. this.userInfo = Object.assign(this.userInfo,this.$store.state.vuex_wxinfo);
  44. }
  45. }else{
  46. this.userInfo = [];
  47. };
  48. let locationLocaturl = window.location.search;
  49. this.code = getUrlParams(locationLocaturl,"code");
  50. if(this.code&&!this.$store.state.vuex_wxinfo.openId){this.handleGetWXInfo(this.code)};
  51. },
  52. methods: {
  53. openPage(path) {
  54. console.log('path',path);
  55. this.$u.route({
  56. url: path
  57. })
  58. },
  59. //登录判断
  60. login(status){
  61. if(!status){
  62. console.log('config',this.config);
  63. window.location.replace(this.config.loginUrl)
  64. }
  65. },
  66. getCode () {
  67. var local = window.location.href // 获取页面url
  68. let locationLocaturl = window.location.search;
  69. this.code = getUrlParams(locationLocaturl,"code"); // 截取code
  70. if (this.code == null || this.code === '') { // 如果没有code,则去请求
  71. window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.config.wxAppid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&#wechat_redirect`
  72. } else {
  73. this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
  74. }
  75. },
  76. handleGetWXInfo (code) { // 通过code获取 openId等用户信息,/api/user/wechat/login 为后台接口
  77. let _this = this
  78. this.$u.api.getWXInfo(code).then((res) => {
  79. if (res.code === 200 ) {
  80. this.$u.vuex('vuex_wxinfo',res.data);
  81. // 继续支付
  82. }
  83. }).catch((err) => {
  84. this.$refs.uToast.show({
  85. title: err.msg,
  86. type: 'error',
  87. });
  88. })
  89. },
  90. handlewxpay(){
  91. if(!this.$store.state.vuex_wxinfo.openId){ // 如果微信openId,则需用code去后台获取
  92. this.$refs.uToast.show({
  93. title: '请登录后重试!',
  94. type: 'info',
  95. // url: '/pages/user/index'
  96. });
  97. this.getCode();
  98. } else {
  99. // 别的业务逻辑
  100. this.getWXPay();
  101. }
  102. },
  103. async getWXPay(id){
  104. let params = {
  105. orderId:id||new Date().getTime(),
  106. openid:this.$store.state.vuex_wxinfo.openId,
  107. tradeType:"test"
  108. };
  109. await this.$wxApi.config();
  110. this.$pay.wxPay(params).then(res =>{
  111. console.log('wxPay',res.code);
  112. if(res.code == 0){
  113. // 成功
  114. // uni.reLaunch({
  115. // url: '/pages/buySuccess/buySuccess?orderId=' + params.orderId
  116. // })
  117. }else if(res.code == 1){
  118. // 取消
  119. // uni.redirectTo({
  120. // url: '/pages/userCenter/myOrder/myOrder'
  121. // })
  122. }else if(res.code == 2){
  123. this.$refs.uToast.show({
  124. title: '支付失败,请检查!',
  125. type: 'error',
  126. // url: '/pages/user/index'
  127. });
  128. }
  129. });
  130. },
  131. }
  132. }
  133. </script>
  134. <style lang="scss" scoped>
  135. page{background-color: $my-page-bg-color;}
  136. .user-box{background-color: #fff;}
  137. </style>