index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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;" @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 { getUserRegisterEnterpriseApply, getEnterpriseRegisterApply } from '@/api/enterprise'
  101. const useUserStore = userStore()
  102. const accountLoginRef = ref()
  103. const registerForm = ref()
  104. const protocol = ref(false)
  105. const state = ref({
  106. isMobileEnd: false, // 手机号输入完毕
  107. codeText: '获取验证码',
  108. sms: {
  109. phone: '',
  110. code: ''
  111. },
  112. register: {
  113. phone: '',
  114. code: ''
  115. },
  116. account: {
  117. phone: '1687284007@qq.com', // 1687284007 18406571584
  118. password: 'Citu123456'
  119. },
  120. rules: {
  121. phone: emailRequired,
  122. password,
  123. },
  124. smsRules: {
  125. code,
  126. phone: mobile
  127. }
  128. })
  129. // 设置默认账号密码便于开发快捷登录
  130. if (window && window.location && window.location.hostname && window.location.hostname === 'localhost') {
  131. state.value.account.phone = '1687284007@qq.com'
  132. state.value.account.password = 'Citu123456'
  133. }
  134. // 忘记密码
  135. const handleForgotPassword = () => {
  136. uni.navigateTo({
  137. url: '/pagesA/forgotPassword/index'
  138. })
  139. }
  140. const changeType = ref('login')
  141. // 获取验证码
  142. const handleRegisterCode = () => {
  143. if (!state.value.register.phone) {
  144. uni.showToast({
  145. title: '请输入手机号',
  146. icon: 'none',
  147. duration: 2000
  148. })
  149. return
  150. }
  151. getSmsCode('smsRegister', state.value.register.phone)
  152. }
  153. // 查看协议详情
  154. const handleToDetail = (type) => {
  155. const url = type === 'user' ? '/pagesB/agreement/user' : '/pagesB/agreement/privacy'
  156. uni.navigateTo({
  157. url
  158. })
  159. }
  160. // 注册
  161. function handleChangeRegister () {
  162. changeType.value = 'register'
  163. }
  164. // 登录
  165. function handleChangeLogin () {
  166. changeType.value = 'login'
  167. }
  168. async function handleRegister () {
  169. const validate = await unref(registerForm).validate()
  170. if (!validate) return
  171. const query = state.value.register
  172. Object.assign(query, {
  173. account: query.phone,
  174. autoRegister: true
  175. })
  176. try {
  177. await useUserStore.handleRegister(query)
  178. uni.showToast({
  179. title: '手机号验证成功',
  180. icon: 'none',
  181. duration: 2000
  182. })
  183. const { data } = await getUserRegisterEnterpriseApply()
  184. changeType.value = 'login'
  185. const hasData = data && Object.keys(data).length > 0 && data.status !== '1'
  186. console.log(data, '查看用户是否有在申请中的数据')
  187. uni.navigateTo({
  188. url: hasData ? '/pages/register/review' : '/pages/register/index'
  189. })
  190. } finally {
  191. }
  192. }
  193. const handleCheckEnterprise = async () => {
  194. const { data } = await getEnterpriseRegisterApply(state.value.account.phone)
  195. if (data && Object.keys(data).length) {
  196. // 查看申请状态
  197. uni.setStorageSync('entRegisterData', JSON.stringify(data))
  198. uni.navigateTo({
  199. url: '/pages/register/review?hasData=true'
  200. })
  201. }
  202. closeAuthModal()
  203. }
  204. // 登录
  205. const handleLogin = async () => {
  206. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  207. const validate = await unref(accountLoginRef).validate()
  208. if (!validate) return
  209. const query = state.value.account
  210. Object.assign(query, {
  211. account: query.phone
  212. })
  213. console.log(query, '登录参数')
  214. try {
  215. await useUserStore.handleSmsLogin(query, 1)
  216. closeAuthModal()
  217. } catch (err) {
  218. console.log(err, '登录失败')
  219. if (!err.code || (err?.message && err.message.includes('timeout'))) return closeAuthModal()
  220. if (err.code === 1100017022) {
  221. // 密码不安全
  222. uni.showToast({ title: '您的密码不安全,请重置密码', icon: 'none', duration: 2000 })
  223. closeAuthModal()
  224. return
  225. }
  226. if (err.code === 1100021016) {
  227. // 企业注册申请中
  228. handleCheckEnterprise()
  229. return
  230. }
  231. if (err.code === 1100017019) {
  232. uni.showToast({ title: '您的邮箱还未注册过,请先注册', icon: 'none', duration: 2000 })
  233. changeType.value = 'register'
  234. }
  235. }
  236. }
  237. </script>
  238. <style scoped lang="scss">
  239. .login-code {
  240. width: 73px;
  241. min-width: 73px;
  242. color: #00B760;
  243. text-align: center;
  244. font-size: 12px;
  245. cursor: pointer;
  246. // border: 1px dashed #00B760;
  247. // border-radius: 26px;
  248. padding: 0;
  249. }
  250. .head-title {
  251. font-size: 40rpx;
  252. text-align: center;
  253. color: #00B760;
  254. margin-top: 30rpx;
  255. }
  256. .quickLogon {
  257. display: flex;
  258. justify-content: space-between;
  259. padding: 0 20rpx;
  260. align-items: center;
  261. }
  262. .wxLogon {
  263. font-size: .85em;
  264. color: #00B760;
  265. border: none;
  266. margin: 0;
  267. padding: 0;
  268. }
  269. .register {
  270. widows: 100%;
  271. font-size: .85em;
  272. color: red;
  273. // text-align: right;
  274. &.login {
  275. color: #00B760;
  276. }
  277. }
  278. .pb-20 {
  279. padding-bottom: 40rpx;
  280. }
  281. </style>