wxlogin.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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">{{name}}</view>
  12. </view>
  13. <view class="login-notice">为了提供更优质的服务,请先登录</view>
  14. <view class="btns">
  15. <button
  16. class="author-btn"
  17. open-type="getPhoneNumber"
  18. @getphonenumber="phoneLogin" >一键授权手机号登录</button>
  19. <button class="author-btn" v-if="isAgreement==false" @click="handleAgree()">一键授权手机号登录</button>
  20. </view>
  21. <button class="close-btn" @tap="back">暂不登录</button>
  22. <view class="tips">
  23. <checkbox :checked="isAgreement" @click="handleAgreement()" iconColor="#48a2e1"
  24. style="transform:scale(0.9)"/>
  25. <view @click="handleAgreement()">您同意并接受</view>
  26. <view class="btn" @click="openH5('/h5/userAgreement')">《用户协议》</view>
  27. <view class="btn" @click="openH5('/h5/privacyPolicy')">《隐私保护》</view>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- #endif -->
  32. </view>
  33. </template>
  34. <script>
  35. import { loginByMp} from '@/api/user'
  36. import {loginByMiniApp , getUserInfo } from '@/api/user'
  37. export default {
  38. data() {
  39. return {
  40. code:null,
  41. isAgreement:false,
  42. companyId:null,
  43. }
  44. },
  45. computed: {
  46. imgPath() {
  47. return this.$store.state.imgpath
  48. },
  49. name() {
  50. return this.$store.state.logoname
  51. },
  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. 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="wx29d26f63f836be7f";
  107. var url="https://company.h5.test.ylrztop.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. // if(!uni.getStorageSync('userInfos')){
  115. // uni.navigateTo({
  116. // url:'/pages_course/webview'
  117. // })
  118. // return
  119. // }
  120. },
  121. openH5(url){
  122. var requestPath = uni.getStorageSync('requestPath');
  123. uni.setStorageSync('url',requestPath+url);
  124. uni.navigateTo({
  125. url:'/pages/index/h5'
  126. })
  127. },
  128. getCode(){
  129. var that=this;
  130. this.utils.getProvider()
  131. .then(provider => {
  132. console.log('当前的环境商',provider)
  133. if (!provider) {
  134. reject()
  135. }
  136. // uni登录
  137. uni.login({
  138. provider: provider,
  139. success: async loginRes => {
  140. that.code = loginRes.code
  141. }
  142. })
  143. })
  144. .catch(err => {
  145. })
  146. },
  147. // 微信用户登录
  148. phoneLogin(e) {
  149. console.log(e)
  150. if(!this.isAgreement){
  151. uni.showToast({
  152. icon:'none',
  153. title: "请先同意协议后再登录",
  154. });
  155. return false;
  156. }
  157. // console.log(uni.getStorageSync('userInfos'))
  158. uni.showLoading({
  159. title: "处理中..."
  160. });
  161. if (e.mp.detail.errMsg == 'getPhoneNumber:ok') {
  162. this.utils.getProvider()
  163. .then(provider => {
  164. if (!provider) {
  165. reject()
  166. }
  167. // uni登录
  168. uni.login({
  169. provider: provider,
  170. success: async loginRes => {
  171. console.log(loginRes)
  172. console.log(e)
  173. let code = loginRes.code // 获取开发code
  174. // const userinfos=JSON.parse(uni.getStorageSync('userInfos'))
  175. loginByMiniApp({
  176. encryptedData: e.mp.detail.encryptedData,
  177. iv: e.mp.detail.iv,
  178. code: code,
  179. // nickname:userinfos.nickname,
  180. // avatar:userinfos.avatar
  181. })
  182. .then( res => {
  183. if(res.code==200){
  184. console.log(res)
  185. uni.hideLoading();
  186. uni.showToast({
  187. icon:'none',
  188. title: "登录成功",
  189. });
  190. uni.setStorageSync('TOKEN_WEXIN', res.token);
  191. uni.setStorageSync('userInfo', JSON.stringify(res.user));
  192. this.userInfo=res.user;
  193. uni.hideLoading()
  194. setTimeout(()=>{
  195. uni.navigateTo({
  196. url:'/pages/enterprise/enterprise'
  197. })
  198. },200)
  199. }
  200. else{
  201. uni.hideLoading();
  202. uni.showToast({
  203. icon:'none',
  204. title: "授权登录失败,请重新登录",
  205. });
  206. }
  207. })
  208. }
  209. })
  210. })
  211. .catch(err => {
  212. uni.showToast({
  213. icon:'none',
  214. title: err,
  215. });
  216. })
  217. } else {
  218. uni.showToast({
  219. title: '已拒绝授权',
  220. icon: 'none',
  221. duration: 2000,
  222. })
  223. }
  224. },
  225. back() {
  226. uni.navigateBack()
  227. }
  228. }
  229. }
  230. </script>
  231. <style lang="scss">
  232. radio .wx-radio-input{
  233. width: 36rpx;
  234. height: 36rpx;
  235. border: 1px solid #CCCCCC;
  236. }
  237. /* 选中后的 背景样式 */
  238. radio .wx-radio-input.wx-radio-input-checked{
  239. background: linear-gradient(135deg, #48a2e1 0%, #60CDC3 100%);
  240. border: 1px solid #48a2e1;
  241. }
  242. /* 选中后的 对勾样式 (白色对勾) */
  243. radio .wx-radio-input.wx-radio-input-checked::before{
  244. color: #ffffff;
  245. }
  246. checkbox .wx-checkbox-input{
  247. width: 36rpx;
  248. height: 36rpx;
  249. border: 1px solid #CCCCCC;
  250. border-radius: 50%;
  251. }
  252. checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  253. background: linear-gradient(135deg, #48a2e1 0%, #60CDC3 100%);
  254. border: 1px solid #48a2e1;
  255. border-radius: 50%;
  256. }
  257. checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
  258. color: #ffffff;
  259. }
  260. .container {
  261. flex: 1;
  262. display: flex;
  263. flex-direction: column;
  264. justify-content: flex-start;
  265. position: relative;
  266. }
  267. .force-login-wrap {
  268. width: 100%;
  269. height: 100%;
  270. overflow: hidden;
  271. z-index: 11111;
  272. top: 0;
  273. .force-login__content {
  274. position: absolute;
  275. left: 50%;
  276. top: 40%;
  277. transform: translate(-50%, -50%);
  278. .logo{
  279. display: flex;
  280. flex-direction: column;
  281. justify-content: center;
  282. align-items: center;
  283. .logo-img{
  284. border: 4upx solid #FFFFFF;
  285. box-shadow: 0px 5px 15px 2px rgba(0,0,0,0.1);
  286. border-radius: 50%;
  287. width: 80px;
  288. height: 80px;
  289. image{
  290. border-radius: 50%;
  291. width: 100%;
  292. height: 100%;
  293. overflow: hidden;
  294. }
  295. }
  296. .title{
  297. margin-top: 20rpx;
  298. font-size: 35rpx;
  299. font-family: PingFang SC;
  300. font-weight: bold;
  301. color: #000;
  302. margin-bottom: 30rpx;
  303. }
  304. }
  305. .login-notice {
  306. font-size: 28rpx;
  307. font-family: PingFang SC;
  308. font-weight: 400;
  309. color: #000;
  310. line-height: 44rpx;
  311. width: 500rpx;
  312. text-align: center;
  313. margin-bottom: 80rpx;
  314. }
  315. .btns{
  316. position: relative;
  317. width: 630rpx;
  318. height: 80rpx;
  319. .author-btn{
  320. z-index:100;
  321. position: absolute;
  322. width: 630rpx;
  323. height: 80rpx;
  324. background: linear-gradient(to right, #48a2e1 0%, #2aa7B9 100%);
  325. background: -moz-linear-gradient(to right, #48a2e1 0%, #2aa7B9 100%);
  326. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  327. border-radius: 40rpx;
  328. font-size: 30rpx;
  329. font-family: PingFang SC;
  330. font-weight: 500;
  331. color: rgba(255, 255, 255, 1);
  332. }
  333. }
  334. // .author-btn{
  335. // z-index:100;
  336. // // position: absolute;
  337. // width: 630rpx;
  338. // height: 80rpx;
  339. // background: linear-gradient(to right, #48a2e1 0%, #2aa7B9 100%);
  340. // background: -moz-linear-gradient(to right, #48a2e1 0%, #2aa7B9 100%);
  341. // // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  342. // border-radius: 40rpx;
  343. // font-size: 30rpx;
  344. // font-family: PingFang SC;
  345. // font-weight: 500;
  346. // text-align: center;
  347. // line-height: 80rpx;
  348. // color: rgba(255, 255, 255, 1);
  349. // }
  350. // .author-btn {
  351. // width: 630rpx;
  352. // height: 80rpx;
  353. // background: linear-gradient(to right, #48a2e1 0%, #2aa7B9 100%);
  354. // background: -moz-linear-gradient(to right, #48a2e1 0%, #2aa7B9 100%);
  355. // // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  356. // border-radius: 40rpx;
  357. // font-size: 30rpx;
  358. // font-family: PingFang SC;
  359. // font-weight: 500;
  360. // color: rgba(255, 255, 255, 1);
  361. // }
  362. .close-btn {
  363. width: 630rpx;
  364. height: 80rpx;
  365. margin-top: 30rpx;
  366. border-radius: 40rpx;
  367. border: 2rpx solid #48a2e1;
  368. background: none;
  369. font-size: 30rpx;
  370. font-family: PingFang SC;
  371. font-weight: 500;
  372. color: #48a2e1;
  373. }
  374. }
  375. }
  376. .tips{
  377. margin-top: 30rpx;
  378. display: flex;
  379. justify-content: center;
  380. align-items: center;
  381. font-size: 28rpx;
  382. color: #000;
  383. checkbox{
  384. }
  385. .btn{
  386. color: #48a2e1;
  387. }
  388. }
  389. .wx-login{
  390. background: rgba(0,0,0,0.7);
  391. z-index: 99999;
  392. position: fixed;
  393. top: 0;
  394. left: 0;
  395. height: 100%;
  396. width: 100%;
  397. display: flex;
  398. align-items: center;
  399. justify-content: center;
  400. .form{
  401. border-radius: 20rpx;
  402. padding: 60rpx 30rpx;
  403. width: 500upx;
  404. height: 300upx;
  405. background-color: #fff;
  406. .title{
  407. font-size: 32upx;
  408. font-family: PingFang SC;
  409. font-weight: bold;
  410. }
  411. .desc{
  412. font-size: 28upx;
  413. margin: 60upx 0upx 60upx 0upx;
  414. font-family: PingFang SC;
  415. font-weight: 500;
  416. }
  417. .btn-box{
  418. margin-top: 30rpx;
  419. width: 100%;
  420. display: flex;
  421. align-items: center;
  422. justify-content: center;
  423. .btn{
  424. display: flex;
  425. align-items: center;
  426. justify-content: center;
  427. margin-left: 10upx;
  428. width: 50%;
  429. height: 80rpx;
  430. border-radius: 5rpx;
  431. background-color: #48a2e1;
  432. font-size: 30rpx;
  433. font-family: PingFang SC;
  434. font-weight: 500;
  435. color: #fff;
  436. position: relative;
  437. }
  438. .btn-close{
  439. margin-right: 10upx;
  440. width: 50%;
  441. height: 80rpx;
  442. border-radius: 5rpx;
  443. border: 2rpx solid #48a2e1;
  444. background: none;
  445. font-size: 30rpx;
  446. font-family: PingFang SC;
  447. font-weight: 500;
  448. color: #48a2e1;
  449. }
  450. }
  451. }
  452. }
  453. .auth_btn{
  454. width: 100%;
  455. height: 100%;
  456. top:0upx;
  457. position: absolute;
  458. opacity:0.0;
  459. }
  460. </style>