index.vue 9.4 KB

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