login.uvue 5.8 KB

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