index.vue 15 KB

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