index.vue 11 KB

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