login.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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">坤德精选a</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. appId: getApp().globalData.appId
  169. })
  170. .then( res => {
  171. if(res.code==200){
  172. uni.hideLoading();
  173. uni.showToast({
  174. icon:'none',
  175. title: "登录成功",
  176. });
  177. uni.setStorageSync('AppToken',res.token);
  178. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  179. uni.hideLoading()
  180. //that.getUserInfo()
  181. uni.$emit('refreshLogin');
  182. uni.navigateBack({
  183. delta:1
  184. })
  185. }
  186. else{
  187. uni.hideLoading();
  188. uni.showToast({
  189. icon:'none',
  190. title: "授权登录失败,请重新登录",
  191. });
  192. }
  193. })
  194. .catch(error => {
  195. console.log(error)
  196. uni.hideLoading();
  197. uni.showToast({
  198. icon:'none',
  199. title: "登录接口调用失败",
  200. });
  201. })
  202. }
  203. })
  204. })
  205. .catch(err => {
  206. uni.showToast({
  207. icon:'none',
  208. title: err,
  209. });
  210. })
  211. },
  212. // 微信用户手机号登录
  213. phoneLogin(e) {
  214. console.log(e)
  215. var that=this;
  216. if(!this.isAgreement){
  217. uni.showToast({
  218. icon:'none',
  219. title: "请先同意协议后再登录",
  220. });
  221. return false;
  222. }
  223. uni.showLoading({
  224. title:"处理中..."
  225. })
  226. if (e.mp.detail.errMsg == 'getUserInfo:ok') {
  227. this.utils.getProvider()
  228. .then(provider => {
  229. console.log('当前的环境商',provider)
  230. if (!provider) {
  231. reject()
  232. }
  233. // uni登录
  234. uni.login({
  235. provider: provider,
  236. success: async loginRes => {
  237. console.log(loginRes)
  238. let code = loginRes.code // 获取开发code
  239. var userCode=uni.getStorageSync('userCode');
  240. loginByMiniApp({
  241. encryptedData: e.mp.detail.encryptedData,
  242. iv: e.mp.detail.iv,
  243. code: code,
  244. userCode:userCode,
  245. appId: getApp().globalData.appId
  246. })
  247. .then( res => {
  248. if(res.code==200){
  249. uni.hideLoading();
  250. uni.showToast({
  251. icon:'none',
  252. title: "登录成功",
  253. });
  254. uni.setStorageSync('AppToken',res.token);
  255. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  256. uni.hideLoading()
  257. //that.getUserInfo()
  258. uni.$emit('refreshLogin');
  259. uni.navigateBack({
  260. delta:1
  261. })
  262. }
  263. else{
  264. uni.hideLoading();
  265. uni.showToast({
  266. icon:'none',
  267. title: "授权登录失败,请重新登录",
  268. });
  269. }
  270. })
  271. .catch(error => {
  272. console.log(error)
  273. uni.hideLoading();
  274. uni.showToast({
  275. icon:'none',
  276. title: "登录接口调用失败",
  277. });
  278. })
  279. }
  280. })
  281. })
  282. .catch(err => {
  283. uni.showToast({
  284. icon:'none',
  285. title: err,
  286. });
  287. })
  288. } else {
  289. uni.showToast({
  290. title: '已拒绝授权',
  291. icon: 'none',
  292. duration: 2000,
  293. })
  294. }
  295. },
  296. back() {
  297. uni.switchTab({
  298. url: '/pages/home/index'
  299. })
  300. }
  301. }
  302. }
  303. </script>
  304. <style lang="scss">
  305. .container {
  306. flex: 1;
  307. display: flex;
  308. flex-direction: column;
  309. justify-content: flex-start;
  310. position: relative;
  311. }
  312. .force-login-wrap {
  313. width: 100%;
  314. height: 100%;
  315. overflow: hidden;
  316. z-index: 11111;
  317. top: 0;
  318. .force-login__content {
  319. position: absolute;
  320. left: 50%;
  321. top: 40%;
  322. transform: translate(-50%, -50%);
  323. .logo{
  324. display: flex;
  325. flex-direction: column;
  326. justify-content: center;
  327. align-items: center;
  328. .logo-img{
  329. border: 4upx solid #FFFFFF;
  330. box-shadow: 0px 5px 15px 2px rgba(0,0,0,0.1);
  331. border-radius: 50%;
  332. width: 80px;
  333. height: 80px;
  334. image{
  335. border-radius: 50%;
  336. width: 100%;
  337. height: 100%;
  338. overflow: hidden;
  339. }
  340. }
  341. .title{
  342. margin-top: 20rpx;
  343. font-size: 35rpx;
  344. font-family: PingFang SC;
  345. font-weight: bold;
  346. color: #000;
  347. margin-bottom: 30rpx;
  348. }
  349. }
  350. .login-notice {
  351. font-size: 28rpx;
  352. font-family: PingFang SC;
  353. font-weight: 400;
  354. color: #000;
  355. line-height: 44rpx;
  356. width: 500rpx;
  357. text-align: center;
  358. margin-bottom: 80rpx;
  359. }
  360. .btns{
  361. position: relative;
  362. width: 630rpx;
  363. height: 80rpx;
  364. .author-btn{
  365. z-index:100;
  366. position: absolute;
  367. width: 630rpx;
  368. height: 80rpx;
  369. background: linear-gradient(to right, #1BB99D 0%, rgba(192, 62, 37, 0.6) 100%);
  370. background: -moz-linear-gradient(to right, #1BB99D 0%, rgba(192, 62, 37, 0.6) 100%);
  371. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  372. border-radius: 40rpx;
  373. font-size: 30rpx;
  374. font-family: PingFang SC;
  375. font-weight: 500;
  376. color: rgba(255, 255, 255, 1);
  377. }
  378. }
  379. .author-btn{
  380. z-index:100;
  381. // position: absolute;
  382. width: 630rpx;
  383. height: 80rpx;
  384. background: linear-gradient(to right, #1BB99D 0%, rgba(192, 62, 37, 0.6) 100%);
  385. background: -moz-linear-gradient(to right, #1BB99D 0%, rgba(192, 62, 37, 0.6) 100%);
  386. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  387. border-radius: 40rpx;
  388. font-size: 30rpx;
  389. font-family: PingFang SC;
  390. font-weight: 500;
  391. color: rgba(255, 255, 255, 1);
  392. }
  393. .author-btn {
  394. width: 630rpx;
  395. height: 80rpx;
  396. background: linear-gradient(to right, #1BB99D 0%, rgba(192, 62, 37, 0.6) 100%);
  397. background: -moz-linear-gradient(to right, #1BB99D 0%, rgba(192, 62, 37, 0.6) 100%);
  398. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  399. border-radius: 40rpx;
  400. font-size: 30rpx;
  401. font-family: PingFang SC;
  402. font-weight: 500;
  403. color: rgba(255, 255, 255, 1);
  404. }
  405. .close-btn {
  406. width: 630rpx;
  407. height: 80rpx;
  408. margin-top: 30rpx;
  409. border-radius: 40rpx;
  410. border: 2rpx solid #1BB99D;
  411. background: none;
  412. font-size: 30rpx;
  413. font-family: PingFang SC;
  414. font-weight: 500;
  415. color: #1BB99D;
  416. }
  417. }
  418. }
  419. .tips{
  420. margin-top: 30rpx;
  421. display: flex;
  422. justify-content: center;
  423. align-items: center;
  424. font-size: 28rpx;
  425. color: #000;
  426. checkbox{
  427. }
  428. .btn{
  429. color: #1BB99D;
  430. }
  431. }
  432. .wx-login{
  433. background: rgba(0,0,0,0.7);
  434. z-index: 99999;
  435. position: fixed;
  436. top: 0;
  437. left: 0;
  438. height: 100%;
  439. width: 100%;
  440. display: flex;
  441. align-items: center;
  442. justify-content: center;
  443. .form{
  444. border-radius: 20rpx;
  445. padding: 60rpx 30rpx;
  446. width: 500upx;
  447. height: 300upx;
  448. background-color: #fff;
  449. .title{
  450. font-size: 32upx;
  451. font-family: PingFang SC;
  452. font-weight: bold;
  453. }
  454. .desc{
  455. font-size: 28upx;
  456. margin: 60upx 0upx 60upx 0upx;
  457. font-family: PingFang SC;
  458. font-weight: 500;
  459. }
  460. .btn-box{
  461. margin-top: 30rpx;
  462. width: 100%;
  463. display: flex;
  464. align-items: center;
  465. justify-content: center;
  466. .btn{
  467. display: flex;
  468. align-items: center;
  469. justify-content: center;
  470. margin-left: 10upx;
  471. width: 50%;
  472. height: 80rpx;
  473. border-radius: 5rpx;
  474. background-color: #1BB99D;
  475. font-size: 30rpx;
  476. font-family: PingFang SC;
  477. font-weight: 500;
  478. color: #fff;
  479. position: relative;
  480. }
  481. .btn-close{
  482. margin-right: 10upx;
  483. width: 50%;
  484. height: 80rpx;
  485. border-radius: 5rpx;
  486. border: 2rpx solid #1BB99D;
  487. background: none;
  488. font-size: 30rpx;
  489. font-family: PingFang SC;
  490. font-weight: 500;
  491. color: #1BB99D;
  492. }
  493. }
  494. }
  495. }
  496. .auth_btn{
  497. width: 100%;
  498. height: 100%;
  499. top:0upx;
  500. position: absolute;
  501. opacity:0.0;
  502. }
  503. </style>