index.vue 13 KB

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