index.vue 12 KB

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