login.vue 8.9 KB

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