wxlogin.vue 14 KB

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