index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. that.$util.Tips({
  284. title: that.$t(`登录成功`),
  285. icon: 'success'
  286. }, {
  287. tab: 3
  288. });
  289. });
  290. },
  291. setUserInfo(e) {
  292. uni.showLoading({
  293. title: this.$t(`正在登录中`)
  294. });
  295. Routine.getCode()
  296. .then(code => {
  297. this.getWxUser(code);
  298. })
  299. .catch(res => {
  300. uni.hideLoading();
  301. });
  302. },
  303. //小程序授权api替换 getUserInfo
  304. getUserProfile() {
  305. uni.showLoading({
  306. title: this.$t(`正在登录中`)
  307. });
  308. let self = this;
  309. Routine.getUserProfile()
  310. .then(res => {
  311. let userInfo = res.userInfo;
  312. console.log(this.code);
  313. console.log(userInfo);
  314. userInfo.code = this.code;
  315. uni.showToast({
  316. title: "手机号暂未注册,即将跳转注册界面",
  317. icon: 'none',
  318. duration: 2000
  319. });
  320. setTimeout(() => {
  321. // 自动登录失败时,跳转用户注册
  322. uni.navigateTo({
  323. url: '/pages/users/register/index'
  324. })
  325. }, 2000);
  326. // userInfo.spread_spid = app.globalData.spid || this.$Cache.get('spread'); //获取推广人ID
  327. // userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  328. // 模拟授权登录成功
  329. // self.$store.commit('LOGIN', {
  330. // token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ2NC5jcm1lYi5uZXQiLCJhdWQiOiJ2NC5jcm1lYi5uZXQiLCJpYXQiOjE2NjY1Nzk2NzgsIm5iZiI6MTY2NjU3OTY3OCwiZXhwIjoxNjY5MTcxNjc4LCJqdGkiOnsiaWQiOjM0MzkxLCJ0eXBlIjoiYXBpIn19.GA3Lyr3HueKXwz9GWS6KrO4rYKmJzn3l7ko1nsl7E7Q',
  331. // time: 9999999999999
  332. // });
  333. // this.getUserInfo()
  334. // Routine.authUserInfo(userInfo)
  335. // .then(res => {
  336. // if (res.data.key !== undefined && res.data.key) {
  337. // uni.hideLoading();
  338. // self.authKey = res.data.key;
  339. // self.isPhoneBox = true;
  340. // } else {
  341. // uni.hideLoading();
  342. // let time = res.data.expires_time - self.$Cache.time();
  343. // self.$store.commit('LOGIN', {
  344. // token: res.data.token,
  345. // time: time
  346. // });
  347. // this.getUserInfo()
  348. // }
  349. // })
  350. // .catch(res => {
  351. // uni.hideLoading();
  352. // uni.showToast({
  353. // title: res.msg,
  354. // icon: 'none',
  355. // duration: 2000
  356. // });
  357. // });
  358. })
  359. .catch(res => {
  360. uni.hideLoading();
  361. });
  362. },
  363. getWxUser(code) {
  364. let self = this;
  365. Routine.getUserInfo()
  366. .then(res => {
  367. let userInfo = res.userInfo;
  368. userInfo.code = code;
  369. userInfo.spread_spid = app.globalData.spid; //获取推广人ID
  370. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  371. Routine.authUserInfo(userInfo)
  372. .then(res => {
  373. if (res.data.key !== undefined && res.data.key) {
  374. uni.hideLoading();
  375. self.authKey = res.data.key;
  376. self.isPhoneBox = true;
  377. } else {
  378. uni.hideLoading();
  379. let time = res.data.expires_time - self.$Cache.time();
  380. self.$store.commit('LOGIN', {
  381. token: res.data.token,
  382. time: time
  383. });
  384. self.$util.Tips({
  385. title: res.msg,
  386. icon: 'success'
  387. }, {
  388. tab: 3
  389. });
  390. }
  391. })
  392. .catch(res => {
  393. uni.hideLoading();
  394. uni.showToast({
  395. title: res.msg,
  396. icon: 'none',
  397. duration: 2000
  398. });
  399. });
  400. })
  401. .catch(res => {
  402. uni.hideLoading();
  403. });
  404. },
  405. // #endif
  406. // #ifdef H5
  407. // 获取url后面的参数
  408. getQueryString(name) {
  409. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  410. var reg_rewrite = new RegExp('(^|/)' + name + '/([^/]*)(/|$)', 'i');
  411. var r = window.location.search.substr(1).match(reg);
  412. var q = window.location.pathname.substr(1).match(reg_rewrite);
  413. if (r != null) {
  414. return unescape(r[2]);
  415. } else if (q != null) {
  416. return unescape(q[2]);
  417. } else {
  418. return null;
  419. }
  420. },
  421. // 公众号登录
  422. wechatLogin() {
  423. if (!this.code || this.options.scope !== 'snsapi_base') {
  424. this.$wechat.oAuth('snsapi_userinfo', '/pages/users/wechat_login/index');
  425. } else {
  426. if (this.authKey) {
  427. this.isUp = true;
  428. }
  429. }
  430. },
  431. // 输入手机号后的回调
  432. wechatPhone() {
  433. console.log(2222)
  434. this.$Cache.clear('snsapiKey');
  435. if (this.options.back_url) {
  436. let url = uni.getStorageSync('snRouter');
  437. url = url.indexOf('/pages/index/index') != -1 ? '/' : url;
  438. if (url.indexOf('/pages/users/wechat_login/index') !== -1) {
  439. url = '/';
  440. }
  441. if (!url) {
  442. url = '/pages/index/index';
  443. }
  444. this.isUp = false;
  445. uni.showToast({
  446. title: this.$t(`登录成功`),
  447. icon: 'none'
  448. });
  449. setTimeout(res => {
  450. location.href = url;
  451. }, 800);
  452. } else {
  453. this.isUp = false;
  454. uni.showToast({
  455. title: this.$t(`登录成功`),
  456. icon: 'none'
  457. });
  458. setTimeout(res => {
  459. location.href = '/pages/index/index';
  460. }, 800);
  461. }
  462. }
  463. // #endif
  464. }
  465. };
  466. </script>
  467. <style lang="scss">
  468. page {
  469. background: #fff;
  470. }
  471. .wechat_login {
  472. padding: 72rpx 34rpx;
  473. .img image {
  474. width: 100%;
  475. }
  476. .btn-wrapper {
  477. margin-top: 86rpx;
  478. padding: 0 66rpx;
  479. button {
  480. width: 100%;
  481. height: 86rpx;
  482. line-height: 86rpx;
  483. margin-bottom: 40rpx;
  484. border-radius: 120rpx;
  485. font-size: 30rpx;
  486. &.btn1 {
  487. color: #fff;
  488. }
  489. &.btn2 {
  490. color: #666666;
  491. border: 1px solid #666666;
  492. }
  493. }
  494. }
  495. }
  496. .title-bar {
  497. position: relative;
  498. display: flex;
  499. align-items: center;
  500. justify-content: center;
  501. font-size: 36rpx;
  502. }
  503. .icon {
  504. position: absolute;
  505. left: 30rpx;
  506. top: 0;
  507. display: flex;
  508. align-items: center;
  509. justify-content: center;
  510. width: 86rpx;
  511. height: 86rpx;
  512. image {
  513. width: 50rpx;
  514. height: 50rpx;
  515. }
  516. }
  517. </style>