login.uvue 7.7 KB

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