index.vue 14 KB

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