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="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(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. 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.switchTab({
  295. url: '/pages/home/index'
  296. })
  297. }
  298. }
  299. }
  300. </script>
  301. <style lang="scss">
  302. .container {
  303. flex: 1;
  304. display: flex;
  305. flex-direction: column;
  306. justify-content: flex-start;
  307. position: relative;
  308. }
  309. .force-login-wrap {
  310. width: 100%;
  311. height: 100%;
  312. overflow: hidden;
  313. z-index: 11111;
  314. top: 0;
  315. .force-login__content {
  316. position: absolute;
  317. left: 50%;
  318. top: 40%;
  319. transform: translate(-50%, -50%);
  320. .logo{
  321. display: flex;
  322. flex-direction: column;
  323. justify-content: center;
  324. align-items: center;
  325. .logo-img{
  326. border: 4upx solid #FFFFFF;
  327. box-shadow: 0px 5px 15px 2px rgba(0,0,0,0.1);
  328. border-radius: 50%;
  329. width: 80px;
  330. height: 80px;
  331. image{
  332. border-radius: 50%;
  333. width: 100%;
  334. height: 100%;
  335. overflow: hidden;
  336. }
  337. }
  338. .title{
  339. margin-top: 20rpx;
  340. font-size: 35rpx;
  341. font-family: PingFang SC;
  342. font-weight: bold;
  343. color: #000;
  344. margin-bottom: 30rpx;
  345. }
  346. }
  347. .login-notice {
  348. font-size: 28rpx;
  349. font-family: PingFang SC;
  350. font-weight: 400;
  351. color: #000;
  352. line-height: 44rpx;
  353. width: 500rpx;
  354. text-align: center;
  355. margin-bottom: 80rpx;
  356. }
  357. .btns{
  358. position: relative;
  359. width: 630rpx;
  360. height: 80rpx;
  361. .author-btn{
  362. z-index:100;
  363. position: absolute;
  364. width: 630rpx;
  365. height: 80rpx;
  366. background: linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  367. background: -moz-linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  368. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  369. border-radius: 40rpx;
  370. font-size: 30rpx;
  371. font-family: PingFang SC;
  372. font-weight: 500;
  373. color: rgba(255, 255, 255, 1);
  374. }
  375. }
  376. .author-btn{
  377. z-index:100;
  378. // position: absolute;
  379. width: 630rpx;
  380. height: 80rpx;
  381. background: linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  382. background: -moz-linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  383. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  384. border-radius: 40rpx;
  385. font-size: 30rpx;
  386. font-family: PingFang SC;
  387. font-weight: 500;
  388. color: rgba(255, 255, 255, 1);
  389. }
  390. .author-btn {
  391. width: 630rpx;
  392. height: 80rpx;
  393. background: linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  394. background: -moz-linear-gradient(to right, #2BC7B9 0%, #2aa7B9 100%);
  395. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  396. border-radius: 40rpx;
  397. font-size: 30rpx;
  398. font-family: PingFang SC;
  399. font-weight: 500;
  400. color: rgba(255, 255, 255, 1);
  401. }
  402. .close-btn {
  403. width: 630rpx;
  404. height: 80rpx;
  405. margin-top: 30rpx;
  406. border-radius: 40rpx;
  407. border: 2rpx solid #2BC7B9;
  408. background: none;
  409. font-size: 30rpx;
  410. font-family: PingFang SC;
  411. font-weight: 500;
  412. color: #2BC7B9;
  413. }
  414. }
  415. }
  416. .tips{
  417. margin-top: 30rpx;
  418. display: flex;
  419. justify-content: center;
  420. align-items: center;
  421. font-size: 28rpx;
  422. color: #000;
  423. checkbox{
  424. }
  425. .btn{
  426. color: #2BC7B9;
  427. }
  428. }
  429. .wx-login{
  430. background: rgba(0,0,0,0.7);
  431. z-index: 99999;
  432. position: fixed;
  433. top: 0;
  434. left: 0;
  435. height: 100%;
  436. width: 100%;
  437. display: flex;
  438. align-items: center;
  439. justify-content: center;
  440. .form{
  441. border-radius: 20rpx;
  442. padding: 60rpx 30rpx;
  443. width: 500upx;
  444. height: 300upx;
  445. background-color: #fff;
  446. .title{
  447. font-size: 32upx;
  448. font-family: PingFang SC;
  449. font-weight: bold;
  450. }
  451. .desc{
  452. font-size: 28upx;
  453. margin: 60upx 0upx 60upx 0upx;
  454. font-family: PingFang SC;
  455. font-weight: 500;
  456. }
  457. .btn-box{
  458. margin-top: 30rpx;
  459. width: 100%;
  460. display: flex;
  461. align-items: center;
  462. justify-content: center;
  463. .btn{
  464. display: flex;
  465. align-items: center;
  466. justify-content: center;
  467. margin-left: 10upx;
  468. width: 50%;
  469. height: 80rpx;
  470. border-radius: 5rpx;
  471. background-color: #2BC7B9;
  472. font-size: 30rpx;
  473. font-family: PingFang SC;
  474. font-weight: 500;
  475. color: #fff;
  476. position: relative;
  477. }
  478. .btn-close{
  479. margin-right: 10upx;
  480. width: 50%;
  481. height: 80rpx;
  482. border-radius: 5rpx;
  483. border: 2rpx solid #2BC7B9;
  484. background: none;
  485. font-size: 30rpx;
  486. font-family: PingFang SC;
  487. font-weight: 500;
  488. color: #2BC7B9;
  489. }
  490. }
  491. }
  492. }
  493. .auth_btn{
  494. width: 100%;
  495. height: 100%;
  496. top:0upx;
  497. position: absolute;
  498. opacity:0.0;
  499. }
  500. </style>