login.uvue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 :disabled="!agree"
  26. :pt="{ className: '!h-[45px] !rounded-full w-[200px] mx-auto' }" :loading="loading"
  27. @tap="toLogin">
  28. 登录
  29. </cl-button>
  30. </view>
  31. <cl-checkbox v-model="agree">
  32. <view class="flex flex-row items-center w-full text-[12px]">
  33. 同意并阅读
  34. <cl-text :size="12" color="primary" @tap.stop="toAgreement">
  35. 《用户服务协议》
  36. </cl-text>和
  37. <cl-text :size="12" color="primary" @tap.stop="toPrivacy">
  38. 《隐私政策》
  39. </cl-text>
  40. </view>
  41. </cl-checkbox>
  42. </cl-form>
  43. </view>
  44. </view>
  45. <view class="absolute bottom-20 w-full flex flex-row gap-4 items-center justify-center">
  46. <view>
  47. <cl-button type="success" :pt="{ className: '!h-[40px] !rounded-full w-[40px] mx-auto !p-1' }"
  48. :loading="loading" @tap="toWechatLogin">
  49. <cl-image src="https://oss.xiaoxiongcode.com/static/个人中心/微信.png" 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-if="val === 'password'">
  59. <cl-button type="info" :pt="{ className: '!h-[40px] !rounded-full w-[40px] mx-auto !p-1' }"
  60. :loading="loading" @tap="tabsChange('quickly_login')">
  61. <cl-image src="https://oss.xiaoxiongcode.com/static/home/phone.svg" mode="heightFix"
  62. height="20px" 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 v-else>
  71. <cl-button type="info" :pt="{ className: '!h-[40px] !rounded-full w-[40px] mx-auto !p-1' }"
  72. :loading="loading" @tap="tabsChange('password')">
  73. <cl-image src="https://oss.xiaoxiongcode.com/static/home/password.svg" mode="heightFix"
  74. height="20px" width="auto"></cl-image>
  75. </cl-button>
  76. <view class="mt-1">
  77. <cl-text :size="14" color="#666">
  78. 密码登录
  79. </cl-text>
  80. </view>
  81. </view>
  82. </view>
  83. </view>
  84. <cl-action-sheet ref="actionSheetRef">
  85. <template #prepend>
  86. <view class=" mb-3 font-bold text-center">
  87. <cl-text :size="20" color="#666">
  88. 选择登录账号
  89. </cl-text>
  90. </view>
  91. </template>
  92. </cl-action-sheet>
  93. </cl-page>
  94. </template>
  95. <script lang="ts" setup>
  96. import Back from '@/components/back.uvue'
  97. import type { LoginForm } from "./components/types";
  98. import phone from './components/phone.uvue';
  99. import password from './components/password.uvue';
  100. import { ref } from 'vue'
  101. import type { ClTabsItem } from "@/uni_modules/cool-ui";
  102. import { encryptPassword, user, router } from '@/.cool';
  103. import { loginApi, wechatLogin, quicklyRegister } from "@/services/user";
  104. import type { ClActionSheetOptions } from "@/uni_modules/cool-ui";
  105. const actionSheetRef = ref<ClActionSheetComponentPublicInstance | null>(null);
  106. const val = ref('quickly_login')
  107. const agree = ref(false)
  108. const list: ClTabsItem[] = [
  109. {
  110. label: '验证码登录',
  111. value: 'quickly_login'
  112. },
  113. {
  114. label: '密码登录',
  115. value: 'password'
  116. },
  117. ]
  118. const formData = ref<LoginForm>({
  119. username: '',
  120. password: '',
  121. randomStr: 0,
  122. grant_type: 'quickly_login',
  123. scope: 'server',
  124. loginType: 99,
  125. code: ''
  126. })
  127. const loading = ref(false)
  128. function tabsChange(type: string) {
  129. val.value = type
  130. if (val.value === 'quickly_login') {
  131. formData.value.code = ''
  132. formData.value.loginType = 99
  133. formData.value.grant_type = 'quickly_login'
  134. formData.value.password = ''
  135. } else {
  136. formData.value.grant_type = 'password'
  137. formData.value.password = ''
  138. formData.value.loginType = 1
  139. formData.value.code = ''
  140. }
  141. }
  142. function toLogin() {
  143. // loading.value = true
  144. console.log(formData.value);
  145. loginApi({
  146. ...formData.value,
  147. password: formData.value.password && encryptPassword(formData.value.password),
  148. }).then(res => {
  149. user.setToken(res)
  150. uni.showToast({
  151. title: '登录成功',
  152. icon: 'success'
  153. })
  154. router.home();
  155. }).catch(err => {
  156. if (err.message == '手机号不存在' && val.value === 'quickly_login') {
  157. quicklyRegister({
  158. username: formData.value.username,
  159. password: 'ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413',
  160. }).then(res => {
  161. handleSelect(formData.value.username)
  162. })
  163. } else {
  164. uni.showToast({
  165. title: err.message,
  166. icon: 'none'
  167. })
  168. }
  169. })
  170. }
  171. function toWechatLogin() {
  172. uni.login({
  173. provider: 'weixin',
  174. success(res) {
  175. wechatLogin({
  176. code: res.code,
  177. }).then(res => {
  178. console.log(res);
  179. if (res.length === 0) {
  180. uni.showToast({
  181. title: '请先绑定账号',
  182. icon: 'none'
  183. })
  184. return
  185. } else if (res.length === 1) {
  186. handleSelect(res[0])
  187. return
  188. }
  189. const list = res.map(item => ({
  190. label: item,
  191. icon: "user-line",
  192. callback() {
  193. handleSelect(item)
  194. }
  195. }))
  196. actionSheetRef.value!.open({
  197. list: list
  198. } as ClActionSheetOptions);
  199. })
  200. }
  201. })
  202. }
  203. function handleSelect(item: string) {
  204. formData.value.username = item
  205. formData.value.password = 'quickly_login'
  206. formData.value.grant_type = 'quickly_login'
  207. formData.value.loginType = 88
  208. loginApi({
  209. ...formData.value,
  210. }).then(res => {
  211. user.setToken(res)
  212. uni.showToast({
  213. title: '登录成功',
  214. icon: 'success'
  215. })
  216. router.home();
  217. })
  218. }
  219. function toAgreement() {
  220. router.push({
  221. path: '/pages/user/agreement'
  222. })
  223. }
  224. function toPrivacy() {
  225. router.push({
  226. path: '/pages/user/privacy'
  227. })
  228. }
  229. </script>
  230. <style lang="scss" scoped>
  231. .content {
  232. @apply w-full h-full;
  233. // 渐变背景
  234. background: linear-gradient(180deg, #169deb5e 0%, #fff 20%);
  235. }
  236. .title {
  237. position: absolute;
  238. top: 10%;
  239. //设置字间距
  240. letter-spacing: 0.1em;
  241. @apply flex flex-col w-full items-center justify-center;
  242. }
  243. .group {
  244. @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;
  245. display: flex;
  246. align-items: center;
  247. flex-direction: row;
  248. }
  249. </style>