index.vue 9.9 KB

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