login.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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.setStorageSync('AppToken',res.token);
  157. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  158. uni.hideLoading()
  159. //that.getUserInfo()
  160. uni.$emit('refreshLogin');
  161. uni.navigateBack({
  162. delta:1
  163. })
  164. }
  165. else{
  166. uni.hideLoading();
  167. uni.showToast({
  168. icon:'none',
  169. title: "授权登录失败,请重新登录",
  170. });
  171. }
  172. })
  173. .catch(error => {
  174. console.log(error)
  175. uni.hideLoading();
  176. uni.showToast({
  177. icon:'none',
  178. title: "登录接口调用失败",
  179. });
  180. })
  181. }
  182. })
  183. })
  184. .catch(err => {
  185. uni.showToast({
  186. icon:'none',
  187. title: err,
  188. });
  189. })
  190. } else {
  191. uni.showToast({
  192. title: '已拒绝授权',
  193. icon: 'none',
  194. duration: 2000,
  195. })
  196. }
  197. },
  198. back() {
  199. // uni.switchTab({
  200. // url: '/pages/home/index'
  201. // });
  202. uni.navigateBack()
  203. }
  204. }
  205. }
  206. </script>
  207. <style lang="scss">
  208. .container {
  209. flex: 1;
  210. display: flex;
  211. flex-direction: column;
  212. justify-content: flex-start;
  213. position: relative;
  214. }
  215. .force-login-wrap {
  216. width: 100%;
  217. height: 100%;
  218. overflow: hidden;
  219. z-index: 11111;
  220. top: 0;
  221. .force-login__content {
  222. position: absolute;
  223. left: 50%;
  224. top: 40%;
  225. transform: translate(-50%, -50%);
  226. .user-avatar {
  227. border: 4upx solid #FFFFFF;
  228. box-shadow: 0px 5px 15px 2px rgba(0,0,0,0.1);
  229. width: 160rpx;
  230. height: 160rpx;
  231. border-radius: 50%;
  232. overflow: hidden;
  233. margin-bottom: 40rpx;
  234. }
  235. .user-name {
  236. font-size: 35rpx;
  237. font-family: PingFang SC;
  238. font-weight: bold;
  239. color: #000;
  240. margin-bottom: 30rpx;
  241. }
  242. .login-notice {
  243. font-size: 28rpx;
  244. font-family: PingFang SC;
  245. font-weight: 400;
  246. color: #000;
  247. line-height: 44rpx;
  248. width: 500rpx;
  249. text-align: center;
  250. margin-bottom: 80rpx;
  251. }
  252. .author-btn {
  253. width: 630rpx;
  254. height: 80rpx;
  255. background: linear-gradient(to right, #56ef8e 0%, #018C39 100%);
  256. background: -moz-linear-gradient(to right, #56ef8e 0%, #018C39 100%);
  257. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  258. border-radius: 40rpx;
  259. font-size: 30rpx;
  260. font-family: PingFang SC;
  261. font-weight: 500;
  262. color: rgba(255, 255, 255, 1);
  263. }
  264. .close-btn {
  265. width: 630rpx;
  266. height: 80rpx;
  267. margin-top: 30rpx;
  268. border-radius: 40rpx;
  269. border: 2rpx solid #018C39;
  270. background: none;
  271. font-size: 30rpx;
  272. font-family: PingFang SC;
  273. font-weight: 500;
  274. color: #018C39;
  275. }
  276. }
  277. }
  278. .tips{
  279. margin-top: 30rpx;
  280. display: flex;
  281. justify-content: center;
  282. align-items: center;
  283. font-size: 28rpx;
  284. color: #000;
  285. checkbox{
  286. }
  287. .btn{
  288. color: #018C39;
  289. }
  290. }
  291. .wx-login{
  292. background: rgba(0,0,0,0.7);
  293. z-index: 99999;
  294. position: fixed;
  295. top: 0;
  296. left: 0;
  297. height: 100%;
  298. width: 100%;
  299. display: flex;
  300. align-items: center;
  301. justify-content: center;
  302. .form{
  303. border-radius: 20rpx;
  304. padding: 60rpx 30rpx;
  305. width: 500upx;
  306. height: 300upx;
  307. background-color: #fff;
  308. .title{
  309. font-size: 32upx;
  310. font-family: PingFang SC;
  311. font-weight: bold;
  312. }
  313. .desc{
  314. font-size: 28upx;
  315. margin: 60upx 0upx 60upx 0upx;
  316. font-family: PingFang SC;
  317. font-weight: 500;
  318. }
  319. .btn-box{
  320. margin-top: 30rpx;
  321. width: 100%;
  322. display: flex;
  323. align-items: center;
  324. justify-content: center;
  325. .btn{
  326. display: flex;
  327. align-items: center;
  328. justify-content: center;
  329. margin-left: 10upx;
  330. width: 50%;
  331. height: 80rpx;
  332. border-radius: 5rpx;
  333. background-color: #018C39;
  334. font-size: 30rpx;
  335. font-family: PingFang SC;
  336. font-weight: 500;
  337. color: #fff;
  338. position: relative;
  339. }
  340. .btn-close{
  341. margin-right: 10upx;
  342. width: 50%;
  343. height: 80rpx;
  344. border-radius: 5rpx;
  345. border: 2rpx solid #018C39;
  346. background: none;
  347. font-size: 30rpx;
  348. font-family: PingFang SC;
  349. font-weight: 500;
  350. color: #018C39;
  351. }
  352. }
  353. }
  354. }
  355. .auth_btn{
  356. width: 100%;
  357. height: 100%;
  358. top:0upx;
  359. position: absolute;
  360. opacity:0.0;
  361. }
  362. </style>