authorization.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. _that.$util.msg(err, 2000)
  72. }
  73. })
  74. },
  75. fail() {
  76. _that.$util.msg('授权失败', 2000)
  77. }
  78. })
  79. },
  80. getuserinfo(e) {
  81. //微信授权
  82. if (e.detail.userInfo) {
  83. this.checkedUser()
  84. // 授权成功后保存微信用户信息
  85. this.$store.commit('wxLogin', e.detail.userInfo)
  86. } else {
  87. this.$util.msg('授权失败', 2000)
  88. }
  89. },
  90. undGetuserinfo() {
  91. uni.navigateBack({ delta: 1 })
  92. },
  93. // 用户验证
  94. async checkedUser() {
  95. const wechatCode = await authorize.getCode('weixin')
  96. // 用户是否填写了邀请码
  97. this.UserService.userInfoLogin({ code: wechatCode })
  98. .then(res => {
  99. console.log('授权登录成功')
  100. uni.reLaunch({
  101. url: '/pages/index/index'
  102. })
  103. })
  104. .catch(error => {
  105. console.log('游客登录')
  106. // 缓存游客openid
  107. uni.setStorageSync('openid', error.data.openid)
  108. uni.reLaunch({
  109. url: '/pages/login/login'
  110. })
  111. })
  112. }
  113. }
  114. }
  115. </script>
  116. <style lang="scss">
  117. .login {
  118. width: 100%;
  119. height: auto;
  120. .model-warp.none {
  121. display: none;
  122. }
  123. .model-warp.show {
  124. display: block;
  125. }
  126. .login-main {
  127. width: 100%;
  128. display: flex;
  129. flex-direction: column;
  130. align-items: center;
  131. height: auto;
  132. padding: 80rpx 0 0 0;
  133. .logo {
  134. width: 152rpx;
  135. height: 152rpx;
  136. display: block;
  137. border-radius: 50%;
  138. }
  139. .logo-text {
  140. font-size: $font-size-40;
  141. line-height: 40rpx;
  142. color: $color-system;
  143. margin-top: 40rpx;
  144. }
  145. }
  146. .login-content {
  147. width: 580rpx;
  148. margin: 70rpx auto;
  149. line-height: 48rpx;
  150. font-size: 26rpx;
  151. color: #666666;
  152. text-align: justify;
  153. }
  154. .login-form {
  155. width: 702rpx;
  156. height: auto;
  157. display: flex;
  158. flex-direction: column;
  159. align-items: center;
  160. margin: 0 auto;
  161. .login-btn {
  162. width: 600rpx;
  163. height: 90rpx;
  164. border-radius: 45rpx;
  165. font-size: $font-size-32;
  166. line-height: 80rpx;
  167. color: #ffffff;
  168. text-align: center;
  169. background: linear-gradient(135deg, rgba(4, 190, 2, 1) 0%, rgba(4, 183, 2, 1) 100%);
  170. display: flex;
  171. flex-direction: column;
  172. align-items: center;
  173. &.back {
  174. height: 76rpx;
  175. background: #ffffff;
  176. border: 1px solid #666;
  177. color: #666;
  178. margin-top: 30rpx;
  179. }
  180. }
  181. }
  182. }
  183. </style>