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