login.vue 9.8 KB

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