login.vue 12 KB

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