login.vue 12 KB

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