index.vue 9.5 KB

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