index.vue 11 KB

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