index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <template>
  2. <view :style="colorStyle" class="wrapper">
  3. <view class="bag">
  4. <img :src="`../static/login-bg_${colorStatus}.jpg`" alt="" srcset="">
  5. </view>
  6. <view class="system-height" :style="{ height: statusBarHeight }"></view>
  7. <!-- #ifdef MP -->
  8. <view class="title-bar" style="height: 43px;">
  9. <view class="icon" @click="back" v-if="!isHome">
  10. <image src="../static/left.png"></image>
  11. </view>
  12. <view class="icon" @click="home" v-else>
  13. <image src="../static/home.png"></image>
  14. </view>
  15. {{$t(`商城登录`)}}
  16. </view>
  17. <!-- #endif -->
  18. <view class="merchant-msg">
  19. <img :src="configData.wap_login_logo" />
  20. <view class="name">
  21. {{configData.site_name}}
  22. </view>
  23. </view>
  24. <view class="wechat_login">
  25. <view class="btn-wrapper">
  26. <!-- #ifdef H5 -->
  27. <button hover-class="none" @click="wechatLogin" class="bg-theme btn1">{{$t(`微信登录`)}}</button>
  28. <!-- #endif -->
  29. <!-- #ifdef MP -->
  30. <template v-if="configData.wechat_auth_switch">
  31. <button class="bg-theme btn1" v-if="bindPhone" open-type="getPhoneNumber"
  32. @getphonenumber="getphonenumber">{{$t(`授权登录`)}}</button>
  33. <button class="bg-theme btn1" v-else-if="!bindPhone" @click="getAuthLogin">
  34. {{$t(`授权登录`)}}
  35. </button>
  36. </template>
  37. <button v-if="configData.phone_auth_switch" hover-class="none" @click="phoneLogin"
  38. class="btn2">{{$t(`手机号登录`)}}</button>
  39. <!-- #endif -->
  40. </view>
  41. </view>
  42. <view class="protocol" v-if="!canGetPrivacySetting">
  43. <checkbox-group @click.stop='ChangeIsDefault'>
  44. <checkbox :class="inAnimation?'trembling':''" @animationend='inAnimation=false'
  45. :checked="protocol ? true : false" /> <text @click.stop='ChangeIsDefault'>{{$t(`已阅读并同意`)}}</text>
  46. <text class="main-color" @click.stop="privacy(4)">{{$t(`《用户协议》`)}}</text>
  47. {{$t(`与`)}}<text class="main-color" @click.stop="privacy(3)">{{$t(`《隐私协议》`)}}</text>
  48. </checkbox-group>
  49. </view>
  50. <block v-if="isUp">
  51. <mobileLogin :isUp="isUp" :canClose="canClose" @close="maskClose" :authKey="authKey"
  52. @wechatPhone="wechatPhone"></mobileLogin>
  53. </block>
  54. <block v-if="isPhoneBox">
  55. <routinePhone :logoUrl="logoUrl" :isPhoneBox="isPhoneBox" @loginSuccess="bindPhoneClose" :authKey="authKey">
  56. </routinePhone>
  57. </block>
  58. <block>
  59. <editUserModal :isShow="isShow" @closeEdit="closeEdit" @editSuccess="editSuccess">
  60. </editUserModal>
  61. </block>
  62. <!-- #ifdef MP -->
  63. <privacyAgreementPopup v-if="canGetPrivacySetting" @onReject="onReject" @onAgree="onAgree">
  64. </privacyAgreementPopup>
  65. <!-- #endif -->
  66. </view>
  67. </template>
  68. <script>
  69. const app = getApp();
  70. let statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
  71. import mobileLogin from '../components/login_mobile/index.vue';
  72. import routinePhone from '../components/login_mobile/routine_phone.vue';
  73. import editUserModal from '@/components/eidtUserModal/index.vue'
  74. import privacyAgreementPopup from '@/components/privacyAgreementPopup/index.vue'
  75. import {
  76. getLogo,
  77. silenceAuth,
  78. routineBindingPhone,
  79. wechatAuthV2,
  80. authType,
  81. authLogin,
  82. wechatAuthLogin
  83. } from '@/api/public';
  84. import {
  85. LOGO_URL,
  86. EXPIRES_TIME,
  87. USER_INFO,
  88. STATE_R_KEY
  89. } from '@/config/cache';
  90. import {
  91. getUserInfo
  92. } from '@/api/user.js';
  93. import Routine from '@/libs/routine';
  94. import wechat from '@/libs/wechat';
  95. import colors from '@/mixins/color.js';
  96. import Auth from '@/libs/wechat.js';
  97. import {
  98. HTTP_REQUEST_URL
  99. } from '@/config/app';
  100. import {
  101. isWeixin
  102. } from "@/utils";
  103. import Cache from '@/utils/cache';
  104. export default {
  105. mixins: [colors],
  106. data() {
  107. return {
  108. imgHost: HTTP_REQUEST_URL,
  109. isUp: false,
  110. canClose: true,
  111. phone: '',
  112. statusBarHeight: statusBarHeight,
  113. isHome: false,
  114. isPhoneBox: false,
  115. protocol: false,
  116. isShow: false,
  117. isLogin: false,
  118. logoUrl: '',
  119. code: '',
  120. authKey: '',
  121. options: '',
  122. userInfo: {},
  123. codeNum: 0,
  124. canUseGetUserProfile: false,
  125. canGetPrivacySetting: false,
  126. inAnimation: false,
  127. colorStatus: uni.getStorageSync('color_status'),
  128. mp_is_new: this.$Cache.get('MP_VERSION_ISNEW') || false,
  129. configData: Cache.get('BASIC_CONFIG'),
  130. bindPhone: false
  131. };
  132. },
  133. components: {
  134. mobileLogin,
  135. routinePhone,
  136. editUserModal,
  137. privacyAgreementPopup
  138. },
  139. onLoad(options) {
  140. if (uni.getUserProfile) {
  141. this.canUseGetUserProfile = true
  142. }
  143. // #ifdef MP
  144. if (wx.getPrivacySetting) {
  145. this.canGetPrivacySetting = true
  146. }
  147. // #endif
  148. let that = this;
  149. // #ifdef MP
  150. this.userLogin()
  151. // #endif
  152. // #ifdef H5
  153. const {
  154. code,
  155. state
  156. } = options;
  157. if (code) {
  158. let spread = this.$Cache.get("spread") || '';
  159. let backUrl = state ? decodeURIComponent(state) : ''
  160. this.wechatAuthLogin({
  161. code,
  162. spread
  163. }, backUrl)
  164. }
  165. // #endif
  166. let pages = getCurrentPages();
  167. let prePage = pages[pages.length - 2];
  168. if (prePage && prePage.route == 'pages/order_addcart/order_addcart') {
  169. this.isHome = true;
  170. } else {
  171. this.isHome = false;
  172. }
  173. },
  174. methods: {
  175. wechatAuthLogin(d, back_url) {
  176. uni.showLoading({
  177. title: this.$t(`正在登录中`)
  178. });
  179. wechatAuthLogin(d).then(res => {
  180. uni.hideLoading();
  181. if (res.data.bindPhone) {
  182. this.authKey = res.data.key
  183. uni.navigateTo({
  184. url: `/pages/users/binding_phone/index?authKey=${this.authKey}&backUrl=${back_url}`
  185. })
  186. } else {
  187. let time = res.data.expires_time - this.$Cache.time();
  188. this.$store.commit('LOGIN', {
  189. token: res.data.token,
  190. time: time
  191. });
  192. this.getUserInfo(0, back_url)
  193. }
  194. }).catch(err => {
  195. uni.hideLoading();
  196. uni.showToast({
  197. title: err,
  198. icon: 'none',
  199. duration: 2000
  200. });
  201. });
  202. },
  203. onAgree() {
  204. this.protocol = true
  205. },
  206. // 小程序 22.11.8日删除getUserProfile 接口获取用户昵称头像
  207. userLogin() {
  208. // if (!this.protocol) {
  209. // uni.showToast({
  210. // title: this.$t('请先阅读并同意协议'),
  211. // icon: 'none',
  212. // duration: 2000
  213. // });
  214. // return
  215. // }
  216. Routine.getCode()
  217. .then(code => {
  218. // uni.showLoading({
  219. // title: this.$t(`正在登录中`)
  220. // });
  221. authType({
  222. code,
  223. spread_spid: app.globalData.spid,
  224. spread_code: app.globalData.code
  225. }).then(res => {
  226. uni.hideLoading();
  227. this.authKey = res.data.key;
  228. this.bindPhone = res.data.bindPhone
  229. // uni.navigateTo({
  230. // url: `/pages/users/binding_phone/index?authKey=${res.data.key}`
  231. // })
  232. })
  233. .catch(err => {
  234. uni.hideLoading();
  235. uni.showToast({
  236. title: err,
  237. icon: 'none',
  238. duration: 2000
  239. });
  240. });
  241. })
  242. .catch(err => {
  243. console.log(err)
  244. });
  245. },
  246. getAuthLogin() {
  247. if (!this.authKey) return
  248. if (!this.protocol) {
  249. uni.showToast({
  250. title: this.$t('请先阅读并同意协议'),
  251. icon: 'none',
  252. duration: 2000
  253. });
  254. return
  255. }
  256. uni.showLoading({
  257. title: this.$t(`正在登录中`)
  258. });
  259. authLogin({
  260. key: this.authKey
  261. }).then(res => {
  262. console.log(res)
  263. let time = res.data.expires_time - this.$Cache.time();
  264. this.$store.commit('LOGIN', {
  265. token: res.data.token,
  266. time: time
  267. });
  268. this.getUserInfo(res.data.bindName)
  269. }).catch(err => {
  270. uni.hideLoading();
  271. uni.showToast({
  272. title: err,
  273. icon: 'none',
  274. duration: 2000
  275. });
  276. });
  277. },
  278. ChangeIsDefault(e) {
  279. this.$set(this, 'protocol', !this.protocol);
  280. },
  281. editSuccess() {
  282. this.isShow = false
  283. },
  284. phoneLogin() {
  285. uni.navigateTo({
  286. url: `/pages/users/binding_phone/index?authKey=${this.authKey}&pageType=0`
  287. })
  288. },
  289. closeEdit() {
  290. this.isShow = false
  291. this.$util.Tips({
  292. title: this.$t(`登录成功`),
  293. icon: 'success'
  294. }, {
  295. tab: 3
  296. });
  297. },
  298. onReject() {
  299. uni.navigateBack();
  300. },
  301. // #ifdef MP
  302. back() {
  303. if (this.isLogin) {
  304. this.$store.commit('LOGIN', {
  305. token: '',
  306. time: 0
  307. });
  308. }
  309. uni.navigateBack();
  310. },
  311. // #endif
  312. // #ifndef MP
  313. back() {
  314. uni.navigateBack({
  315. delta: 1
  316. })
  317. },
  318. // #endif
  319. home() {
  320. uni.switchTab({
  321. url: '/pages/index/index'
  322. })
  323. },
  324. // 弹窗关闭
  325. maskClose(new_user) {
  326. this.isUp = false;
  327. // #ifdef MP
  328. if (new_user) {
  329. this.isShow = true
  330. }
  331. // #endif
  332. },
  333. bindPhoneClose(data) {
  334. this.isPhoneBox = false;
  335. if (data.isStatus) {
  336. // #ifdef MP
  337. this.getUserInfo(data.new_user)
  338. // #endif
  339. // #ifndef MP
  340. this.$util.Tips({
  341. title: this.$t(`登录成功`),
  342. icon: 'success'
  343. }, {
  344. tab: 3
  345. });
  346. // #endif
  347. }
  348. },
  349. // #ifdef MP
  350. // 小程序获取手机号码
  351. getphonenumber(e) {
  352. if (!this.protocol) {
  353. uni.showToast({
  354. title: this.$t('请先阅读并同意协议'),
  355. icon: 'none',
  356. duration: 2000
  357. });
  358. return
  359. }
  360. uni.showLoading({
  361. title: this.$t(`正在登录中`)
  362. });
  363. Routine.getCode()
  364. .then(code => {
  365. this.getUserPhoneNumber(e.detail.encryptedData, e.detail.iv, code);
  366. })
  367. .catch(error => {
  368. uni.$emit('closePage', false);
  369. uni.hideLoading();
  370. });
  371. },
  372. // 小程序获取手机号码回调
  373. getUserPhoneNumber(encryptedData, iv, code) {
  374. routineBindingPhone({
  375. encryptedData: encryptedData,
  376. iv: iv,
  377. code: code,
  378. spread_spid: app.globalData.spid,
  379. spread_code: app.globalData.code,
  380. key: this.authKey
  381. })
  382. .then(res => {
  383. let time = res.data.expires_time - this.$Cache.time();
  384. this.$store.commit('LOGIN', {
  385. token: res.data.token,
  386. time: time
  387. });
  388. // this.userInfo = res.data.userInfo;
  389. // this.$store.commit('SETUID', res.data.userInfo.uid);
  390. // this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  391. this.$Cache.clear('snsapiKey');
  392. this.getUserInfo(res.data.bindName)
  393. // this.$util.Tips({
  394. // title: this.$t(`登录成功`),
  395. // icon: 'success'
  396. // }, {
  397. // tab: 3
  398. // });
  399. })
  400. .catch(res => {
  401. uni.hideLoading();
  402. });
  403. },
  404. // #endif
  405. /**
  406. * 获取个人用户信息
  407. */
  408. getUserInfo(new_user, back_url) {
  409. let that = this;
  410. getUserInfo().then(res => {
  411. uni.hideLoading();
  412. that.userInfo = res.data;
  413. that.$store.commit('SETUID', res.data.uid);
  414. that.$store.commit('UPDATE_USERINFO', res.data);
  415. if (new_user) {
  416. this.isShow = true
  417. } else {
  418. // #ifdef MP
  419. that.$util.Tips({
  420. title: that.$t(`登录成功`),
  421. icon: 'success'
  422. }, {
  423. tab: 3
  424. });
  425. // #endif
  426. // #ifndef MP
  427. that.$util.Tips({
  428. title: that.$t(`登录成功`),
  429. icon: 'success'
  430. }, {
  431. tab: 4,
  432. url: back_url || '/pages/user/index'
  433. });
  434. // #endif
  435. }
  436. }).catch(err => {
  437. uni.hideLoading();
  438. uni.showToast({
  439. title: err.msg,
  440. icon: 'none',
  441. duration: 2000
  442. });
  443. });
  444. },
  445. privacy(type) {
  446. uni.navigateTo({
  447. url: "/pages/users/privacy/index?type=" + type
  448. })
  449. },
  450. // #ifdef H5
  451. // 获取url后面的参数
  452. getQueryString(name) {
  453. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  454. var reg_rewrite = new RegExp('(^|/)' + name + '/([^/]*)(/|$)', 'i');
  455. var r = window.location.search.substr(1).match(reg);
  456. var q = window.location.pathname.substr(1).match(reg_rewrite);
  457. if (r != null) {
  458. return unescape(r[2]);
  459. } else if (q != null) {
  460. return unescape(q[2]);
  461. } else {
  462. return null;
  463. }
  464. },
  465. // 公众号登录
  466. wechatLogin() {
  467. if (!this.protocol) {
  468. uni.showToast({
  469. title: this.$t('请先阅读并同意协议'),
  470. icon: 'none',
  471. duration: 2000
  472. });
  473. return
  474. }
  475. if (!this.code || this.options.scope !== 'snsapi_base') {
  476. this.$wechat.oAuth('snsapi_userinfo', location.href);
  477. } else {
  478. if (this.authKey) {
  479. // this.isUp = true;
  480. uni.navigateTo({
  481. url: `/pages/users/binding_phone/index?authKey=${this.authKey}`
  482. })
  483. }
  484. }
  485. },
  486. // 输入手机号后的回调
  487. wechatPhone() {
  488. this.$Cache.clear('snsapiKey');
  489. if (this.options.back_url) {
  490. let url = uni.getStorageSync('snRouter');
  491. url = url.indexOf('/pages/index/index') != -1 ? '/' : url;
  492. if (url.indexOf('/pages/users/wechat_login/index') !== -1) {
  493. url = '/';
  494. }
  495. if (!url) {
  496. url = '/pages/index/index';
  497. }
  498. this.isUp = false;
  499. uni.showToast({
  500. title: this.$t(`登录成功`),
  501. icon: 'none'
  502. });
  503. setTimeout(res => {
  504. location.href = url;
  505. }, 800);
  506. } else {
  507. this.isUp = false;
  508. uni.showToast({
  509. title: this.$t(`登录成功`),
  510. icon: 'none'
  511. });
  512. setTimeout(res => {
  513. location.href = '/pages/index/index';
  514. }, 800);
  515. }
  516. }
  517. // #endif
  518. }
  519. };
  520. </script>
  521. <style lang="scss">
  522. page {
  523. background: #fff;
  524. }
  525. .wrapper {
  526. position: relative;
  527. height: 100vh;
  528. .bag {
  529. position: absolute;
  530. top: 0;
  531. left: 0;
  532. width: 100%;
  533. opacity: .8;
  534. z-index: -1;
  535. /* #ifdef H5 */
  536. z-index: 0;
  537. /* #endif */
  538. img {
  539. width: 100%;
  540. height: 838rpx;
  541. }
  542. }
  543. .merchant-msg {
  544. padding-top: 252rpx;
  545. display: flex;
  546. justify-content: center;
  547. align-items: center;
  548. flex-direction: column;
  549. z-index: 2;
  550. /* #ifdef H5 */
  551. position: relative;
  552. /* #endif */
  553. img {
  554. width: 152rpx;
  555. height: 152rpx;
  556. border-radius: 50%;
  557. }
  558. .name {
  559. font-size: 40rpx;
  560. font-weight: 500;
  561. color: #333333;
  562. line-height: 56rpx;
  563. margin-top: 32rpx;
  564. }
  565. }
  566. }
  567. .wechat_login {
  568. margin-top: 96rpx;
  569. .img image {
  570. width: 100%;
  571. }
  572. .btn-wrapper {
  573. padding: 0 66rpx;
  574. button {
  575. width: 100%;
  576. height: 86rpx;
  577. line-height: 86rpx;
  578. margin-bottom: 40rpx;
  579. border-radius: 120rpx;
  580. font-size: 30rpx;
  581. &.btn1 {
  582. color: #fff;
  583. }
  584. &.btn2 {
  585. color: #666666;
  586. border: 1px solid #E4E4E4;
  587. }
  588. }
  589. }
  590. }
  591. .title-bar {
  592. position: relative;
  593. display: flex;
  594. align-items: center;
  595. justify-content: center;
  596. font-size: 34rpx;
  597. font-weight: 500;
  598. color: #333333;
  599. line-height: 48rpx;
  600. }
  601. .icon {
  602. position: absolute;
  603. left: 30rpx;
  604. top: 0;
  605. display: flex;
  606. align-items: center;
  607. justify-content: center;
  608. width: 80rpx;
  609. height: 80rpx;
  610. image {
  611. width: 50rpx;
  612. height: 50rpx;
  613. }
  614. }
  615. .protocol {
  616. position: fixed;
  617. bottom: 52rpx;
  618. left: 0;
  619. width: 100%;
  620. margin: 0 auto;
  621. color: #999999;
  622. font-size: 24rpx;
  623. line-height: 22rpx;
  624. text-align: center;
  625. bottom: calc(52rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  626. bottom: calc(52rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
  627. .main-color {
  628. color: var(--view-theme);
  629. }
  630. .trembling {
  631. animation: shake 0.6s;
  632. }
  633. /deep/ uni-checkbox .uni-checkbox-input {
  634. width: 28rpx;
  635. height: 28rpx;
  636. }
  637. /deep/ uni-checkbox .uni-checkbox-input.uni-checkbox-input-checked::before {
  638. font-size: 24rpx
  639. }
  640. /deep/ uni-checkbox .uni-checkbox-wrapper {
  641. margin-bottom: 1px;
  642. }
  643. /*checkbox 选项框大小 */
  644. /deep/ checkbox .wx-checkbox-input {
  645. width: 28rpx;
  646. height: 28rpx;
  647. }
  648. /*checkbox选中后样式 */
  649. /deep/ checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  650. background: white;
  651. }
  652. /*checkbox选中后图标样式 */
  653. /deep/ checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
  654. width: 28rpx;
  655. height: 28rpx;
  656. line-height: 28rpx;
  657. text-align: center;
  658. font-size: 22rpx;
  659. background: transparent;
  660. transform: translate(-50%, -50%) scale(1);
  661. -webkit-transform: translate(-50%, -50%) scale(1);
  662. }
  663. }
  664. </style>