login.vue 9.7 KB

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