login.vue 11 KB

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