index.vue 11 KB

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