index.vue 11 KB

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