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