login.vue 17 KB

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