index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. <template>
  2. <view :style="colorStyle">
  3. <view class="system-height" :style="{ height: statusBarHeight }"></view>
  4. <!-- #ifdef MP -->
  5. <view class="title-bar" style="height: 43px;">
  6. <!-- <view class="icon" @click="back" v-if="!isHome">
  7. <image src="../static/left.png"></image>
  8. </view>
  9. <view class="icon" @click="home" v-else>
  10. <image src="../static/home.png"></image>
  11. </view> -->
  12. 小程序登录
  13. </view>
  14. <!-- #endif -->
  15. <view class="w_login">
  16. <view class="circle">
  17. </view>
  18. <view class="info1">
  19. 欢迎进入中意购
  20. </view>
  21. <view class="info2">
  22. 为您提供优质服务,中意够需要获取以下信息
  23. 您的公开信息 (昵称、头像)
  24. </view>
  25. <button class="now" open-type="getPhoneNumber" hover-class="none" @getphonenumber="getPhoneNumber">
  26. 使用微信权限登录
  27. </button>
  28. <button class="not_now" @click="cancelLogin">
  29. 暂不登录
  30. </button>
  31. </view>
  32. <block v-if="isUp">
  33. <mobileLogin :isUp="isUp" @close="maskClose" :wxcode="code" :authKey="authKey" @wechatPhone="wechatPhone">
  34. </mobileLogin>
  35. </block>
  36. <block v-if="isPhoneBox">
  37. <routinePhone :logoUrl="logoUrl" :isPhoneBox="isPhoneBox" @close="bindPhoneClose" :authKey="authKey">
  38. </routinePhone>
  39. </block>
  40. </view>
  41. </template>
  42. <script>
  43. const app = getApp();
  44. let statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
  45. import mobileLogin from '../components/login_mobile/index.vue';
  46. import routinePhone from '../components/login_mobile/routine_phone.vue';
  47. import {
  48. getLogo,
  49. silenceAuth,
  50. getUserPhone,
  51. wechatAuthV2
  52. } from '@/api/public';
  53. import {
  54. LOGO_URL,
  55. EXPIRES_TIME,
  56. USER_INFO,
  57. STATE_R_KEY
  58. } from '@/config/cache';
  59. import {
  60. getUserInfo
  61. } from '@/api/user.js';
  62. import {
  63. postLoginAuth
  64. } from '@/api/home.js';
  65. import Routine from '@/libs/routine';
  66. import wechat from '@/libs/wechat';
  67. import colors from '@/mixins/color.js';
  68. export default {
  69. mixins: [colors],
  70. data () {
  71. return {
  72. isUp: false,
  73. phone: '',
  74. statusBarHeight: statusBarHeight,
  75. isHome: false,
  76. isPhoneBox: false,
  77. logoUrl: '',
  78. code: '',
  79. authKey: '',
  80. options: '',
  81. userInfo: {},
  82. codeNum: 0,
  83. canUseGetUserProfile: false
  84. };
  85. },
  86. components: {
  87. mobileLogin,
  88. routinePhone
  89. },
  90. onLoad (options) {
  91. if (uni.getUserProfile) {
  92. this.canUseGetUserProfile = true
  93. }
  94. this.logoUrl = "https://demo26.crmeb.net/statics/system_images/login_logo.jpeg";
  95. // getLogo().then(res => {
  96. // this.logoUrl = res.data.logo_url;
  97. // });
  98. let that = this;
  99. // #ifdef MP
  100. Routine.getCode()
  101. .then(code => {
  102. this.code = code
  103. })
  104. // #endif
  105. // #ifdef H5
  106. document.body.addEventListener('focusout', () => {
  107. setTimeout(() => {
  108. const scrollHeight = document.documentElement.scrollTop || document.body
  109. .scrollTop ||
  110. 0;
  111. window.scrollTo(0, Math.max(scrollHeight - 1, 0));
  112. }, 100);
  113. });
  114. const {
  115. code,
  116. state,
  117. scope
  118. } = options;
  119. this.options = options;
  120. // 获取确认授权code
  121. this.code = code || '';
  122. if (code && this.options.scope !== 'snsapi_base') {
  123. let spread = app.globalData.spid ? app.globalData.spid : '';
  124. //公众号授权登录回调
  125. wechat
  126. .auth(code, state)
  127. .then(res => {
  128. if (res.key !== undefined && res.key) {
  129. that.authKey = res.key;
  130. that.isUp = true;
  131. } else {
  132. let time = res.expires_time - that.$Cache.time();
  133. that.$store.commit('LOGIN', {
  134. token: res.token,
  135. time: time
  136. });
  137. that.userInfo = res.userInfo;
  138. that.$store.commit('SETUID', res.userInfo.userId);
  139. that.$store.commit('UPDATE_USERINFO', res.userInfo);
  140. that.wechatPhone();
  141. }
  142. })
  143. .catch(error => {
  144. // location.replace("/");
  145. });
  146. }
  147. // #endif
  148. let pages = getCurrentPages();
  149. let prePage = pages[pages.length - 2];
  150. if (prePage && prePage.route == 'pages/order_addcart/order_addcart') {
  151. this.isHome = true;
  152. } else {
  153. this.isHome = false;
  154. }
  155. },
  156. methods: {
  157. back () {
  158. uni.navigateBack();
  159. },
  160. home () {
  161. uni.switchTab({
  162. url: '/pages/index/index'
  163. })
  164. },
  165. // 弹窗关闭
  166. maskClose () {
  167. this.isUp = false;
  168. },
  169. bindPhoneClose (data) {
  170. if (data.isStatus) {
  171. this.isPhoneBox = false;
  172. this.$util.Tips({
  173. title: this.$t(`登录成功`),
  174. icon: 'success'
  175. }, {
  176. tab: 3
  177. });
  178. } else {
  179. this.isPhoneBox = false;
  180. }
  181. },
  182. cancelLogin () {
  183. uni.switchTab({
  184. url: '/pages/index/index'
  185. })
  186. },
  187. getPhoneNumber (e) {
  188. let _this = this;
  189. console.log(e)
  190. if (e.detail.errMsg === 'getPhoneNumber:fail user deny' || !e.detail.code) {
  191. console.log("用户拒绝获取手机号");
  192. return
  193. }
  194. uni.showLoading({
  195. title: this.$t(`正在登录中`)
  196. });
  197. let data = {
  198. code: '',
  199. getPhoneNumberCode: ''
  200. }
  201. data.getPhoneNumberCode = e.detail.code
  202. uni.login({
  203. "provider": "weixin",
  204. "onlyAuthorize": true, // 微信登录仅请求授权认证
  205. success: function (event) {
  206. console.log(event.code)
  207. data.code = event.code
  208. console.log(data)
  209. //客户端成功获取授权临时票据(code),向业务服务器发起登录请求。
  210. postLoginAuth(data).then(res => {
  211. if (res.code === 200) {
  212. _this.$store.commit('LOGIN', {
  213. token: res.data.access_token,
  214. time: res.data.expires_in
  215. });
  216. _this.$store.commit('OPENID', res.data.openId);
  217. _this.getUserInfo();
  218. // 登录成功,跳转首页
  219. uni.showToast({
  220. title: "登录成功,即将跳转首页",
  221. icon: 'none',
  222. duration: 2000
  223. });
  224. setTimeout(() => {
  225. // 自动登录失败时,跳转用户注册
  226. uni.switchTab({
  227. url: '/pages/index/index',
  228. success: (success) => {
  229. //获取当前页面的实例按照数组的顺序排列[0]开始首页,因为我就是首页和我的页面所以分别为0和1
  230. let page = getCurrentPages().pop()
  231. console.log(page,success, '肏!',page.$vm.$refs.diy);
  232. page.$vm.$refs.diy.$refs.latestGroupBuying.onLoadData(0);
  233. },
  234. })
  235. }, 2000);
  236. } else {
  237. uni.hideLoading();
  238. uni.showToast({
  239. title: "登录失败",
  240. icon: 'none',
  241. duration: 2000
  242. });
  243. }
  244. }).catch(res => {
  245. uni.hideLoading();
  246. });
  247. },
  248. fail: function (err) {
  249. // 登录授权失败
  250. uni.$u.toast('登录失败,错误代码:' + err.code);
  251. }
  252. })
  253. },
  254. getPhoneCode (e) {
  255. console.log(e)
  256. if (e.detail.errMsg == 'getPhoneNumber:fail user deny' || !e.detail.code) {
  257. console.log("用户拒绝获取手机号");
  258. return
  259. }
  260. uni.showLoading({
  261. title: this.$t(`正在登录中`)
  262. });
  263. postLoginAuth({
  264. getPhoneNumberCode: e.detail.code,
  265. // iv: iv,
  266. code: this.code,
  267. // spread_spid: app.globalData.spid,
  268. // spread_code: app.globalData.code
  269. })
  270. .then(res => {
  271. if (res.code == 200) {
  272. this.$store.commit('LOGIN', {
  273. token: res.data.access_token,
  274. time: res.data.expires_in
  275. });
  276. this.$store.commit('OPENID', res.data.openId);
  277. this.getUserInfo();
  278. } else {
  279. uni.hideLoading();
  280. uni.showToast({
  281. title: "手机号暂未注册,即将跳转注册界面",
  282. icon: 'none',
  283. duration: 2000
  284. });
  285. setTimeout(() => {
  286. // 自动登录失败时,跳转用户注册
  287. uni.navigateTo({
  288. url: '/pages/users/register/index?account=' + res.data
  289. })
  290. }, 2000);
  291. }
  292. })
  293. .catch(res => {
  294. uni.hideLoading();
  295. });
  296. },
  297. // #ifdef MP
  298. // 小程序获取手机号码
  299. getphonenumber (e) {
  300. uni.showLoading({
  301. title: this.$t(`正在登录中`)
  302. });
  303. Routine.getCode()
  304. .then(code => {
  305. this.getUserPhoneNumber(e.detail.encryptedData, e.detail.iv, code);
  306. })
  307. .catch(error => {
  308. uni.$emit('closePage', false);
  309. uni.hideLoading();
  310. });
  311. },
  312. // 小程序获取手机号码回调
  313. getUserPhoneNumber (encryptedData, iv, code) {
  314. console.log(this.code);
  315. console.log(code);
  316. postLoginAuth({
  317. getPhoneNumberCode: code,
  318. // iv: iv,
  319. code: this.code,
  320. // spread_spid: app.globalData.spid,
  321. // spread_code: app.globalData.code
  322. })
  323. .then(res => {
  324. let time = res.data.expires_time - this.$Cache.time();
  325. this.$store.commit('LOGIN', {
  326. token: res.data.token,
  327. time: time
  328. });
  329. this.userInfo = res.data.userInfo;
  330. this.$store.commit('SETUID', res.data.userInfo.userId);
  331. this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  332. this.$Cache.clear('snsapiKey');
  333. this.$util.Tips({
  334. title: this.$t(`登录成功`),
  335. icon: 'success'
  336. }, {
  337. tab: 4
  338. });
  339. })
  340. .catch(res => {
  341. uni.hideLoading();
  342. });
  343. },
  344. /**
  345. * 获取个人用户信息
  346. */
  347. getUserInfo: function () {
  348. console.log("获取个人信息+++++++++++++++++++++++++++++++++")
  349. let that = this;
  350. getUserInfo().then(res => {
  351. uni.hideLoading();
  352. that.userInfo = res.data;
  353. that.$store.commit('SETUID', res.data.userId);
  354. that.$store.commit('UPDATE_USERINFO', res.data);
  355. let GOOD_ID_LOGIN = this.$Cache.get('GOOD_ID_LOGIN')
  356. if (GOOD_ID_LOGIN) {
  357. that.$util.Tips({
  358. title: that.$t(`登录成功`),
  359. icon: 'success'
  360. });
  361. } else {
  362. that.$util.Tips({
  363. title: that.$t(`登录成功`),
  364. icon: 'success'
  365. }, {
  366. tab: 3
  367. });
  368. }
  369. });
  370. },
  371. setUserInfo (e) {
  372. uni.showLoading({
  373. title: this.$t(`正在登录中`)
  374. });
  375. Routine.getCode()
  376. .then(code => {
  377. this.getWxUser(code);
  378. })
  379. .catch(res => {
  380. uni.hideLoading();
  381. });
  382. },
  383. //小程序授权api替换 getUserInfo
  384. getUserProfile () {
  385. uni.showLoading({
  386. title: this.$t(`正在登录中`)
  387. });
  388. let self = this;
  389. Routine.getUserProfile()
  390. .then(res => {
  391. let userInfo = res.userInfo;
  392. console.log(this.code);
  393. console.log(userInfo);
  394. userInfo.code = this.code;
  395. uni.showToast({
  396. title: "手机号暂未注册,即将跳转注册界面",
  397. icon: 'none',
  398. duration: 2000
  399. });
  400. setTimeout(() => {
  401. // 自动登录失败时,跳转用户注册
  402. uni.navigateTo({
  403. url: '/pages/users/register/index'
  404. })
  405. }, 2000);
  406. // userInfo.spread_spid = app.globalData.spid || this.$Cache.get('spread'); //获取推广人ID
  407. // userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  408. // 模拟授权登录成功
  409. // self.$store.commit('LOGIN', {
  410. // token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ2NC5jcm1lYi5uZXQiLCJhdWQiOiJ2NC5jcm1lYi5uZXQiLCJpYXQiOjE2NjY1Nzk2NzgsIm5iZiI6MTY2NjU3OTY3OCwiZXhwIjoxNjY5MTcxNjc4LCJqdGkiOnsiaWQiOjM0MzkxLCJ0eXBlIjoiYXBpIn19.GA3Lyr3HueKXwz9GWS6KrO4rYKmJzn3l7ko1nsl7E7Q',
  411. // time: 9999999999999
  412. // });
  413. // this.getUserInfo()
  414. // Routine.authUserInfo(userInfo)
  415. // .then(res => {
  416. // if (res.data.key !== undefined && res.data.key) {
  417. // uni.hideLoading();
  418. // self.authKey = res.data.key;
  419. // self.isPhoneBox = true;
  420. // } else {
  421. // uni.hideLoading();
  422. // let time = res.data.expires_time - self.$Cache.time();
  423. // self.$store.commit('LOGIN', {
  424. // token: res.data.token,
  425. // time: time
  426. // });
  427. // this.getUserInfo()
  428. // }
  429. // })
  430. // .catch(res => {
  431. // uni.hideLoading();
  432. // uni.showToast({
  433. // title: res.msg,
  434. // icon: 'none',
  435. // duration: 2000
  436. // });
  437. // });
  438. })
  439. .catch(res => {
  440. uni.hideLoading();
  441. });
  442. },
  443. getWxUser (code) {
  444. let self = this;
  445. Routine.getUserInfo()
  446. .then(res => {
  447. let userInfo = res.userInfo;
  448. userInfo.code = code;
  449. userInfo.spread_spid = app.globalData.spid; //获取推广人ID
  450. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  451. Routine.authUserInfo(userInfo)
  452. .then(res => {
  453. if (res.data.key !== undefined && res.data.key) {
  454. uni.hideLoading();
  455. self.authKey = res.data.key;
  456. self.isPhoneBox = true;
  457. } else {
  458. uni.hideLoading();
  459. let time = res.data.expires_time - self.$Cache.time();
  460. self.$store.commit('LOGIN', {
  461. token: res.data.token,
  462. time: time
  463. });
  464. self.$util.Tips({
  465. title: res.msg,
  466. icon: 'success'
  467. }, {
  468. tab: 3
  469. });
  470. }
  471. })
  472. .catch(res => {
  473. uni.hideLoading();
  474. uni.showToast({
  475. title: res.msg,
  476. icon: 'none',
  477. duration: 2000
  478. });
  479. });
  480. })
  481. .catch(res => {
  482. uni.hideLoading();
  483. });
  484. },
  485. // #endif
  486. // #ifdef H5
  487. // 获取url后面的参数
  488. getQueryString (name) {
  489. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  490. var reg_rewrite = new RegExp('(^|/)' + name + '/([^/]*)(/|$)', 'i');
  491. var r = window.location.search.substr(1).match(reg);
  492. var q = window.location.pathname.substr(1).match(reg_rewrite);
  493. if (r != null) {
  494. return unescape(r[2]);
  495. } else if (q != null) {
  496. return unescape(q[2]);
  497. } else {
  498. return null;
  499. }
  500. },
  501. // 公众号登录
  502. wechatLogin () {
  503. if (!this.code || this.options.scope !== 'snsapi_base') {
  504. this.$wechat.oAuth('snsapi_userinfo', '/pages/users/wechat_login/index');
  505. } else {
  506. if (this.authKey) {
  507. this.isUp = true;
  508. }
  509. }
  510. },
  511. // 输入手机号后的回调
  512. wechatPhone () {
  513. console.log(2222)
  514. this.$Cache.clear('snsapiKey');
  515. if (this.options.back_url) {
  516. let url = uni.getStorageSync('snRouter');
  517. url = url.indexOf('/pages/index/index') != -1 ? '/' : url;
  518. if (url.indexOf('/pages/users/wechat_login/index') !== -1) {
  519. url = '/';
  520. }
  521. if (!url) {
  522. url = '/pages/index/index';
  523. }
  524. this.isUp = false;
  525. uni.showToast({
  526. title: this.$t(`登录成功`),
  527. icon: 'none'
  528. });
  529. setTimeout(res => {
  530. location.href = url;
  531. }, 800);
  532. } else {
  533. this.isUp = false;
  534. uni.showToast({
  535. title: this.$t(`登录成功`),
  536. icon: 'none'
  537. });
  538. setTimeout(res => {
  539. location.href = '/pages/index/index';
  540. }, 800);
  541. }
  542. }
  543. // #endif
  544. }
  545. };
  546. </script>
  547. <style lang="scss">
  548. page {
  549. background: #fff;
  550. }
  551. .wechat_login {
  552. padding: 72rpx 34rpx;
  553. .img image {
  554. width: 100%;
  555. }
  556. .btn-wrapper {
  557. margin-top: 86rpx;
  558. padding: 0 66rpx;
  559. button {
  560. width: 100%;
  561. height: 86rpx;
  562. line-height: 86rpx;
  563. margin-bottom: 40rpx;
  564. border-radius: 120rpx;
  565. font-size: 30rpx;
  566. &.btn1 {
  567. color: #fff;
  568. }
  569. &.btn2 {
  570. color: #666666;
  571. border: 1px solid #666666;
  572. }
  573. }
  574. }
  575. }
  576. .title-bar {
  577. position: relative;
  578. display: flex;
  579. align-items: center;
  580. justify-content: center;
  581. font-size: 36rpx;
  582. }
  583. .icon {
  584. position: absolute;
  585. left: 30rpx;
  586. top: 0;
  587. display: flex;
  588. align-items: center;
  589. justify-content: center;
  590. width: 86rpx;
  591. height: 86rpx;
  592. image {
  593. width: 50rpx;
  594. height: 50rpx;
  595. }
  596. }
  597. .w_login {
  598. padding: 30rpx;
  599. .circle {
  600. border-radius: 50%;
  601. width: 106rpx;
  602. height: 106rpx;
  603. background: rgb(238, 238, 238);
  604. margin: auto;
  605. }
  606. .info1 {
  607. text-align: center;
  608. font-size: 48rpx;
  609. font-weight: 600;
  610. color: #333333;
  611. line-height: 66rpx;
  612. margin-bottom: 30rpx;
  613. }
  614. .info2 {
  615. text-align: center;
  616. font-size: 28rpx;
  617. font-weight: 400;
  618. color: #999999;
  619. line-height: 44rpx;
  620. margin-bottom: 200rpx;
  621. }
  622. .now {
  623. width: 100%;
  624. height: 96rpx;
  625. background: #B42A3E;
  626. border-radius: 8rpx;
  627. font-size: 32rpx;
  628. font-weight: 400;
  629. color: #FFFFFF;
  630. line-height: 96rpx;
  631. margin-bottom: 20rpx;
  632. }
  633. .not_now {
  634. width: 100%;
  635. height: 96rpx;
  636. background: #F5F5F5;
  637. border-radius: 8rpx;
  638. font-size: 32rpx;
  639. font-weight: 400;
  640. color: #999999;
  641. line-height: 96rpx;
  642. }
  643. }
  644. </style>