login.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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="https://hos-1309931967.cos.ap-chongqing.myqcloud.com/fs/20250721/efce6c594de34caba6537992083a5e47.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. wxLogin() {
  163. var that = this;
  164. if (!this.isAgreement) {
  165. uni.showToast({
  166. icon: 'none',
  167. title: "请先同意协议后再登录",
  168. });
  169. return false;
  170. }
  171. uni.showLoading({
  172. title: "处理中..."
  173. })
  174. this.utils.getProvider()
  175. .then(provider => {
  176. console.log('当前的环境商', provider)
  177. if (!provider) {
  178. reject()
  179. }
  180. // uni登录
  181. uni.login({
  182. provider: provider,
  183. success: async loginRes => {
  184. console.log(loginRes)
  185. let code = loginRes.code // 获取开发code
  186. var userCode = uni.getStorageSync('userCode');
  187. loginByMiniApp({
  188. // encryptedData: e.mp.detail.encryptedData,
  189. // iv: e.mp.detail.iv,
  190. code: code,
  191. userCode: userCode
  192. })
  193. .then(res => {
  194. if (res.code == 200) {
  195. uni.hideLoading();
  196. uni.showToast({
  197. icon: 'none',
  198. title: "登录成功",
  199. });
  200. uni.setStorageSync('AppToken', res.token);
  201. uni.setStorageSync('userInfo', JSON.stringify(res.user));
  202. uni.hideLoading()
  203. uni.setStorageSync('isLiveLogin', true);
  204. //that.getUserInfo()
  205. uni.$emit('refreshLogin');
  206. uni.navigateBack({
  207. delta: 1
  208. })
  209. } else {
  210. uni.hideLoading();
  211. uni.showToast({
  212. icon: 'none',
  213. title: "授权登录失败,请重新登录",
  214. });
  215. }
  216. })
  217. .catch(error => {
  218. console.log(error)
  219. uni.hideLoading();
  220. uni.showToast({
  221. icon: 'none',
  222. title: "登录接口调用失败",
  223. });
  224. })
  225. }
  226. })
  227. })
  228. .catch(err => {
  229. uni.showToast({
  230. icon: 'none',
  231. title: err,
  232. });
  233. })
  234. },
  235. // 微信用户手机号登录
  236. phoneLogin(e) {
  237. var that = this;
  238. if (!this.isAgreement) {
  239. uni.showToast({
  240. icon: 'none',
  241. title: "请先同意协议后再登录",
  242. });
  243. return false;
  244. }
  245. uni.showLoading({
  246. title: "处理中..."
  247. })
  248. if (e.mp.detail.errMsg == 'getPhoneNumber:ok') {
  249. this.utils.getProvider()
  250. .then(provider => {
  251. console.log('当前的环境商', provider)
  252. if (!provider) {
  253. reject()
  254. }
  255. // uni登录
  256. uni.login({
  257. provider: provider,
  258. success: async loginRes => {
  259. console.log(loginRes)
  260. let code = loginRes.code // 获取开发code
  261. var userCode = uni.getStorageSync('userCode');
  262. loginByMiniApp({
  263. encryptedData: e.mp.detail.encryptedData,
  264. iv: e.mp.detail.iv,
  265. code: code,
  266. userCode: userCode
  267. })
  268. .then(res => {
  269. if (res.code == 200) {
  270. uni.hideLoading();
  271. uni.showToast({
  272. icon: 'none',
  273. title: "登录成功",
  274. });
  275. uni.setStorageSync('AppToken', res.token);
  276. uni.setStorageSync('userInfo', JSON.stringify(res
  277. .user));
  278. uni.hideLoading()
  279. //that.getUserInfo()
  280. uni.$emit('refreshLogin');
  281. uni.setStorageSync('isLiveLogin', true);
  282. uni.navigateBack({
  283. delta: 1
  284. })
  285. } else {
  286. uni.hideLoading();
  287. uni.showToast({
  288. icon: 'none',
  289. title: "授权登录失败,请重新登录",
  290. });
  291. }
  292. })
  293. .catch(error => {
  294. console.log(error)
  295. uni.hideLoading();
  296. uni.showToast({
  297. icon: 'none',
  298. title: "登录接口调用失败",
  299. });
  300. })
  301. }
  302. })
  303. })
  304. .catch(err => {
  305. uni.showToast({
  306. icon: 'none',
  307. title: err,
  308. });
  309. })
  310. } else {
  311. uni.showToast({
  312. title: '已拒绝授权',
  313. icon: 'none',
  314. duration: 2000,
  315. })
  316. }
  317. },
  318. back() {
  319. uni.navigateBack()
  320. // uni.switchTab({
  321. // url:'/pages/home/index'
  322. // })
  323. }
  324. }
  325. }
  326. </script>
  327. <style lang="scss">
  328. .container {
  329. flex: 1;
  330. display: flex;
  331. flex-direction: column;
  332. justify-content: flex-start;
  333. position: relative;
  334. }
  335. .force-login-wrap {
  336. width: 100%;
  337. height: 100%;
  338. overflow: hidden;
  339. z-index: 11111;
  340. top: 0;
  341. .force-login__content {
  342. position: absolute;
  343. left: 50%;
  344. top: 40%;
  345. transform: translate(-50%, -50%);
  346. .logo {
  347. display: flex;
  348. flex-direction: column;
  349. justify-content: center;
  350. align-items: center;
  351. .logo-img {
  352. border: 4upx solid #FFFFFF;
  353. box-shadow: 0px 5px 15px 2px rgba(0, 0, 0, 0.1);
  354. border-radius: 50%;
  355. width: 80px;
  356. height: 80px;
  357. image {
  358. border-radius: 50%;
  359. width: 100%;
  360. height: 100%;
  361. overflow: hidden;
  362. }
  363. }
  364. .title {
  365. margin-top: 20rpx;
  366. font-size: 35rpx;
  367. font-family: PingFang SC;
  368. font-weight: bold;
  369. color: #000;
  370. margin-bottom: 30rpx;
  371. }
  372. }
  373. .login-notice {
  374. font-size: 28rpx;
  375. font-family: PingFang SC;
  376. font-weight: 400;
  377. color: #000;
  378. line-height: 44rpx;
  379. width: 500rpx;
  380. text-align: center;
  381. margin-bottom: 80rpx;
  382. }
  383. .btns {
  384. position: relative;
  385. width: 630rpx;
  386. height: 80rpx;
  387. .author-btn {
  388. z-index: 100;
  389. position: absolute;
  390. width: 630rpx;
  391. height: 80rpx;
  392. background: linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  393. background: -moz-linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  394. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  395. border-radius: 40rpx;
  396. font-size: 30rpx;
  397. font-family: PingFang SC;
  398. font-weight: 500;
  399. color: rgba(255, 255, 255, 1);
  400. }
  401. }
  402. .author-btn {
  403. z-index: 100;
  404. // position: absolute;
  405. width: 630rpx;
  406. height: 80rpx;
  407. background: linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  408. background: -moz-linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  409. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  410. border-radius: 40rpx;
  411. font-size: 30rpx;
  412. font-family: PingFang SC;
  413. font-weight: 500;
  414. color: rgba(255, 255, 255, 1);
  415. }
  416. .author-btn {
  417. width: 630rpx;
  418. height: 80rpx;
  419. background: linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  420. background: -moz-linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  421. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  422. border-radius: 40rpx;
  423. font-size: 30rpx;
  424. font-family: PingFang SC;
  425. font-weight: 500;
  426. color: rgba(255, 255, 255, 1);
  427. }
  428. .close-btn {
  429. width: 630rpx;
  430. height: 80rpx;
  431. margin-top: 30rpx;
  432. border-radius: 40rpx;
  433. border: 2rpx solid #2BC7B9;
  434. background: none;
  435. font-size: 30rpx;
  436. font-family: PingFang SC;
  437. font-weight: 500;
  438. color: #2BC7B9;
  439. }
  440. }
  441. }
  442. .tips {
  443. margin-top: 30rpx;
  444. display: flex;
  445. justify-content: center;
  446. align-items: center;
  447. font-size: 28rpx;
  448. color: #000;
  449. checkbox {}
  450. .btn {
  451. color: #2BC7B9;
  452. }
  453. }
  454. .wx-login {
  455. background: rgba(0, 0, 0, 0.7);
  456. z-index: 99999;
  457. position: fixed;
  458. top: 0;
  459. left: 0;
  460. height: 100%;
  461. width: 100%;
  462. display: flex;
  463. align-items: center;
  464. justify-content: center;
  465. .form {
  466. border-radius: 20rpx;
  467. padding: 60rpx 30rpx;
  468. width: 500upx;
  469. height: 300upx;
  470. background-color: #fff;
  471. .title {
  472. font-size: 32upx;
  473. font-family: PingFang SC;
  474. font-weight: bold;
  475. }
  476. .desc {
  477. font-size: 28upx;
  478. margin: 60upx 0upx 60upx 0upx;
  479. font-family: PingFang SC;
  480. font-weight: 500;
  481. }
  482. .btn-box {
  483. margin-top: 30rpx;
  484. width: 100%;
  485. display: flex;
  486. align-items: center;
  487. justify-content: center;
  488. .btn {
  489. display: flex;
  490. align-items: center;
  491. justify-content: center;
  492. margin-left: 10upx;
  493. width: 50%;
  494. height: 80rpx;
  495. border-radius: 5rpx;
  496. background-color: #2BC7B9;
  497. font-size: 30rpx;
  498. font-family: PingFang SC;
  499. font-weight: 500;
  500. color: #fff;
  501. position: relative;
  502. }
  503. .btn-close {
  504. margin-right: 10upx;
  505. width: 50%;
  506. height: 80rpx;
  507. border-radius: 5rpx;
  508. border: 2rpx solid #2BC7B9;
  509. background: none;
  510. font-size: 30rpx;
  511. font-family: PingFang SC;
  512. font-weight: 500;
  513. color: #2BC7B9;
  514. }
  515. }
  516. }
  517. }
  518. .auth_btn {
  519. width: 100%;
  520. height: 100%;
  521. top: 0upx;
  522. position: absolute;
  523. opacity: 0.0;
  524. }
  525. </style>