authorize.vue 4.1 KB

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