login.vue 11 KB

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