wxlogin.vue 13 KB

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