金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

708 lines
16 KiB

4 years ago
  1. <template>
  2. <view id="login-type">
  3. <view class="phone__warning">
  4. <image src="https://cdn.guojiang.club/app_guojiang_logo.png"></image>
  5. <!--<i class="iconfont icon-anquanjinggao"></i>-->
  6. </view>
  7. <!-- #ifdef MP-WEIXIN -->
  8. <button class="wechat-phoneBtn phone__btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  9. <i class="iconfont icon-hudong"></i>
  10. <view class="btn__text">微信登录</view>
  11. </button>
  12. <!-- #endif -->
  13. <!-- #ifdef APP-PLUS -->
  14. <button class="wechat-phoneBtn phone__btn" @click="appLogin">
  15. <i class="iconfont icon-hudong"></i>
  16. <view class="btn__text">微信登录</view>
  17. </button>
  18. <!-- #endif -->
  19. <!-- #ifdef MP-WEIXIN-->
  20. <view class="phone_login" @tap="jumpLogin">
  21. 手机号登录
  22. </view>
  23. <!-- #endif -->
  24. <!-- #ifdef APP-PLUS -->
  25. <view class="phone_login" @tap="jumploginType">
  26. 手机号登录
  27. </view>
  28. <!-- #endif -->
  29. <view class="select" @tap="chageAgreement">
  30. <text> 注册/登录即代表同意果酱使用协议</text>
  31. </view>
  32. <view class="mask" :hidden="!showAgreement">
  33. </view>
  34. <view class="greement-box" :hidden="!showAgreement">
  35. <view class="title mx-1px-bottom">
  36. 用户协议
  37. </view>
  38. <view class="content">
  39. <!-- <u-parse :content="agreement.user_agreement" /> -->
  40. </view>
  41. <view class="btn mx-1px-top" @tap="chageAgreement">
  42. 确定
  43. </view>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import {
  49. pageLogin,
  50. getUrl,
  51. config,
  52. wechat,
  53. GetUrl
  54. } from '@/common/js/utils.js';
  55. import uParse from '@/components/gaoyia-parse/parse.vue';
  56. export default {
  57. components: {
  58. uParse
  59. },
  60. data() {
  61. return {
  62. code: '',
  63. url: '',
  64. logo: '',
  65. author: config.PACKAGES.author,
  66. config: '',
  67. showAgreement: false,
  68. agreement: '',
  69. open_id: '',
  70. h5_url: ''
  71. };
  72. },
  73. onShow() {
  74. var token = this.$cookieStorage.get('user_token');
  75. // this.setData({
  76. // token: token
  77. // });
  78. this.token = token;
  79. var initInfo = this.$cookieStorage.get('init');
  80. if (initInfo && initInfo.shop_show_logo) {
  81. this.setData({
  82. logo: initInfo.shop_show_logo
  83. });
  84. }
  85. if (token) {
  86. wx.switchTab({
  87. url: '/pages/user/personal/personal'
  88. });
  89. } else {
  90. // #ifdef MP-WEIXIN
  91. this.wxLogin();
  92. // #endif
  93. }
  94. },
  95. onLoad(e) {
  96. // 第三方平台配置颜色
  97. var bgConfig = this.$cookieStorage.get('globalConfig') || '';
  98. this.setData({
  99. config: bgConfig
  100. });
  101. if (e.url) {
  102. // #ifdef APP-PLUS
  103. console.log(e.url)
  104. this.url = e.url
  105. // #endif
  106. // #ifdef MP-WEIXIN || H5
  107. this.url = decodeURIComponent(e.url);
  108. console.log('this.url',this.url)
  109. // #endif
  110. }
  111. if (e.shop_id) {
  112. this.$cookieStorage.set('shop_id', e.shop_id);
  113. }
  114. this.getGreementInfo();
  115. // #ifdef APP-PLUS
  116. var appid = plus.runtime.appid;
  117. // #endif
  118. // #ifdef H5
  119. var env = {
  120. isIPad: /ipad/i.test(window.navigator.userAgent),
  121. isIphone: /iphone|ipad|ipod/i.test(window.navigator.userAgent),
  122. isWechat: /MicroMessenger/i.test(window.navigator.userAgent)
  123. }
  124. if(env.isWechat){
  125. if(e.openid){
  126. this.$cookieStorage.set('openid',e.openid)
  127. }
  128. var openid = this.$cookieStorage.get('openid') || e.openid;
  129. if(openid){
  130. this.postNewQuickLogin(openid);
  131. } else{
  132. let origin = window.location.origin;
  133. var url = origin+'/pages/user/register/register';
  134. window.location.href = this.$config.GLOBAL.baseUrl+'oauth/wxOauth?redirect_url='+encodeURIComponent(url);
  135. }
  136. } else{
  137. let url = this.url;
  138. wx.navigateTo({
  139. url:'/pages/user/loginType/loginType?url='+url
  140. })
  141. }
  142. // #endif
  143. },
  144. methods: {
  145. jumploginType(){
  146. var url = this.url;
  147. if(url){
  148. wx.navigateTo({
  149. url:'/pages/user/loginType/loginType?url='+encodeURIComponent(JSON.stringify(this.url))+'&is_app_bind=app'
  150. })
  151. } else{
  152. wx.navigateTo({
  153. url:'/pages/user/loginType/loginType?is_app_bind=app'
  154. })
  155. }
  156. },
  157. //h5调用微信登陆接口
  158. postNewQuickLogin(open_id){
  159. wx.showLoading({
  160. title: '正在登录',
  161. mask: true
  162. });
  163. this.$http.post({
  164. api:'api/oauth/newQuickLogin',
  165. data:{
  166. open_id:open_id
  167. }
  168. }).then(res=>{
  169. res = res.data;
  170. if(res.status){
  171. if(res.data.access_token){
  172. var access_token = res.data.token_type + ' ' + res.data.access_token;
  173. var expires_in = res.data.expires_in || 315360000;
  174. this.$cookieStorage.set("user_token", access_token, expires_in);
  175. //如果用户没有绑定手机号
  176. if(res.data.has_bind_mobile == 0){
  177. wx.reLaunch({
  178. url:'/pages/user/bindingphone/bindingphone'
  179. })
  180. } else{
  181. var url = this.url;
  182. if(url){
  183. wx.redirectTo({
  184. url:'/'+ url,
  185. fail:res=>{
  186. wx.switchTab({
  187. url:'/'+ url
  188. })
  189. }
  190. })
  191. } else{
  192. wx.switchTab({
  193. url:'/pages/user/personal/personal'
  194. })
  195. }
  196. }
  197. } else{
  198. wx.showModal({
  199. content: res.message || '请求失败,请重试',
  200. showCancel: false
  201. });
  202. }
  203. } else{
  204. wx.showModal({
  205. content: res.message || '请求失败,请重试',
  206. showCancel: false
  207. });
  208. }
  209. wx.hideLoading()
  210. }).catch(rej=>{
  211. wx.showModal({
  212. content: rej.message || '请求失败,请重试',
  213. showCancel: false
  214. });
  215. wx.hideLoading()
  216. })
  217. },
  218. // app调用登录接口
  219. appLogin() {
  220. var that = this;
  221. uni.getProvider({
  222. service: 'oauth',
  223. success: function(res) {
  224. uni.login({
  225. provider: "weixin",
  226. success: function(loginRes) {
  227. uni.getUserInfo({
  228. provider: 'weixin',
  229. success: function (infoRes) {
  230. var app_info = {
  231. nickName:infoRes.userInfo.nickName,
  232. avatarUrl:infoRes.userInfo.avatarUrl
  233. }
  234. that.$cookieStorage.set('app_user_info',app_info)
  235. }
  236. });
  237. console.log('loginRes',loginRes);
  238. that.postUnionidLogin(loginRes)
  239. }
  240. })
  241. }
  242. })
  243. },
  244. // app登录请求的接口
  245. postUnionidLogin(data) {
  246. var newobj = {
  247. openid: data.authResult.openid,
  248. unionid: data.authResult.unionid
  249. }
  250. this.$http.post({
  251. api: 'api/oauth/app/unionid/login',
  252. data: {
  253. openid: data.authResult.openid,
  254. unionid: data.authResult.unionid
  255. }
  256. }).then(res => {
  257. res = res.data;
  258. if (res.data) {
  259. if (res.data.access_token) {
  260. var access_token = res.data.token_type + ' ' + res.data.access_token;
  261. var expires_in = res.data.expires_in || 315360000;
  262. this.$cookieStorage.set("user_token", access_token, expires_in);
  263. if (this.url) {
  264. wx.redirectTo({
  265. url: "/" + this.url,
  266. fail: res => {
  267. wx.switchTab({
  268. url: "/" + this.url
  269. });
  270. }
  271. });
  272. } else {
  273. wx.switchTab({
  274. url: '/pages/user/personal/personal'
  275. });
  276. }
  277. } else {
  278. var open_id = res.data.open_id;
  279. wx.showToast({
  280. title: '需先绑定手机号',
  281. duration: 3000,
  282. success: res => {
  283. var mobileurl = '/pages/user/loginType/loginType?url=' + encodeURIComponent(JSON.stringify(this.url))+ '&open_id=' +encodeURIComponent(JSON.stringify(open_id)) +'&is_app_weixin=appweixin'
  284. wx.redirectTo({
  285. url: mobileurl
  286. })
  287. }
  288. })
  289. }
  290. } else {
  291. wx.showModal({
  292. content: res.message || '请求失败,请重试',
  293. showCancel: false
  294. });
  295. }
  296. })
  297. },
  298. chageAgreement() {
  299. this.setData({
  300. showAgreement: !this.showAgreement
  301. });
  302. },
  303. // 获取用户协议
  304. getGreementInfo() {
  305. this.$http.get({
  306. api: 'api/user/agreement'
  307. }).then(res => {
  308. if (res.statusCode == 200) {
  309. res = res.data;
  310. if (res.status) {
  311. this.setData({
  312. agreement: res.data
  313. });
  314. }
  315. }
  316. });
  317. },
  318. wxLogin() {
  319. //H5判断是否是微信环境
  320. //#ifdef H5
  321. /* if (!wechat.isWechat()) {
  322. this.jumpLogin();
  323. return;
  324. } else {
  325. var url = this.$route.query.url;
  326. var obj_url = GetUrl(url);
  327. if (obj_url == undefined || obj_url.openid == undefined) {
  328. this.officialAccountLogin();
  329. } else {
  330. this.open_id = obj_url.openid;
  331. this.url = url;
  332. this.quickLoginByOpenId(url)
  333. }
  334. */
  335. // }
  336. //#endif
  337. // #ifdef APP-PLUS
  338. this.jumpLogin();
  339. return;
  340. // #endif
  341. //#ifdef MP-WEIXIN
  342. wx.showLoading({
  343. title: '正在自动登录',
  344. mask: true
  345. });
  346. wx.login({
  347. success: res => {
  348. if (res.code) {
  349. this.autoLogin(res.code);
  350. } else {
  351. wx.showToast({
  352. title: '获取code失败',
  353. icon: 'error'
  354. });
  355. }
  356. },
  357. });
  358. // #endif
  359. },
  360. quickLoginByOpenId(url) {
  361. var token = this.$cookieStorage.get('user_token');
  362. if (wechat.isWechat() && !token) {
  363. this.$http.post({
  364. api: 'api/oauth/official-account/quick-login',
  365. data: {
  366. open_id: this.open_id
  367. }
  368. }).then(res => {
  369. if (res.statusCode == 200) {
  370. res = res.data;
  371. if (res.status) {
  372. if (res.data.open_id != '') {
  373. var url_ = '/pages/user/loginType/loginType?url=' + decodeURIComponent(this.url) + '&open_id=' + this.open_id;
  374. wx.redirectTo({
  375. url: url_
  376. });
  377. return;
  378. }
  379. if (res.data.access_token) {
  380. var access_token = res.data.token_type + ' ' + res.data.access_token;
  381. var expires_in = res.data.expires_in || 315360000;
  382. this.$cookieStorage.set("user_token", access_token, expires_in);
  383. }
  384. if (this.url) {
  385. wx.redirectTo({
  386. url: "/" + this.url,
  387. fail: rej => {
  388. wx.switchTab({
  389. url: "/" + this.url
  390. });
  391. }
  392. });
  393. } else {
  394. wx.switchTab({
  395. url: '/pages/user/personal/personal'
  396. });
  397. }
  398. } else {
  399. wx.showModal({
  400. content: res.message || '请求失败,请重试',
  401. showCancel: false
  402. });
  403. }
  404. } else {
  405. wx.showModal({
  406. title: '',
  407. content: res.message || '请求失败,请重试',
  408. showCancel: false
  409. });
  410. }
  411. wx.hideLoading();
  412. }, err => {
  413. wx.hideLoading();
  414. });
  415. }
  416. },
  417. officialAccountLogin() {
  418. var url = this.url ? encodeURIComponent(this.url) : 'pages/user/personal/personal'
  419. var redirect_url = config.GLOBAL.mobile_url + 'pages/user/register/register?url=' + url;
  420. console.log(redirect_url);
  421. this.$http.get({
  422. api: 'api/oauth/official-account/get-redirect-url',
  423. data: {
  424. redirect_url: redirect_url
  425. }
  426. }).then(res => {
  427. if (res.statusCode == 200) {
  428. res = res.data;
  429. if (res.status) {
  430. if (res.data.url) {
  431. window.location.href = res.data.url;
  432. }
  433. } else {
  434. wx.showModal({
  435. title: '',
  436. content: '请求失败',
  437. showCancel: false
  438. });
  439. }
  440. } else {
  441. wx.showModal({
  442. title: '',
  443. content: '请求失败',
  444. showCancel: false
  445. });
  446. }
  447. wx.hideLoading();
  448. }, err => {
  449. wx.hideLoading();
  450. });
  451. },
  452. jumpLogin() {
  453. if (this.url) {
  454. wx.navigateTo({
  455. url: '/pages/user/loginType/loginType?url=' + encodeURIComponent(this.url) + '&open_id=' + this.open_id
  456. });
  457. } else {
  458. wx.navigateTo({
  459. url: '/pages/user/loginType/loginType?open_id=' + this.open_id
  460. });
  461. }
  462. },
  463. getPhoneNumber(e) {
  464. if (e.detail.encryptedData) {
  465. wx.login({
  466. success: res => {
  467. if (res.code) {
  468. this.setData({
  469. code: res.code
  470. });
  471. this.phone(e);
  472. } else {
  473. wx.showModal({
  474. content: " 获取code失败",
  475. showCancel: false
  476. });
  477. }
  478. }
  479. });
  480. return;
  481. } else {
  482. this.jumpLogin();
  483. }
  484. },
  485. phone(e) {
  486. wx.showLoading({
  487. title: '正在登录',
  488. mask: true
  489. });
  490. this.$http.post({
  491. api: 'api/oauth/miniprogram/mobile',
  492. data: {
  493. open_type: 'miniprogram',
  494. code: this.code,
  495. encryptedData: e.detail.encryptedData,
  496. iv: e.detail.iv,
  497. open_id: this.open_id,
  498. shop_id: this.$cookieStorage.get('shop_id') || '',
  499. agent_code: this.$cookieStorage.get('coupon_agent_code') || this.$cookieStorage.get('agent_code') || '',
  500. clerk_id: this.$cookieStorage.get('clerk_id') || '',
  501. agent_code_time: this.$cookieStorage.get('agent_code_time') || '',
  502. shop_id_time: this.$cookieStorage.get('shop_id_time') || ''
  503. }
  504. }).then(res => {
  505. if (res.statusCode == 200) {
  506. res = res.data;
  507. if (res.data.access_token) {
  508. var access_token = res.data.token_type + ' ' + res.data.access_token;
  509. var expires_in = res.data.expires_in || 315360000; // debugger;
  510. this.$cookieStorage.set("user_token", access_token, expires_in); // this.$cookieStorage.set("user_token",access_token,expires_in);
  511. // wx.setStorageSync("user_token",access_token);
  512. if (this.url) {
  513. var path = ['pages/entity/store/store', 'pages/index/index/index',
  514. 'pages/index/classification/classification', 'pages/store/tabCart/tabCart', 'pages/user/personal/personal',
  515. 'pages/travels/index/index', 'pages/user/collar/collar'
  516. ];
  517. var pathIndex = path.indexOf(this.url);
  518. if (pathIndex == -1) {
  519. wx.redirectTo({
  520. url: "/" + this.url
  521. });
  522. } else {
  523. wx.switchTab({
  524. url: "/" + this.url
  525. });
  526. }
  527. } else {
  528. wx.switchTab({
  529. url: '/pages/user/personal/personal'
  530. });
  531. }
  532. } else {
  533. wx.showModal({
  534. content: res.message || '请求失败,请重试',
  535. showCancel: false
  536. });
  537. }
  538. } else {
  539. wx.showModal({
  540. content: '请求失败,请重试',
  541. showCancel: false
  542. });
  543. }
  544. wx.hideLoading();
  545. }).catch(rej => {
  546. wx.hideLoading();
  547. wx.showModal({
  548. content: '请求失败,请重试',
  549. showCancel: false
  550. });
  551. });
  552. },
  553. autoLogin(code) {
  554. this.$http.post({
  555. api: 'api/oauth/miniprogram/login',
  556. data: {
  557. code: code,
  558. open_type: 'miniprogram',
  559. shop_id: this.$cookieStorage.get('shop_id') || '',
  560. agent_code: this.$cookieStorage.get('coupon_agent_code') || this.$cookieStorage.get('agent_code') || '',
  561. clerk_id: this.$cookieStorage.get('clerk_id') || '',
  562. agent_code_time: this.$cookieStorage.get('agent_code_time') || '',
  563. shop_id_time: this.$cookieStorage.get('shop_id_time') || ''
  564. }
  565. }).then(res => {
  566. res = res.data;
  567. if (res.data && res.data.open_id) {
  568. this.setData({
  569. open_id: res.data.open_id
  570. });
  571. } // 如果接口返回token就直接登录,如果没有则弹出授权
  572. if (res.data.access_token) {
  573. console.log('已经返回给我了token');
  574. wx.hideLoading();
  575. var access_token = res.data.token_type + ' ' + res.data.access_token;
  576. var expires_in = res.data.expires_in || 315360000;
  577. this.$cookieStorage.set("user_token", access_token, expires_in);
  578. if (this.url) {
  579. wx.redirectTo({
  580. url: "/" + this.url,
  581. fail: () => {
  582. wx.switchTab({
  583. url: "/" + this.url
  584. });
  585. }
  586. })
  587. } else {
  588. wx.switchTab({
  589. url: '/pages/user/personal/personal'
  590. });
  591. }
  592. } else {
  593. wx.hideLoading();
  594. }
  595. }).catch(rej => {
  596. wx.hideLoading();
  597. wx.showModal({
  598. content: '请求失败,请重试',
  599. showCancel: false,
  600. success: res => {
  601. if (res.confirm || !res.cancel && !res.confirm) {
  602. this.wxLogin();
  603. }
  604. }
  605. });
  606. });
  607. },
  608. setData: function(obj) {
  609. let that = this;
  610. let keys = [];
  611. let val, data;
  612. Object.keys(obj).forEach(function(key) {
  613. keys = key.split('.');
  614. val = obj[key];
  615. data = that.$data;
  616. keys.forEach(function(key2, index) {
  617. if (index + 1 == keys.length) {
  618. that.$set(data, key2, val);
  619. } else {
  620. if (!data[key2]) {
  621. that.$set(data, key2, {});
  622. }
  623. }
  624. data = data[key2];
  625. });
  626. });
  627. }
  628. },
  629. computed: {},
  630. watch: {}
  631. };
  632. </script>
  633. <style rel="stylesheet/less" lang="less">
  634. @import "register";
  635. </style>