loginMobile.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. <template>
  2. <view class="container">
  3. <!-- #ifndef MP-WEIXIN -->
  4. <text class="back-btn iconfont iconziyuan2" @click="navBack"></text>
  5. <!-- #endif -->
  6. <!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
  7. <view class="wrapper">
  8. <view class="login-title">
  9. {{$L('验证码登录')}}
  10. </view>
  11. <view class="input-content">
  12. <view class="input-item">
  13. <input type="number" :value="mobile" :placeholder="$L('请输入手机号')" maxlength="11" data-key="mobile" @input="inputChange" @focus="setFocus"/>
  14. <text class="clear-account iconfont iconziyuan34" v-show="mobile&&curFocus=='mobile'" @click="clearContent('mobile')"></text>
  15. </view>
  16. <view class="input-item pwd_wrap">
  17. <input type="number" :value="smsCode" maxlength="6" :placeholder="$L('请输入短信验证码')" data-key="smsCode" @input="inputChange"
  18. @confirm="toLogin" @focus="setFocus"/>
  19. <view class="pwd-right">
  20. <text class="clear-pwd iconfont iconziyuan34" v-show="smsCode&&curFocus=='smsCode'" @click="clearContent('smsCode')"></text>
  21. <view :style="{opacity: countDownM?0.3:1}" class="sms-code-view" @click="getSmsCode">
  22. <text class="sms-code">{{countDownM?`${countDownM}s后重新获取`:'获取验证码'}}</text>
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. <button class="confirm-btn" @click="toLogin" :style="{opacity: (!(mobile&&smsCode)||logining)?0.5:1}">登录</button>
  28. <view class="login-register">
  29. <text class="mobile-login" @click="navTo('/pages/public/login',1)">{{$L('密码登陆')}}</text>
  30. <text class="register" @click="navTo('/pages/public/register',1)">{{$L('用户注册')}}</text>
  31. </view>
  32. </view>
  33. <view class="other-login">
  34. <!-- #ifdef H5 -->
  35. <view class="title" v-if="isWeiXinH5">
  36. <text >{{$L('其他登录')}}</text>
  37. </view>
  38. <!-- #endif -->
  39. <!-- #ifndef H5 -->
  40. <view class="title">
  41. <text >{{$L('其他登录')}}</text>
  42. </view>
  43. <!-- #endif -->
  44. <view class="login-method">
  45. <!-- #ifdef MP-WEIXIN -->
  46. <button class="wechat-login" v-if="canIUseGetUserProfile" @tap="getUserProfile">
  47. <image class="wechat-icon" :src="imgUrl+'wechat.png'" mode="aspectFill" />
  48. <text>{{$L('微信登录')}}</text>
  49. </button>
  50. <button class="wechat-login" v-if="!canIUseGetUserProfile" open-type="getUserInfo" @getuserinfo="getUser">
  51. <image class="wechat-icon" :src="imgUrl+'wechat.png'" mode="aspectFill" />
  52. <text>{{$L('微信登录')}}</text>
  53. </button>
  54. <!-- #endif -->
  55. <!-- #ifdef H5-->
  56. <view class="wechat-login" @tap="quickLogin" v-if="isWeiXinH5">
  57. <image class="wechat-icon" :src="imgUrl+'wechat.png'" mode="aspectFill" />
  58. <text>{{$L('微信登录')}}</text>
  59. </view>
  60. <!-- #endif -->
  61. <!-- #ifdef APP-PLUS -->
  62. <view class="wechat-login" @tap="quickLogin">
  63. <image class="wechat-icon" :src="imgUrl+'wechat.png'" mode="aspectFill" />
  64. <text>{{$L('微信登录')}}</text>
  65. </view>
  66. <!-- #endif -->
  67. </view>
  68. </view>
  69. <!-- #ifndef APP-PLUS -->
  70. <view class="agreement-part">
  71. {{$L('登录即代表您已同意')}}
  72. <text class="agreement" @click="agreement">{{$L('《用户隐私政策》')}}</text>
  73. </view>
  74. <!-- #endif -->
  75. <!-- #ifdef APP-PLUS -->
  76. <view class="agreement-part flex_row_center_center">
  77. <image @click="checkAgrement" class="register_icon" :src="show_check_icon" mode="aspectFill" />
  78. {{$L('我已阅读并同意')}}
  79. <text class="agreement" @click="agreement">{{$L('《用户隐私政策》')}}</text>
  80. </view>
  81. <!-- #endif -->
  82. </view>
  83. </template>
  84. <script>
  85. import {
  86. mapMutations
  87. } from 'vuex';
  88. export default {
  89. data() {
  90. return {
  91. imgUrl: getApp().globalData.imgUrl,
  92. mobile: '',
  93. smsCode: '',
  94. logining: false,
  95. countDownM: 0, //短信验证码倒计时
  96. timeOutId: '', //定时器的返回值
  97. curFocus:'',//当前光标所在的位置
  98. client: 1, //终端类型, 1、H5(微信内部浏览器) 2、H5(微信小程序);3、app
  99. oriUrl:'',//不带code的页面地址
  100. isWeiXinH5:true, //是否为微信浏览器h5
  101. check_agreement: false,
  102. show_check_icon: getApp().globalData.imgUrl+'register_uncheck.png',
  103. canIUseGetUserProfile: false,//是否可以使用getUserProfile获取用户信息,用于微信小程序
  104. }
  105. },
  106. onLoad(option) {
  107. this.client = this.$getLoginClient();
  108. //#ifdef MP-WEIXIN
  109. if (uni.getUserProfile) {
  110. this.canIUseGetUserProfile = true;
  111. }
  112. //#endif
  113. //#ifdef H5
  114. let code = this.$getQueryVariable('code');
  115. this.isWeiXinH5 = this.$isWeiXinBrower();
  116. if(code){
  117. let oriUrl = option.ori_url+'pages/public/loginMobile';
  118. let tmp_data = '';
  119. for(let i in option){
  120. if(i!='ori_url'){
  121. tmp_data += i+'='+option[i]+'&'
  122. }
  123. }
  124. oriUrl += '?'+tmp_data;
  125. this.oriUrl = oriUrl;
  126. if(this.$isWeiXinBrower()){
  127. //微信浏览器的话要把浏览器地址里面的code去掉
  128. history.replaceState({},'',this.oriUrl);
  129. }
  130. let tar_params = {};
  131. tar_params.source = this.client;
  132. tar_params.code = code;
  133. this.goLogin(tar_params);
  134. }
  135. //#endif
  136. },
  137. methods: {
  138. ...mapMutations(['login', 'setUserCenterData']),
  139. //注册协议点击事件
  140. checkAgrement() {
  141. this.check_agreement = !this.check_agreement;
  142. this.show_check_icon = this.check_agreement ? getApp().globalData.imgUrl+'register_checked.png' : getApp().globalData.imgUrl+'register_uncheck.png';
  143. },
  144. getUser(e) {
  145. if (e.detail.errMsg == "getUserInfo:ok") {
  146. let userinfo = e.detail.userInfo;
  147. this.getWxXcxCoce(userinfo);
  148. }
  149. },
  150. //微信小程序根据用户信息获取code
  151. getWxXcxCoce(userinfo) {
  152. uni.setStorageSync('fromurl', '/pages/user/user');
  153. let {client} = this;
  154. let _this = this;
  155. uni.showLoading({
  156. title: '正在请求...',
  157. mask: true
  158. });
  159. uni.login({
  160. success: code => {
  161. let tar_params = {};
  162. tar_params.source = client;
  163. tar_params.code = code.code;
  164. tar_params.userInfo = JSON.stringify(userinfo);
  165. _this.goLogin(tar_params);
  166. }
  167. });
  168. },
  169. getUserProfile(e) {
  170. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  171. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  172. uni.getUserProfile({
  173. desc: '用于完善个人信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  174. success: (res) => {
  175. if(res.errMsg == "getUserProfile:ok"){
  176. let userinfo = res.userInfo;
  177. this.getWxXcxCoce(userinfo);
  178. }
  179. }
  180. })
  181. },
  182. //授权登录
  183. quickLogin (){
  184. // #ifdef APP-PLUS
  185. if (!this.check_agreement) {
  186. this.$api.msg('请同意用户隐私政策!');
  187. return false;
  188. }
  189. // #endif
  190. uni.setStorageSync('fromurl', '/pages/user/user');
  191. let {client} = this;
  192. let _this = this;
  193. //#ifdef APP-PLUS
  194. uni.login({
  195. provider: 'weixin',
  196. success: function (loginRes) {
  197. if(loginRes.errMsg == 'login:ok'){
  198. //授权登录成功
  199. // 获取用户信息
  200. uni.getUserInfo({
  201. provider: 'weixin',
  202. success: function (infoRes) {
  203. let tar_params = {};
  204. tar_params.unionid = loginRes.authResult.unionid;
  205. tar_params.openid = loginRes.authResult.openid;
  206. tar_params.userInfo = JSON.stringify(infoRes.userInfo);
  207. tar_params.source = client;
  208. _this.goLogin(tar_params);
  209. }
  210. });
  211. }
  212. }
  213. });
  214. //#endif
  215. //#ifdef H5
  216. let tar_url = location.href;
  217. tar_url += location.href.indexOf('?')>-1?'&':'?';
  218. tar_url += 'ori_url='+location.href;
  219. let uricode = encodeURIComponent(tar_url)
  220. window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + getApp().globalData.h5AppId +
  221. '&redirect_uri=' + uricode + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
  222. //#endif
  223. },
  224. //登录 data为除购物车信息外的接口参数,对象类型
  225. goLogin(data){
  226. // #ifdef APP-PLUS
  227. if (!this.check_agreement) {
  228. this.$api.msg('请同意用户隐私政策!');
  229. return false;
  230. }
  231. // #endif
  232. let _this = this;
  233. let param = {};
  234. param.url = 'v3/member/front/login/wechat/login';
  235. param.data = {...data};
  236. //如果有缓存的购物车数据,登录需要把数据同步,并清除本地缓存
  237. let local_cart_list = uni.getStorageSync('cart_list')
  238. let cartInfo = []
  239. if (local_cart_list) {
  240. local_cart_list.cartInfoList.map(item => {
  241. if (item.isChecked == 1) {
  242. cartInfo.push({
  243. goodsId: item.goodsId,
  244. productId: item.productId,
  245. buyNum: item.buyNum,
  246. productPrice: item.productPrice,
  247. checked: 1
  248. })
  249. }
  250. })
  251. param.data.cartInfo = JSON.stringify(cartInfo);
  252. }
  253. param.method = 'POST';
  254. this.$request(param).then(res => {
  255. if (res.state == 200) {
  256. uni.removeStorage({
  257. key: 'cart_list'
  258. }); //清除购物车数据
  259. if(res.data.redirect == undefined){
  260. res.data.loginTime = Date.parse(new Date());//登录时间
  261. _this.login(res.data);
  262. //登录成功 获取个人中心的数据
  263. _this.$request({
  264. url: 'v3/member/front/member/memberInfo',
  265. }).then(result => {
  266. _this.setUserCenterData(result.data);
  267. _this.$loginGoPage();
  268. }).catch((e) => {})
  269. }else if(res.data.redirect != undefined){
  270. //用户未注册,需要绑定手机号进行注册
  271. uni.navigateTo({
  272. url: '/pages/public/bindMobile?code='+res.data.bindKey
  273. })
  274. }
  275. } else {
  276. //错误提示
  277. _this.$api.msg(res.msg);
  278. }
  279. uni.hideLoading();
  280. }).catch((e) => {
  281. uni.hideLoading();
  282. })
  283. },
  284. //光标聚焦事件
  285. setFocus(e){
  286. this.curFocus = e.currentTarget.dataset.key;
  287. },
  288. inputChange(e) {
  289. const key = e.currentTarget.dataset.key;
  290. this[key] = e.detail.value;
  291. },
  292. navBack() {
  293. uni.navigateBack();
  294. },
  295. toLogin() {
  296. const {
  297. mobile,
  298. smsCode,
  299. logining
  300. } = this;
  301. if (!(mobile && smsCode) || logining) {
  302. return;
  303. }
  304. // #ifdef APP-PLUS
  305. if (!this.check_agreement) {
  306. this.$api.msg('请同意用户隐私政策!');
  307. return false;
  308. }
  309. // #endif
  310. let param = {};
  311. param.url = 'v3/member/front/login/freeSecretLogin';
  312. param.data = {};
  313. param.data.mobile = mobile;
  314. param.data.smsCode = smsCode;
  315. if (!this.$checkMobile(mobile)) {
  316. return false
  317. }
  318. //如果有缓存的购物车数据,登录需要把数据同步,并清除本地缓存
  319. let local_cart_data = uni.getStorageSync('cart_data'); //这里需要同步获取
  320. if (local_cart_data) {
  321. param.data.cartInfo = JSON.stringify(local_cart_data);
  322. }
  323. param.method = 'POST';
  324. this.logining = true;
  325. this.$request(param).then(res => {
  326. if (res.state == 200) {
  327. uni.removeStorage({
  328. key: 'cart_data'
  329. }); //清除购物车数据
  330. //获取个人中心的数据
  331. res.data.loginTime = Date.parse(new Date());//登录时间
  332. this.login(res.data);
  333. this.$request({
  334. url: 'v3/member/front/member/memberInfo',
  335. }).then(result => {
  336. this.setUserCenterData(result.data);
  337. this.$loginGoPage();
  338. }).catch((e) => {})
  339. } else {
  340. //错误提示
  341. this.$api.msg(res.msg);
  342. }
  343. this.logining = false;
  344. }).catch((e) => {})
  345. },
  346. //清空输入的内容
  347. clearContent(type) {
  348. this[type] = '';
  349. },
  350. //获取短信验证码
  351. getSmsCode() {
  352. if (this.countDownM) {
  353. return;
  354. }
  355. if (!this.$checkMobile(this.mobile)) {
  356. return false
  357. } else {
  358. let param = {};
  359. param.url = 'v3/msg/front/commons/smsCode';
  360. param.data = {};
  361. param.data.mobile = this.mobile;
  362. param.data.type = 'free';
  363. this.$request(param).then(res => {
  364. this.$api.msg(res.msg)
  365. if (res.state == 200) {
  366. this.countDownM = 60;
  367. this.countDown();
  368. }
  369. })
  370. }
  371. },
  372. //账号登录跳转事件
  373. loginByAccount() {
  374. uni.redirectTo({
  375. url: './login'
  376. });
  377. },
  378. //跳转事件 type:跳转类型,1为redirectTo 2为navigateTo
  379. navTo(url, type) {
  380. if (type == 1) {
  381. uni.redirectTo({
  382. url
  383. })
  384. } else if (type == 2) {
  385. uni.navigateTo({
  386. url
  387. })
  388. }
  389. },
  390. //倒计时
  391. countDown() {
  392. this.countDownM--;
  393. if (this.countDownM == 0) {
  394. clearTimeout(this.timeOutId);
  395. } else {
  396. this.timeOutId = setTimeout(this.countDown, 1000);
  397. }
  398. },
  399. //跳转协议页面
  400. agreement() {
  401. uni.navigateTo({
  402. url: '/pages/privacyPolicy/privacyPolicy'
  403. })
  404. },
  405. },
  406. }
  407. </script>
  408. <style lang='scss'>
  409. page {
  410. background: #fff;
  411. width: 750rpx;
  412. margin: 0 auto;
  413. }
  414. .container {
  415. position: relative;
  416. width: 750rpx;
  417. height: 100vh;
  418. overflow: hidden;
  419. background: #fff;
  420. }
  421. .wrapper {
  422. position: relative;
  423. z-index: 90;
  424. background: #fff;
  425. padding-bottom: 40upx;
  426. }
  427. .back-btn {
  428. margin-left: 40rpx;
  429. margin-top: 40rpx;
  430. /* #ifndef H5 */
  431. margin-top: 88rpx;
  432. /* #endif */
  433. font-size: 32rpx;
  434. color: $main-font-color;
  435. display: inline-block;
  436. }
  437. .login-title {
  438. position: relative;
  439. margin-top: 90rpx;
  440. margin-bottom: 70rpx;
  441. margin-left: 65rpx;
  442. font-size: 36rpx;
  443. color: #333;
  444. font-weight: bold;
  445. &:after {
  446. position: absolute;
  447. left: 0;
  448. bottom: -10rpx;
  449. content: '';
  450. width: 76rpx;
  451. height: 6rpx;
  452. background: linear-gradient(90deg, rgba(252, 28, 28, 1) 0%, rgba(255, 138, 0, 0) 100%);
  453. }
  454. }
  455. .input-content {
  456. padding: 0 65rpx;
  457. }
  458. .input-item {
  459. display: flex;
  460. flex-direction: column;
  461. align-items: flex-start;
  462. justify-content: center;
  463. height: 80rpx;
  464. margin-bottom: 50upx;
  465. border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  466. position: relative;
  467. input {
  468. color: #2D2D2D;
  469. font-size: 30rpx;
  470. }
  471. .clear-account {
  472. position: absolute;
  473. right: 6rpx;
  474. top: 28rpx;
  475. font-size: 26rpx;
  476. color: #ddd;
  477. }
  478. &:last-child {
  479. margin-bottom: 0;
  480. .pwd-right {
  481. position: absolute;
  482. right: 6rpx;
  483. top: 6rpx;
  484. display: flex;
  485. align-items: center;
  486. .clear-pwd {
  487. font-size: 26rpx;
  488. color: #ddd;
  489. }
  490. .sms-code-view {
  491. border: 1px solid $main-color;
  492. padding: 14rpx;
  493. border-radius: 6rpx;
  494. line-height: 0;
  495. margin-left: 20rpx;
  496. .sms-code {
  497. color: $main-color;
  498. font-size: 24rpx;
  499. line-height: 24rpx;
  500. }
  501. }
  502. }
  503. }
  504. .tit {
  505. height: 50upx;
  506. line-height: 56upx;
  507. font-size: $font-sm+2upx;
  508. color: $font-color-base;
  509. }
  510. input {
  511. height: 60upx;
  512. font-size: $font-base + 2upx;
  513. color: $font-color-dark;
  514. }
  515. }
  516. .confirm-btn {
  517. width: 620rpx;
  518. height: 88rpx;
  519. line-height: 88rpx;
  520. margin-top: 90rpx;
  521. background: linear-gradient(90deg, rgba(252, 31, 29, 1) 0%, rgba(253, 115, 38, 1) 100%);
  522. box-shadow: 0px 3rpx 14rpx 1rpx rgba(253, 38, 29, 0.26);
  523. border-radius: 44rpx;
  524. color: #fff;
  525. font-size: 36rpx;
  526. }
  527. .other-login {
  528. position: absolute;
  529. left: 0;
  530. bottom: 140rpx;
  531. width: 100%;
  532. display: flex;
  533. flex-direction: column;
  534. .title {
  535. display: flex;
  536. justify-content: center;
  537. align-items: center;
  538. &:before {
  539. content: ' ';
  540. width: 150rpx;
  541. height: 1rpx;
  542. background: #CBCBCB;
  543. }
  544. &:after {
  545. content: ' ';
  546. width: 150rpx;
  547. height: 1rpx;
  548. background: #CBCBCB;
  549. }
  550. text {
  551. color: #999999;
  552. font-size: 26rpx;
  553. margin: 0 20rpx;
  554. }
  555. }
  556. .login-method {
  557. display: flex;
  558. justify-content: center;
  559. margin-top: 20rpx;
  560. .wechat-login {
  561. display: flex;
  562. flex-direction: column;
  563. align-items: center;
  564. width: 100%;
  565. background: transparent;
  566. line-height: auto;
  567. height: auto;
  568. &::after {
  569. border: none;
  570. }
  571. .wechat-icon {
  572. width: 110rpx;
  573. height: 110rpx;
  574. }
  575. text {
  576. color: #666666;
  577. font-size: 26rpx;
  578. }
  579. }
  580. }
  581. }
  582. .agreement-part {
  583. position: absolute;
  584. left: 0;
  585. bottom: 60rpx;
  586. width: 100%;
  587. font-size: 26rpx;
  588. color: #999999;
  589. text-align: center;
  590. .register_icon {
  591. width: 46rpx;
  592. height: 46rpx;
  593. }
  594. .agreement {
  595. color: #FC1E1E;
  596. border-bottom: 1rpx solid #FC1E1E;
  597. }
  598. }
  599. .login-register {
  600. display: flex;
  601. justify-content: center;
  602. margin-top: 33rpx;
  603. .mobile-login {
  604. color: #2D2D2D;
  605. font-size: 28rpx;
  606. line-height: 34rpx;
  607. border-right: 1px solid rgba(0, 0, 0, .1);
  608. padding-right: 30rpx;
  609. margin-right: 30rpx;
  610. }
  611. .register {
  612. color: #FC1C1C;
  613. font-size: 28rpx;
  614. line-height: 34rpx;
  615. }
  616. }
  617. </style>