login.vue 19 KB

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