index.vue 12 KB

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