login.uvue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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="group">
  8. <view class="title">
  9. <cl-image src="https://oss.xiaoxiongcode.com/static/home/logo.png" width="120px" height="120px"
  10. mode="heightFix" />
  11. <cl-text :size="20" color="#666">
  12. 少儿物理启蒙
  13. </cl-text>
  14. </view>
  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" ref="formRef" :pt="{ className: 'mt-1' }">
  18. <view v-if="val === 'quickly_login'">
  19. <phone :form="formData" />
  20. </view>
  21. <view v-else-if="val === 'password'">
  22. <password :form="formData" />
  23. </view>
  24. <view v-else-if="val === 'register'">
  25. <register :form="formData" />
  26. </view>
  27. <view class="flex flex-row items-center justify-center mb-5">
  28. <cl-button v-if="val === 'weixin'" :pt="{ className: '!h-[45px] !rounded-full w-[200px] mx-auto' }"
  29. type="success" @tap="toWechatLogin">
  30. <view class="flex flex-row items-center justify-center">
  31. <cl-image src="https://oss.xiaoxiongcode.com/static/个人中心/微信.png" mode="heightFix" height="20px"
  32. width="auto"></cl-image>
  33. <view class="ml-2">
  34. 微信快捷登录
  35. </view>
  36. </view>
  37. </cl-button>
  38. <cl-button v-else-if="val === 'register'" :disabled="!agree || !agree2"
  39. :pt="{ className: '!h-[45px] !rounded-full w-[200px] mx-auto' }" :loading="loading" @tap="toRegister">
  40. 注册并登录
  41. </cl-button>
  42. <cl-button v-else :disabled="!agree || !agree2"
  43. :pt="{ className: '!h-[45px] !rounded-full w-[200px] mx-auto' }" :loading="loading" @tap="toLogin">
  44. 登录
  45. </cl-button>
  46. </view>
  47. <cl-checkbox v-model="agree" v-if="val !== 'weixin'">
  48. <view class="flex flex-row items-center w-full text-[12px]">
  49. 同意并阅读
  50. <cl-text :size="12" color="primary" @tap.stop="toAgreement">
  51. 《用户服务协议》
  52. </cl-text>
  53. </view>
  54. </cl-checkbox>
  55. <cl-checkbox v-model="agree2" v-if="val !== 'weixin'">
  56. <view class="flex flex-row items-center w-full text-[12px]">
  57. 同意并阅读
  58. <cl-text :size="12" color="primary" @tap.stop="toPrivacy">
  59. 《隐私政策》
  60. </cl-text>
  61. </view>
  62. </cl-checkbox>
  63. </cl-form>
  64. </view>
  65. </view>
  66. <view class="absolute bottom-20 w-full flex flex-row gap-4 items-center justify-center">
  67. <view v-if="val !== 'weixin'">
  68. <cl-button type="success" :pt="{ className: '!h-[40px] !rounded-full w-[40px] mx-auto !p-1' }"
  69. :loading="loading" @tap="tabsChange('weixin')">
  70. <cl-image src="https://oss.xiaoxiongcode.com/static/个人中心/微信.png" mode="heightFix" height="20px"
  71. width="auto"></cl-image>
  72. </cl-button>
  73. <view class="mt-1">
  74. <cl-text :size="14" color="#666">
  75. 微信登录
  76. </cl-text>
  77. </view>
  78. </view>
  79. <view v-if="val !== 'quickly_login'">
  80. <cl-button type="info" :pt="{ className: '!h-[40px] !rounded-full w-[40px] mx-auto !p-1' }" :loading="loading"
  81. @tap="tabsChange('quickly_login')">
  82. <cl-image src="https://oss.xiaoxiongcode.com/static/home/phone.svg" mode="heightFix" height="20px"
  83. width="auto"></cl-image>
  84. </cl-button>
  85. <view class="mt-1">
  86. <cl-text :size="14" color="#666">
  87. 验证码登录
  88. </cl-text>
  89. </view>
  90. </view>
  91. <view v-if="val !== 'password'">
  92. <cl-button type="info" :pt="{ className: '!h-[40px] !rounded-full w-[40px] mx-auto !p-1' }" :loading="loading"
  93. @tap="tabsChange('password')">
  94. <cl-image src="https://oss.xiaoxiongcode.com/static/home/password.svg" mode="heightFix" height="20px"
  95. width="auto"></cl-image>
  96. </cl-button>
  97. <view class="mt-1">
  98. <cl-text :size="14" color="#666">
  99. 密码登录
  100. </cl-text>
  101. </view>
  102. </view>
  103. </view>
  104. <view v-if="val !== 'register'"
  105. class="absolute bottom-12 w-full flex flex-row gap-4 items-center justify-center">
  106. <cl-text :size="14" color="primary" @tap="tabsChange('register')">
  107. 注册账号
  108. </cl-text>
  109. </view>
  110. </view>
  111. <cl-action-sheet ref="actionSheetRef">
  112. <template #prepend>
  113. <view class=" mb-3 font-bold text-center">
  114. <cl-text :size="20" color="#666">
  115. 选择登录账号
  116. </cl-text>
  117. </view>
  118. </template>
  119. </cl-action-sheet>
  120. <cl-popup v-model="visible" showClose :show-header="false" direction="center" :pt="{
  121. inner: {
  122. className: '!bg-transparent'
  123. }
  124. }">
  125. <view class="w-[330px] relative">
  126. <image src="https://oss.xiaoxiongcode.com/static/home/6172.png" mode="widthFix" class="w-[330px] relative ">
  127. </image>
  128. <view class=" wz-1 ">
  129. 当前微信账号,<br />尚未绑定,<br />无法快捷登录
  130. </view>
  131. <view class="wz-2 " @tap="handleClosePopup">
  132. 立即登录
  133. </view>
  134. </view>
  135. </cl-popup>
  136. </cl-page>
  137. </template>
  138. <script lang="ts" setup>
  139. import Back from '@/components/back.uvue'
  140. import type { LoginForm } from "./components/types";
  141. import phone from './components/phone.uvue';
  142. import password from './components/password.uvue';
  143. import register from './components/register.uvue';
  144. import { ref } from 'vue'
  145. import type { ClTabsItem } from "@/uni_modules/cool-ui";
  146. import { encryptPassword, user, router } from '@/.cool';
  147. import { loginApi, wechatLogin, quicklyRegister, selfRegister } from "@/services/user";
  148. import type { ClActionSheetOptions } from "@/uni_modules/cool-ui";
  149. import { type ClFormRule, useForm } from "@/uni_modules/cool-ui";
  150. const actionSheetRef = ref<ClActionSheetComponentPublicInstance | null>(null);
  151. const visible = ref(false)
  152. const { formRef, validate, clearValidate } = useForm();
  153. const val = ref('weixin')
  154. const agree = ref(false)
  155. const agree2 = ref(false)
  156. const list = [
  157. {
  158. label: '验证码登录',
  159. value: 'quickly_login'
  160. },
  161. {
  162. label: '密码登录',
  163. value: 'password'
  164. },
  165. {
  166. label: '微信登录',
  167. value: 'weixin'
  168. },
  169. {
  170. label: '注册账号',
  171. value: 'register'
  172. }
  173. ]
  174. const formData = ref<LoginForm>({
  175. username: '',
  176. password: '',
  177. randomStr: 0,
  178. grant_type: 'quickly_login',
  179. scope: 'server',
  180. loginType: 99,
  181. code: ''
  182. })
  183. const loading = ref(false)
  184. function tabsChange(type: string) {
  185. val.value = type
  186. if (val.value === 'quickly_login') {
  187. formData.value.code = ''
  188. formData.value.loginType = 99
  189. formData.value.grant_type = 'quickly_login'
  190. formData.value.password = ''
  191. } else {
  192. formData.value.grant_type = 'password'
  193. formData.value.password = ''
  194. formData.value.loginType = 1
  195. formData.value.code = ''
  196. }
  197. }
  198. async function toRegister() {
  199. validate(async (valid, errors) => {
  200. if (valid) {
  201. try {
  202. await selfRegister(formData.value)
  203. uni.showToast({
  204. title: '注册成功',
  205. icon: 'success'
  206. })
  207. handleSelect(formData.value.username)
  208. } catch (error) {
  209. uni.showToast({
  210. title: '注册失败',
  211. icon: 'none'
  212. })
  213. }
  214. } else {
  215. // 验证不通过,可以提示错误信息 errors
  216. console.log(formData.value, 2);
  217. }
  218. });
  219. }
  220. function toLogin() {
  221. loading.value = true
  222. validate((valid, errors) => {
  223. if (valid) {
  224. console.log(formData.value);
  225. loginApi({
  226. ...formData.value,
  227. password: formData.value.password && encryptPassword(formData.value.password),
  228. }).then(res => {
  229. user.setToken(res)
  230. uni.showToast({
  231. title: '登录成功',
  232. icon: 'success'
  233. })
  234. router.home();
  235. }).catch(err => {
  236. if (err.message == '手机号不存在' && val.value === 'quickly_login') {
  237. quicklyRegister({
  238. username: formData.value.username,
  239. password: 'ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413',
  240. }).then(res => {
  241. handleSelect(formData.value.username)
  242. })
  243. } else {
  244. uni.showToast({
  245. title: err.message,
  246. icon: 'none'
  247. })
  248. }
  249. })
  250. } else {
  251. // 验证不通过,可以提示错误信息 errors
  252. loading.value = false
  253. console.log(formData.value, 2);
  254. }
  255. });
  256. }
  257. function handleClosePopup() {
  258. visible.value = false
  259. tabsChange('quickly_login')
  260. }
  261. function toWechatLogin() {
  262. uni.login({
  263. provider: 'weixin',
  264. success(res) {
  265. wechatLogin({
  266. code: res.code,
  267. }).then(res => {
  268. console.log(res);
  269. if (res.length === 0) {
  270. visible.value = true
  271. return
  272. } else if (res.length === 1) {
  273. handleSelect(res[0])
  274. return
  275. }
  276. const list = res.map(item => ({
  277. label: item,
  278. icon: "user-line",
  279. callback() {
  280. handleSelect(item)
  281. }
  282. }))
  283. actionSheetRef.value!.open({
  284. list: list
  285. } as ClActionSheetOptions);
  286. })
  287. }
  288. })
  289. }
  290. function handleSelect(item: string) {
  291. formData.value.username = item
  292. formData.value.password = 'quickly_login'
  293. formData.value.grant_type = 'quickly_login'
  294. formData.value.loginType = 88
  295. loginApi({
  296. ...formData.value,
  297. }).then(res => {
  298. user.setToken(res)
  299. uni.showToast({
  300. title: '登录成功',
  301. icon: 'success'
  302. })
  303. router.home();
  304. })
  305. }
  306. function toAgreement() {
  307. router.push({
  308. path: '/pages/user/agreement'
  309. })
  310. }
  311. function toPrivacy() {
  312. router.push({
  313. path: '/pages/user/privacy'
  314. })
  315. }
  316. </script>
  317. <style lang="scss" scoped>
  318. .content {
  319. @apply w-full h-full;
  320. // 渐变背景
  321. background: linear-gradient(180deg, #169deb5e 0%, #fff 20%);
  322. }
  323. .wz-1 {
  324. @apply absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 text-black text-3xl font-bold w-[300px] text-center;
  325. line-height: 40px;
  326. }
  327. .wz-2 {
  328. @apply absolute bottom-[12px] text-black text-2xl font-bold text-center;
  329. line-height: 40px;
  330. left: 50%;
  331. transform: translateX(-50%);
  332. }
  333. .title {
  334. //设置字间距
  335. letter-spacing: 0.1em;
  336. margin-bottom: 20px;
  337. @apply flex flex-col w-full items-center justify-center;
  338. }
  339. .group {
  340. @apply absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 px-4 py-2 pb-10 rounded-xl;
  341. display: flex;
  342. height: 80%;
  343. align-items: center;
  344. flex-direction: column;
  345. }
  346. </style>