index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. let time = res.data.expires_time - this.$Cache.time();
  263. this.$store.commit('LOGIN', {
  264. token: res.data.token,
  265. time: time
  266. });
  267. this.getUserInfo(res.data.bindName)
  268. }).catch(err => {
  269. uni.hideLoading();
  270. uni.showToast({
  271. title: err,
  272. icon: 'none',
  273. duration: 2000
  274. });
  275. });
  276. },
  277. ChangeIsDefault(e) {
  278. this.$set(this, 'protocol', !this.protocol);
  279. },
  280. editSuccess() {
  281. this.isShow = false
  282. },
  283. phoneLogin() {
  284. uni.navigateTo({
  285. url: `/pages/users/binding_phone/index?authKey=${this.authKey}&pageType=0`
  286. })
  287. },
  288. closeEdit() {
  289. this.isShow = false
  290. this.$util.Tips({
  291. title: this.$t(`登录成功`),
  292. icon: 'success'
  293. }, {
  294. tab: 3
  295. });
  296. },
  297. onReject() {
  298. uni.navigateBack();
  299. },
  300. // #ifdef MP
  301. back() {
  302. if (this.isLogin) {
  303. this.$store.commit('LOGIN', {
  304. token: '',
  305. time: 0
  306. });
  307. }
  308. uni.navigateBack();
  309. },
  310. // #endif
  311. // #ifndef MP
  312. back() {
  313. uni.navigateBack({
  314. delta: 1
  315. })
  316. },
  317. // #endif
  318. home() {
  319. uni.switchTab({
  320. url: '/pages/index/index'
  321. })
  322. },
  323. // 弹窗关闭
  324. maskClose(new_user) {
  325. this.isUp = false;
  326. // #ifdef MP
  327. if (new_user) {
  328. this.isShow = true
  329. }
  330. // #endif
  331. },
  332. bindPhoneClose(data) {
  333. this.isPhoneBox = false;
  334. if (data.isStatus) {
  335. // #ifdef MP
  336. this.getUserInfo(data.new_user)
  337. // #endif
  338. // #ifndef MP
  339. this.$util.Tips({
  340. title: this.$t(`登录成功`),
  341. icon: 'success'
  342. }, {
  343. tab: 3
  344. });
  345. // #endif
  346. }
  347. },
  348. // #ifdef MP
  349. // 小程序获取手机号码
  350. getphonenumber(e) {
  351. if (!this.protocol) {
  352. uni.showToast({
  353. title: this.$t('请先阅读并同意协议'),
  354. icon: 'none',
  355. duration: 2000
  356. });
  357. return
  358. }
  359. uni.showLoading({
  360. title: this.$t(`正在登录中`)
  361. });
  362. Routine.getCode()
  363. .then(code => {
  364. this.getUserPhoneNumber(e.detail.encryptedData, e.detail.iv, code);
  365. })
  366. .catch(error => {
  367. uni.$emit('closePage', false);
  368. uni.hideLoading();
  369. });
  370. },
  371. // 小程序获取手机号码回调
  372. getUserPhoneNumber(encryptedData, iv, code) {
  373. routineBindingPhone({
  374. encryptedData: encryptedData,
  375. iv: iv,
  376. code: code,
  377. spread_spid: app.globalData.spid,
  378. spread_code: app.globalData.code,
  379. key: this.authKey
  380. })
  381. .then(res => {
  382. let time = res.data.expires_time - this.$Cache.time();
  383. this.$store.commit('LOGIN', {
  384. token: res.data.token,
  385. time: time
  386. });
  387. // this.userInfo = res.data.userInfo;
  388. // this.$store.commit('SETUID', res.data.userInfo.uid);
  389. // this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  390. this.$Cache.clear('snsapiKey');
  391. this.getUserInfo(res.data.bindName)
  392. // this.$util.Tips({
  393. // title: this.$t(`登录成功`),
  394. // icon: 'success'
  395. // }, {
  396. // tab: 3
  397. // });
  398. })
  399. .catch(res => {
  400. uni.hideLoading();
  401. });
  402. },
  403. // #endif
  404. /**
  405. * 获取个人用户信息
  406. */
  407. getUserInfo(new_user, back_url) {
  408. let that = this;
  409. getUserInfo().then(res => {
  410. uni.hideLoading();
  411. that.userInfo = res.data;
  412. that.$store.commit('SETUID', res.data.uid);
  413. that.$store.commit('UPDATE_USERINFO', res.data);
  414. if (new_user) {
  415. this.isShow = true
  416. } else {
  417. // #ifdef MP
  418. that.$util.Tips({
  419. title: that.$t(`登录成功`),
  420. icon: 'success'
  421. }, {
  422. tab: 3
  423. });
  424. // #endif
  425. // #ifndef MP
  426. that.$util.Tips({
  427. title: that.$t(`登录成功`),
  428. icon: 'success'
  429. }, {
  430. tab: 4,
  431. url: back_url || '/pages/user/index'
  432. });
  433. // #endif
  434. }
  435. }).catch(err => {
  436. uni.hideLoading();
  437. uni.showToast({
  438. title: err.msg,
  439. icon: 'none',
  440. duration: 2000
  441. });
  442. });
  443. },
  444. privacy(type) {
  445. uni.navigateTo({
  446. url: "/pages/users/privacy/index?type=" + type
  447. })
  448. },
  449. // #ifdef H5
  450. // 获取url后面的参数
  451. getQueryString(name) {
  452. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  453. var reg_rewrite = new RegExp('(^|/)' + name + '/([^/]*)(/|$)', 'i');
  454. var r = window.location.search.substr(1).match(reg);
  455. var q = window.location.pathname.substr(1).match(reg_rewrite);
  456. if (r != null) {
  457. return unescape(r[2]);
  458. } else if (q != null) {
  459. return unescape(q[2]);
  460. } else {
  461. return null;
  462. }
  463. },
  464. // 公众号登录
  465. wechatLogin() {
  466. if (!this.protocol) {
  467. uni.showToast({
  468. title: this.$t('请先阅读并同意协议'),
  469. icon: 'none',
  470. duration: 2000
  471. });
  472. return
  473. }
  474. if (!this.code || this.options.scope !== 'snsapi_base') {
  475. this.$wechat.oAuth('snsapi_userinfo', location.href);
  476. } else {
  477. if (this.authKey) {
  478. // this.isUp = true;
  479. uni.navigateTo({
  480. url: `/pages/users/binding_phone/index?authKey=${this.authKey}`
  481. })
  482. }
  483. }
  484. },
  485. // 输入手机号后的回调
  486. wechatPhone() {
  487. this.$Cache.clear('snsapiKey');
  488. if (this.options.back_url) {
  489. let url = uni.getStorageSync('snRouter');
  490. url = url.indexOf('/pages/index/index') != -1 ? '/' : url;
  491. if (url.indexOf('/pages/users/wechat_login/index') !== -1) {
  492. url = '/';
  493. }
  494. if (!url) {
  495. url = '/pages/index/index';
  496. }
  497. this.isUp = false;
  498. uni.showToast({
  499. title: this.$t(`登录成功`),
  500. icon: 'none'
  501. });
  502. setTimeout(res => {
  503. location.href = url;
  504. }, 800);
  505. } else {
  506. this.isUp = false;
  507. uni.showToast({
  508. title: this.$t(`登录成功`),
  509. icon: 'none'
  510. });
  511. setTimeout(res => {
  512. location.href = '/pages/index/index';
  513. }, 800);
  514. }
  515. }
  516. // #endif
  517. }
  518. };
  519. </script>
  520. <style lang="scss">
  521. page {
  522. background: #fff;
  523. }
  524. .wrapper {
  525. position: relative;
  526. height: 100vh;
  527. .bag {
  528. position: absolute;
  529. top: 0;
  530. left: 0;
  531. width: 100%;
  532. opacity: .8;
  533. z-index: -1;
  534. /* #ifdef H5 */
  535. z-index: 0;
  536. /* #endif */
  537. img {
  538. width: 100%;
  539. height: 838rpx;
  540. }
  541. }
  542. .merchant-msg {
  543. padding-top: 252rpx;
  544. display: flex;
  545. justify-content: center;
  546. align-items: center;
  547. flex-direction: column;
  548. z-index: 2;
  549. /* #ifdef H5 */
  550. position: relative;
  551. /* #endif */
  552. img {
  553. width: 152rpx;
  554. height: 152rpx;
  555. border-radius: 50%;
  556. }
  557. .name {
  558. font-size: 40rpx;
  559. font-weight: 500;
  560. color: #333333;
  561. line-height: 56rpx;
  562. margin-top: 32rpx;
  563. }
  564. }
  565. }
  566. .wechat_login {
  567. margin-top: 96rpx;
  568. .img image {
  569. width: 100%;
  570. }
  571. .btn-wrapper {
  572. padding: 0 66rpx;
  573. button {
  574. width: 100%;
  575. height: 86rpx;
  576. line-height: 86rpx;
  577. margin-bottom: 40rpx;
  578. border-radius: 120rpx;
  579. font-size: 30rpx;
  580. &.btn1 {
  581. color: #fff;
  582. }
  583. &.btn2 {
  584. color: #666666;
  585. border: 1px solid #E4E4E4;
  586. }
  587. }
  588. }
  589. }
  590. .title-bar {
  591. position: relative;
  592. display: flex;
  593. align-items: center;
  594. justify-content: center;
  595. font-size: 34rpx;
  596. font-weight: 500;
  597. color: #333333;
  598. line-height: 48rpx;
  599. }
  600. .icon {
  601. position: absolute;
  602. left: 30rpx;
  603. top: 0;
  604. display: flex;
  605. align-items: center;
  606. justify-content: center;
  607. width: 80rpx;
  608. height: 80rpx;
  609. image {
  610. width: 50rpx;
  611. height: 50rpx;
  612. }
  613. }
  614. .protocol {
  615. position: fixed;
  616. bottom: 52rpx;
  617. left: 0;
  618. width: 100%;
  619. margin: 0 auto;
  620. color: #999999;
  621. font-size: 24rpx;
  622. line-height: 22rpx;
  623. text-align: center;
  624. bottom: calc(52rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  625. bottom: calc(52rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
  626. .main-color {
  627. color: var(--view-theme);
  628. }
  629. .trembling {
  630. animation: shake 0.6s;
  631. }
  632. /deep/ uni-checkbox .uni-checkbox-input {
  633. width: 28rpx;
  634. height: 28rpx;
  635. }
  636. /deep/ uni-checkbox .uni-checkbox-input.uni-checkbox-input-checked::before {
  637. font-size: 24rpx
  638. }
  639. /deep/ uni-checkbox .uni-checkbox-wrapper {
  640. margin-bottom: 1px;
  641. }
  642. /*checkbox 选项框大小 */
  643. /deep/ checkbox .wx-checkbox-input {
  644. width: 28rpx;
  645. height: 28rpx;
  646. }
  647. /*checkbox选中后样式 */
  648. /deep/ checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  649. background: white;
  650. }
  651. /*checkbox选中后图标样式 */
  652. /deep/ checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
  653. width: 28rpx;
  654. height: 28rpx;
  655. line-height: 28rpx;
  656. text-align: center;
  657. font-size: 22rpx;
  658. background: transparent;
  659. transform: translate(-50%, -50%) scale(1);
  660. -webkit-transform: translate(-50%, -50%) scale(1);
  661. }
  662. }
  663. </style>