index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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="logoUrl" 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 v-if="canUseGetUserProfile && code" hover-class="none" @tap="getUserProfile"
  25. class="bg-green btn1">微信登录</button>
  26. <button v-else hover-class="none" open-type="getUserInfo" @getuserinfo="setUserInfo"
  27. class="bg-green btn1">微信登录</button>
  28. <!-- #endif -->
  29. <button hover-class="none" @click="isUp = true" class="btn2">手机号登录</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. getLogo().then(res => {
  91. this.logoUrl = res.data.logo_url;
  92. });
  93. let that = this;
  94. // #ifdef MP
  95. Routine.getCode()
  96. .then(code => {
  97. this.code = code
  98. })
  99. // #endif
  100. // #ifdef H5
  101. document.body.addEventListener('focusout', () => {
  102. setTimeout(() => {
  103. const scrollHeight = document.documentElement.scrollTop || document.body
  104. .scrollTop ||
  105. 0;
  106. window.scrollTo(0, Math.max(scrollHeight - 1, 0));
  107. }, 100);
  108. });
  109. const {
  110. code,
  111. state,
  112. scope
  113. } = options;
  114. this.options = options;
  115. // 获取确认授权code
  116. this.code = code || '';
  117. if (code && this.options.scope !== 'snsapi_base') {
  118. let spread = app.globalData.spid ? app.globalData.spid : '';
  119. //公众号授权登录回调
  120. wechat
  121. .auth(code, state)
  122. .then(res => {
  123. uni.setStorageSync('snsapiCode', code);
  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.uid);
  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. onShow() {
  153. uni.removeStorageSync('form_type_cart');
  154. },
  155. methods: {
  156. back() {
  157. uni.navigateBack();
  158. },
  159. home() {
  160. uni.switchTab({
  161. url: '/pages/index/index'
  162. })
  163. },
  164. // 弹窗关闭
  165. maskClose() {
  166. this.isUp = false;
  167. },
  168. bindPhoneClose(data) {
  169. if (data.isStatus) {
  170. this.isPhoneBox = false;
  171. this.$util.Tips({
  172. title: '登录成功',
  173. icon: 'success'
  174. }, {
  175. tab: 3
  176. });
  177. } else {
  178. this.isPhoneBox = false;
  179. }
  180. },
  181. // #ifdef MP
  182. // 小程序获取手机号码
  183. getphonenumber(e) {
  184. uni.showLoading({
  185. title: '正在登录中'
  186. });
  187. Routine.getCode()
  188. .then(code => {
  189. this.getUserPhoneNumber(e.detail.encryptedData, e.detail.iv, code);
  190. })
  191. .catch(error => {
  192. uni.$emit('closePage', false);
  193. uni.hideLoading();
  194. });
  195. },
  196. // 小程序获取手机号码回调
  197. getUserPhoneNumber(encryptedData, iv, code) {
  198. getUserPhone({
  199. encryptedData: encryptedData,
  200. iv: iv,
  201. code: code,
  202. spread_spid: app.globalData.spid,
  203. spread_code: app.globalData.code
  204. })
  205. .then(res => {
  206. let time = res.data.expires_time - this.$Cache.time();
  207. this.$store.commit('LOGIN', {
  208. token: res.data.token,
  209. time: time
  210. });
  211. this.userInfo = res.data.userInfo;
  212. this.$store.commit('SETUID', res.data.userInfo.uid);
  213. this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
  214. this.$Cache.clear('snsapiKey');
  215. this.$util.Tips({
  216. title: '登录成功',
  217. icon: 'success'
  218. }, {
  219. tab: 3
  220. });
  221. })
  222. .catch(res => {
  223. uni.hideLoading();
  224. });
  225. },
  226. /**
  227. * 获取个人用户信息
  228. */
  229. getUserInfo: function() {
  230. let that = this;
  231. getUserInfo().then(res => {
  232. uni.hideLoading();
  233. that.userInfo = res.data;
  234. that.$store.commit('SETUID', res.data.uid);
  235. that.$store.commit('UPDATE_USERINFO', res.data);
  236. that.$util.Tips({
  237. title: '登录成功',
  238. icon: 'success'
  239. }, {
  240. tab: 3
  241. });
  242. });
  243. },
  244. setUserInfo(e) {
  245. uni.showLoading({
  246. title: '正在登录中'
  247. });
  248. Routine.getCode()
  249. .then(code => {
  250. this.getWxUser(code);
  251. })
  252. .catch(res => {
  253. uni.hideLoading();
  254. });
  255. },
  256. //小程序授权api替换 getUserInfo
  257. getUserProfile() {
  258. uni.showLoading({
  259. title: '正在登录中'
  260. });
  261. let self = this;
  262. Routine.getUserProfile()
  263. .then(res => {
  264. let userInfo = res.userInfo;
  265. userInfo.code = this.code;
  266. userInfo.spread_spid = app.globalData.spid || this.$Cache.get('spread'); //获取推广人ID
  267. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  268. Routine.authUserInfo(userInfo)
  269. .then(res => {
  270. if (res.data.key !== undefined && res.data.key) {
  271. uni.hideLoading();
  272. self.authKey = res.data.key;
  273. self.isPhoneBox = true;
  274. } else {
  275. uni.hideLoading();
  276. let time = res.data.expires_time - self.$Cache.time();
  277. self.$store.commit('LOGIN', {
  278. token: res.data.token,
  279. time: time
  280. });
  281. this.getUserInfo()
  282. }
  283. })
  284. .catch(res => {
  285. uni.hideLoading();
  286. uni.showToast({
  287. title: res.msg,
  288. icon: 'none',
  289. duration: 2000
  290. });
  291. });
  292. })
  293. .catch(res => {
  294. uni.hideLoading();
  295. });
  296. },
  297. getWxUser(code) {
  298. let self = this;
  299. Routine.getUserInfo()
  300. .then(res => {
  301. let userInfo = res.userInfo;
  302. userInfo.code = code;
  303. userInfo.spread_spid = app.globalData.spid; //获取推广人ID
  304. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  305. Routine.authUserInfo(userInfo)
  306. .then(res => {
  307. if (res.data.key !== undefined && res.data.key) {
  308. uni.hideLoading();
  309. self.authKey = res.data.key;
  310. self.isPhoneBox = true;
  311. } else {
  312. uni.hideLoading();
  313. let time = res.data.expires_time - self.$Cache.time();
  314. self.$store.commit('LOGIN', {
  315. token: res.data.token,
  316. time: time
  317. });
  318. self.$util.Tips({
  319. title: res.msg,
  320. icon: 'success'
  321. }, {
  322. tab: 3
  323. });
  324. }
  325. })
  326. .catch(res => {
  327. uni.hideLoading();
  328. uni.showToast({
  329. title: res.msg,
  330. icon: 'none',
  331. duration: 2000
  332. });
  333. });
  334. })
  335. .catch(res => {
  336. uni.hideLoading();
  337. });
  338. },
  339. // #endif
  340. // #ifdef H5
  341. // 获取url后面的参数
  342. getQueryString(name) {
  343. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  344. var reg_rewrite = new RegExp('(^|/)' + name + '/([^/]*)(/|$)', 'i');
  345. var r = window.location.search.substr(1).match(reg);
  346. var q = window.location.pathname.substr(1).match(reg_rewrite);
  347. if (r != null) {
  348. return unescape(r[2]);
  349. } else if (q != null) {
  350. return unescape(q[2]);
  351. } else {
  352. return null;
  353. }
  354. },
  355. // 公众号登录
  356. wechatLogin() {
  357. if (this.authKey) {
  358. this.isUp = true;
  359. return;
  360. }
  361. if (!this.code || this.options.scope !== 'snsapi_base') {
  362. this.$wechat.oAuth('snsapi_userinfo', '/pages/users/wechat_login/index');
  363. }
  364. },
  365. // 输入手机号后的回调
  366. wechatPhone() {
  367. this.$Cache.clear('snsapiKey');
  368. if (this.options.back_url) {
  369. let url = uni.getStorageSync('snRouter');
  370. url = url.indexOf('/pages/index/index') != -1 ? '/' : url;
  371. if (url.indexOf('/pages/users/wechat_login/index') !== -1) {
  372. url = '/';
  373. }
  374. if (!url) {
  375. url = '/pages/index/index';
  376. }
  377. let self = this;
  378. this.isUp = false;
  379. uni.showToast({
  380. title: '登录成功',
  381. icon: 'none'
  382. });
  383. setTimeout(res => {
  384. location.href = url;
  385. }, 800);
  386. } else {
  387. uni.navigateBack();
  388. }
  389. }
  390. // #endif
  391. }
  392. };
  393. </script>
  394. <style lang="scss">
  395. page {
  396. background: #fff;
  397. }
  398. .height-add {
  399. height: 43px;
  400. }
  401. .wechat_login {
  402. padding: 72rpx 34rpx;
  403. .img image {
  404. width: 100%;
  405. }
  406. .btn-wrapper {
  407. margin-top: 86rpx;
  408. padding: 0 66rpx;
  409. button {
  410. width: 100%;
  411. height: 86rpx;
  412. line-height: 86rpx;
  413. margin-bottom: 40rpx;
  414. border-radius: 120rpx;
  415. font-size: 30rpx;
  416. &.btn1 {
  417. color: #fff;
  418. }
  419. &.btn2 {
  420. color: #666666;
  421. border: 1px solid #666666;
  422. }
  423. }
  424. }
  425. }
  426. .title-bar {
  427. position: relative;
  428. display: flex;
  429. align-items: center;
  430. justify-content: center;
  431. font-size: 36rpx;
  432. }
  433. .icon {
  434. position: absolute;
  435. left: 30rpx;
  436. top: 0;
  437. display: flex;
  438. align-items: center;
  439. justify-content: center;
  440. width: 86rpx;
  441. height: 86rpx;
  442. image {
  443. width: 50rpx;
  444. height: 50rpx;
  445. }
  446. }
  447. </style>