login.vue 9.6 KB

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