authorization.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. lang="zh_CN"
  17. open-type="getUserInfo"
  18. @getuserinfo="getUserInfo"
  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. ...mapMutations(['wxLogin']),
  57. getUserProfile() {
  58. const _that = this
  59. // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
  60. wx.getUserProfile({
  61. desc: '用于完善会员资料',
  62. success(res) {
  63. console.log(res)
  64. console.log('新API', res)
  65. _that.wxLogin(res.userInfo)
  66. // _that.$store.commit('wxLogin',e.detail.userInfo)
  67. uni.login({
  68. provider: 'weixin',
  69. scopes: 'auth_user',
  70. success() {
  71. _that.checkedUser()
  72. },
  73. fail(err) {
  74. _that.$util.msg(err, 2000)
  75. }
  76. })
  77. },
  78. fail() {
  79. _that.$util.msg('授权失败', 2000)
  80. }
  81. })
  82. },
  83. getUserInfo(e) {
  84. const _that = this
  85. if (e.detail.userInfo) {
  86. // 缓存用户授权状态
  87. this.wxLogin(e.detail.userInfo)
  88. uni.login({
  89. provider: 'weixin',
  90. scopes: 'auth_user',
  91. success() {
  92. _that.checkedUser()
  93. },
  94. fail(err) {
  95. _that.$util.msg(err, 2000)
  96. }
  97. })
  98. } else {
  99. console.log('授权失败!')
  100. }
  101. },
  102. undGetuserinfo() {
  103. uni.navigateBack({ delta: 1 })
  104. },
  105. // 用户验证
  106. async checkedUser() {
  107. const wechatCode = await authorize.getCode('weixin')
  108. // 用户是否填写了邀请码
  109. this.UserService.userInfoLogin({ code: wechatCode })
  110. .then(res => {
  111. console.log('授权登录成功')
  112. uni.reLaunch({
  113. url: '/pages/index/index'
  114. })
  115. })
  116. .catch(error => {
  117. console.log('游客登录')
  118. // 缓存游客openid
  119. uni.setStorageSync('openid', error.data.openid)
  120. uni.reLaunch({
  121. url: '/pages/login/login'
  122. })
  123. })
  124. }
  125. }
  126. }
  127. </script>
  128. <style lang="scss">
  129. .login {
  130. width: 100%;
  131. height: auto;
  132. .model-warp.none {
  133. display: none;
  134. }
  135. .model-warp.show {
  136. display: block;
  137. }
  138. .login-main {
  139. width: 100%;
  140. display: flex;
  141. flex-direction: column;
  142. align-items: center;
  143. height: auto;
  144. padding: 80rpx 0 0 0;
  145. .logo {
  146. width: 152rpx;
  147. height: 152rpx;
  148. display: block;
  149. border-radius: 50%;
  150. }
  151. .logo-text {
  152. font-size: $font-size-40;
  153. line-height: 40rpx;
  154. color: $color-system;
  155. margin-top: 40rpx;
  156. }
  157. }
  158. .login-content {
  159. width: 580rpx;
  160. margin: 70rpx auto;
  161. line-height: 48rpx;
  162. font-size: 26rpx;
  163. color: #666666;
  164. text-align: justify;
  165. }
  166. .login-form {
  167. width: 702rpx;
  168. height: auto;
  169. display: flex;
  170. flex-direction: column;
  171. align-items: center;
  172. margin: 0 auto;
  173. .login-btn {
  174. width: 600rpx;
  175. height: 90rpx;
  176. border-radius: 45rpx;
  177. font-size: $font-size-32;
  178. line-height: 80rpx;
  179. color: #ffffff;
  180. text-align: center;
  181. background: linear-gradient(135deg, rgba(4, 190, 2, 1) 0%, rgba(4, 183, 2, 1) 100%);
  182. display: flex;
  183. flex-direction: column;
  184. align-items: center;
  185. &.back {
  186. height: 76rpx;
  187. background: #ffffff;
  188. border: 1px solid #666;
  189. color: #666;
  190. margin-top: 30rpx;
  191. }
  192. }
  193. }
  194. }
  195. </style>