login.uvue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <cl-page>
  3. <Back />
  4. <img src="https://oss.xiaoxiongcode.com/static/home/1.jpg" alt="" class="w-full h-full object-cover">
  5. <view class="title">
  6. 少儿物理启蒙
  7. </view>
  8. <view class="group">
  9. <cl-image src="https://oss.xiaoxiongcode.com/static/home/8.png" width="150px" height="150px" mode="heightFix" />
  10. <view class="w-[40vw]">
  11. <cl-tabs v-model="val" :list="list" :form="formData" @change="tabsChange"></cl-tabs>
  12. <cl-form v-model="formData" :pt="{ className: 'mt-1' }">
  13. <view v-if="val === 'quickly_login'">
  14. <phone :form="formData" />
  15. </view>
  16. <view v-else>
  17. <password :form="formData" />
  18. </view>
  19. <view class="flex flex-row items-center justify-center gap-4">
  20. <cl-button :pt="{ className: '!h-[45px] !rounded-full w-[140px] mx-auto' }" :loading="loading"
  21. @tap="toLogin">
  22. 登录
  23. </cl-button>
  24. <cl-button type="success" :pt="{ className: '!h-[45px] !rounded-full w-[140px] mx-auto ' }"
  25. :loading="loading" @tap="toWechatLogin">
  26. <view class="flex flex-row items-center justify-center gap-2">
  27. <cl-image src="https://oss.xiaoxiongcode.com/static/个人中心/微信.png" mode="heightFix" height="20px"
  28. width="auto"></cl-image> 登录
  29. </view>
  30. </cl-button>
  31. </view>
  32. </cl-form>
  33. </view>
  34. </view>
  35. <cl-action-sheet ref="actionSheetRef">
  36. <template #prepend>
  37. <view class=" mb-3 font-bold text-center">
  38. <cl-text :size="20" color="#666">
  39. 选择登录账号
  40. </cl-text>
  41. </view>
  42. </template>
  43. </cl-action-sheet>
  44. </cl-page>
  45. </template>
  46. <script lang="ts" setup>
  47. import Back from '@/components/back.uvue'
  48. import type { LoginForm } from "./components/types";
  49. import phone from './components/phone.uvue';
  50. import password from './components/password.uvue';
  51. import { ref } from 'vue'
  52. import type { ClTabsItem } from "@/uni_modules/cool-ui";
  53. import { encryptPassword, user, router } from '@/.cool';
  54. import { loginApi, wechatLogin } from "@/services/user";
  55. import type { ClActionSheetOptions } from "@/uni_modules/cool-ui";
  56. const actionSheetRef = ref<ClActionSheetComponentPublicInstance | null>(null);
  57. const val = ref('quickly_login')
  58. const list: ClTabsItem[] = [
  59. {
  60. label: '验证码登录',
  61. value: 'quickly_login'
  62. },
  63. {
  64. label: '密码登录',
  65. value: 'password'
  66. },
  67. ]
  68. const formData = ref<LoginForm>({
  69. username: '',
  70. password: '',
  71. randomStr: 0,
  72. grant_type: 'quickly_login',
  73. scope: 'server',
  74. loginType: 99,
  75. code: ''
  76. })
  77. const loading = ref(false)
  78. function tabsChange() {
  79. if (val.value === 'quickly_login') {
  80. formData.value.code = ''
  81. formData.value.loginType = 99
  82. formData.value.grant_type = 'quickly_login'
  83. formData.value.password = ''
  84. } else {
  85. formData.value.grant_type = 'password'
  86. formData.value.password = ''
  87. formData.value.loginType = 1
  88. formData.value.code = ''
  89. }
  90. }
  91. function toLogin() {
  92. // loading.value = true
  93. console.log(formData.value);
  94. loginApi({
  95. ...formData.value,
  96. password: formData.value.password && encryptPassword(formData.value.password),
  97. }).then(res => {
  98. user.setToken(res)
  99. uni.showToast({
  100. title: '登录成功',
  101. icon: 'success'
  102. })
  103. router.nextLogin();
  104. })
  105. }
  106. function toWechatLogin() {
  107. uni.login({
  108. provider: 'weixin',
  109. success(res) {
  110. wechatLogin({
  111. code: res.code,
  112. }).then(res => {
  113. console.log(res);
  114. const list = res.map(item => ({
  115. label: item,
  116. icon: "user-line",
  117. callback() {
  118. handleSelect(item)
  119. }
  120. }))
  121. actionSheetRef.value!.open({
  122. list: list
  123. } as ClActionSheetOptions);
  124. })
  125. }
  126. })
  127. }
  128. function handleSelect(item: string) {
  129. formData.value.username = item
  130. formData.value.password = 'quickly_login'
  131. formData.value.grant_type = 'quickly_login'
  132. formData.value.loginType = 88
  133. loginApi({
  134. ...formData.value,
  135. }).then(res => {
  136. user.setToken(res)
  137. uni.showToast({
  138. title: '登录成功',
  139. icon: 'success'
  140. })
  141. router.nextLogin();
  142. })
  143. }
  144. </script>
  145. <style lang="scss" scoped>
  146. .title {
  147. position: absolute;
  148. top: 6%;
  149. left: 50%;
  150. transform: translateX(-50%);
  151. //设置字间距
  152. letter-spacing: 0.1em;
  153. @apply text-white text-5xl font-bold;
  154. }
  155. .group {
  156. @apply absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 bg-white px-4 py-2 rounded-xl;
  157. display: flex;
  158. align-items: center;
  159. flex-direction: row;
  160. height: 60vh;
  161. }
  162. </style>