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