login.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. }
  312. }
  313. }
  314. </script>
  315. <style lang="scss">
  316. .container {
  317. flex: 1;
  318. display: flex;
  319. flex-direction: column;
  320. justify-content: flex-start;
  321. position: relative;
  322. }
  323. .force-login-wrap {
  324. width: 100%;
  325. height: 100%;
  326. overflow: hidden;
  327. z-index: 11111;
  328. top: 0;
  329. .force-login__content {
  330. position: absolute;
  331. left: 50%;
  332. top: 40%;
  333. transform: translate(-50%, -50%);
  334. .logo{
  335. display: flex;
  336. flex-direction: column;
  337. justify-content: center;
  338. align-items: center;
  339. .logo-img{
  340. border: 4upx solid #FFFFFF;
  341. box-shadow: 0px 5px 15px 2px rgba(0,0,0,0.1);
  342. border-radius: 50%;
  343. width: 80px;
  344. height: 80px;
  345. image{
  346. border-radius: 50%;
  347. width: 100%;
  348. height: 100%;
  349. overflow: hidden;
  350. }
  351. }
  352. .title{
  353. margin-top: 20rpx;
  354. font-size: 35rpx;
  355. font-family: PingFang SC;
  356. font-weight: bold;
  357. color: #000;
  358. margin-bottom: 30rpx;
  359. }
  360. }
  361. .login-notice {
  362. font-size: 28rpx;
  363. font-family: PingFang SC;
  364. font-weight: 400;
  365. color: #000;
  366. line-height: 44rpx;
  367. width: 500rpx;
  368. text-align: center;
  369. margin-bottom: 80rpx;
  370. }
  371. .btns{
  372. position: relative;
  373. width: 630rpx;
  374. height: 80rpx;
  375. .author-btn{
  376. z-index:100;
  377. position: absolute;
  378. width: 630rpx;
  379. height: 80rpx;
  380. background: linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  381. background: -moz-linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  382. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  383. border-radius: 40rpx;
  384. font-size: 30rpx;
  385. font-family: PingFang SC;
  386. font-weight: 500;
  387. color: rgba(255, 255, 255, 1);
  388. }
  389. }
  390. .author-btn{
  391. z-index:100;
  392. // position: absolute;
  393. width: 630rpx;
  394. height: 80rpx;
  395. background: linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  396. background: -moz-linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  397. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  398. border-radius: 40rpx;
  399. font-size: 30rpx;
  400. font-family: PingFang SC;
  401. font-weight: 500;
  402. color: rgba(255, 255, 255, 1);
  403. }
  404. .author-btn {
  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. .close-btn {
  417. width: 630rpx;
  418. height: 80rpx;
  419. margin-top: 30rpx;
  420. border-radius: 40rpx;
  421. border: 2rpx solid #2BC7B9;
  422. background: none;
  423. font-size: 30rpx;
  424. font-family: PingFang SC;
  425. font-weight: 500;
  426. color: #2BC7B9;
  427. }
  428. }
  429. }
  430. .tips{
  431. margin-top: 30rpx;
  432. display: flex;
  433. justify-content: center;
  434. align-items: center;
  435. font-size: 28rpx;
  436. color: #000;
  437. checkbox{
  438. }
  439. .btn{
  440. color: #2BC7B9;
  441. }
  442. }
  443. .wx-login{
  444. background: rgba(0,0,0,0.7);
  445. z-index: 99999;
  446. position: fixed;
  447. top: 0;
  448. left: 0;
  449. height: 100%;
  450. width: 100%;
  451. display: flex;
  452. align-items: center;
  453. justify-content: center;
  454. .form{
  455. border-radius: 20rpx;
  456. padding: 60rpx 30rpx;
  457. width: 500upx;
  458. height: 300upx;
  459. background-color: #fff;
  460. .title{
  461. font-size: 32upx;
  462. font-family: PingFang SC;
  463. font-weight: bold;
  464. }
  465. .desc{
  466. font-size: 28upx;
  467. margin: 60upx 0upx 60upx 0upx;
  468. font-family: PingFang SC;
  469. font-weight: 500;
  470. }
  471. .btn-box{
  472. margin-top: 30rpx;
  473. width: 100%;
  474. display: flex;
  475. align-items: center;
  476. justify-content: center;
  477. .btn{
  478. display: flex;
  479. align-items: center;
  480. justify-content: center;
  481. margin-left: 10upx;
  482. width: 50%;
  483. height: 80rpx;
  484. border-radius: 5rpx;
  485. background-color: #2BC7B9;
  486. font-size: 30rpx;
  487. font-family: PingFang SC;
  488. font-weight: 500;
  489. color: #fff;
  490. position: relative;
  491. }
  492. .btn-close{
  493. margin-right: 10upx;
  494. width: 50%;
  495. height: 80rpx;
  496. border-radius: 5rpx;
  497. border: 2rpx solid #2BC7B9;
  498. background: none;
  499. font-size: 30rpx;
  500. font-family: PingFang SC;
  501. font-weight: 500;
  502. color: #2BC7B9;
  503. }
  504. }
  505. }
  506. }
  507. .auth_btn{
  508. width: 100%;
  509. height: 100%;
  510. top:0upx;
  511. position: absolute;
  512. opacity:0.0;
  513. }
  514. </style>