index.vue 10.0 KB

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