login.vue 11 KB

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