index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <template>
  2. <view class="ss-p-30 head-box">
  3. <template v-if="changeType === 'login'">
  4. <view class="head-title ss-m-b-30 MiSans-Semibold">欢迎来到门墩儿</view>
  5. <uni-segmented-control
  6. class="ss-m-t-30 MiSans-Medium"
  7. :current="current"
  8. :values="items"
  9. style-type="text"
  10. active-color="#00B760"
  11. @clickItem="onClickItem"
  12. />
  13. <!-- <view class="head-subtitle ss-m-t-10">未注册的手机号,验证后自动注册账号</view> -->
  14. <view class="ss-m-t-30">
  15. <!-- 手机号快捷登录 -->
  16. <view v-if="current === 0">
  17. <button v-if="!protocol" class="send-button MiSans-Medium" @click="showProtocolToast">手机号快捷登录</button>
  18. <button v-else class="send-button MiSans-Medium" :loading="phoneNumberLoading" :disabled="phoneNumberLoading" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号快捷登录</button>
  19. </view>
  20. <!-- 短信验证码登录 -->
  21. <uni-forms
  22. v-if="current === 1"
  23. ref="smsLoginRef"
  24. v-model="state.sms"
  25. :rules="state.smsRules"
  26. validateTrigger="bind"
  27. labelWidth="140"
  28. labelAlign="center"
  29. >
  30. <uni-forms-item name="phone" label="手机号">
  31. <uni-easyinput placeholder="请输入手机号" v-model="state.sms.phone" :inputBorder="false" type="number">
  32. </uni-easyinput>
  33. </uni-forms-item>
  34. <uni-forms-item name="code" label="验证码">
  35. <uni-easyinput placeholder="请输入验证码" v-model="state.sms.code" :inputBorder="false" type="number" maxlength="6">
  36. <template v-slot:right>
  37. <button class="login-code MiSans-Normal" :disabled="state.isMobileEnd" :class="{ 'code-btn-end': state.isMobileEnd }" @tap="handleCode">
  38. {{ getSmsTimer('smsLogin') }}
  39. </button>
  40. </template>
  41. </uni-easyinput>
  42. </uni-forms-item>
  43. </uni-forms>
  44. <!-- 账号密码登录 -->
  45. <uni-forms
  46. v-if="current === 2"
  47. ref="accountLoginRef"
  48. v-model="state.account"
  49. :rules="state.rules"
  50. validateTrigger="bind"
  51. labelWidth="140"
  52. labelAlign="center"
  53. >
  54. <uni-forms-item name="phone" label="账号">
  55. <uni-easyinput placeholder="请输入账号" v-model="state.account.phone" :inputBorder="false"></uni-easyinput>
  56. </uni-forms-item>
  57. <uni-forms-item name="password" label="密码">
  58. <uni-easyinput type="password" placeholder="请输入密码" v-model="state.account.password" :inputBorder="false"></uni-easyinput>
  59. </uni-forms-item>
  60. </uni-forms>
  61. <button v-if="current !== 0" class="send-button MiSans-Medium" :loading="smsLoginLoading" :disabled="smsLoginLoading" @tap="handleLogin"> 登 录 </button>
  62. <view class="agreement-box ss-flex ss-row-center">
  63. <uni-icons size="20" :type="protocol ? 'checkbox-filled' : 'circle'" :color="protocol ? '#00B760' : '#ccc'" @tap="protocol = !protocol"></uni-icons>
  64. <view class="color-999 ss-flex ss-col-center ss-m-l-8 font-size-13">
  65. <span class="MiSans-Normal">我已阅读并遵守</span>
  66. <view class="color-primary MiSans-Medium" @tap.stop="handleToDetail('user')">
  67. 《用户协议》
  68. </view>
  69. <view class="agreement-text MiSans-Normal">和</view>
  70. <view class="color-primary MiSans-Medium" @tap.stop="handleToDetail('privacy')">
  71. 《隐私协议》
  72. </view>
  73. </view>
  74. </view>
  75. <view class="register MiSans-Normal ss-m-t-30" style="margin-bottom: 30px;" @tap="handleChangeRegister">还没有登录账户?去注册</view>
  76. </view>
  77. </template>
  78. <template v-if="changeType === 'register'">
  79. <view class="head-title pb-20 MiSans-Semibold">手机号注册</view>
  80. <uni-forms
  81. ref="registerForm"
  82. v-model="state.register"
  83. :rules="state.smsRules"
  84. validateTrigger="bind"
  85. labelWidth="140"
  86. labelAlign="center"
  87. >
  88. <uni-forms-item name="phone" label="手机号">
  89. <uni-easyinput placeholder="请输入手机号" v-model="state.register.phone" :inputBorder="false" type="number">
  90. </uni-easyinput>
  91. </uni-forms-item>
  92. <uni-forms-item name="code" label="验证码">
  93. <uni-easyinput
  94. placeholder="请输入验证码"
  95. v-model="state.register.code"
  96. :inputBorder="false"
  97. type="number"
  98. maxlength="6"
  99. >
  100. <template v-slot:right>
  101. <button
  102. class="login-code MiSans-Normal"
  103. :disabled="state.isMobileEnd"
  104. :class="{ 'code-btn-end': state.isMobileEnd }"
  105. @tap="handleRegisterCode"
  106. >
  107. {{ getSmsTimer('smsRegister') }}
  108. </button>
  109. </template>
  110. </uni-easyinput>
  111. </uni-forms-item>
  112. </uni-forms>
  113. <view class="register login color-primary ss-p-b-5 MiSans-Normal" style="text-align: end;" @tap="handleChangeLogin">已有账户?去登陆</view>
  114. <view>
  115. <button class="send-button" :loading="registerLoading" :disabled="registerLoading" @tap="handleRegister"> 注 册 </button>
  116. </view>
  117. <view class="color-999 ss-flex ss-col-center ss-row-center ss-m-l-8 font-size-13" style="margin-bottom: 30px;">
  118. <span class="MiSans-Normal">点击注册即代表您同意</span>
  119. <view class="color-primary" @tap.stop="handleToDetail('user')">
  120. 《用户协议》
  121. </view>
  122. <view class="agreement-text">和</view>
  123. <view class="color-primary" @tap.stop="handleToDetail('privacy')">
  124. 《隐私协议》
  125. </view>
  126. </view>
  127. </template>
  128. </view>
  129. </template>
  130. <script setup>
  131. import { ref, unref } from 'vue'
  132. import { mobile, password, code } from '@/utils/validate'
  133. import { getSmsCode, getSmsTimer } from '@/utils/code'
  134. import { userStore } from '@/store/user'
  135. const useUserStore = userStore()
  136. const items = ['手机号快捷登录', '短信登录', '账号登录']
  137. const current = ref(0)
  138. const accountLoginRef = ref()
  139. const smsLoginRef = ref()
  140. const registerForm = ref()
  141. const phoneNumberLoading = ref(false)
  142. const smsLoginLoading = ref(false)
  143. const registerLoading = ref(false)
  144. const protocol = ref(false)
  145. const state = ref({
  146. isMobileEnd: false, // 手机号输入完毕
  147. codeText: '获取验证码',
  148. sms: {
  149. phone: '',
  150. code: ''
  151. },
  152. register: {
  153. phone: '',
  154. code: ''
  155. },
  156. account: {
  157. phone: '',
  158. password: ''
  159. },
  160. rules: {
  161. phone: mobile,
  162. password,
  163. },
  164. smsRules: {
  165. code,
  166. phone: mobile
  167. }
  168. })
  169. // 设置默认账号密码便于开发快捷登录
  170. if (window && window.location && window.location.hostname && window.location.hostname === 'localhost') {
  171. state.value.account.phone = '13229740092'
  172. state.value.account.password = 'Citu123456'
  173. }
  174. const changeType = ref('login')
  175. // 登录成功后的返回页面
  176. // const backUrl = getCurrentPages().length ? getCurrentPages()[getCurrentPages().length - 2].route : ''
  177. const onClickItem = (e) => {
  178. current.value = e.currentIndex
  179. }
  180. // 获取验证码
  181. const handleCode = () => {
  182. if (!state.value.sms.phone) {
  183. uni.showToast({
  184. title: '请输入手机号',
  185. icon: 'none',
  186. duration: 2000
  187. })
  188. return
  189. }
  190. getSmsCode('smsLogin', state.value.sms.phone)
  191. }
  192. // 获取验证码
  193. const handleRegisterCode = () => {
  194. if (!state.value.register.phone) {
  195. uni.showToast({
  196. title: '请输入手机号',
  197. icon: 'none',
  198. duration: 2000
  199. })
  200. return
  201. }
  202. getSmsCode('smsRegister', state.value.register.phone)
  203. }
  204. // 查看协议详情
  205. const handleToDetail = (type) => {
  206. const url = type === 'user' ? '/pagesB/agreement/user' : '/pagesB/agreement/privacy'
  207. uni.navigateTo({
  208. url
  209. })
  210. }
  211. // 注册
  212. function handleChangeRegister () {
  213. changeType.value = 'register'
  214. }
  215. // 登录
  216. function handleChangeLogin () {
  217. changeType.value = 'login'
  218. }
  219. const showProtocolToast = () => {
  220. uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  221. }
  222. // 微信登录
  223. const getPhoneNumber = async (e) => {
  224. if (e?.detail?.errMsg !== 'getPhoneNumber:ok') {
  225. uni.showToast({ title: '微信登录失败', icon: 'none' })
  226. return
  227. }
  228. changeType.value = 'login'
  229. phoneNumberLoading.value = true
  230. wx.login({
  231. success: async (result) => {
  232. const wxLoginCode = result?.code || ''
  233. const query = {
  234. loginCode: wxLoginCode,
  235. phoneCode: e.detail.code,
  236. state: e.detail.encryptedData,
  237. }
  238. await useUserStore.handleSmsLogin(query, current.value)
  239. phoneNumberLoading.value = false
  240. },
  241. fail:(res)=> {
  242. phoneNumberLoading.value = false
  243. console.log("获取登录凭证code失败!", res)
  244. }
  245. })
  246. }
  247. async function handleRegister () {
  248. const validate = await unref(registerForm).validate()
  249. if (!validate) return
  250. try {
  251. registerLoading.value = true
  252. await useUserStore.handleRegister(state.value.register)
  253. changeType.value = 'login'
  254. } finally {
  255. registerLoading.value = false
  256. }
  257. }
  258. // 登录
  259. const handleLogin = async () => {
  260. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  261. const validate = await unref(current.value === 1 ? smsLoginRef : accountLoginRef).validate()
  262. if (!validate) return
  263. try {
  264. smsLoginLoading.value = true
  265. const query = current.value === 1 ? state.value.sms : state.value.account
  266. Object.assign(query, {
  267. account: query.phone
  268. })
  269. await useUserStore.handleSmsLogin(query, current.value)
  270. } finally {
  271. smsLoginLoading.value = false
  272. }
  273. }
  274. </script>
  275. <style scoped lang="scss">
  276. .login-code {
  277. width: 73px;
  278. min-width: 73px;
  279. color: #00B760;
  280. text-align: center;
  281. font-size: 12px;
  282. cursor: pointer;
  283. // border: 1px dashed #00B760;
  284. // border-radius: 26px;
  285. padding: 0;
  286. }
  287. .head-title {
  288. font-size: 40rpx;
  289. text-align: center;
  290. color: #00B760;
  291. margin-top: 30rpx;
  292. }
  293. .quickLogon {
  294. display: flex;
  295. justify-content: space-between;
  296. padding: 0 20rpx;
  297. align-items: center;
  298. }
  299. .wxLogon {
  300. font-size: .85em;
  301. color: #00B760;
  302. border: none;
  303. margin: 0;
  304. padding: 0;
  305. }
  306. .register {
  307. widows: 100%;
  308. font-size: .85em;
  309. text-align: center;
  310. color: #00B760;
  311. padding-bottom: 2px;
  312. text-decoration: underline;
  313. &.login {
  314. color: #00B760;
  315. }
  316. }
  317. .pb-20 {
  318. padding-bottom: 40rpx;
  319. }
  320. </style>