login.vue 12 KB

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