login.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <template>
  2. <view class="container">
  3. <!-- #ifdef MP-WEIXIN -->
  4. <view class="force-login-wrap">
  5. <view class="force-login__content y-f">
  6. <view class="logo">
  7. <view class="logo-img">
  8. <image
  9. src="/static/images/logo.png">
  10. </image>
  11. </view>
  12. <view class="title">芳华臻选</view>
  13. </view>
  14. <view class="login-notice">为了提供更优质的服务,请先登录</view>
  15. <!-- <button
  16. class="author-btn"
  17. @click="wxLogin()" >微信授权登录</button> -->
  18. <view class="btns">
  19. <button class="author-btn" open-type="getPhoneNumber"
  20. @getphonenumber="phoneLogin">一键授权手机号登录</button>
  21. <button class="author-btn" v-if="isAgreement==false" @click="handleAgree()">一键授权手机号登录</button>
  22. </view>
  23. <!-- <button
  24. class="author-btn"
  25. open-type="getPhoneNumber"
  26. @getphonenumber="phoneLogin" >微信授权登录</button> -->
  27. <button class="close-btn" @tap="back">暂不登录</button>
  28. <view class="tips">
  29. <checkbox :checked="isAgreement" @click="handleAgreement()" />
  30. <view @click="handleAgreement()">您同意并接受</view>
  31. <view class="btn" @click="openH5(1)">《用户协议》</view>
  32. <view class="btn" @click="openH5(2)">《隐私保护》</view>
  33. </view>
  34. </view>
  35. </view>
  36. <!-- #endif -->
  37. </view>
  38. </template>
  39. <script>
  40. import {
  41. loginByMiniApp,
  42. getUserInfo,
  43. loginByMp
  44. } from '@/api/user'
  45. export default {
  46. data() {
  47. return {
  48. isLive: null,
  49. code: null,
  50. isAgreement: false,
  51. }
  52. },
  53. computed: {},
  54. onLoad(option) {
  55. // #ifdef MP-WEIXIN
  56. uni.$on('refreshLogin', () => {
  57. uni.navigateBack({
  58. delta: 1
  59. })
  60. })
  61. //选获取CODE,防止后请求的时候腾讯服务端未同步报错
  62. this.getCode();
  63. // #endif
  64. // #ifdef H5
  65. if (this.checkWeixin()) {
  66. this.getWxCode()
  67. } else {
  68. uni.showToast({
  69. icon: 'none',
  70. title: "请在微信中打开",
  71. });
  72. //跳转到手机号密码登录
  73. }
  74. // #endif
  75. },
  76. onUnload() {},
  77. mounted() {
  78. },
  79. methods: {
  80. handleAgree() {
  81. console.log(123)
  82. if (!this.isAgreement) {
  83. uni.showToast({
  84. icon: 'none',
  85. title: "请先同意协议后再登录",
  86. });
  87. }
  88. },
  89. checkWeixin() {
  90. var ua = window.navigator.userAgent.toLowerCase();
  91. if (ua.match(/micromessenger/i) == 'micromessenger') {
  92. return true;
  93. } else {
  94. return false;
  95. }
  96. },
  97. //URL地址是否存在CODE
  98. getUrlCode(name) {
  99. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) ||
  100. [, ''
  101. ])[1]
  102. .replace(/\+/g, '%20')) || null
  103. },
  104. //获取微信CODE
  105. getWxCode() {
  106. //在微信公众号请求用户网页授权之前,开发者需要先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名。请注意,这里填写的是域名(是一个字符串),而不是URL,因此请勿加 http:// 等协议头;
  107. //http://shequ.natapp1.cc/#/pages/index/index?deviceId=8
  108. var appId = "wx503cf8ab31f83dd4";
  109. var url = "https://api.zxfh.cdwjyyh.com";
  110. window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appId +
  111. '&redirect_uri=' + encodeURIComponent(url + "/#/pages/auth/wxLogin") +
  112. '&response_type=code&scope=snsapi_userinfo&state=JeffreySu-954&connect_redirect=1#wechat_redirect';
  113. //console.log('https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appId+'&redirect_uri=' +encodeURIComponent("http://shequ.natapp1.cc/#/pages/index/index?deviceId="+this.deviceId) +'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect')
  114. // redirect_uri是授权成功后,跳转的url地址,微信会帮我们跳转到该链接,并且通过?的形式拼接code
  115. },
  116. handleAgreement() {
  117. this.isAgreement = !this.isAgreement;
  118. console.log(545454)
  119. uni.requestSubscribeMessage({
  120. tmplIds: ['K0RUbGggwYz7V4yjtJjFVXtthnx4hOJgHvr7RNOyRSE',
  121. '5ZSzz2nPmJo9EuenZa78mQPScoOMc84LnEfEpV0-i04'
  122. ], // 模板ID列表,从微信公众平台获取
  123. success(res) {
  124. console.log('订阅消息成功', res);
  125. if (res.errMsg == 'requestSubscribeMessage:ok') {
  126. console.log('订阅消息成功11111');
  127. }
  128. // 根据返回的 res 处理后续逻辑,例如显示成功消息给用户
  129. },
  130. fail(err) {
  131. console.error('订阅消息失败', err);
  132. // 处理失败情况,例如提示用户重新尝试或检查权限设置
  133. }
  134. });
  135. },
  136. openH5(url) {
  137. var requestPath = uni.getStorageSync('requestPath');
  138. // uni.setStorageSync('url',requestPath+url);
  139. uni.navigateTo({
  140. url: '../home/h5?type=' + url
  141. })
  142. },
  143. getCode() {
  144. var that = this;
  145. this.utils.getProvider()
  146. .then(provider => {
  147. console.log('当前的环境商', provider)
  148. if (!provider) {
  149. reject()
  150. }
  151. // uni登录
  152. uni.login({
  153. provider: provider,
  154. success: async loginRes => {
  155. that.code = loginRes.code
  156. }
  157. })
  158. })
  159. .catch(err => {
  160. })
  161. },
  162. getUserInfo() {
  163. getUserInfo().then((res) => {
  164. if (res.code == 200) {
  165. console.log("获取用户信息成功", res.user)
  166. this.userData = res.user
  167. uni.setStorageSync('userData', JSON.stringify(res.user));
  168. } else {
  169. uni.showToast({
  170. icon: 'none',
  171. title: '请求失败'
  172. })
  173. }
  174. },
  175. (rej) => {}
  176. )
  177. },
  178. wxLogin() {
  179. var that = this;
  180. if (!this.isAgreement) {
  181. uni.showToast({
  182. icon: 'none',
  183. title: "请先同意协议后再登录",
  184. });
  185. return false;
  186. }
  187. uni.showLoading({
  188. title: "处理中..."
  189. })
  190. this.utils.getProvider()
  191. .then(provider => {
  192. console.log('当前的环境商', provider)
  193. if (!provider) {
  194. reject()
  195. }
  196. // uni登录
  197. uni.login({
  198. provider: provider,
  199. success: async loginRes => {
  200. console.log(loginRes)
  201. let code = loginRes.code // 获取开发code
  202. var userCode = uni.getStorageSync('userCode');
  203. loginByMiniApp({
  204. // encryptedData: e.mp.detail.encryptedData,
  205. // iv: e.mp.detail.iv,
  206. code: code,
  207. userCode: userCode
  208. })
  209. .then(res => {
  210. if (res.code == 200) {
  211. uni.hideLoading();
  212. uni.showToast({
  213. icon: 'none',
  214. title: "登录成功",
  215. });
  216. uni.setStorageSync('AppToken', res.token);
  217. uni.setStorageSync('userInfo', JSON.stringify(res.user));
  218. uni.hideLoading()
  219. uni.setStorageSync('isLiveLogin', true);
  220. this.getUserInfo()
  221. uni.$emit('refreshLogin');
  222. uni.navigateBack({
  223. delta: 1
  224. })
  225. } else {
  226. uni.hideLoading();
  227. uni.showToast({
  228. icon: 'none',
  229. title: "授权登录失败,请重新登录",
  230. });
  231. }
  232. })
  233. .catch(error => {
  234. console.log(error)
  235. uni.hideLoading();
  236. uni.showToast({
  237. icon: 'none',
  238. title: "登录接口调用失败",
  239. });
  240. })
  241. }
  242. })
  243. })
  244. .catch(err => {
  245. uni.showToast({
  246. icon: 'none',
  247. title: err,
  248. });
  249. })
  250. },
  251. // 微信用户手机号登录
  252. phoneLogin(e) {
  253. var that = this;
  254. if (!this.isAgreement) {
  255. uni.showToast({
  256. icon: 'none',
  257. title: "请先同意协议后再登录",
  258. });
  259. return false;
  260. }
  261. uni.showLoading({
  262. title: "处理中..."
  263. })
  264. if (e.mp.detail.errMsg == 'getPhoneNumber:ok') {
  265. this.utils.getProvider()
  266. .then(provider => {
  267. console.log('当前的环境商', provider)
  268. if (!provider) {
  269. reject()
  270. }
  271. // uni登录
  272. uni.login({
  273. provider: provider,
  274. success: async loginRes => {
  275. console.log(loginRes)
  276. let code = loginRes.code // 获取开发code
  277. var userCode = uni.getStorageSync('userCode');
  278. loginByMiniApp({
  279. encryptedData: e.mp.detail.encryptedData,
  280. iv: e.mp.detail.iv,
  281. code: code,
  282. userCode: userCode
  283. })
  284. .then(res => {
  285. if (res.code == 200) {
  286. uni.hideLoading();
  287. uni.showToast({
  288. icon: 'none',
  289. title: "登录成功",
  290. });
  291. uni.setStorageSync('AppToken', res.token);
  292. uni.setStorageSync('userInfo', JSON.stringify(res
  293. .user));
  294. uni.hideLoading()
  295. this.getUserInfo()
  296. uni.$emit('refreshLogin');
  297. uni.setStorageSync('isLiveLogin', true);
  298. uni.navigateBack({
  299. delta: 1
  300. })
  301. } else {
  302. uni.hideLoading();
  303. uni.showToast({
  304. icon: 'none',
  305. title: "授权登录失败,请重新登录",
  306. });
  307. }
  308. })
  309. .catch(error => {
  310. console.log(error)
  311. uni.hideLoading();
  312. uni.showToast({
  313. icon: 'none',
  314. title: "登录接口调用失败",
  315. });
  316. })
  317. }
  318. })
  319. })
  320. .catch(err => {
  321. uni.showToast({
  322. icon: 'none',
  323. title: err,
  324. });
  325. })
  326. } else {
  327. uni.showToast({
  328. title: '已拒绝授权',
  329. icon: 'none',
  330. duration: 2000,
  331. })
  332. }
  333. },
  334. back() {
  335. uni.navigateBack()
  336. // uni.switchTab({
  337. // url:'/pages/home/index'
  338. // })
  339. }
  340. }
  341. }
  342. </script>
  343. <style lang="scss">
  344. .container {
  345. flex: 1;
  346. display: flex;
  347. flex-direction: column;
  348. justify-content: flex-start;
  349. position: relative;
  350. }
  351. .force-login-wrap {
  352. width: 100%;
  353. height: 100%;
  354. overflow: hidden;
  355. z-index: 11111;
  356. top: 0;
  357. .force-login__content {
  358. position: absolute;
  359. left: 50%;
  360. top: 40%;
  361. transform: translate(-50%, -50%);
  362. .logo {
  363. display: flex;
  364. flex-direction: column;
  365. justify-content: center;
  366. align-items: center;
  367. .logo-img {
  368. border: 4upx solid #FFFFFF;
  369. box-shadow: 0px 5px 15px 2px rgba(0, 0, 0, 0.1);
  370. border-radius: 50%;
  371. width: 80px;
  372. height: 80px;
  373. image {
  374. border-radius: 50%;
  375. width: 100%;
  376. height: 100%;
  377. overflow: hidden;
  378. }
  379. }
  380. .title {
  381. margin-top: 20rpx;
  382. font-size: 35rpx;
  383. font-family: PingFang SC;
  384. font-weight: bold;
  385. color: #000;
  386. margin-bottom: 30rpx;
  387. }
  388. }
  389. .login-notice {
  390. font-size: 28rpx;
  391. font-family: PingFang SC;
  392. font-weight: 400;
  393. color: #000;
  394. line-height: 44rpx;
  395. width: 500rpx;
  396. text-align: center;
  397. margin-bottom: 80rpx;
  398. }
  399. .btns {
  400. position: relative;
  401. width: 630rpx;
  402. height: 80rpx;
  403. .author-btn {
  404. z-index: 100;
  405. position: absolute;
  406. width: 630rpx;
  407. height: 80rpx;
  408. background: linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  409. background: -moz-linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  410. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  411. border-radius: 40rpx;
  412. font-size: 30rpx;
  413. font-family: PingFang SC;
  414. font-weight: 500;
  415. color: rgba(255, 255, 255, 1);
  416. }
  417. }
  418. .author-btn {
  419. z-index: 100;
  420. // position: absolute;
  421. width: 630rpx;
  422. height: 80rpx;
  423. background: linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  424. background: -moz-linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  425. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  426. border-radius: 40rpx;
  427. font-size: 30rpx;
  428. font-family: PingFang SC;
  429. font-weight: 500;
  430. color: rgba(255, 255, 255, 1);
  431. }
  432. .author-btn {
  433. width: 630rpx;
  434. height: 80rpx;
  435. background: linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  436. background: -moz-linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  437. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  438. border-radius: 40rpx;
  439. font-size: 30rpx;
  440. font-family: PingFang SC;
  441. font-weight: 500;
  442. color: rgba(255, 255, 255, 1);
  443. }
  444. .close-btn {
  445. width: 630rpx;
  446. height: 80rpx;
  447. margin-top: 30rpx;
  448. border-radius: 40rpx;
  449. border: 2rpx solid #2BC7B9;
  450. background: none;
  451. font-size: 30rpx;
  452. font-family: PingFang SC;
  453. font-weight: 500;
  454. color: #2BC7B9;
  455. }
  456. }
  457. }
  458. .tips {
  459. margin-top: 30rpx;
  460. display: flex;
  461. justify-content: center;
  462. align-items: center;
  463. font-size: 28rpx;
  464. color: #000;
  465. checkbox {}
  466. .btn {
  467. color: #2BC7B9;
  468. }
  469. }
  470. .wx-login {
  471. background: rgba(0, 0, 0, 0.7);
  472. z-index: 99999;
  473. position: fixed;
  474. top: 0;
  475. left: 0;
  476. height: 100%;
  477. width: 100%;
  478. display: flex;
  479. align-items: center;
  480. justify-content: center;
  481. .form {
  482. border-radius: 20rpx;
  483. padding: 60rpx 30rpx;
  484. width: 500upx;
  485. height: 300upx;
  486. background-color: #fff;
  487. .title {
  488. font-size: 32upx;
  489. font-family: PingFang SC;
  490. font-weight: bold;
  491. }
  492. .desc {
  493. font-size: 28upx;
  494. margin: 60upx 0upx 60upx 0upx;
  495. font-family: PingFang SC;
  496. font-weight: 500;
  497. }
  498. .btn-box {
  499. margin-top: 30rpx;
  500. width: 100%;
  501. display: flex;
  502. align-items: center;
  503. justify-content: center;
  504. .btn {
  505. display: flex;
  506. align-items: center;
  507. justify-content: center;
  508. margin-left: 10upx;
  509. width: 50%;
  510. height: 80rpx;
  511. border-radius: 5rpx;
  512. background-color: #2BC7B9;
  513. font-size: 30rpx;
  514. font-family: PingFang SC;
  515. font-weight: 500;
  516. color: #fff;
  517. position: relative;
  518. }
  519. .btn-close {
  520. margin-right: 10upx;
  521. width: 50%;
  522. height: 80rpx;
  523. border-radius: 5rpx;
  524. border: 2rpx solid #2BC7B9;
  525. background: none;
  526. font-size: 30rpx;
  527. font-family: PingFang SC;
  528. font-weight: 500;
  529. color: #2BC7B9;
  530. }
  531. }
  532. }
  533. }
  534. .auth_btn {
  535. width: 100%;
  536. height: 100%;
  537. top: 0upx;
  538. position: absolute;
  539. opacity: 0.0;
  540. }
  541. </style>