index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. <view class="ss-m-t-30">
  6. <!-- 企业邮箱+密码登录 -->
  7. <uni-forms
  8. ref="accountLoginRef"
  9. v-model="state.account"
  10. :rules="state.rules"
  11. validateTrigger="bind"
  12. labelWidth="140"
  13. labelAlign="center"
  14. >
  15. <uni-forms-item name="phone" label="企业邮箱">
  16. <uni-easyinput placeholder="请输入企业邮箱" v-model="state.account.phone" :inputBorder="false"></uni-easyinput>
  17. </uni-forms-item>
  18. <uni-forms-item name="password" label="登录密码">
  19. <uni-easyinput type="password" placeholder="请输入密码" v-model="state.account.password" :inputBorder="false"></uni-easyinput>
  20. </uni-forms-item>
  21. </uni-forms>
  22. <view class="quickLogon">
  23. <view class="color-666" style="font-size: .85em; text-decoration: underline;" @tap.stop="handleForgotPassword">忘记密码</view>
  24. <view class="register" @tap="handleChangeRegister">还没有登录账户?去注册</view>
  25. </view>
  26. <button class="send-button" @tap="handleLogin"> 登 录 </button>
  27. <view class="agreement-box ss-flex ss-row-center" style="margin-bottom: 30px;">
  28. <uni-icons size="20" :type="protocol ? 'checkbox-filled' : 'circle'" :color="protocol ? '#00B760' : '#ccc'" @tap="protocol = !protocol"></uni-icons>
  29. <view class="color-999 ss-flex ss-col-center ss-m-l-8 font-size-13">
  30. 我已阅读并遵守
  31. <view class="color-primary" @tap.stop="handleToDetail('user')">
  32. 《用户协议》
  33. </view>
  34. <view class="agreement-text">和</view>
  35. <view class="color-primary" @tap.stop="handleToDetail('privacy')">
  36. 《隐私协议》
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <template v-if="changeType === 'register'">
  43. <view class="head-title pb-20">手机号验证</view>
  44. <uni-forms
  45. ref="registerForm"
  46. v-model="state.register"
  47. :rules="state.smsRules"
  48. validateTrigger="bind"
  49. labelWidth="140"
  50. labelAlign="center"
  51. >
  52. <uni-forms-item name="phone" label="手机号">
  53. <uni-easyinput placeholder="请输入手机号" v-model="state.register.phone" :inputBorder="false" type="number">
  54. </uni-easyinput>
  55. </uni-forms-item>
  56. <uni-forms-item name="code" label="验证码">
  57. <uni-easyinput
  58. placeholder="请输入验证码"
  59. v-model="state.register.code"
  60. :inputBorder="false"
  61. type="number"
  62. maxlength="6"
  63. >
  64. <template v-slot:right>
  65. <button
  66. class="login-code"
  67. :disabled="state.isMobileEnd"
  68. :class="{ 'code-btn-end': state.isMobileEnd }"
  69. @tap="handleRegisterCode"
  70. >
  71. {{ getSmsTimer('smsRegister') }}
  72. </button>
  73. </template>
  74. </uni-easyinput>
  75. </uni-forms-item>
  76. </uni-forms>
  77. <view class="register login" style="text-align: end;" @tap="handleChangeLogin">已有账户?去登陆</view>
  78. <view>
  79. <button class="send-button" @tap="handleRegister"> 注 册 </button>
  80. </view>
  81. <view class="color-999 ss-flex ss-col-center ss-row-center ss-m-l-8 font-size-13" style="margin-bottom: 30px;">
  82. 点击注册即代表您同意
  83. <view class="color-primary" @tap.stop="handleToDetail('user')">
  84. 《用户协议》
  85. </view>
  86. <view class="agreement-text">和</view>
  87. <view class="color-primary" @tap.stop="handleToDetail('privacy')">
  88. 《隐私协议》
  89. </view>
  90. </view>
  91. </template>
  92. </view>
  93. </template>
  94. <script setup>
  95. import { ref, unref } from 'vue'
  96. import { mobile, password, code, emailRequired } from '@/utils/validate'
  97. import { getSmsCode, getSmsTimer } from '@/utils/code'
  98. import { userStore } from '@/store/user'
  99. import { closeAuthModal } from '@/hooks/useModal'
  100. import { getEnterpriseRegisterApply } from '@/api/enterprise'
  101. import { testEnvBool } from '@/utils/config'
  102. const useUserStore = userStore()
  103. const accountLoginRef = ref()
  104. const registerForm = ref()
  105. const protocol = ref(false)
  106. const state = ref({
  107. isMobileEnd: false, // 手机号输入完毕
  108. codeText: '获取验证码',
  109. sms: {
  110. phone: '',
  111. code: ''
  112. },
  113. register: {
  114. phone: '',
  115. code: ''
  116. },
  117. account: {
  118. phone: testEnvBool ? '1687284007@qq.com' : '', // 1687284007 18406571584
  119. password: testEnvBool ? 'Citu123456' : '',
  120. },
  121. rules: {
  122. phone: emailRequired,
  123. password,
  124. },
  125. smsRules: {
  126. code,
  127. phone: mobile
  128. }
  129. })
  130. // 设置默认账号密码便于开发快捷登录
  131. if (window && window.location && window.location.hostname && window.location.hostname === 'localhost') {
  132. state.value.account.phone = '1687284007@qq.com'
  133. state.value.account.password = 'Citu123456'
  134. }
  135. // 忘记密码
  136. const handleForgotPassword = () => {
  137. uni.navigateTo({
  138. url: '/pagesA/forgotPassword/index'
  139. })
  140. }
  141. const changeType = ref('login')
  142. // 获取验证码
  143. const handleRegisterCode = () => {
  144. if (!state.value.register.phone) {
  145. uni.showToast({
  146. title: '请输入手机号',
  147. icon: 'none',
  148. duration: 2000
  149. })
  150. return
  151. }
  152. getSmsCode('smsRegister', state.value.register.phone)
  153. }
  154. // 查看协议详情
  155. const handleToDetail = (type) => {
  156. const url = type === 'user' ? '/pagesB/agreement/user' : '/pagesB/agreement/privacy'
  157. uni.navigateTo({
  158. url
  159. })
  160. }
  161. // 注册
  162. function handleChangeRegister () {
  163. changeType.value = 'register'
  164. }
  165. // 登录
  166. function handleChangeLogin () {
  167. changeType.value = 'login'
  168. }
  169. async function handleRegister () {
  170. const validate = await unref(registerForm).validate()
  171. if (!validate) return
  172. const query = state.value.register
  173. Object.assign(query, {
  174. account: query.phone,
  175. autoRegister: true
  176. })
  177. try {
  178. await useUserStore.handleRegister(query)
  179. uni.showToast({
  180. title: '手机号验证成功',
  181. icon: 'none',
  182. duration: 2000
  183. })
  184. // 查看用户是否有在申请中的数据
  185. const { code } = await useUserStore.getPersonalApplyingData()
  186. changeType.value = 'login'
  187. uni.navigateTo({ url: code ? '/pages/register/review?hasData=true' : '/pages/register/index' })
  188. } finally {
  189. }
  190. }
  191. const handleCheckEnterprise = async () => {
  192. const { data } = await getEnterpriseRegisterApply(state.value.account.phone)
  193. if (data && Object.keys(data).length) {
  194. // 查看申请状态
  195. uni.setStorageSync('entRegisterData', JSON.stringify(data))
  196. uni.navigateTo({
  197. url: '/pages/register/review?hasData=true'
  198. })
  199. }
  200. closeAuthModal()
  201. }
  202. // 登录
  203. const handleLogin = async () => {
  204. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  205. const validate = await unref(accountLoginRef).validate()
  206. if (!validate) return
  207. const query = state.value.account
  208. Object.assign(query, {
  209. account: query.phone
  210. })
  211. console.log(query, '登录参数')
  212. try {
  213. await useUserStore.handleSmsLogin(query, 1)
  214. closeAuthModal()
  215. } catch (err) {
  216. console.log(err, '登录失败')
  217. if (!err.code || (err?.message && err.message.includes('timeout'))) return closeAuthModal()
  218. if (err.code === 1100017022) {
  219. // 密码不安全
  220. uni.showToast({ title: '您的密码不安全,请重置密码', icon: 'none', duration: 2000 })
  221. closeAuthModal()
  222. return
  223. }
  224. if (err.code === 1100021016) {
  225. // 企业注册申请中
  226. handleCheckEnterprise()
  227. return
  228. }
  229. if (err.code === 1100017019) {
  230. uni.showToast({ title: '您的邮箱还未注册过,请先注册', icon: 'none', duration: 2000 })
  231. changeType.value = 'register'
  232. }
  233. }
  234. }
  235. </script>
  236. <style scoped lang="scss">
  237. .login-code {
  238. width: 73px;
  239. min-width: 73px;
  240. color: #00B760;
  241. text-align: center;
  242. font-size: 12px;
  243. cursor: pointer;
  244. padding: 0;
  245. }
  246. .head-title {
  247. font-size: 40rpx;
  248. text-align: center;
  249. color: #00B760;
  250. margin-top: 30rpx;
  251. }
  252. .quickLogon {
  253. display: flex;
  254. justify-content: space-between;
  255. padding: 0 20rpx;
  256. align-items: center;
  257. }
  258. .register {
  259. widows: 100%;
  260. font-size: .85em;
  261. color: #00B760;
  262. text-decoration: underline;
  263. &.login {
  264. color: #00B760;
  265. }
  266. }
  267. .pb-20 {
  268. padding-bottom: 40rpx;
  269. }
  270. </style>