authorization.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view class="container login" :style="{ paddingTop: CustomBar + 'px' }">
  3. <view class="login-main">
  4. <image class="logo" src="../../static/ws/logo.png" mode="widthFix"></image>
  5. </view>
  6. <view class="login-content">
  7. <text
  8. >您暂未授权维沙采购商城小程序获取你的信息,将无法正常使用小程序的功能。如需正常使用,请点击“授权”按钮,打开头像,昵称等信息的授权</text
  9. >
  10. </view>
  11. <view class="login-form">
  12. <button
  13. class="login-btn use"
  14. type="primary"
  15. size="small"
  16. open-type="getUserInfo"
  17. lang="zh_CN"
  18. @click="getUserProfile"
  19. >
  20. 微信授权
  21. </button>
  22. <view class="login-btn back" @tap="undGetuserinfo">取消</view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import { mapState, mapMutations } from 'vuex'
  28. import authorize from '@/common/config/authorize.js'
  29. import wxLogin from '@/common/config/wxLogin.js'
  30. export default {
  31. data() {
  32. return {
  33. nvabarData: {
  34. //顶部自定义导航
  35. showCapsule: 1, // 是否显示左上角图标 1表示显示 0表示不显示,
  36. showSearch: 0,
  37. title: '微信授权', // 导航栏 中间的标题
  38. haveBack: true,
  39. haveHome: false,
  40. textLeft: this.$store.state.isIphone
  41. },
  42. CustomBar: this.CustomBar, // 顶部导航栏高度
  43. authorizeType: ''
  44. }
  45. },
  46. onLoad(e) {
  47. this.authorizeType = e.type
  48. if (this.authorizeType == '4') {
  49. this.nvabarData.haveBack = false
  50. }
  51. },
  52. computed: {
  53. ...mapState(['hasLogin', 'userInfo'])
  54. },
  55. methods: {
  56. getUserProfile() {
  57. const _that = this
  58. // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
  59. wx.getUserProfile({
  60. desc: '用于完善会员资料',
  61. success(res) {
  62. console.log(res)
  63. // _that.$store.commit('wxLogin',e.detail.userInfo)
  64. uni.login({
  65. provider: 'weixin',
  66. scopes: 'auth_user',
  67. success() {
  68. _that.checkedUser()
  69. },
  70. fail(err) {
  71. uni.showToast({
  72. title: err
  73. })
  74. }
  75. })
  76. }
  77. })
  78. },
  79. getuserinfo(e) {
  80. //微信授权
  81. if (e.detail.userInfo) {
  82. this.checkedUser()
  83. // 授权成功后保存微信用户信息
  84. this.$store.commit('wxLogin', e.detail.userInfo)
  85. } else {
  86. this.$util.msg('授权失败', 2000)
  87. }
  88. },
  89. undGetuserinfo() {
  90. uni.navigateBack({ delta: 1 })
  91. },
  92. // 用户验证
  93. async checkedUser() {
  94. const wechatCode = await authorize.getCode('weixin')
  95. // 用户是否填写了邀请码
  96. this.UserService.userInfoLogin({ code: wechatCode })
  97. .then(res => {
  98. console.log('授权登录成功')
  99. uni.reLaunch({
  100. url: '/pages/index/index'
  101. })
  102. })
  103. .catch(error => {
  104. console.log('游客登录')
  105. // 缓存游客openid
  106. uni.setStorageSync('openid', error.data.openid)
  107. uni.reLaunch({
  108. url: '/pages/login/login'
  109. })
  110. })
  111. }
  112. }
  113. }
  114. </script>
  115. <style lang="scss">
  116. .login {
  117. width: 100%;
  118. height: auto;
  119. .model-warp.none {
  120. display: none;
  121. }
  122. .model-warp.show {
  123. display: block;
  124. }
  125. .login-main {
  126. width: 100%;
  127. display: flex;
  128. flex-direction: column;
  129. align-items: center;
  130. height: auto;
  131. padding: 80rpx 0 0 0;
  132. .logo {
  133. width: 152rpx;
  134. height: 152rpx;
  135. display: block;
  136. border-radius: 50%;
  137. }
  138. .logo-text {
  139. font-size: $font-size-40;
  140. line-height: 40rpx;
  141. color: $color-system;
  142. margin-top: 40rpx;
  143. }
  144. }
  145. .login-content {
  146. width: 580rpx;
  147. margin: 70rpx auto;
  148. line-height: 48rpx;
  149. font-size: 26rpx;
  150. color: #666666;
  151. text-align: justify;
  152. }
  153. .login-form {
  154. width: 702rpx;
  155. height: auto;
  156. display: flex;
  157. flex-direction: column;
  158. align-items: center;
  159. margin: 0 auto;
  160. .login-btn {
  161. width: 600rpx;
  162. height: 90rpx;
  163. border-radius: 45rpx;
  164. font-size: $font-size-32;
  165. line-height: 80rpx;
  166. color: #ffffff;
  167. text-align: center;
  168. background: linear-gradient(135deg, rgba(4, 190, 2, 1) 0%, rgba(4, 183, 2, 1) 100%);
  169. display: flex;
  170. flex-direction: column;
  171. align-items: center;
  172. &.back {
  173. height: 76rpx;
  174. background: #ffffff;
  175. border: 1px solid #666;
  176. color: #666;
  177. margin-top: 30rpx;
  178. }
  179. }
  180. }
  181. }
  182. </style>