wxLogin.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <view class="login-container">
  3. <!-- <navbar ref="navbar" :config="config" backColor="#000" /> -->
  4. <uv-navbar title="登录" placeholder autoBack></uv-navbar>
  5. <image :src="$handleImageUrl('/logo.png')" mode="aspectFill"></image>
  6. <!-- <view class="title">赣鄱特产,浓情家味</view> -->
  7. <button class="auth-btn" type="default" @click="authLogin">授权登录</button>
  8. <view class="agree">
  9. <uv-checkbox-group v-model="checked">
  10. <uv-checkbox customStyle="display:flex;" name="1" activeColor="#ff4736">
  11. <view>我已阅读并同意 <text class="agree-text" @click.stop="goToAgreement()">《隐私协议》</text></view>
  12. </uv-checkbox>
  13. </uv-checkbox-group>
  14. <!-- <view class="agree_text">《隐私协议》</view> -->
  15. </view>
  16. <register ref="RegisterRef" />
  17. </view>
  18. </template>
  19. <script setup>
  20. import {
  21. authorizationLogin,
  22. getOpenId,
  23. userInfo
  24. } from "@/api/login.js"
  25. import register from "./components/register.vue"
  26. import { ref } from 'vue'
  27. const checked = ref([]);
  28. let code = "";
  29. let isLoading = false, openId = null;
  30. const RegisterRef = ref();
  31. const authLogin = () => {
  32. if (!checked.value || checked.value.length === 0) {
  33. uni.showToast({
  34. title: '请先勾选隐私协议!',
  35. icon: 'none'
  36. });
  37. return false
  38. }
  39. if (isLoading) return
  40. isLoading = true;
  41. uni.showLoading({
  42. title: '登录中...'
  43. })
  44. uni.login({
  45. provider: 'weixin', //使用微信登录
  46. success: async (loginRes) => {
  47. if (loginRes.errMsg && loginRes.errMsg == 'login:ok' && loginRes.code) {
  48. code = loginRes.code;
  49. wxAuthorization(loginRes.code);
  50. } else {
  51. isLoading = false;
  52. uni.showToast({
  53. title: '登录失败!',
  54. icon: 'none'
  55. });
  56. }
  57. },
  58. fail: () => {
  59. isLoading = false;
  60. uni.showToast({
  61. title: '登录失败!',
  62. icon: 'none'
  63. });
  64. }
  65. })
  66. }
  67. const goAgreement = (val) => {
  68. uni.navigateTo({
  69. url: "/pages/user/agreement?type=" + val
  70. })
  71. }
  72. // 登录
  73. const wxAuthorization = (code) => {
  74. getOpenId({ code: code }).then(res => {
  75. const {
  76. openId,
  77. } = res.data || {}
  78. authorizationLogin({ openId: openId, clientType: 1}).then(res => {
  79. if (res.data.token) {
  80. // 已注册
  81. getUser(res.data.token)
  82. } else {
  83. // 未注册 - 前往注册
  84. RegisterRef.value.open(openId)
  85. }
  86. }).catch((e) => {
  87. // 未注册 - 前往注册
  88. if (e.code == 100011) {
  89. RegisterRef.value.open(openId)
  90. }
  91. }).finally(() => {
  92. uni.hideLoading();
  93. })
  94. }).catch(err => {
  95. uni.showToast({
  96. title: '登录失败!',
  97. icon: 'none'
  98. })
  99. }).finally(() => {
  100. uni.hideLoading();
  101. isLoading = false;
  102. })
  103. }
  104. const getUser = (token) => {
  105. uni.setStorageSync('apiToken', token);
  106. userInfo().then(res => {
  107. uni.setStorageSync("personal", res.data)
  108. uni.switchTab({
  109. url: '/pages/tabtar/personalCenter'
  110. })
  111. })
  112. }
  113. const goToAgreement = () => {
  114. uni.navigateTo({
  115. url: "/pages/protocol/index?code=privacy_protocol"
  116. })
  117. }
  118. </script>
  119. <style lang='scss' scoped>
  120. .login-container {
  121. display: flex;
  122. flex-direction: column;
  123. justify-content: center;
  124. width: 100%;
  125. height: 100%;
  126. padding: 30rpx;
  127. box-sizing: border-box;
  128. > image {
  129. width: 210rpx;
  130. height: 210rpx;
  131. margin: 100rpx auto 30rpx;
  132. }
  133. .title {
  134. margin: 0 0 70rpx;
  135. font-size: 34rpx;
  136. text-align: center;
  137. }
  138. .auth-btn {
  139. width: 100%;
  140. height: 90rpx;
  141. line-height: 90rpx;
  142. margin-bottom: 30rpx;
  143. color: #fff;
  144. background-color: #ff4736;
  145. border-radius: 40rpx;
  146. }
  147. .privacy {
  148. display: flex;
  149. align-items: center;
  150. justify-content: center;
  151. font-size: 28rpx;
  152. .txt {
  153. color: #ff4736;
  154. }
  155. }
  156. .agree {
  157. display: flex;
  158. justify-content: center;
  159. align-items: center;
  160. text-align: center;
  161. font-size: 24rpx;
  162. color: #999999;
  163. .agree-text {
  164. color: #0073ef;
  165. // font-size: 28rpx;
  166. }
  167. }
  168. }
  169. ::v-deep .uv-checkbox-group {
  170. justify-content: center;
  171. flex: none;
  172. }
  173. // .agree_text {
  174. // color: #0073ef;
  175. // font-size: 28rpx;
  176. // }
  177. </style>