login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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="/static/img/logo.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/privacy')">《法律条款》</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/privacy')">《法律条款》</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.redirectTo(this.redirectUrl || '/pages/user/index')
  171. },
  172. onConfirmGetProfile(e) {
  173. let pagePath = '/pages/user/info-edit?avatar=' + encodeURIComponent(e.detail.avatarUrl)
  174. if (this.redirectUrl) {
  175. pagePath += '&redirect=' + encodeURIComponent(this.redirectUrl)
  176. }
  177. this.redirectTo(pagePath)
  178. },
  179. onCheckChange(checked) {
  180. this.protocolChecked = checked
  181. },
  182. onClickPhoneLoginOverlay() {
  183. this.showPhoneLogin = false
  184. },
  185. onClearPhoneNumber() {
  186. this.phoneNumber = ''
  187. },
  188. onClearCheckCode() {
  189. this.checkCode = ''
  190. },
  191. onPhoneLoginClick() {
  192. this.checkProtocol()
  193. },
  194. onPhoneLogin(e) {
  195. const openId = uni.getStorageSync('openId')
  196. if (e.detail.code) {
  197. login({
  198. open_id: openId,
  199. code: e.detail.code,
  200. encrypted_data: e.detail.encryptedData,
  201. iv: e.detail.iv
  202. }).then(res => {
  203. if (res.data) {
  204. this.$store.commit('SET_TOKEN', res.data)
  205. this.$store.dispatch('getInfo').then(res => {
  206. const user = res.data
  207. if (!user.nick_name || !user.avatar) {
  208. this.showGetProfile = true
  209. } else {
  210. this.redirectTo(this.redirectUrl || '/pages/user/index')
  211. }
  212. })
  213. } else {
  214. this.showToast('微信服务器忙线中,稍候再试')
  215. }
  216. })
  217. }
  218. },
  219. onPhoneLoginOther() {
  220. this.showPhoneLogin = true
  221. },
  222. onPhoneSubmit() {
  223. const openId = uni.getStorageSync('openId')
  224. if (!this.checkProtocol()) {
  225. return
  226. }
  227. this.phoneNumber = this.phoneNumber.trim()
  228. this.validCode = this.validCode.trim()
  229. if (!this.phoneNumber) {
  230. this.showToast('请填写手机号')
  231. return
  232. }
  233. if (!this.validCode) {
  234. this.showToast('请填写验证码')
  235. return
  236. }
  237. this.loginLoading = true
  238. phoneLogin({
  239. phone: this.phoneNumber,
  240. valid_code: this.validCode,
  241. open_id: openId
  242. }).then(res => {
  243. console.log("手机验证码登录:", res)
  244. this.loginLoading = false
  245. if (res.data) {
  246. this.$store.commit('SET_TOKEN', res.data)
  247. this.$store.dispatch('getInfo').then(res => {
  248. const user = res.data
  249. if (!user.nick_name || !user.avatar) {
  250. this.showGetProfile = true
  251. } else {
  252. this.redirectTo(this.redirectUrl || '/pages/user/index')
  253. }
  254. })
  255. this.showPhoneLogin = false
  256. } else {
  257. this.showToast('微信服务器忙线中,稍候再试')
  258. }
  259. })
  260. }
  261. }
  262. }
  263. </script>
  264. <style lang="scss">
  265. .user-login{
  266. height: 100%;
  267. display: flex;
  268. flex-direction: column;
  269. &>u-scroll-view {
  270. flex:1;
  271. min-height: 1px;
  272. }
  273. .main-container{
  274. @include display-flex-center;
  275. flex-direction: column;
  276. height: 100%;
  277. }
  278. .user-login-box{
  279. width: 565rpx;
  280. }
  281. .user-login-logo{
  282. width: 377rpx;
  283. height: 93rpx;
  284. margin: 0 auto;
  285. }
  286. .user-login-button{
  287. display: grid;
  288. grid-template-columns: 1fr;
  289. grid-row-gap: 22rpx;
  290. margin-top: 254rpx;
  291. }
  292. .user-login-protocol-text{
  293. margin-top: 39rpx;
  294. font-size: $fontSize2;
  295. color: #555555;
  296. line-height: 30rpx;
  297. }
  298. .user-phone-login-wrapper{
  299. display: flex;
  300. align-items: center;
  301. justify-content: center;
  302. height: 100%;
  303. }
  304. .user-phone-login{
  305. width: 670rpx;
  306. padding: 51rpx 59rpx 96rpx 59rpx;
  307. background-color: #ffffff;
  308. border-radius: 10rpx;
  309. &>view:first-child{
  310. font-size: $fontSize3;
  311. color: #333333;
  312. text-align: center;
  313. }
  314. .input-phone-number{
  315. margin-top: 54rpx;
  316. border-bottom: 1rpx solid #D9D9D9;
  317. padding-bottom: 31rpx;
  318. height: 60rpx;
  319. }
  320. .input-phone-number,.input-check-code{
  321. position: relative;
  322. font-size: $fontSize3;
  323. .clear{
  324. position: absolute;
  325. right: 10rpx;
  326. top: 10rpx;
  327. z-index: 1;
  328. }
  329. .van-icon {
  330. color: #555555;
  331. }
  332. }
  333. .input-check-code{
  334. display: flex;
  335. align-items: center;
  336. margin-top: 44rpx;
  337. .van-button{
  338. width: 205rpx;
  339. height: 80rpx;
  340. border-radius: 50rpx;
  341. }
  342. .clear{
  343. top: 26rpx;
  344. right: 250rpx;
  345. }
  346. input{
  347. flex: 1;
  348. }
  349. padding-bottom: 0;
  350. }
  351. .submit{
  352. margin-top: 50rpx;
  353. }
  354. }
  355. /* 定义振动动画 */
  356. @keyframes shake {
  357. 0% {
  358. transform: translateX(0);
  359. }
  360. 25% {
  361. transform: translateX(-10rpx);
  362. }
  363. 50% {
  364. transform: translateX(10rpx);
  365. }
  366. 75% {
  367. transform: translateX(-10rpx);
  368. }
  369. 100% {
  370. transform: translateX(0);
  371. }
  372. }
  373. .protocol-shake {
  374. display: inline-block;
  375. animation: shake 0.3s ease-in-out infinite;
  376. }
  377. }
  378. </style>