login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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.jpg'"></image> -->
  9. <image src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/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="stylus">
  222. checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  223. background:#FF233C;
  224. border: 1px solid #FF233C;
  225. border-radius: 50%;
  226. }
  227. </style>
  228. <style lang="scss">
  229. page{
  230. background-color: #fff !important;
  231. }
  232. .logo{
  233. display: flex;
  234. flex-direction: column;
  235. justify-content: center;
  236. align-items: center;
  237. margin-bottom: 30rpx;
  238. .logo-img{
  239. border: 4upx solid #FFFFFF;
  240. //box-shadow: 0px 5px 15px 2px rgba(0,0,0,0.1);
  241. border-radius: 50%;
  242. width: 210rpx;
  243. height: 210rpx;
  244. image{
  245. border-radius: 50%;
  246. width: 100%;
  247. height: 100%;
  248. overflow: hidden;
  249. }
  250. }
  251. .title{
  252. margin-top: 20rpx;
  253. font-size: 35rpx;
  254. font-family: PingFang SC;
  255. font-weight: bold;
  256. color: #000;
  257. margin-bottom: 30rpx;
  258. }
  259. }
  260. .container {
  261. flex: 1;
  262. display: flex;
  263. flex-direction: column;
  264. justify-content: flex-start;
  265. // position: relative;
  266. }
  267. .force-login-wrap {
  268. width: 100%;
  269. height: 100%;
  270. overflow: hidden;
  271. z-index: 11111;
  272. top: 0;
  273. .force-login__content {
  274. position: absolute;
  275. left: 50%;
  276. top: 40%;
  277. transform: translate(-50%, -50%);
  278. .user-avatar {
  279. border: 4upx solid #FFFFFF;
  280. box-shadow: 0px 5px 15px 2px rgba(0,0,0,0.1);
  281. width: 160rpx;
  282. height: 160rpx;
  283. border-radius: 50%;
  284. overflow: hidden;
  285. margin-bottom: 40rpx;
  286. }
  287. .user-name {
  288. font-size: 35rpx;
  289. font-family: PingFang SC;
  290. font-weight: bold;
  291. color: #000;
  292. margin-bottom: 30rpx;
  293. }
  294. .login-notice {
  295. font-family: PingFangSC, PingFang SC;
  296. font-weight: 600;
  297. font-size: 36rpx;
  298. color: #000;
  299. line-height: 44rpx;
  300. // width: 500rpx;
  301. text-align: center;
  302. margin-bottom: 80rpx;
  303. }
  304. .author-btn {
  305. width: 630rpx;
  306. height: 108rpx;
  307. background: linear-gradient( 135deg, #FF5267 0%, #FF233C 100%);
  308. // background: linear-gradient(to right, #66b2ef 0%, #66b2ef 100%);
  309. //background: -moz-linear-gradient(to right, #66b2ef 0%, #66b2ef 100%);
  310. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  311. border-radius: 52rpx;
  312. font-weight: 600;
  313. font-size: 40rpx;
  314. color: #FFFFFF;
  315. color: rgba(255, 255, 255, 1);
  316. }
  317. .close-btn {
  318. width: 630rpx;
  319. height: 108rpx;
  320. margin-top: 40rpx;
  321. border-radius: 52rpx;
  322. border: 2rpx solid #FF233C;
  323. background: none;
  324. font-weight: 600;
  325. font-size: 40rpx;
  326. color: #FFFFFF;
  327. color: #FF233C;
  328. }
  329. }
  330. }
  331. .tips{
  332. margin-top: 30rpx;
  333. display: flex;
  334. justify-content: center;
  335. align-items: center;
  336. font-family: PingFangSC, PingFang SC;
  337. font-weight: 400;
  338. font-size: 32rpx;
  339. color: #999;
  340. .btn{
  341. color: #FF233C;
  342. }
  343. }
  344. .wx-login{
  345. background: rgba(0,0,0,0.7);
  346. z-index: 99999;
  347. position: fixed;
  348. top: 0;
  349. left: 0;
  350. height: 100%;
  351. width: 100%;
  352. display: flex;
  353. align-items: center;
  354. justify-content: center;
  355. .form{
  356. border-radius: 20rpx;
  357. padding: 60rpx 30rpx;
  358. width: 500upx;
  359. height: 300upx;
  360. background-color: #fff;
  361. .title{
  362. font-size: 32upx;
  363. font-family: PingFang SC;
  364. font-weight: bold;
  365. }
  366. .desc{
  367. font-size: 28upx;
  368. margin: 60upx 0upx 60upx 0upx;
  369. font-family: PingFang SC;
  370. font-weight: 500;
  371. }
  372. .btn-box{
  373. margin-top: 30rpx;
  374. width: 100%;
  375. display: flex;
  376. align-items: center;
  377. justify-content: center;
  378. .btn{
  379. display: flex;
  380. align-items: center;
  381. justify-content: center;
  382. margin-left: 10upx;
  383. width: 50%;
  384. height: 80rpx;
  385. border-radius: 5rpx;
  386. background-color: #66b2ef;
  387. font-size: 30rpx;
  388. font-family: PingFang SC;
  389. font-weight: 500;
  390. color: #fff;
  391. position: relative;
  392. }
  393. .btn-close{
  394. margin-right: 10upx;
  395. width: 50%;
  396. height: 80rpx;
  397. border-radius: 5rpx;
  398. border: 2rpx solid #66b2ef;
  399. background: none;
  400. font-size: 30rpx;
  401. font-family: PingFang SC;
  402. font-weight: 500;
  403. color: #66b2ef;
  404. }
  405. }
  406. }
  407. }
  408. .auth_btn{
  409. width: 100%;
  410. height: 100%;
  411. top:0upx;
  412. position: absolute;
  413. opacity:0.0;
  414. }
  415. </style>