Login.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. <template><page-meta :root-font-size="fontSize+'px'"></page-meta>
  2. <home-base :show="false">
  3. <view class="div container">
  4. <view class="div common-header-wrap">
  5. <view :style="'height:'+navHeight+'px'"></view>
  6. <view class="common-header-holder"></view>
  7. <view class="common-header-fixed">
  8. <title-header />
  9. <uni-nav-bar title="登录" class="common-header" left-icon="back" @clickLeft="goBack()">
  10. <!-- <view v-if="config && (config.sms_register==1 || config.member_normal_register==1)" class="div common-btn btn"
  11. slot="right" @click="onSignup">注册</view> -->
  12. </uni-nav-bar>
  13. </view>
  14. </view>
  15. <view class="div main-content">
  16. <view class="div topList">
  17. <view class="div list">
  18. <view class="div item" v-for="(item, index) in items" :key="index" v-on:click="onClickItem(index)">
  19. <label class="title" v-bind:class="{
  20. active: index === currentIndex,
  21. normal: index !== currentIndex
  22. }">{{ getTitle(item) }}</label>
  23. </view>
  24. </view>
  25. </view>
  26. <view class="div top-wrapper">
  27. <flex-line class="input-wrapper" v-if="currentIndex === 0" :show-border="true">
  28. <input v-model="username" placeholder="用户名/邮箱/手机号" type="text" />
  29. </flex-line>
  30. <flex-line v-if="currentIndex !== 0" class="input-wrapper" :show-border="true">
  31. <input v-model="mobileusername" placeholder="请输入手机号"
  32. :attr="{oninput: 'if(value.length>11)value=value.slice(0,11)'}" type="text" />
  33. </flex-line>
  34. <!-- <flex-line v-if="currentIndex === 0 && config.captcha_status_login=='1'" class="input-wrapper"
  35. :show-border="true">
  36. <input v-model="pictureCode" placeholder="请输入图片验证码" maxlength="6" />
  37. <image mode="aspectFit" slot="right" @click="changePictureCode" :src="pictureCodeUrl" class="img countdown">
  38. </flex-line> -->
  39. <flex-line v-if="currentIndex !== 0" class="input-wrapper" :show-border="true">
  40. <input v-model="code" placeholder="请输入短信验证码" type="number"
  41. :attr="{ oninput: 'if(value.length>6)value=value.slice(0,6)' }" />
  42. <view slot="right" class="div common-btn send-btn" @click="sendVerifyCodeMobile">{{sendStateTextMobile}}
  43. </view>
  44. </flex-line>
  45. <flex-line v-if="currentIndex === 0" class="input-wrapper" :show-border="true">
  46. <input type="password" v-model="password" placeholder="请输入密码" />
  47. </flex-line>
  48. </view>
  49. <view class="div common-btn ds-button-large mt-10 mb-10" @click="signin">登录</view>
  50. <view class="div retrieve-wrapper" style="display: flex;justify-content: space-between;">
  51. <text class="span retrieve-item" @click="onRetrieve">忘记密码?</text>
  52. <text class="span retrieve-item" v-if="config && (config.sms_register==1 || config.member_normal_register==1)" @click="onSignup">立即注册</text>
  53. </view>
  54. </view>
  55. <view class="div bottom-wrapper" v-if="isShowWechat || isShowSina || isShowQQ">
  56. <view class="div auth-title-wrapper">
  57. <view class="div auth-line"></view>
  58. <label class="auth-title">第三方登录</label>
  59. <view class="div auth-line"></view>
  60. </view>
  61. <view class="div auth-bottom-wrapper">
  62. <view class="div auth-item" v-if="isShowWechat" @click="onWechat">
  63. <view class="div auth-item-icon">
  64. <image mode="aspectFit" class="img icon" src="../../../static/image/weixin.png" />
  65. </view>
  66. <label class="auth-title auth-item-title">微信</label>
  67. </view>
  68. <view class="div auth-item" v-if="isShowSina" @click="onSina">
  69. <view class="div auth-item-icon">
  70. <image mode="aspectFit" class="img icon" src="../../../static/image/weibo.png" />
  71. </view>
  72. <label class="auth-title auth-item-title">微博</label>
  73. </view>
  74. <view class="div auth-item" v-if="isShowQQ" @click="onTencent">
  75. <view class="div auth-item-icon">
  76. <image mode="aspectFit" class="img icon" src="../../../static/image/qq.png" />
  77. </view>
  78. <label class="auth-title auth-item-title">QQ</label>
  79. </view>
  80. </view>
  81. </view>
  82. </view>
  83. </home-base>
  84. </template>
  85. <script>
  86. import {getFontSize} from '@/util/common'
  87. import TitleHeader from '../../TitleHeader'
  88. import HomeBase from '../HomeBase'
  89. import { urlencode } from '@/util/common'
  90. import {
  91. login,
  92. wechatLogin,
  93. LoginByMobile,
  94. getThirdLogin,
  95. wechatcheck,
  96. wechatUser
  97. } from '../../../api/memberLogin'
  98. import {
  99. isWechat
  100. } from '../../../util/wechat'
  101. import {
  102. getSmsCaptcha,
  103. checkPictureCaptcha
  104. } from '../../../api/common'
  105. import {
  106. mapMutations,
  107. mapActions,
  108. mapState
  109. } from 'vuex'
  110. import flexLine from '../../flexLine'
  111. import {
  112. env
  113. } from '../../../static/config'
  114. export default {
  115. name: 'Login',
  116. data(){
  117. return {
  118. navHeight: 0,
  119. clear: 0,
  120. isTokenInvalid: 0,
  121. isFromInfoEdit: 0,
  122. isFromShare: 0,
  123. referrer: '',
  124. pictureCodeUrl: '',
  125. pictureCode: '',
  126. code: '',
  127. verifyCodeMobile: '',
  128. canSendMobile: true,
  129. timeIntervalMobile: false,
  130. sendStateTextMobile: '发送',
  131. mobileusername: '',
  132. username: '',
  133. password: '',
  134. currentIndex: 0,
  135. items: [{
  136. id: 1,
  137. title: '普通登录'
  138. }]
  139. }
  140. },
  141. components:{
  142. TitleHeader,
  143. HomeBase,
  144. flexLine
  145. },
  146. computed:{
  147. fontSize(){
  148. return getFontSize()
  149. },
  150. ...mapState({
  151. inviter_id: state => state.member.inviterId,
  152. config: state => state.config.config
  153. }),
  154. isShowWechat: function() {
  155. if(this.config && this.config.weixin_isuse == 1){
  156. // #ifdef H5
  157. if (isWechat()) {
  158. return true
  159. }
  160. return false
  161. // #endif
  162. // #ifndef H5
  163. return true
  164. // #endif
  165. }else{
  166. return false
  167. }
  168. },
  169. isShowSina: function() {
  170. // #ifdef H5
  171. if (!isWechat() && !this.uniapp && this.config && this.config.sina_isuse == 1) {
  172. return true
  173. }
  174. // #endif
  175. return false
  176. },
  177. isShowQQ: function() {
  178. // #ifdef H5
  179. if (!isWechat() && !this.uniapp && this.config && this.config.qq_isuse == 1) {
  180. return true
  181. }
  182. // #endif
  183. return false
  184. }
  185. },
  186. watch: {
  187. mobileusername(newVal, oldVal) {
  188. newVal = newVal.replace(/[^\d]/g, '')
  189. if (!newVal) {
  190. this.$nextTick(() => {
  191. this.mobileusername = newVal
  192. })
  193. }
  194. },
  195. },
  196. beforeDestroy: function() {
  197. clearInterval(this.timeIntervalMobile)
  198. },
  199. onLoad: function(option) {
  200. this.isTokenInvalid = option.isTokenInvalid
  201. this.isFromInfoEdit = option.isFromInfoEdit
  202. this.isFromShare = option.isFromShare
  203. this.clear = option.clear
  204. this.referrer = option.referrer
  205. uni.hideLoading()
  206. if(option.id){
  207. this.getThirdLogin(option.id)
  208. }
  209. if (this.clear) {
  210. this.memberLogout()
  211. }
  212. this.fetchConfig({}).then(
  213. res => {
  214. var config = res.result.config_list
  215. if (config.sms_login == 1) {
  216. this.items.push({
  217. id: 2,
  218. title: '手机登录'
  219. })
  220. }
  221. },
  222. error => {
  223. uni.showToast({
  224. icon: 'none',
  225. title: error.message
  226. })
  227. }
  228. )
  229. },
  230. mounted(){
  231. // #ifdef MP-WEIXIN
  232. this.navHeight = uni.getMenuButtonBoundingClientRect().height
  233. // #endif
  234. let isTokenInvalid = this.isTokenInvalid
  235. if (isTokenInvalid) {
  236. uni.showToast({
  237. icon: 'none',
  238. title: '登录过期'
  239. })
  240. }
  241. // this.changePictureCode()
  242. },
  243. methods: {
  244. getThirdLogin(id){
  245. getThirdLogin(id).then(res => {
  246. let user_info = res.result.user_info
  247. let key = res.result.key
  248. if (user_info && key) {
  249. this.saveAuthInfo({
  250. token: key,
  251. info: user_info
  252. })
  253. this.goHome()
  254. }
  255. })
  256. },
  257. goBack() {
  258. let referrer
  259. var pages = getCurrentPages()
  260. var page = pages[pages.length - 2]
  261. if (page) {
  262. referrer = '/'+page.route
  263. }
  264. if (!referrer || (referrer.search('pages/member') != -1)) {
  265. uni.reLaunch({
  266. url: '/pages/home/index/Index'
  267. });
  268. }else{
  269. uni.navigateBack({
  270. delta: 1
  271. })
  272. }
  273. },
  274. ...mapMutations({
  275. saveAuthInfo: 'memberLogin',
  276. memberLogout: 'memberLogout'
  277. }),
  278. ...mapActions({
  279. fetchConfig: 'fetchConfig'
  280. }),
  281. changePictureCode() {
  282. this.pictureCodeUrl = env.API_HOST + '/Seccode/makecode?r=' + Math.random()
  283. },
  284. sendVerifyCodeMobile() {
  285. if (!this.mobileusername) {
  286. uni.showToast({
  287. icon: 'none',
  288. title: '请先输入手机号'
  289. })
  290. return
  291. }
  292. if (!this.canSendMobile) {
  293. return
  294. }
  295. getSmsCaptcha(2, this.mobileusername).then(res => {
  296. this.canSendMobile = false
  297. let second = 60
  298. uni.showToast({
  299. icon: 'none',
  300. title: res.message
  301. })
  302. let _this = this
  303. this.timeIntervalMobile = setInterval(function() {
  304. if (second <= 0) {
  305. _this.canSendMobile = true
  306. _this.sendStateTextMobile = '发送'
  307. clearInterval(_this.timeIntervalMobile)
  308. } else {
  309. _this.sendStateTextMobile = second + 's'
  310. }
  311. second--
  312. }, 1000)
  313. }).catch(function(error) {
  314. uni.showToast({
  315. icon: 'none',
  316. title: error.message
  317. })
  318. })
  319. },
  320. getTitle(item) {
  321. return item ? item.title : ''
  322. },
  323. isShowLine(index) {
  324. return index === this.currentIndex
  325. },
  326. onClickItem(index) {
  327. if (this.currentIndex !== index) {
  328. this.currentIndex = index
  329. }
  330. },
  331. onTencent() {
  332. window.location.href = env.API_HOST + '/api/oa_qq'
  333. },
  334. onSina() {
  335. window.location.href = env.API_HOST + '/api/oa_sina'
  336. },
  337. signin() {
  338. if (this.currentIndex == 0) {
  339. let username = this.username
  340. let password = this.password
  341. if (username.length === 0) {
  342. uni.showToast({
  343. icon: 'none',
  344. title: '请输入用户名/邮箱/手机号'
  345. })
  346. return
  347. }
  348. // TODO: 用户名(为手机号或邮箱)校验
  349. if (password.length === 0) {
  350. uni.showToast({
  351. icon: 'none',
  352. title: '请输入密码'
  353. })
  354. return
  355. }
  356. if (password.length < 6) {
  357. uni.showToast({
  358. icon: 'none',
  359. title: '至少输入6位密码'
  360. })
  361. return
  362. }
  363. uni.showLoading({
  364. title: '加载中'
  365. })
  366. login(username, password, this.pictureCode).then(
  367. response => {
  368. uni.hideLoading()
  369. this.saveAuthInfo({
  370. token: response.result.token,
  371. info: response.result.info
  372. })
  373. this.goHome()
  374. },
  375. error => {
  376. uni.hideLoading()
  377. uni.showToast({
  378. icon: 'none',
  379. title: error.message
  380. })
  381. }
  382. )
  383. } else {
  384. let mobileusername = this.mobileusername
  385. if (mobileusername.length != 11) {
  386. uni.showToast({
  387. icon: 'none',
  388. title: '请输入11位手机号'
  389. })
  390. return
  391. }
  392. uni.showLoading({
  393. title: '加载中'
  394. })
  395. LoginByMobile(mobileusername, this.code).then(
  396. response => {
  397. uni.hideLoading()
  398. this.saveAuthInfo({
  399. token: response.result.token,
  400. info: response.result.info
  401. })
  402. this.goHome()
  403. },
  404. error => {
  405. uni.hideLoading()
  406. uni.showToast({
  407. icon: 'none',
  408. title: error.message
  409. })
  410. }
  411. )
  412. }
  413. },
  414. goHome() {
  415. let referrer = this.getReferrer()
  416. if (referrer) { // 返回之前的页面
  417. referrer = decodeURIComponent(referrer)
  418. uni.redirectTo({
  419. url: referrer
  420. })
  421. } else {
  422. uni.redirectTo({
  423. url: '/pages/member/index/Index'
  424. })
  425. }
  426. },
  427. getReferrer() {
  428. let referrer = this.referrer
  429. if (!referrer) {
  430. var pages = getCurrentPages()
  431. var page = pages[pages.length - 2]
  432. if (page) {
  433. referrer = '/'+page.route+(page.options?('?'+urlencode(page.options)):'')
  434. }
  435. }
  436. if (referrer && (referrer.search('memberlogin') != -1 || referrer.search('memberregister') != -1 || referrer
  437. .search('memberbind') != -1)) {
  438. referrer = '/pages/member/index/Index'
  439. }
  440. return referrer
  441. },
  442. onSignup() {
  443. uni.navigateTo({
  444. url: '/pages/home/memberregister/Register'
  445. })
  446. },
  447. onRetrieve() {
  448. uni.navigateTo({
  449. url: '/pages/home/memberforget/Forget'
  450. })
  451. },
  452. onWechat() {
  453. let referrer = this.getReferrer()
  454. if (referrer) { // 返回之前的页面
  455. referrer = decodeURIComponent(referrer)
  456. }
  457. // #ifdef H5
  458. let ref = encodeURIComponent(referrer)
  459. wechatLogin(ref?ref:'', this.inviter_id).then(res => {
  460. window.location.href = res.result
  461. }).catch(function(error) {
  462. uni.showToast({
  463. icon: 'none',
  464. title: error.message
  465. })
  466. })
  467. // #endif
  468. // #ifndef H5
  469. var _this = this
  470. uni.login({
  471. provider: 'weixin',
  472. onlyAuthorize:true,
  473. success: function(res) {
  474. var from = ''
  475. // #ifdef APP-PLUS
  476. from = 'app'
  477. // #endif
  478. // #ifdef MP-WEIXIN
  479. from = 'miniprogram'
  480. // #endif
  481. wechatUser({code:res.code,from:from}).then(res=>{
  482. wechatcheck({
  483. inviter_id: _this.inviter_id,
  484. openid: res.result.openid,
  485. unionid: res.result.unionid,
  486. access_token: res.result.access_token,
  487. from: from,
  488. }).then(res => {
  489. if(res.result.has_user){
  490. _this.getThirdLogin(res.result.unionid)
  491. }else{
  492. uni.navigateTo({
  493. url: res.result.ret_url
  494. })
  495. }
  496. }).catch(error => {
  497. uni.showToast({
  498. icon: 'none',
  499. title: error.message
  500. })
  501. })
  502. }).catch(error=>{
  503. uni.showToast({
  504. icon: 'none',
  505. title: error.message
  506. })
  507. })
  508. },
  509. fail: function(err) {
  510. uni.showToast({
  511. icon: 'loading',
  512. title: '登录失败'
  513. })
  514. }
  515. });
  516. // #endif
  517. }
  518. }
  519. }
  520. </script>
  521. <style scoped lang='scss'>
  522. .common-header {
  523. .btn {
  524. background: #000;
  525. color: #fff;
  526. box-shadow: 0px 2px 4px #d2d2d2;
  527. }
  528. }
  529. .ds-button-large {
  530. margin: 0 $pageSpace
  531. }
  532. .container {
  533. flex: 1;
  534. display: flex;
  535. flex-direction: column;
  536. justify-content: flex-start;
  537. align-items: stretch;
  538. .main-content {
  539. background: #fff;
  540. padding: 1rem 0;
  541. }
  542. .countdown {
  543. min-width: 4rem;
  544. }
  545. .send-btn {
  546. border: 1px solid $primaryColor;
  547. color: $primaryColor;
  548. min-width: 2rem
  549. }
  550. .top-wrapper {
  551. .input-wrapper {
  552. display: flex;
  553. align-content: center;
  554. align-items: center;
  555. padding-left: 0.4rem;
  556. background-color: #fff;
  557. height: 2.2rem;
  558. .img {
  559. height: 1.75rem;
  560. width: auto;
  561. margin: 0;
  562. padding: 0;
  563. }
  564. input {
  565. flex: 1;
  566. font-size: $subFontSize;
  567. }
  568. }
  569. }
  570. }
  571. .retrieve-wrapper {
  572. margin: 0 $pageSpace;
  573. display: flex;
  574. flex-direction: row;
  575. font-size: $h2;
  576. color: $primaryColor
  577. }
  578. .bottom-wrapper {
  579. display: flex;
  580. flex: 1;
  581. flex-direction: column;
  582. justify-content: flex-end;
  583. align-items: stretch;
  584. }
  585. .auth-wrapper {
  586. display: flex;
  587. flex-direction: column;
  588. justify-content: flex-start;
  589. align-items: center;
  590. height: 13rem;
  591. }
  592. .auth-title-wrapper {
  593. height: 1rem;
  594. display: flex;
  595. flex-direction: row;
  596. justify-content: flex-start;
  597. align-items: center;
  598. margin-bottom: 2rem;
  599. margin-top: 1rem;
  600. }
  601. .auth-title {
  602. color: $descTextColor;
  603. font-size: $subFontSize;
  604. }
  605. .auth-line {
  606. display: flex;
  607. flex: 1;
  608. height: 1px;
  609. background-color: #eee;
  610. margin-left: 0.8rem;
  611. margin-right: 0.8rem;
  612. }
  613. .auth-item {
  614. display: flex;
  615. flex-direction: column;
  616. justify-content: flex-start;
  617. align-items: center;
  618. }
  619. .auth-item-icon {
  620. width: 3rem;
  621. height: 3rem;
  622. border-radius: 50%;
  623. background: #fff;
  624. font-size: 3rem;
  625. box-shadow: 0px 4px 4px #e0e0e0;
  626. display: flex;
  627. justify-content: center;
  628. align-items: center;
  629. .icon {
  630. width: 60%;
  631. }
  632. }
  633. .auth-item-title {
  634. margin-top: 0.6rem;
  635. color: $formInputColor
  636. }
  637. .auth-bottom-wrapper {
  638. display: flex;
  639. flex-direction: row;
  640. justify-content: space-around;
  641. align-items: center;
  642. margin-left: 0.75rem;
  643. margin-right: 0.75rem;
  644. margin-bottom: 1rem;
  645. }
  646. .topList {
  647. height: 2rem;
  648. padding: 1rem $pageSpace;
  649. .list {
  650. height: 100%;
  651. display: flex;
  652. flex-direction: row;
  653. justify-content: flex-start;
  654. align-content: center;
  655. align-items: stretch;
  656. background-color: #fff;
  657. }
  658. .item {
  659. padding: 0 .5rem;
  660. position: relative;
  661. display: flex;
  662. flex-direction: column;
  663. justify-content: center;
  664. align-items: center;
  665. }
  666. .title {
  667. text-align: center;
  668. font-size: $h2;
  669. color: $formInputColor;
  670. }
  671. .active {
  672. color: $primaryColor;
  673. font-size: 1.1rem;
  674. }
  675. .normal {
  676. color: #404245;
  677. }
  678. }
  679. .top-wrapper {
  680. margin: 0 $pageSpace;
  681. }
  682. </style>