wxlogin.vue 14 KB

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