authorize.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <view class="container login" :style="{ paddingTop: CustomBar + 'px' }">
  3. <view class="login-main">
  4. <image class="logo" :src="StaticUrl + 'icon-logo@2x.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. open-type="getUserInfo"
  16. size="small"
  17. @click="getUserProfile"
  18. lang="zh_CN"
  19. >
  20. 微信授权
  21. </button>
  22. <view class="login-btn back" @tap="undGetuserinfo">取消</view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import { mapGetters, mapMutations } from 'vuex'
  28. import authorize from '@/common/config/authorize.js'
  29. import wxLogin from '@/services/wxLogin.js'
  30. export default {
  31. data() {
  32. return {
  33. StaticUrl: this.$Static,
  34. nvabarData: {
  35. //顶部自定义导航
  36. showCapsule: 1, // 是否显示左上角图标 1表示显示 0表示不显示,
  37. showSearch: 0,
  38. title: '微信授权', // 导航栏 中间的标题
  39. haveBack: true,
  40. haveHome: false,
  41. textLeft: this.$store.state.isIphone
  42. },
  43. CustomBar: this.CustomBar, // 顶部导航栏高度
  44. authorizeType: ''
  45. }
  46. },
  47. onLoad(e) {
  48. this.authorizeType = e.type
  49. if (this.authorizeType == '4') {
  50. this.nvabarData.haveBack = false
  51. }
  52. },
  53. computed: {
  54. ...mapGetters(['userInfo'])
  55. },
  56. methods: {
  57. ...mapMutations('app', ['wxLogin']),
  58. getUserProfile() {
  59. const self = this
  60. // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
  61. wx.getUserProfile({
  62. desc: '采美采购商城小程序获取您的信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  63. success(res) {
  64. console.log('新API', res)
  65. self.wxLogin(res.userInfo)
  66. uni.login({
  67. provider: 'weixin',
  68. scopes: 'auth_user',
  69. success(res) {
  70. self.WxGetUserInfo()
  71. },
  72. fail(err) {
  73. self.$util.msg(err, 2000)
  74. }
  75. })
  76. },
  77. fail() {
  78. self.$util.msg('授权失败', 2000)
  79. }
  80. })
  81. },
  82. undGetuserinfo() {
  83. this.hanldBackFn()
  84. },
  85. hanldNavigateBack() {
  86. this.hanldBackFn()
  87. },
  88. hanldBackFn() {
  89. switch (this.authorizeType) {
  90. case '0':
  91. this.$api.switchTabTo('/pages/tabBar/index/index')
  92. break
  93. default:
  94. uni.navigateBack({ delta: 1 })
  95. }
  96. },
  97. WxGetUserInfo() {
  98. let self = this
  99. wx.getUserInfo({
  100. success: res => {
  101. wxLogin.wxLoginAuthorize()
  102. switch (self.authorizeType) {
  103. case '0':
  104. self.$api.switchTabTo('/pages/tabBar/index/index')
  105. break
  106. case '1':
  107. self.$api.navigateTo('/pages/login/login')
  108. break
  109. default:
  110. setTimeout(() => {
  111. uni.navigateBack({ delta: 1 })
  112. }, 2000)
  113. }
  114. }
  115. })
  116. }
  117. }
  118. }
  119. </script>
  120. <style lang="scss">
  121. .login {
  122. width: 100%;
  123. height: auto;
  124. .model-warp.none {
  125. display: none;
  126. }
  127. .model-warp.show {
  128. display: block;
  129. }
  130. .login-main {
  131. width: 100%;
  132. display: flex;
  133. flex-direction: column;
  134. align-items: center;
  135. height: auto;
  136. padding: 80rpx 0 0 0;
  137. .logo {
  138. width: 200rpx;
  139. height: 200rpx;
  140. display: block;
  141. }
  142. .logo-text {
  143. font-size: $font-size-40;
  144. line-height: 40rpx;
  145. color: $color-system;
  146. margin-top: 40rpx;
  147. }
  148. }
  149. .login-content {
  150. width: 580rpx;
  151. margin: 70rpx auto;
  152. line-height: 48rpx;
  153. font-size: 26rpx;
  154. color: #666666;
  155. text-align: justify;
  156. }
  157. .login-form {
  158. width: 702rpx;
  159. height: auto;
  160. display: flex;
  161. flex-direction: column;
  162. align-items: center;
  163. margin: 0 auto;
  164. .login-btn {
  165. width: 550rpx;
  166. height: 90rpx;
  167. border-radius: 45rpx;
  168. font-size: $font-size-32;
  169. line-height: 80rpx;
  170. color: #ffffff;
  171. text-align: center;
  172. background: linear-gradient(135deg, rgba(4, 190, 2, 1) 0%, rgba(4, 183, 2, 1) 100%);
  173. display: flex;
  174. flex-direction: column;
  175. align-items: center;
  176. &.back {
  177. height: 80rpx;
  178. background: #e1e1e1;
  179. border: 1px solid #ffffff;
  180. color: #ffffff;
  181. margin-top: 30rpx;
  182. }
  183. }
  184. }
  185. }
  186. </style>