login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <template>
  2. <page-layout class="user-login">
  3. <nav-bar :transparent="true" @init="onInitNavbar"></nav-bar>
  4. <u-scroll-view>
  5. <view class="main-container">
  6. <view class="user-login-box">
  7. <view class="user-login-logo">
  8. <image src="https://oss.productronicachina.com.cn/resources/common/up/0000010000/20241216/675fe6c0afc65.jpg" mode="aspectFill" />
  9. </view>
  10. <view class="user-login-button">
  11. <van-button type="primary" size="large" :open-type="protocolChecked?'getPhoneNumber': ''" @getphonenumber="onPhoneLogin" @click="onPhoneLoginClick">手机号快捷登录</van-button>
  12. <van-button type="default" size="large" @click="onPhoneLoginOther">其它手机号登录</van-button>
  13. </view>
  14. <view class="user-login-protocol" :class="{ 'protocol-shake': protocolShake }">
  15. <van-checkbox :value="protocolChecked" shape="square" @change="onCheckChange">
  16. <view class="user-login-protocol-text">
  17. <text>我已仔细阅读主办方的</text>
  18. <text class="link-text" @click="navigateTo('/pages/disclaimer/privacy')">《隐私政策》</text>
  19. <text></text>
  20. <text class="link-text" @click="navigateTo('/pages/disclaimer/legalNotice')">《法律条款》</text>
  21. <text>的全部内容并同意遵守。</text>
  22. </view>
  23. </van-checkbox>
  24. </view>
  25. </view>
  26. </view>
  27. <van-overlay :show="showPhoneLogin" @click="onClickPhoneLoginOverlay">
  28. <view class="user-phone-login-wrapper">
  29. <view class="user-phone-login" @click.stop>
  30. <view>获取手机号</view>
  31. <view class="input-phone-number">
  32. <input v-model="phoneNumber" placeholder="请输入手机号" type="number" />
  33. <view v-if="phoneNumber" class="clear" @click="onClearPhoneNumber" ><van-icon name="clear" /></view>
  34. </view>
  35. <view class="input-check-code">
  36. <input v-model="validCode" placeholder="请输入短信验证码" type="number" maxlength="6"/>
  37. <view v-if="validCode" class="clear" @click="onClearCheckCode" ><van-icon name="clear" /></view>
  38. <van-button type="primary" :loading="validCodeLoading" :disabled="countDown > 0" @click="onSendValidCode">
  39. <template v-if="countDown === 0">
  40. 发送验证码
  41. </template>
  42. <template v-else>
  43. 重新发送({{ countDown }}s)
  44. </template>
  45. </van-button>
  46. </view>
  47. <view class="user-phone-login-protocol" :class="{ 'protocol-shake': protocolShake }">
  48. <van-checkbox :value="protocolChecked" shape="square" @change="onCheckChange">
  49. <view class="user-login-protocol-text">
  50. <text>我已仔细阅读主办方的</text>
  51. <text class="link-text" @click="navigateTo('/pages/disclaimer/privacy')">《隐私政策》</text>
  52. <text></text>
  53. <text class="link-text" @click="navigateTo('/pages/disclaimer/legalNotice')">《法律条款》</text>
  54. <text>的全部内容并同意遵守。</text>
  55. </view>
  56. </van-checkbox>
  57. </view>
  58. <view class="submit">
  59. <van-button type="primary" :loading="loginLoading" size="large" @click="onPhoneSubmit">提交</van-button>
  60. </view>
  61. </view>
  62. </view>
  63. </van-overlay>
  64. <van-dialog
  65. :show.sync="showGetProfile"
  66. title="登录成功"
  67. message="设置您的的头像和呢称?"
  68. show-cancel-button
  69. confirm-button-open-type="chooseAvatar"
  70. @chooseavatar="onConfirmGetProfile"
  71. @cancel="onCancelGetProfile"
  72. >
  73. </van-dialog>
  74. </u-scroll-view>
  75. </page-layout>
  76. </template>
  77. <script>
  78. import { login, wechatLogin, getInfo, updateInfo, phoneLogin, getSmsCode } from '@/api/user'
  79. import NavBar from '@/components/layout/nav-bar'
  80. import UScrollView from '@/components/common/u-scroll-view'
  81. import VanCheckbox from '@/wxcomponents/vant/checkbox/index'
  82. import VanOverlay from '@/wxcomponents/vant/overlay/index'
  83. import VanDialog from '@/wxcomponents/vant/dialog/index'
  84. import PageLayout from "@/components/layout/page-layout";
  85. export default {
  86. options: {
  87. styleIsolation: 'shared'
  88. },
  89. components: {
  90. PageLayout,
  91. NavBar,
  92. VanCheckbox,
  93. VanOverlay,
  94. UScrollView,
  95. VanDialog
  96. },
  97. data() {
  98. return {
  99. showGetProfile: false,
  100. protocolChecked: false,
  101. protocolShake: false,
  102. showPhoneLogin: false,
  103. phoneNumber: '',
  104. validCode: '',
  105. validCodeLoading: false,
  106. redirectUrl: '',
  107. uesrId: 0,
  108. countDown: 0,
  109. countDownTimer: null,
  110. loginLoading: false
  111. }
  112. },
  113. onLoad(options) {
  114. if (options.redirect) {
  115. this.redirectUrl = decodeURIComponent(options.redirect)
  116. } else {
  117. this.redirectUrl = ''
  118. }
  119. },
  120. created() {
  121. const openId = uni.getStorageSync('openId')
  122. uni.login({
  123. provider: 'weixin',
  124. success: res => {
  125. wechatLogin({
  126. code: res.code
  127. }).then(res => {
  128. uni.setStorageSync('openId', res.data)
  129. })
  130. }
  131. })
  132. },
  133. methods: {
  134. onSendValidCode() {
  135. this.phoneNumber = this.phoneNumber.trim()
  136. if (!this.checkProtocol()) {
  137. return
  138. }
  139. if (!this.phoneNumber) {
  140. this.showToast('请填写手机号')
  141. return
  142. }
  143. this.validCodeLoading = true
  144. getSmsCode({
  145. phone: this.phoneNumber
  146. }).then(res => {
  147. this.validCodeLoading = false
  148. this.showToast('短信已发送')
  149. this.countDown = 60
  150. this.countDownTimer = setInterval(() => {
  151. this.countDown--
  152. if (this.countDown === 0) {
  153. clearInterval(this.countDownTimer)
  154. this.countDownTimer = null
  155. }
  156. }, 1000)
  157. })
  158. },
  159. checkProtocol() {
  160. if (!this.protocolChecked) {
  161. this.showToast('请查看协议并同意')
  162. this.protocolShake = true
  163. setTimeout(() => {
  164. this.protocolShake = false
  165. }, 500)
  166. }
  167. return this.protocolChecked
  168. },
  169. onCancelGetProfile() {
  170. // this.refreshData()
  171. this.redirectTo(this.redirectUrl || '/pages/user/index')
  172. },
  173. onConfirmGetProfile(e) {
  174. let pagePath = '/pages/user/info-edit?avatar=' + encodeURIComponent(e.detail.avatarUrl)
  175. if (this.redirectUrl) {
  176. pagePath += '&redirect=' + encodeURIComponent(this.redirectUrl)
  177. }
  178. // this.refreshData()
  179. this.redirectTo(pagePath)
  180. },
  181. onCheckChange(checked) {
  182. this.protocolChecked = checked
  183. },
  184. onClickPhoneLoginOverlay() {
  185. this.showPhoneLogin = false
  186. },
  187. onClearPhoneNumber() {
  188. this.phoneNumber = ''
  189. },
  190. onClearCheckCode() {
  191. this.checkCode = ''
  192. },
  193. onPhoneLoginClick() {
  194. this.checkProtocol()
  195. },
  196. onPhoneLogin(e) {
  197. const openId = uni.getStorageSync('openId')
  198. if (e.detail.code) {
  199. login({
  200. open_id: openId,
  201. code: e.detail.code,
  202. encrypted_data: e.detail.encryptedData,
  203. iv: e.detail.iv
  204. }).then(res => {
  205. if (res.data) {
  206. this.$store.commit('SET_TOKEN', res.data)
  207. this.$store.dispatch('getInfo').then(res => {
  208. const user = res.data
  209. if (!user.nick_name || !user.avatar) {
  210. this.showGetProfile = true
  211. } else {
  212. // uni.navigateBack()
  213. // this.refreshData()
  214. this.redirectTo(this.redirectUrl || '/pages/user/index')
  215. }
  216. })
  217. } else {
  218. this.showToast('微信服务器忙线中,稍候再试')
  219. }
  220. })
  221. }
  222. },
  223. onPhoneLoginOther() {
  224. this.showPhoneLogin = true
  225. },
  226. onPhoneSubmit() {
  227. const openId = uni.getStorageSync('openId')
  228. if (!this.checkProtocol()) {
  229. return
  230. }
  231. this.phoneNumber = this.phoneNumber.trim()
  232. this.validCode = this.validCode.trim()
  233. if (!this.phoneNumber) {
  234. this.showToast('请填写手机号')
  235. return
  236. }
  237. if (!this.validCode) {
  238. this.showToast('请填写验证码')
  239. return
  240. }
  241. this.loginLoading = true
  242. phoneLogin({
  243. phone: this.phoneNumber,
  244. valid_code: this.validCode,
  245. open_id: openId
  246. }).then(res => {
  247. this.loginLoading = false
  248. if (res.data) {
  249. this.$store.commit('SET_TOKEN', res.data)
  250. this.$store.dispatch('getInfo').then(res => {
  251. const user = res.data
  252. if (!user.nick_name || !user.avatar) {
  253. this.showGetProfile = true
  254. } else {
  255. // uni.navigateBack()
  256. // this.refreshData()
  257. this.redirectTo(this.redirectUrl || '/pages/user/index')
  258. }
  259. })
  260. this.showPhoneLogin = false
  261. } else {
  262. this.showToast('微信服务器忙线中,稍候再试')
  263. }
  264. })
  265. }
  266. }
  267. }
  268. </script>
  269. <style lang="scss">
  270. .user-login{
  271. height: 100%;
  272. display: flex;
  273. flex-direction: column;
  274. &>u-scroll-view {
  275. flex:1;
  276. min-height: 1px;
  277. }
  278. .main-container{
  279. @include display-flex-center;
  280. flex-direction: column;
  281. height: 100%;
  282. }
  283. .user-login-box{
  284. width: 565rpx;
  285. }
  286. .user-login-logo{
  287. width: 377rpx;
  288. height: 93rpx;
  289. margin: 0 auto;
  290. }
  291. .user-login-button{
  292. display: grid;
  293. grid-template-columns: 1fr;
  294. grid-row-gap: 22rpx;
  295. margin-top: 254rpx;
  296. }
  297. .user-login-protocol-text{
  298. margin-top: 39rpx;
  299. font-size: $fontSize2;
  300. color: #555555;
  301. line-height: 30rpx;
  302. }
  303. .user-phone-login-wrapper{
  304. display: flex;
  305. align-items: center;
  306. justify-content: center;
  307. height: 100%;
  308. }
  309. .user-phone-login{
  310. width: 670rpx;
  311. padding: 51rpx 59rpx 96rpx 59rpx;
  312. background-color: #ffffff;
  313. border-radius: 10rpx;
  314. &>view:first-child{
  315. font-size: $fontSize3;
  316. color: #333333;
  317. text-align: center;
  318. }
  319. .input-phone-number{
  320. margin-top: 54rpx;
  321. border-bottom: 1rpx solid #D9D9D9;
  322. padding-bottom: 31rpx;
  323. height: 60rpx;
  324. }
  325. .input-phone-number,.input-check-code{
  326. position: relative;
  327. font-size: $fontSize3;
  328. .clear{
  329. position: absolute;
  330. right: 10rpx;
  331. top: 10rpx;
  332. z-index: 1;
  333. }
  334. .van-icon {
  335. color: #555555;
  336. }
  337. }
  338. .input-check-code{
  339. display: flex;
  340. align-items: center;
  341. margin-top: 44rpx;
  342. .van-button{
  343. width: 205rpx;
  344. height: 80rpx;
  345. border-radius: 50rpx;
  346. }
  347. .clear{
  348. top: 26rpx;
  349. right: 250rpx;
  350. }
  351. input{
  352. flex: 1;
  353. }
  354. padding-bottom: 0;
  355. }
  356. .submit{
  357. margin-top: 50rpx;
  358. }
  359. }
  360. /* 定义振动动画 */
  361. @keyframes shake {
  362. 0% {
  363. transform: translateX(0);
  364. }
  365. 25% {
  366. transform: translateX(-10rpx);
  367. }
  368. 50% {
  369. transform: translateX(10rpx);
  370. }
  371. 75% {
  372. transform: translateX(-10rpx);
  373. }
  374. 100% {
  375. transform: translateX(0);
  376. }
  377. }
  378. .protocol-shake {
  379. display: inline-block;
  380. animation: shake 0.3s ease-in-out infinite;
  381. }
  382. }
  383. </style>