login.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. <template>
  2. <view class="login-container">
  3. <!-- <image @click="navBack" src="../../static/images/index/close.png" style="width: 32upx;height: 32upx;margin-left: 46upx;"></image> -->
  4. <!-- 小程序状态下登录 -->
  5. <!-- #ifdef MP-WEIXIN -->
  6. <view class="mp_wxBox">
  7. <view>
  8. <view class="headers">
  9. <image src="../../static/logo.png" style="border-radius: 50%;"></image>
  10. </view>
  11. <view class="content">
  12. <view>申请获取以下权限</view>
  13. <text>获得你的公开信息(昵称,头像、地区等)</text>
  14. </view>
  15. <button v-show="weixinPhone" style="background: #2FB57A;color: #FFFFFF;" class="bottom"
  16. open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  17. 授权手机号
  18. </button>
  19. <button v-show="!weixinPhone" style="background: #2FB57A;color: #FFFFFF;" class='bottom'
  20. bindtap="getUserProfile" @tap="wxGetUserInfo">
  21. 授权登录
  22. </button>
  23. </view>
  24. </view>
  25. <view style="text-align: center;">
  26. <view class="footer">
  27. <image v-if="showAgree" @tap="isShowAgree"
  28. src="/static/image/selected.png"
  29. style="width: 30upx;height: 30upx;"></image>
  30. <image v-else @tap="isShowAgree"
  31. src="/static/image/noselect.png"
  32. style="width: 30upx;height: 30upx;"></image>
  33. <text style="margin-left: 10upx;margin-right: 0;">同意</text>
  34. <!-- 协议地址 -->
  35. <navigator url="/my/setting/mimi" open-type="navigate">《隐私政策》</navigator>
  36. <navigator url="/my/setting/xieyi" open-type="navigate">《用户协议》</navigator>
  37. </view>
  38. </view>
  39. <!-- #endif -->
  40. <!-- #ifndef MP-WEIXIN -->
  41. <view style="font-size: 44rpx;margin: 32upx;color: #222;text-align: center;font-weight: bold;">欢迎登录上门服务</view>
  42. <view style="text-align: center;">
  43. <image style="width: 150upx;height: 150upx;margin-top: 60upx;border-radius:100upx"
  44. src="../../static/logo.png"></image>
  45. <view style="font-size: 36rpx;margin: 32upx;color: #222;font-weight: bold;">上门服务 专业O2O服务平台</view>
  46. <!-- #ifdef APP-PLUS -->
  47. <button v-if="weixinLogin" class="confirm-btn" @click="weixinLo">微信登录</button>
  48. <button v-if="weixinLogin" class='confirm-btn-weixin' @click="register">手机号登录</button>
  49. <button v-if="!weixinLogin" class='confirm-btn' @click="register">手机号登录</button>
  50. <!-- #endif -->
  51. <!-- #ifdef H5 -->
  52. <button class='confirm-btn' v-if="isopen" @click="bingwx">微信一键登录</button>
  53. <button v-if="!isopen" class='confirm-btn' @click="register">登录</button>
  54. <!-- #endif -->
  55. <view v-if="!isopen"
  56. style="width: 100%;text-align: center;margin-top: 20upx;color: #FF6E98;text-decoration:underline;"
  57. @tap="msgRegister">验证码登录</view>
  58. <!-- 底部信息 -->
  59. <view class="footer">
  60. <image v-if="showAgree" @tap="isShowAgree"
  61. src="/static/image/selected.png"
  62. style="width: 30upx;height: 30upx;"></image>
  63. <image v-else @tap="isShowAgree"
  64. src="/static/image/noselect.png"
  65. style="width: 30upx;height: 30upx;"></image>
  66. <text style="margin-left: 10upx;margin-right: 0;">同意</text>
  67. <!-- 协议地址 -->
  68. <navigator url="/my/setting/mimi" open-type="navigate" style="">《隐私政策》</navigator>
  69. <navigator url="/my/setting/xieyi" open-type="navigate" style="">《用户协议》</navigator>
  70. </view>
  71. </view>
  72. <!-- #endif -->
  73. </view>
  74. </template>
  75. <script>
  76. export default {
  77. data() {
  78. return {
  79. mobile: '',
  80. code: '',
  81. showAgree: false, //协议是否选择
  82. isopen: false,
  83. weixinLogin: false,
  84. sending: false,
  85. sendTime: '获取验证码',
  86. count: 60,
  87. weixinPhone: false,
  88. sendDataList: {},
  89. sessionkey: '',
  90. phoneNum: false
  91. };
  92. },
  93. onLoad(e) {
  94. if (e.inviterCode) {
  95. this.$queue.setData('inviterCode', e.inviterCode);
  96. }
  97. this.$Request.get('/app/common/type/237').then(res => {
  98. if (res.code == 0) {
  99. if (res.data && res.data.value && res.data.value == '是') {
  100. // #ifdef H5
  101. let ua = navigator.userAgent.toLowerCase();
  102. if (ua.indexOf('micromessenger') !== -1) {
  103. this.isopen = true;
  104. } else {
  105. this.isopen = false;
  106. }
  107. // #endif
  108. }
  109. }
  110. });
  111. //微信登录开启
  112. this.$Request.getT('/app/common/type/53').then(res => {
  113. if (res.code == 0) {
  114. if (res.data && res.data.value && res.data.value == '是') {
  115. // this.weixinLogin = true;
  116. }
  117. }
  118. });
  119. this.$Request.getT('/app/common/type/188').then(res => {
  120. if (res.code == 0) {
  121. if (res.data && res.data.value && res.data.value == '是') {
  122. this.phoneNum = true;
  123. }
  124. }
  125. });
  126. // #ifdef H5
  127. this.selbindwx();
  128. // #endif
  129. },
  130. methods: {
  131. isShowAgree() {
  132. //是否选择协议
  133. this.showAgree = !this.showAgree;
  134. },
  135. selbindwx() {
  136. let ua = navigator.userAgent.toLowerCase();
  137. if (ua.indexOf('micromessenger') !== -1) {
  138. let openid = uni.getStorageSync('openid');
  139. let userId = uni.getStorageSync('userId');
  140. let that = this;
  141. if (window.location.href.indexOf('?code=') !== -1 || window.location.href.indexOf('&code=') !==
  142. -1) {
  143. let code;
  144. if (window.location.href.indexOf('?code=') !== -1) {
  145. code = window.location.href.split('?code=')[1].split('&')[0];
  146. } else {
  147. code = window.location.href.split('&code=')[1].split('&')[0];
  148. }
  149. this.$Request.get('/app/Login/getOpenId?code=' + code).then(ret => {
  150. uni.setStorageSync('openid', ret.data.openid)
  151. uni.setStorageSync('openidheadimgurl', ret.data.headimgurl)
  152. uni.setStorageSync('openidnickname', ret.data.nickname)
  153. this.$Request.get('/app/Login/openid/login?openId=' + ret.data.openid).then(res => {
  154. if (res.code == 0) {
  155. this.$queue.setData("phone", res.user.phone);
  156. this.$queue.setData("userId", res.user.userId);
  157. this.$queue.setData("token", res.token);
  158. this.$queue.setData("userName", res.user.userName);
  159. this.$queue.setData("avatar", res.user.avatar);
  160. this.$queue.setData("invitationCode", res.user.invitationCode);
  161. this.$queue.setData("inviterCode", res.user.inviterCode);
  162. uni.switchTab({
  163. url: '/pages/my/index'
  164. })
  165. } else {
  166. uni.navigateTo({
  167. url: '/pages/public/bind'
  168. });
  169. }
  170. });
  171. });
  172. }
  173. }
  174. },
  175. bingwx() {
  176. if (this.showAgree == false) {
  177. uni.showToast({
  178. icon: 'none',
  179. position: 'bottom',
  180. title: '请同意《用户协议》和《隐私政策》'
  181. });
  182. return false;
  183. }
  184. let ua = navigator.userAgent.toLowerCase();
  185. if (ua.indexOf('micromessenger') !== -1) {
  186. let openid = uni.getStorageSync('openid');
  187. let userId = uni.getStorageSync('userId');
  188. let that = this;
  189. if (!openid) {
  190. if (window.location.href.indexOf('?code=') !== -1 || window.location.href.indexOf('&code=') !==
  191. -1) {
  192. let code;
  193. if (window.location.href.indexOf('?code=') !== -1) {
  194. code = window.location.href.split('?code=')[1].split('&')[0];
  195. } else {
  196. code = window.location.href.split('&code=')[1].split('&')[0];
  197. }
  198. this.$Request.get('/app/Login/getOpenId?code=' + code).then(ret => {
  199. uni.setStorageSync('openid', ret.data.openid)
  200. uni.setStorageSync('openidheadimgurl', ret.data.headimgurl)
  201. uni.setStorageSync('openidnickname', ret.data.nickname)
  202. this.$Request.get('/app/Login/openid/login?openId=' + ret.data.openid).then(res => {
  203. if (res.code == 0) {
  204. this.$queue.setData("phone", res.user.phone);
  205. this.$queue.setData("userId", res.user.userId);
  206. this.$queue.setData("token", res.token);
  207. this.$queue.setData("userName", res.user.userName);
  208. this.$queue.setData("avatar", res.user.avatar);
  209. this.$queue.setData("invitationCode", res.user.invitationCode);
  210. this.$queue.setData("inviterCode", res.user.inviterCode);
  211. uni.switchTab({
  212. url: '/pages/my/index'
  213. })
  214. } else {
  215. uni.navigateTo({
  216. url: '/pages/public/bind'
  217. });
  218. }
  219. });
  220. });
  221. } else {
  222. window.location.href =
  223. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  224. that.$queue.getWxAppid() +
  225. '&redirect_uri=' +
  226. window.location.href.split('#')[0] +
  227. '&response_type=code&scope=snsapi_userinfo#wechat_redirect';
  228. }
  229. } else {
  230. this.$Request.get('/app/Login/openid/login?openId=' + openid).then(res => {
  231. if (res.code == 0) {
  232. this.$queue.setData("phone", res.user.phone);
  233. this.$queue.setData("userId", res.user.userId);
  234. this.$queue.setData("token", res.token);
  235. this.$queue.setData("userName", res.user.userName);
  236. this.$queue.setData("avatar", res.user.avatar);
  237. this.$queue.setData("invitationCode", res.user.invitationCode);
  238. this.$queue.setData("inviterCode", res.user.inviterCode);
  239. uni.switchTab({
  240. url: '/pages/my/index'
  241. })
  242. } else {
  243. uni.navigateTo({
  244. url: '/pages/public/bind'
  245. });
  246. }
  247. });
  248. }
  249. }
  250. },
  251. wxGetUserInfo(e) {
  252. if (this.showAgree == false) {
  253. uni.showToast({
  254. icon: 'none',
  255. position: 'bottom',
  256. title: '请同意《用户协议》和《隐私政策》'
  257. });
  258. return false;
  259. }
  260. wx.getUserProfile({
  261. desc: '业务需要',
  262. success: infoRes => {
  263. console.log("infoRes.encryptedData__________:" + JSON.stringify(infoRes.userInfo))
  264. let nickName = infoRes.userInfo.nickName; //昵称
  265. let avatarUrl = infoRes.userInfo.avatarUrl; //头像
  266. let sex = infoRes.userInfo.gender; //头像
  267. try {
  268. this.$queue.showLoading('正在登录中...');
  269. this.login(nickName, avatarUrl, sex);
  270. } catch (e) {}
  271. }
  272. })
  273. },
  274. //登录
  275. login(nickName, avatarUrl, sex) {
  276. if (this.showAgree == false) {
  277. uni.showToast({
  278. icon: 'none',
  279. position: 'bottom',
  280. title: '请同意《用户协议》和《隐私政策》'
  281. });
  282. return false;
  283. }
  284. let that = this;
  285. // 1.wx获取登录用户code
  286. uni.login({
  287. provider: 'weixin',
  288. success: function(loginRes) {
  289. console.log(loginRes, '************')
  290. let data = {
  291. code: loginRes.code,
  292. }
  293. that.$Request.get('/app/Login/wxLogin', data).then(res => {
  294. if (res.code == 0) {
  295. uni.hideLoading()
  296. uni.setStorageSync('openId', res.data.open_id)
  297. uni.setStorageSync('unionId', res.data.unionId)
  298. that.sessionkey = res.data.session_key;
  299. let inviterCode = '';
  300. if (uni.getStorageSync('inviterCode')) {
  301. inviterCode = uni.getStorageSync('inviterCode')
  302. }
  303. let sendData = {
  304. openId: uni.getStorageSync('openId'),
  305. unionId: uni.getStorageSync('unionId'),
  306. userName: nickName,
  307. avatar: avatarUrl,
  308. sex: sex, //性别
  309. inviterCode: inviterCode //别人登录进来携带你的邀请码
  310. };
  311. that.sendDataList = sendData;
  312. that.flag = res.data.flag;
  313. // 第一次登录获取手机号
  314. console.log(that.phoneNum)
  315. if (that.flag == '1' && that.phoneNum) {
  316. that.weixinPhone = true;
  317. } else {
  318. that.getWeixinInfo(sendData);
  319. }
  320. } else {
  321. uni.showToast({
  322. icon: 'none',
  323. title: res.msg,
  324. duration: 2000
  325. });
  326. console.log(res, '失败')
  327. }
  328. })
  329. }
  330. });
  331. },
  332. //小程序微信登录后获取手机号
  333. getPhoneNumber: function(e) {
  334. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  335. console.log('用户拒绝提供手机号');
  336. } else {
  337. console.log('用户同意提供手机号');
  338. console.log(e)
  339. this.setPhoneByInsert(e.detail.encryptedData, e.detail.iv);
  340. }
  341. },
  342. //小程序微信登录后获取手机号
  343. setPhoneByInsert(decryptData, iv) {
  344. let data = {
  345. decryptData: decryptData,
  346. key: this.sessionkey,
  347. iv: iv
  348. };
  349. this.$Request.postJson('/app/Login/selectPhone', data).then(res => {
  350. if (res.code == 0) {
  351. this.phone = res.data.phoneNumber;
  352. this.getWeixinInfo(this.sendDataList);
  353. } else {
  354. uni.showToast({
  355. title: res.msg,
  356. icon: 'none',
  357. duration: 2000
  358. });
  359. }
  360. })
  361. },
  362. //获取个人信息
  363. getWeixinInfo(sendData) {
  364. let that = this;
  365. uni.showLoading({
  366. title: '登录中...'
  367. });
  368. let postData = {
  369. openId: sendData.openId, //小程序openId
  370. unionId: sendData.unionId, //unionId
  371. userName: sendData.userName, //微信名称
  372. avatar: sendData.avatar, //头像
  373. sex: sendData.sex, //性别
  374. phone: that.phone,
  375. inviterCode: sendData.inviterCode
  376. };
  377. that.$Request.postJson('/app/Login/insertWxUser', postData).then(res => {
  378. uni.hideLoading();
  379. if (res.code == 0) {
  380. uni.setStorageSync('token', res.token)
  381. uni.setStorageSync('userName', res.user.userName)
  382. uni.setStorageSync('avatar', res.user.avatar)
  383. uni.setStorageSync('phone', res.user.phone)
  384. uni.setStorageSync('invitationCode', res.user.invitationCode)
  385. uni.setStorageSync('sex', res.user.sex)
  386. uni.setStorageSync('userId', res.user.userId)
  387. uni.setStorageSync('openId', res.user.openId)
  388. this.$Request.get("/app/UserVip/isUserVip").then(res => {
  389. if (res.code == 0 && res.data && res.data.isVip == 2) {
  390. uni.setStorageSync('isVIP', true)
  391. } else {
  392. uni.setStorageSync('isVIP', false)
  393. }
  394. });
  395. uni.navigateBack();
  396. } else {
  397. uni.showModal({
  398. showCancel: false,
  399. title: '登录失败',
  400. content: res.msg,
  401. });
  402. }
  403. })
  404. },
  405. weixinLo() {
  406. let that = this;
  407. uni.login({
  408. provider: 'weixin',
  409. success: function(loginRes) {
  410. that.$queue.showLoading('正在登录中...');
  411. console.error(loginRes.authResult);
  412. that.$queue.setData('weixinToken', loginRes.authResult.access_token);
  413. that.$queue.setData('unionid', loginRes.authResult.unionid);
  414. that.$queue.setData('weixinOpenid', loginRes.authResult.openid);
  415. that.$Request.postJson('/app/login/loginApp', {
  416. token: loginRes.authResult.access_token,
  417. unionid: loginRes.authResult.unionid,
  418. openid: loginRes.authResult.openid
  419. }).then(res => {
  420. console.log(JSON.stringify(res))
  421. if (res.code === 0) {
  422. if (uni.getSystemInfoSync().platform == "android") {
  423. let clientid = plus.push.getClientInfo().clientid;
  424. that.$Request.postT('/app/login/updateClientId?clientId=' +
  425. clientid + '&userId=' + res.userId).then(res => {
  426. });
  427. }
  428. that.$queue.setData("token", res.uuid);
  429. that.$queue.setData("userId", res.userId);
  430. that.getUserInfo(res.userId, res.token);
  431. } else {
  432. uni.hideLoading();
  433. uni.navigateTo({
  434. url: '/pages/public/wxmobile'
  435. });
  436. }
  437. });
  438. }
  439. });
  440. },
  441. forget() {
  442. uni.navigateTo({
  443. url: '/pages/public/pwd'
  444. });
  445. },
  446. msgRegister() {
  447. if (this.showAgree == false) {
  448. uni.showToast({
  449. icon: 'none',
  450. position: 'bottom',
  451. title: '请同意《用户协议》和《隐私政策》'
  452. });
  453. return false;
  454. }
  455. uni.navigateTo({
  456. url: '/pages/public/loginmsg'
  457. });
  458. },
  459. register() {
  460. if (this.showAgree == false) {
  461. uni.showToast({
  462. icon: 'none',
  463. position: 'bottom',
  464. title: '请同意《用户协议》和《隐私政策》'
  465. });
  466. return false;
  467. }
  468. uni.navigateTo({
  469. url: '/pages/public/loginphone'
  470. });
  471. },
  472. inputChange(e) {
  473. const key = e.currentTarget.dataset.key;
  474. this[key] = e.detail.value;
  475. },
  476. navBack() {
  477. uni.navigateBack();
  478. },
  479. getUserInfo(userId, token) {
  480. this.$Request.postJson('/app/selectUserById?userId=' + userId).then(res => {
  481. if (res.code === 0) {
  482. this.$queue.setData('token', res.data.uuid);
  483. this.$queue.setData('image_url', res.data.imageUrl ? res.data.imageUrl :
  484. '/static/img/common/logo.jpg');
  485. this.$queue.setData('inviterCode', res.data.inviterCode);
  486. this.$queue.setData('invitationCode', res.data.invitationCode);
  487. this.$queue.setData('grade', res.data.grade);
  488. this.$queue.setData('mobile', res.data.mobile);
  489. this.$queue.setData('isInvitation', res.data.isInvitation);
  490. this.$queue.setData('nickName', res.data.nickName ? res.data.nickName : res.data.phone);
  491. this.$queue.setData('gender', parseInt(res.data.gender));
  492. uni.switchTab({
  493. url: '/pages/index/index'
  494. });
  495. } else {
  496. uni.showModal({
  497. showCancel: false,
  498. title: '登录失败',
  499. content: res.msg
  500. });
  501. this.$queue.logout();
  502. }
  503. uni.hideLoading();
  504. });
  505. }
  506. }
  507. };
  508. </script>
  509. <style lang="scss">
  510. page {
  511. height: 100%;
  512. background: #FFFFFF !important;
  513. }
  514. .login-container {
  515. top: 0;
  516. padding-top: 50px;
  517. position: relative;
  518. width: 100%;
  519. height: 100%;
  520. overflow: hidden;
  521. background: linear-gradient( 180deg, #FFE5ED 0%, #FFFFFF 37%, #FFE8EF 100%);
  522. color: #FFF;
  523. }
  524. .confirm-btn-weixin {
  525. width: 200px;
  526. height: 42px;
  527. line-height: 42px;
  528. border-radius: 30px;
  529. margin-top: 80upx;
  530. background: #2FB57A;
  531. color: #fff;
  532. font-size: 32upx;
  533. &:after {
  534. border-radius: 60px;
  535. }
  536. }
  537. .headers {
  538. text-align: center;
  539. }
  540. .headers>image {
  541. width: 400upx;
  542. height: 400upx;
  543. }
  544. .footer {
  545. padding-left: 160upx;
  546. margin-top: 32upx;
  547. font-size: 24upx;
  548. color: #666666;
  549. text-align: center;
  550. display: flex;
  551. align-items: center;
  552. position: fixed;
  553. bottom: 60rpx;
  554. }
  555. page {
  556. background: #fff;
  557. }
  558. .send-msg {
  559. border-radius: 30px;
  560. color: black;
  561. background: white;
  562. height: 30px;
  563. font-size: 14px;
  564. line-height: 30px;
  565. }
  566. .container {
  567. top: 0;
  568. padding-top: 32upx;
  569. position: relative;
  570. width: 100%;
  571. height: 100%;
  572. overflow: hidden;
  573. background: #f3f3f3;
  574. color: #FFF;
  575. .mp_wxBox {
  576. .headers {
  577. margin: 35% auto 50rpx;
  578. text-align: center;
  579. border-radius: 60rpx;
  580. width: 650rpx;
  581. height: 300rpx;
  582. line-height: 450rpx;
  583. image {
  584. width: 300rpx;
  585. height: 300rpx;
  586. }
  587. }
  588. .content {
  589. text-align: center;
  590. }
  591. text {
  592. display: block;
  593. color: #9d9d9d;
  594. margin-top: 40rpx;
  595. }
  596. .bottom {
  597. line-height: 80upx;
  598. border-radius: 80upx;
  599. margin: 70rpx 50rpx;
  600. height: 80upx;
  601. font-size: 35rpx;
  602. }
  603. }
  604. }
  605. .wrapper {
  606. position: relative;
  607. z-index: 90;
  608. background: #fff;
  609. padding-bottom: 20px;
  610. }
  611. .input-content {
  612. padding: 0 20px;
  613. }
  614. .confirm-btn {
  615. width: 582rpx;
  616. height: 100rpx;
  617. line-height: 100rpx;
  618. text-align: center;
  619. background: linear-gradient( 143deg, #FFE6EE 0%, #FF9AB2 100%);
  620. border-radius: 86rpx 86rpx 86rpx 86rpx;
  621. font-weight: bold;
  622. font-size: 32rpx;
  623. color: #222222;
  624. margin-top: 80rpx;
  625. }
  626. </style>