index.vue 9.3 KB

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