index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. {{$t(`账户登录`)}}
  13. </view>
  14. <!-- #endif -->
  15. <view class="wechat_login">
  16. <view class="img">
  17. <image src="../static/wechat_login.png" mode="widthFix"></image>
  18. </view>
  19. <view class="btn-wrapper">
  20. <!-- #ifdef H5 -->
  21. <button hover-class="none" @click="wechatLogin" class="bg-green btn1">{{$t(`微信登录`)}}</button>
  22. <!-- #endif -->
  23. <!-- #ifdef MP -->
  24. <button hover-class="none" v-if="mp_is_new" @tap="userLogin"
  25. class="bg-green btn1">{{$t(`微信登录`)}}</button>
  26. <button v-else-if="canUseGetUserProfile && code" hover-class="none" @tap="getUserProfile"
  27. class="bg-green btn1">{{$t(`微信登录`)}}</button>
  28. <button v-else hover-class="none" open-type="getUserInfo" @getuserinfo="setUserInfo"
  29. class="bg-green btn1">{{$t(`微信登录`)}}</button>
  30. <!-- #endif -->
  31. <button hover-class="none" @click="isUp = true" class="btn2">{{$t(`手机号登录`)}}</button>
  32. </view>
  33. </view>
  34. <block v-if="isUp">
  35. <mobileLogin :isUp="isUp" @close="maskClose" :authKey="authKey" @wechatPhone="wechatPhone"></mobileLogin>
  36. </block>
  37. <block v-if="isPhoneBox">
  38. <routinePhone :logoUrl="logoUrl" :isPhoneBox="isPhoneBox" @close="bindPhoneClose" :authKey="authKey">
  39. </routinePhone>
  40. </block>
  41. <block>
  42. <!-- <uni-popup ref="popup" type="bottom" background-color="#fff"> -->
  43. <editUserModal :isShow="isShow" @closeEdit="closeEdit" @editSuccess="editSuccess">
  44. </editUserModal>
  45. <!-- </uni-popup> -->
  46. </block>
  47. </view>
  48. </template>
  49. <script>
  50. const app = getApp();
  51. let statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
  52. import mobileLogin from '../components/login_mobile/index.vue';
  53. import routinePhone from '../components/login_mobile/routine_phone.vue';
  54. import editUserModal from '@/components/eidtUserModal/index.vue'
  55. import {
  56. getLogo,
  57. silenceAuth,
  58. getUserPhone,
  59. wechatAuthV2,
  60. authLogin
  61. } from '@/api/public';
  62. import {
  63. LOGO_URL,
  64. EXPIRES_TIME,
  65. USER_INFO,
  66. STATE_R_KEY
  67. } from '@/config/cache';
  68. import {
  69. getUserInfo
  70. } from '@/api/user.js';
  71. import Routine from '@/libs/routine';
  72. import wechat from '@/libs/wechat';
  73. import colors from '@/mixins/color.js';
  74. import Auth from '@/libs/wechat.js';
  75. export default {
  76. mixins: [colors],
  77. data() {
  78. return {
  79. isUp: false,
  80. phone: '',
  81. statusBarHeight: statusBarHeight,
  82. isHome: false,
  83. isPhoneBox: false,
  84. isShow: false,
  85. logoUrl: '',
  86. code: '',
  87. authKey: '',
  88. options: '',
  89. userInfo: {},
  90. codeNum: 0,
  91. canUseGetUserProfile: false,
  92. mp_is_new: this.$Cache.get('MP_VERSION_ISNEW') || false
  93. };
  94. },
  95. components: {
  96. mobileLogin,
  97. routinePhone,
  98. editUserModal
  99. },
  100. onLoad(options) {
  101. if (uni.getUserProfile) {
  102. this.canUseGetUserProfile = true
  103. }
  104. getLogo().then(res => {
  105. this.logoUrl = res.data.logo_url;
  106. });
  107. let that = this;
  108. // #ifdef MP
  109. Routine.getCode()
  110. .then(code => {
  111. this.code = code
  112. })
  113. // #endif
  114. // #ifdef H5
  115. document.body.addEventListener('focusout', () => {
  116. setTimeout(() => {
  117. const scrollHeight = document.documentElement.scrollTop || document.body
  118. .scrollTop ||
  119. 0;
  120. window.scrollTo(0, Math.max(scrollHeight - 1, 0));
  121. }, 100);
  122. });
  123. const {
  124. code,
  125. state,
  126. scope
  127. } = options;
  128. this.options = options;
  129. // 获取确认授权code
  130. this.code = code || '';
  131. if (code && this.options.scope !== 'snsapi_base') {
  132. let spread = app.globalData.spid ? app.globalData.spid : '';
  133. //公众号授权登录回调
  134. wechat
  135. .auth(code, state)
  136. .then(res => {
  137. if (res.key !== undefined && res.key) {
  138. that.authKey = res.key;
  139. that.isUp = true;
  140. } else {
  141. let time = res.expires_time - that.$Cache.time();
  142. that.$store.commit('LOGIN', {
  143. token: res.token,
  144. time: time
  145. });
  146. that.userInfo = res.userInfo;
  147. that.$store.commit('SETUID', res.userInfo.uid);
  148. that.$store.commit('UPDATE_USERINFO', res.userInfo);
  149. that.wechatPhone();
  150. }
  151. })
  152. .catch(err => {
  153. uni.hideLoading();
  154. uni.showToast({
  155. title: err,
  156. icon: 'none',
  157. duration: 2000
  158. });
  159. });
  160. } else if (code && this.options.scope == 'snsapi_base' && !this.$Cache.has('snsapiKey')) {
  161. //公众号静默授权
  162. let snsapiBase = 'snsapi_base';
  163. let urlData = location.pathname + location.search;
  164. // if (!that.$store.getters.isLogin && uni.getStorageSync('authIng')) {
  165. // uni.setStorageSync('authIng', false)
  166. // }
  167. if (options.back_url) {
  168. uni.setStorageSync('snRouter', options.back_url);
  169. }
  170. if (!that.$store.getters.isLogin && Auth.isWeixin()) {
  171. let code
  172. if (options.code instanceof Array) {
  173. code = options.code[options.code.length - 1]
  174. } else {
  175. code = options.code
  176. }
  177. if (code && code != uni.getStorageSync('snsapiCode') && !this.$Cache.has('snsapiKey')) {
  178. // 存储静默授权code
  179. uni.setStorageSync('snsapiCode', code);
  180. uni.setStorageSync('authIng', true)
  181. silenceAuth({
  182. code: code,
  183. spread: that.$Cache.get('spread'),
  184. spid: that.$Cache.get('spread')
  185. })
  186. .then(res => {
  187. // uni.setStorageSync('authIng', false)
  188. // uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(options.query
  189. // .back_url)));
  190. if (res.data.key !== undefined && res.data.key) {
  191. this.$Cache.set('snsapiKey', res.data.key);
  192. uni.navigateTo({
  193. url: '/pages/users/wechat_login/index'
  194. })
  195. }
  196. })
  197. .catch(error => {
  198. uni.setStorageSync('authIng', false)
  199. let url = ''
  200. if (options.back_url instanceof Array) {
  201. url = options.back_url[options.back_url.length - 1]
  202. } else {
  203. url = options.back_url
  204. }
  205. if (!that.$Cache.has('snsapiKey')) {
  206. Auth.oAuth(snsapiBase, url);
  207. }
  208. });
  209. } else {
  210. Auth.oAuth(snsapiBase, urlData)
  211. }
  212. } else {
  213. if (options.query.back_url) {
  214. location.replace(uni.getStorageSync('snRouter'));
  215. }
  216. }
  217. } else if (!this.$Cache.has('snsapiKey')) {
  218. let urlData = location.pathname + location.search;
  219. Auth.oAuth('snsapi_base', urlData)
  220. }
  221. // #endif
  222. let pages = getCurrentPages();
  223. let prePage = pages[pages.length - 2];
  224. if (prePage && prePage.route == 'pages/order_addcart/order_addcart') {
  225. this.isHome = true;
  226. } else {
  227. this.isHome = false;
  228. }
  229. },
  230. methods: {
  231. // 小程序 22.11.8日删除getUserProfile 接口获取用户昵称头像
  232. userLogin() {
  233. Routine.getCode()
  234. .then(code => {
  235. uni.showLoading({
  236. title: this.$t(`正在登录中`)
  237. });
  238. authLogin({
  239. code,
  240. spread_spid: app.globalData.spid,
  241. spread_code: app.globalData.code
  242. }).then(res => {
  243. if (res.data.key !== undefined && res.data.key) {
  244. uni.hideLoading();
  245. this.authKey = res.data.key;
  246. this.isPhoneBox = true;
  247. } else {
  248. uni.hideLoading();
  249. let time = res.data.expires_time - this.$Cache.time();
  250. this.$store.commit('LOGIN', {
  251. token: res.data.token,
  252. time: time
  253. });
  254. this.getUserInfo(res.data.new_user || 0)
  255. }
  256. })
  257. .catch(err => {
  258. uni.hideLoading();
  259. uni.showToast({
  260. title: err,
  261. icon: 'none',
  262. duration: 2000
  263. });
  264. });
  265. })
  266. .catch(err => {
  267. console.log(err)
  268. });
  269. },
  270. editSuccess() {
  271. this.isShow = false
  272. },
  273. closeEdit() {
  274. this.isShow = false
  275. uni.navigateBack();
  276. },
  277. back() {
  278. uni.navigateBack();
  279. },
  280. home() {
  281. uni.switchTab({
  282. url: '/pages/index/index'
  283. })
  284. },
  285. // 弹窗关闭
  286. maskClose(new_user) {
  287. this.isUp = false;
  288. if (new_user) {
  289. this.isShow = true
  290. }
  291. },
  292. bindPhoneClose(data) {
  293. if (data.isStatus) {
  294. this.isPhoneBox = false;
  295. if (data.new_user) {
  296. this.isShow = true
  297. } else {
  298. this.$util.Tips({
  299. title: this.$t(`登录成功`),
  300. icon: 'success'
  301. }, {
  302. tab: 3
  303. });
  304. }
  305. } else {
  306. this.isPhoneBox = false;
  307. }
  308. },
  309. // #ifdef MP
  310. // 小程序获取手机号码
  311. getphonenumber(e) {
  312. uni.showLoading({
  313. title: this.$t(`正在登录中`)
  314. });
  315. Routine.getCode()
  316. .then(code => {
  317. this.getUserPhoneNumber(e.detail.encryptedData, e.detail.iv, code);
  318. })
  319. .catch(error => {
  320. uni.$emit('closePage', false);
  321. uni.hideLoading();
  322. });
  323. },
  324. // 小程序获取手机号码回调
  325. getUserPhoneNumber(encryptedData, iv, code) {
  326. getUserPhone({
  327. encryptedData: encryptedData,
  328. iv: iv,
  329. code: code,
  330. spread_spid: app.globalData.spid,
  331. spread_code: app.globalData.code
  332. })
  333. .then(res => {
  334. let time = res.data.expires_time - this.$Cache.time();
  335. this.$store.commit('LOGIN', {
  336. token: res.data.token,
  337. time: time
  338. });
  339. this.userInfo = res.data.userInfo;
  340. this.$store.commit('SETUID', res.data.userInfo.uid);
  341. this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  342. this.$Cache.clear('snsapiKey');
  343. this.$util.Tips({
  344. title: this.$t(`登录成功`),
  345. icon: 'success'
  346. }, {
  347. tab: 3
  348. });
  349. })
  350. .catch(res => {
  351. uni.hideLoading();
  352. });
  353. },
  354. /**
  355. * 获取个人用户信息
  356. */
  357. getUserInfo: function(new_user) {
  358. let that = this;
  359. getUserInfo().then(res => {
  360. uni.hideLoading();
  361. that.userInfo = res.data;
  362. that.$store.commit('SETUID', res.data.uid);
  363. that.$store.commit('UPDATE_USERINFO', res.data);
  364. if (new_user) {
  365. that.$util.Tips({
  366. title: that.$t(`登录成功`),
  367. icon: 'success'
  368. });
  369. this.isShow = true
  370. } else {
  371. that.$util.Tips({
  372. title: that.$t(`登录成功`),
  373. icon: 'success'
  374. }, {
  375. tab: 3
  376. });
  377. }
  378. });
  379. },
  380. setUserInfo(e) {
  381. uni.showLoading({
  382. title: this.$t(`正在登录中`)
  383. });
  384. Routine.getCode()
  385. .then(code => {
  386. this.getWxUser(code);
  387. })
  388. .catch(res => {
  389. uni.hideLoading();
  390. });
  391. },
  392. //小程序授权api替换 getUserInfo
  393. getUserProfile() {
  394. uni.showLoading({
  395. title: this.$t(`正在登录中`)
  396. });
  397. let self = this;
  398. Routine.getUserProfile()
  399. .then(res => {
  400. let userInfo = res.userInfo;
  401. userInfo.code = this.code;
  402. userInfo.spread_spid = app.globalData.spid || this.$Cache.get('spread'); //获取推广人ID
  403. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  404. Routine.authUserInfo(userInfo)
  405. .then(res => {
  406. if (res.data.key !== undefined && res.data.key) {
  407. uni.hideLoading();
  408. self.authKey = res.data.key;
  409. self.isPhoneBox = true;
  410. } else {
  411. uni.hideLoading();
  412. let time = res.data.expires_time - self.$Cache.time();
  413. self.$store.commit('LOGIN', {
  414. token: res.data.token,
  415. time: time
  416. });
  417. this.getUserInfo()
  418. }
  419. })
  420. .catch(res => {
  421. uni.hideLoading();
  422. uni.showToast({
  423. title: res.msg,
  424. icon: 'none',
  425. duration: 2000
  426. });
  427. });
  428. })
  429. .catch(res => {
  430. uni.hideLoading();
  431. });
  432. },
  433. getWxUser(code) {
  434. let self = this;
  435. Routine.getUserInfo()
  436. .then(res => {
  437. let userInfo = res.userInfo;
  438. userInfo.code = code;
  439. userInfo.spread_spid = app.globalData.spid; //获取推广人ID
  440. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  441. Routine.authUserInfo(userInfo)
  442. .then(res => {
  443. if (res.data.key !== undefined && res.data.key) {
  444. uni.hideLoading();
  445. self.authKey = res.data.key;
  446. self.isPhoneBox = true;
  447. } else {
  448. uni.hideLoading();
  449. let time = res.data.expires_time - self.$Cache.time();
  450. self.$store.commit('LOGIN', {
  451. token: res.data.token,
  452. time: time
  453. });
  454. self.$util.Tips({
  455. title: res.msg,
  456. icon: 'success'
  457. }, {
  458. tab: 3
  459. });
  460. }
  461. })
  462. .catch(res => {
  463. uni.hideLoading();
  464. uni.showToast({
  465. title: res.msg,
  466. icon: 'none',
  467. duration: 2000
  468. });
  469. });
  470. })
  471. .catch(res => {
  472. uni.hideLoading();
  473. });
  474. },
  475. // #endif
  476. // #ifdef H5
  477. // 获取url后面的参数
  478. getQueryString(name) {
  479. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  480. var reg_rewrite = new RegExp('(^|/)' + name + '/([^/]*)(/|$)', 'i');
  481. var r = window.location.search.substr(1).match(reg);
  482. var q = window.location.pathname.substr(1).match(reg_rewrite);
  483. if (r != null) {
  484. return unescape(r[2]);
  485. } else if (q != null) {
  486. return unescape(q[2]);
  487. } else {
  488. return null;
  489. }
  490. },
  491. // 公众号登录
  492. wechatLogin() {
  493. if (!this.code || this.options.scope !== 'snsapi_base') {
  494. this.$wechat.oAuth('snsapi_userinfo', '/pages/users/wechat_login/index');
  495. } else {
  496. if (this.authKey) {
  497. this.isUp = true;
  498. }
  499. }
  500. },
  501. // 输入手机号后的回调
  502. wechatPhone() {
  503. this.$Cache.clear('snsapiKey');
  504. if (this.options.back_url) {
  505. let url = uni.getStorageSync('snRouter');
  506. url = url.indexOf('/pages/index/index') != -1 ? '/' : url;
  507. if (url.indexOf('/pages/users/wechat_login/index') !== -1) {
  508. url = '/';
  509. }
  510. if (!url) {
  511. url = '/pages/index/index';
  512. }
  513. this.isUp = false;
  514. uni.showToast({
  515. title: this.$t(`登录成功`),
  516. icon: 'none'
  517. });
  518. setTimeout(res => {
  519. location.href = url;
  520. }, 800);
  521. } else {
  522. this.isUp = false;
  523. uni.showToast({
  524. title: this.$t(`登录成功`),
  525. icon: 'none'
  526. });
  527. setTimeout(res => {
  528. location.href = '/pages/index/index';
  529. }, 800);
  530. }
  531. }
  532. // #endif
  533. }
  534. };
  535. </script>
  536. <style lang="scss">
  537. page {
  538. background: #fff;
  539. }
  540. .wechat_login {
  541. padding: 72rpx 34rpx;
  542. .img image {
  543. width: 100%;
  544. }
  545. .btn-wrapper {
  546. margin-top: 86rpx;
  547. padding: 0 66rpx;
  548. button {
  549. width: 100%;
  550. height: 86rpx;
  551. line-height: 86rpx;
  552. margin-bottom: 40rpx;
  553. border-radius: 120rpx;
  554. font-size: 30rpx;
  555. &.btn1 {
  556. color: #fff;
  557. }
  558. &.btn2 {
  559. color: #666666;
  560. border: 1px solid #666666;
  561. }
  562. }
  563. }
  564. }
  565. .title-bar {
  566. position: relative;
  567. display: flex;
  568. align-items: center;
  569. justify-content: center;
  570. font-size: 36rpx;
  571. }
  572. .icon {
  573. position: absolute;
  574. left: 30rpx;
  575. top: 0;
  576. display: flex;
  577. align-items: center;
  578. justify-content: center;
  579. width: 86rpx;
  580. height: 86rpx;
  581. image {
  582. width: 50rpx;
  583. height: 50rpx;
  584. }
  585. }
  586. </style>