index.vue 13 KB

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