login.vue 11 KB

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