login.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <template>
  2. <view 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('https://www.productronicachina.com.cn/%E9%9A%90%E7%A7%81%E6%9D%A1%E6%AC%BE')">《隐私政策》</text>
  19. <text>和</text>
  20. <text class="link-text" @click="navigateTo('https://www.productronicachina.com.cn/%E6%B3%95%E5%BE%8B%E4%BF%A1%E6%81%AF')">《法律条款》</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('https://www.productronicachina.com.cn/%E9%9A%90%E7%A7%81%E6%9D%A1%E6%AC%BE')">《隐私政策》</text>
  52. <text>和</text>
  53. <text class="link-text" @click="navigateTo('https://www.productronicachina.com.cn/%E6%B3%95%E5%BE%8B%E4%BF%A1%E6%81%AF')">《法律条款》</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. </view>
  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. export default {
  85. options: {
  86. styleIsolation: 'shared'
  87. },
  88. components: {
  89. NavBar,
  90. VanCheckbox,
  91. VanOverlay,
  92. UScrollView,
  93. VanDialog
  94. },
  95. data() {
  96. return {
  97. showGetProfile: false,
  98. protocolChecked: false,
  99. protocolShake: false,
  100. showPhoneLogin: false,
  101. phoneNumber: '',
  102. validCode: '',
  103. validCodeLoading: false,
  104. redirectUrl: '',
  105. uesrId: 0,
  106. countDown: 0,
  107. countDownTimer: null,
  108. loginLoading: false
  109. }
  110. },
  111. onLoad(options) {
  112. if (options.redirect) {
  113. this.redirectUrl = decodeURIComponent(options.redirect)
  114. } else {
  115. this.redirectUrl = ''
  116. }
  117. },
  118. created() {
  119. const openId = uni.getStorageSync('openId')
  120. uni.login({
  121. provider: 'weixin',
  122. success: res => {
  123. wechatLogin({
  124. code: res.code
  125. }).then(res => {
  126. uni.setStorageSync('openId', res.data)
  127. })
  128. }
  129. })
  130. },
  131. methods: {
  132. onSendValidCode() {
  133. this.phoneNumber = this.phoneNumber.trim()
  134. if (!this.checkProtocol()) {
  135. return
  136. }
  137. if (!this.phoneNumber) {
  138. this.showToast('请填写手机号')
  139. return
  140. }
  141. this.validCodeLoading = true
  142. getSmsCode({
  143. phone: this.phoneNumber
  144. }).then(res => {
  145. this.validCodeLoading = false
  146. this.showToast('短信已发送')
  147. this.countDown = 60
  148. this.countDownTimer = setInterval(() => {
  149. this.countDown--
  150. if (this.countDown === 0) {
  151. clearInterval(this.countDownTimer)
  152. this.countDownTimer = null
  153. }
  154. }, 1000)
  155. })
  156. },
  157. checkProtocol() {
  158. if (!this.protocolChecked) {
  159. this.showToast('请查看协议并同意')
  160. this.protocolShake = true
  161. setTimeout(() => {
  162. this.protocolShake = false
  163. }, 500)
  164. }
  165. return this.protocolChecked
  166. },
  167. onCancelGetProfile() {
  168. this.redirectTo(this.redirectUrl || '/pages/user/index')
  169. },
  170. onConfirmGetProfile(e) {
  171. let pagePath = '/pages/user/info-edit?avatar=' + encodeURIComponent(e.avatarUrl)
  172. if (this.redirectUrl) {
  173. pagePath += '&redirect=' + encodeURIComponent(this.redirectUrl)
  174. }
  175. this.redirectTo(pagePath)
  176. },
  177. onCheckChange(checked) {
  178. this.protocolChecked = checked
  179. },
  180. onClickPhoneLoginOverlay() {
  181. this.showPhoneLogin = false
  182. },
  183. onClearPhoneNumber() {
  184. this.phoneNumber = ''
  185. },
  186. onClearCheckCode() {
  187. this.checkCode = ''
  188. },
  189. onPhoneLoginClick() {
  190. this.checkProtocol()
  191. },
  192. onPhoneLogin(e) {
  193. const openId = uni.getStorageSync('openId')
  194. if (e.detail.code) {
  195. login({
  196. open_id: openId,
  197. code: e.detail.code,
  198. encrypted_data: e.detail.encryptedData,
  199. iv: e.detail.iv
  200. }).then(res => {
  201. if (res.data) {
  202. this.$store.commit('SET_TOKEN', res.data)
  203. this.$store.dispatch('getInfo').then(res => {
  204. const user = res.data
  205. if (!user.nick_name || !user.avatar) {
  206. this.showGetProfile = true
  207. } else {
  208. this.redirectTo(this.redirectUrl || '/pages/user/index')
  209. }
  210. })
  211. } else {
  212. this.showToast('微信服务器忙线中,稍候再试')
  213. }
  214. })
  215. }
  216. },
  217. onPhoneLoginOther() {
  218. this.showPhoneLogin = true
  219. },
  220. onPhoneSubmit() {
  221. const openId = uni.getStorageSync('openId')
  222. if (!this.checkProtocol()) {
  223. return
  224. }
  225. this.phoneNumber = this.phoneNumber.trim()
  226. this.validCode = this.validCode.trim()
  227. if (!this.phoneNumber) {
  228. this.showToast('请填写手机号')
  229. return
  230. }
  231. if (!this.validCode) {
  232. this.showToast('请填写验证码')
  233. return
  234. }
  235. this.loginLoading = true
  236. phoneLogin({
  237. phone: this.phoneNumber,
  238. valid_code: this.validCode,
  239. open_id: openId
  240. }).then(res => {
  241. console.log("手机验证码登录:", res)
  242. this.loginLoading = false
  243. if (res.data) {
  244. this.$store.commit('SET_TOKEN', res.data)
  245. this.$store.dispatch('getInfo').then(res => {
  246. const user = res.data
  247. if (!user.nick_name || !user.avatar) {
  248. this.showGetProfile = true
  249. } else {
  250. this.redirectTo(this.redirectUrl || '/pages/user/index')
  251. }
  252. })
  253. this.showPhoneLogin = false
  254. } else {
  255. this.showToast('微信服务器忙线中,稍候再试')
  256. }
  257. })
  258. }
  259. }
  260. }
  261. </script>
  262. <style lang="scss">
  263. .user-login{
  264. .main-container{
  265. @include display-flex-center;
  266. flex-direction: column;
  267. height: 100%;
  268. }
  269. .user-login-box{
  270. width: 565rpx;
  271. }
  272. .user-login-logo{
  273. width: 377rpx;
  274. height: 93rpx;
  275. margin: 0 auto;
  276. }
  277. .user-login-button{
  278. display: grid;
  279. grid-template-columns: 1fr;
  280. grid-row-gap: 22rpx;
  281. margin-top: 254rpx;
  282. }
  283. .user-login-protocol-text{
  284. margin-top: 39rpx;
  285. font-size: $fontSize2;
  286. color: #555555;
  287. line-height: 30rpx;
  288. }
  289. .user-phone-login-wrapper{
  290. display: flex;
  291. align-items: center;
  292. justify-content: center;
  293. height: 100%;
  294. }
  295. .user-phone-login{
  296. width: 670rpx;
  297. padding: 51rpx 59rpx 96rpx 59rpx;
  298. background-color: #ffffff;
  299. border-radius: 10rpx;
  300. &>view:first-child{
  301. font-size: $fontSize3;
  302. color: #333333;
  303. text-align: center;
  304. }
  305. .input-phone-number{
  306. margin-top: 54rpx;
  307. border-bottom: 1rpx solid #D9D9D9;
  308. padding-bottom: 31rpx;
  309. height: 60rpx;
  310. }
  311. .input-phone-number,.input-check-code{
  312. position: relative;
  313. font-size: $fontSize3;
  314. .clear{
  315. position: absolute;
  316. right: 10rpx;
  317. top: 10rpx;
  318. z-index: 1;
  319. }
  320. .van-icon {
  321. color: #555555;
  322. }
  323. }
  324. .input-check-code{
  325. display: flex;
  326. align-items: center;
  327. margin-top: 44rpx;
  328. .van-button{
  329. width: 205rpx;
  330. height: 80rpx;
  331. border-radius: 50rpx;
  332. }
  333. .clear{
  334. top: 26rpx;
  335. right: 250rpx;
  336. }
  337. input{
  338. flex: 1;
  339. }
  340. padding-bottom: 0;
  341. }
  342. .submit{
  343. margin-top: 50rpx;
  344. }
  345. }
  346. /* 定义振动动画 */
  347. @keyframes shake {
  348. 0% {
  349. transform: translateX(0);
  350. }
  351. 25% {
  352. transform: translateX(-10rpx);
  353. }
  354. 50% {
  355. transform: translateX(10rpx);
  356. }
  357. 75% {
  358. transform: translateX(-10rpx);
  359. }
  360. 100% {
  361. transform: translateX(0);
  362. }
  363. }
  364. .protocol-shake {
  365. display: inline-block;
  366. animation: shake 0.3s ease-in-out infinite;
  367. }
  368. }
  369. </style>