login.vue 13 KB

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