login.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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="wx40593595e62f61ad";
  82. // var url="http://h5.yjf.runtzh.com";
  83. var appId="wx703c4bd07bbd1695";
  84. var url="https://userapp.zhengzefeng.com";
  85. 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';
  86. //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')
  87. // redirect_uri是授权成功后,跳转的url地址,微信会帮我们跳转到该链接,并且通过?的形式拼接code
  88. },
  89. handleAgreement(){
  90. this.isAgreement=!this.isAgreement;
  91. },
  92. openH5(url){
  93. var requestPath = uni.getStorageSync('requestPath');
  94. uni.setStorageSync('url',requestPath+url);
  95. uni.navigateTo({
  96. url: '../home/h5'
  97. })
  98. },
  99. getCode(){
  100. var that=this;
  101. this.utils.getProvider()
  102. .then(provider => {
  103. console.log('当前的环境商',provider)
  104. if (!provider) {
  105. reject()
  106. }
  107. // uni登录
  108. uni.login({
  109. provider: provider,
  110. success: async loginRes => {
  111. that.code = loginRes.code
  112. }
  113. })
  114. })
  115. .catch(err => {
  116. })
  117. },
  118. // 微信用户手机号登录
  119. phoneLogin(e) {
  120. var that=this;
  121. if(!this.isAgreement){
  122. uni.showToast({
  123. icon:'none',
  124. title: "请先同意协议后再登录",
  125. });
  126. return false;
  127. }
  128. uni.showLoading({
  129. title:"处理中..."
  130. })
  131. if (e.mp.detail.errMsg == 'getPhoneNumber:ok') {
  132. this.utils.getProvider()
  133. .then(provider => {
  134. console.log('当前的环境商',provider)
  135. if (!provider) {
  136. reject()
  137. }
  138. // uni登录
  139. uni.login({
  140. provider: provider,
  141. success: async loginRes => {
  142. console.log(loginRes)
  143. let code = loginRes.code // 获取开发code
  144. var userCode=uni.getStorageSync('userCode');
  145. loginByMiniApp({
  146. encryptedData: e.mp.detail.encryptedData,
  147. iv: e.mp.detail.iv,
  148. code: code,
  149. userCode:userCode
  150. })
  151. .then( res => {
  152. if(res.code==200){
  153. uni.hideLoading();
  154. uni.showToast({
  155. icon:'none',
  156. title: "登录成功",
  157. });
  158. // uni.setStorageSync('AppToken',res.token);
  159. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  160. uni.hideLoading()
  161. //that.getUserInfo()
  162. uni.$emit('refreshLogin');
  163. uni.navigateBack({
  164. delta:1
  165. })
  166. }
  167. else{
  168. uni.hideLoading();
  169. uni.showToast({
  170. icon:'none',
  171. title: "授权登录失败,请重新登录",
  172. });
  173. }
  174. })
  175. .catch(error => {
  176. console.log(error)
  177. uni.hideLoading();
  178. uni.showToast({
  179. icon:'none',
  180. title: "登录接口调用失败",
  181. });
  182. })
  183. }
  184. })
  185. })
  186. .catch(err => {
  187. uni.showToast({
  188. icon:'none',
  189. title: err,
  190. });
  191. })
  192. } else {
  193. uni.showToast({
  194. title: '已拒绝授权',
  195. icon: 'none',
  196. duration: 2000,
  197. })
  198. }
  199. },
  200. back() {
  201. // uni.switchTab({
  202. // url: '/pages/home/index'
  203. // });
  204. uni.navigateBack()
  205. }
  206. }
  207. }
  208. </script>
  209. <style lang="scss">
  210. .container {
  211. flex: 1;
  212. display: flex;
  213. flex-direction: column;
  214. justify-content: flex-start;
  215. position: relative;
  216. }
  217. .force-login-wrap {
  218. width: 100%;
  219. height: 100%;
  220. overflow: hidden;
  221. z-index: 11111;
  222. top: 0;
  223. .force-login__content {
  224. position: absolute;
  225. left: 50%;
  226. top: 40%;
  227. transform: translate(-50%, -50%);
  228. .user-avatar {
  229. border: 4upx solid #FFFFFF;
  230. box-shadow: 0px 5px 15px 2px rgba(0,0,0,0.1);
  231. width: 160rpx;
  232. height: 160rpx;
  233. border-radius: 50%;
  234. overflow: hidden;
  235. margin-bottom: 40rpx;
  236. }
  237. .user-name {
  238. font-size: 35rpx;
  239. font-family: PingFang SC;
  240. font-weight: bold;
  241. color: #000;
  242. margin-bottom: 30rpx;
  243. }
  244. .login-notice {
  245. font-size: 28rpx;
  246. font-family: PingFang SC;
  247. font-weight: 400;
  248. color: #000;
  249. line-height: 44rpx;
  250. width: 500rpx;
  251. text-align: center;
  252. margin-bottom: 80rpx;
  253. }
  254. .author-btn {
  255. width: 630rpx;
  256. height: 80rpx;
  257. background: linear-gradient(to right, #38e663 0%, #018C39 100%);
  258. background: -moz-linear-gradient(to right, #38e663 0%, #018C39 100%);
  259. // box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
  260. border-radius: 40rpx;
  261. font-size: 30rpx;
  262. font-family: PingFang SC;
  263. font-weight: 500;
  264. color: rgba(255, 255, 255, 1);
  265. }
  266. .close-btn {
  267. width: 630rpx;
  268. height: 80rpx;
  269. margin-top: 30rpx;
  270. border-radius: 40rpx;
  271. border: 2rpx solid #018C39;
  272. background: none;
  273. font-size: 30rpx;
  274. font-family: PingFang SC;
  275. font-weight: 500;
  276. color: #018C39;
  277. }
  278. }
  279. }
  280. .tips{
  281. margin-top: 30rpx;
  282. display: flex;
  283. justify-content: center;
  284. align-items: center;
  285. font-size: 28rpx;
  286. color: #000;
  287. checkbox{
  288. }
  289. .btn{
  290. color: #018C39;
  291. }
  292. }
  293. .wx-login{
  294. background: rgba(0,0,0,0.7);
  295. z-index: 99999;
  296. position: fixed;
  297. top: 0;
  298. left: 0;
  299. height: 100%;
  300. width: 100%;
  301. display: flex;
  302. align-items: center;
  303. justify-content: center;
  304. .form{
  305. border-radius: 20rpx;
  306. padding: 60rpx 30rpx;
  307. width: 500upx;
  308. height: 300upx;
  309. background-color: #fff;
  310. .title{
  311. font-size: 32upx;
  312. font-family: PingFang SC;
  313. font-weight: bold;
  314. }
  315. .desc{
  316. font-size: 28upx;
  317. margin: 60upx 0upx 60upx 0upx;
  318. font-family: PingFang SC;
  319. font-weight: 500;
  320. }
  321. .btn-box{
  322. margin-top: 30rpx;
  323. width: 100%;
  324. display: flex;
  325. align-items: center;
  326. justify-content: center;
  327. .btn{
  328. display: flex;
  329. align-items: center;
  330. justify-content: center;
  331. margin-left: 10upx;
  332. width: 50%;
  333. height: 80rpx;
  334. border-radius: 5rpx;
  335. background-color: #018C39;
  336. font-size: 30rpx;
  337. font-family: PingFang SC;
  338. font-weight: 500;
  339. color: #fff;
  340. position: relative;
  341. }
  342. .btn-close{
  343. margin-right: 10upx;
  344. width: 50%;
  345. height: 80rpx;
  346. border-radius: 5rpx;
  347. border: 2rpx solid #018C39;
  348. background: none;
  349. font-size: 30rpx;
  350. font-family: PingFang SC;
  351. font-weight: 500;
  352. color: #018C39;
  353. }
  354. }
  355. }
  356. }
  357. .auth_btn{
  358. width: 100%;
  359. height: 100%;
  360. top:0upx;
  361. position: absolute;
  362. opacity:0.0;
  363. }
  364. </style>