login.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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: #FFAF5E;color: #333333;" class="bottom"
  16. open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  17. 授权手机号
  18. </button>
  19. <button v-show="!weixinPhone" style="background: #FFAF5E;color: #333333;" class='bottom'
  20. bindtap="getUserProfile" @tap="wxGetUserInfo">
  21. 授权登录
  22. </button>
  23. </view>
  24. </view>
  25. <view style="text-align: center;">
  26. <view class="footers">
  27. <image v-if="showAgree" @tap="isShowAgree" src="../../static/images/selected.png"
  28. style="width: 36upx;height: 36upx;"></image>
  29. <image v-else @tap="isShowAgree" src="../../static/images/select.png"
  30. style="width: 36upx;height: 36upx;"></image>
  31. <text style="margin-left: 10upx;margin-right: 0;">同意</text>
  32. <navigator url="/my/setting/mimi" open-type="navigate">《隐私政策》</navigator>和
  33. <navigator url="/my/setting/xieyi" open-type="navigate">《用户服务协议》</navigator>
  34. </view>
  35. </view>
  36. <!-- #endif -->
  37. <!-- #ifndef MP-WEIXIN -->
  38. <view class="register">
  39. <view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view>
  40. <view style="padding-top: 172upx;margin-left: 36upx;">
  41. <view style="color: #333333;font-size: 44upx;">手机快捷登录</view>
  42. <view style="color: #999999;font-size: 24upx;margin-top: 20upx;">未注册的手机号将自动创建账号</view>
  43. </view>
  44. <view style="margin-left: 40upx;margin-top: 104upx;margin-right: 40upx;">
  45. <wInput v-model="phoneData" type="number" maxlength="11" placeholder="请输入手机号"></wInput>
  46. <!-- <wInput v-model="passData" type="number" maxlength="6" placeholder="请输入密码"></wInput> -->
  47. <wInput v-model="verCode" type="number" maxlength="6" placeholder="请输入验证码" isShowCode ref="runCode"
  48. @setCode="sendMsg()"></wInput>
  49. <wInput v-show="show" v-model="invitation" isShowGet ref="setNumberCode" @setNumberCode="isShowGet()"
  50. placeholder="请填写邀请码"></wInput>
  51. </view>
  52. <view class="footer">
  53. <image v-if="showAgree" @tap="isShowAgree" src="../../static/images/selected.png"
  54. style="width: 36upx;height: 36upx;"></image>
  55. <image v-else @tap="isShowAgree" src="../../static/images/select.png"
  56. style="width: 36upx;height: 36upx;"></image>
  57. <text style="margin-left: 10upx;margin-right: 0;">同意</text>
  58. <navigator url="/my/setting/mimi" open-type="navigate">《隐私政策》</navigator>和
  59. <navigator url="/my/setting/xieyi" open-type="navigate">《用户服务协议》</navigator>
  60. </view>
  61. <wButton text="登 录" :rotate="isRotate" @click.native="startReg()"></wButton>
  62. </view>
  63. <!-- #endif -->
  64. </view>
  65. </template>
  66. <script>
  67. import wInput from '@/components/watch-login/watch-input.vue' //input
  68. import wButton from '@/components/watch-login/watch-button.vue' //button
  69. export default {
  70. components: {
  71. wInput,
  72. wButton,
  73. },
  74. data() {
  75. return {
  76. mobile: '',
  77. code: '',
  78. weixinLogin: false,
  79. sending: false,
  80. sendTime: '获取验证码',
  81. count: 60,
  82. weixinPhone: false,
  83. sendDataList: {},
  84. sessionkey: '',
  85. phoneNum: false,
  86. sysphone: 1,
  87. phoneData: '',
  88. verCode: '',
  89. invitation: '',
  90. show: '',
  91. showAgree: false, //协议是否选择
  92. isRotate: false, //是否加载旋转
  93. platform: "h5",
  94. };
  95. },
  96. onLoad(e) {
  97. if (e.inviterCode) {
  98. this.$queue.setData('inviterCode', e.inviterCode);
  99. }
  100. //微信登录开启
  101. this.$Request.getT('/app/common/type/53').then(res => {
  102. if (res.code == 0) {
  103. if (res.data && res.data.value && res.data.value == '是') {
  104. // this.weixinLogin = true;
  105. }
  106. }
  107. });
  108. this.$Request.getT('/app/common/type/188').then(res => {
  109. if (res.code == 0) {
  110. if (res.data && res.data.value && res.data.value == '是') {
  111. this.phoneNum = true;
  112. }
  113. }
  114. });
  115. },
  116. methods: {
  117. isShowAgree() {
  118. this.showAgree = !this.showAgree
  119. },
  120. startReg() {
  121. var that = this
  122. uni.getSystemInfo({
  123. success: function(res) {
  124. console.log(res.model);
  125. if (res.model == 'iPhone') {
  126. that.sysphone = 2
  127. } else if (res.model != 'iPhone') {
  128. that.sysphone = 1
  129. }
  130. }
  131. });
  132. //注册
  133. if (that.isRotate) {
  134. //判断是否加载中,避免重复点击请求
  135. return false;
  136. }
  137. if (!that.phoneData) {
  138. uni.showToast({
  139. icon: 'none',
  140. position: 'bottom',
  141. title: '请输入手机号'
  142. });
  143. return false;
  144. }
  145. if (that.phoneData.length != 11) {
  146. uni.showToast({
  147. icon: 'none',
  148. position: 'bottom',
  149. title: '手机号不正确'
  150. });
  151. return false;
  152. }
  153. if (that.verCode.length != 6) {
  154. uni.showToast({
  155. icon: 'none',
  156. position: 'bottom',
  157. title: '验证码不正确'
  158. });
  159. return false;
  160. }
  161. if (that.showAgree == false) {
  162. uni.showToast({
  163. icon: 'none',
  164. position: 'bottom',
  165. title: '请阅读并同意《隐私政策》和《用户服务协议》'
  166. });
  167. return false;
  168. }
  169. that.isRotate = true;
  170. var openid = this.$queue.getData("openid") ? this.$queue.getData("openid") : '';
  171. let data = {
  172. phone: that.phoneData,
  173. msg: that.verCode,
  174. platform: that.platform,
  175. sysPhone: that.sysphone,
  176. openId: openid,
  177. userType: 1
  178. }
  179. that.$Request.post("/app/Login/registerCode", data).then(res => {
  180. console.log(res)
  181. if (res.code == 0) {
  182. console.log(res.token)
  183. this.$queue.setData("token", res.token);
  184. this.$queue.setData("userId", res.user.userId);
  185. this.$queue.setData("phone", res.user.phone);
  186. uni.setStorageSync('userName', res.user.userName)
  187. uni.setStorageSync('avatar', res.user.avatar)
  188. uni.setStorageSync('phone', res.user.phone)
  189. uni.setStorageSync('sex', res.user.sex)
  190. uni.setStorageSync('userId', res.user.userId)
  191. uni.setStorageSync('wxCode', res.user.wxCode)
  192. uni.setStorageSync('wxQrCode', res.user.wxQrCode)
  193. uni.setStorageSync('zhiFuBao', res.user.zhiFuBao)
  194. uni.setStorageSync('zhiFuBaoName', res.user.zhiFuBaoName)
  195. uni.switchTab({
  196. url: '/pages/index/index'
  197. })
  198. return false;
  199. } else {
  200. that.isRotate = false;
  201. uni.hideLoading();
  202. uni.showModal({
  203. showCancel: false,
  204. title: '登录失败',
  205. content: res.msg,
  206. });
  207. }
  208. });
  209. },
  210. sendMsg() {
  211. const {
  212. phoneData
  213. } = this;
  214. if (!phoneData) {
  215. this.$queue.showToast("请输入手机号");
  216. } else if (phoneData.length !== 11) {
  217. this.$queue.showToast("请输入正确的手机号");
  218. } else {
  219. this.$queue.showLoading("正在发送验证码...");
  220. this.$Request.getT("/app/Login/sendMsg/" + phoneData + "/login").then(res => {
  221. console.log(res)
  222. if (res.code == 0) {
  223. if (res.data === 'register') {
  224. this.show = true;
  225. }
  226. this.sending = true;
  227. this.$queue.showToast('验证码发送成功请注意查收');
  228. this.$refs.runCode.$emit('runCode');
  229. uni.hideLoading();
  230. } else {
  231. uni.hideLoading();
  232. uni.showModal({
  233. showCancel: false,
  234. title: '短信发送失败',
  235. content: res.msg ? res.msg : '请一分钟后再获取验证码',
  236. });
  237. }
  238. });
  239. }
  240. },
  241. wxGetUserInfo(e) {
  242. if (this.showAgree == false) {
  243. uni.showToast({
  244. icon: 'none',
  245. position: 'bottom',
  246. title: '请阅读并同意《隐私政策》和《用户服务协议》'
  247. });
  248. return false;
  249. }
  250. wx.getUserProfile({
  251. desc: '业务需要',
  252. success: infoRes => {
  253. console.log("infoRes.encryptedData__________:" + JSON.stringify(infoRes.userInfo))
  254. let nickName = infoRes.userInfo.nickName; //昵称
  255. let avatarUrl = infoRes.userInfo.avatarUrl; //头像
  256. let sex = infoRes.userInfo.gender; //性别
  257. try {
  258. this.$queue.showLoading('正在登录中...');
  259. this.login(nickName, avatarUrl, sex);
  260. } catch (e) {}
  261. }
  262. })
  263. },
  264. //登录
  265. login(nickName, avatarUrl, sex) {
  266. let that = this;
  267. // 1.wx获取登录用户code
  268. uni.login({
  269. provider: 'weixin',
  270. success: function(loginRes) {
  271. console.log(loginRes, '************')
  272. let data = {
  273. code: loginRes.code,
  274. }
  275. that.$Request.get('/app/Login/wxLogin', data).then(res => {
  276. if (res.code == 0) {
  277. uni.hideLoading()
  278. uni.setStorageSync('openId', res.data.open_id)
  279. uni.setStorageSync('unionId', res.data.unionId)
  280. that.sessionkey = res.data.session_key;
  281. let inviterCode = '';
  282. if (uni.getStorageSync('inviterCode')) {
  283. inviterCode = uni.getStorageSync('inviterCode')
  284. }
  285. let sendData = {
  286. openId: uni.getStorageSync('openId'),
  287. unionId: uni.getStorageSync('unionId'),
  288. userName: nickName,
  289. avatar: avatarUrl,
  290. sex: sex, //性别
  291. inviterCode: inviterCode //别人登录进来携带你的邀请码
  292. };
  293. that.sendDataList = sendData;
  294. // 第一次登录获取手机号
  295. console.log(that.phoneNum)
  296. if (res.data.isPhone == '2' && that.phoneNum) {
  297. that.weixinPhone = true;
  298. } else {
  299. that.getWeixinInfo(sendData);
  300. }
  301. } else {
  302. uni.showToast({
  303. icon: 'none',
  304. title: res.msg,
  305. duration: 2000
  306. });
  307. console.log(res, '失败')
  308. }
  309. })
  310. }
  311. });
  312. },
  313. //小程序微信登录后获取手机号
  314. getPhoneNumber: function(e) {
  315. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  316. console.log('用户拒绝提供手机号');
  317. } else {
  318. console.log('用户同意提供手机号');
  319. console.log(e)
  320. this.setPhoneByInsert(e.detail.encryptedData, e.detail.iv);
  321. }
  322. },
  323. //小程序微信登录后获取手机号
  324. setPhoneByInsert(decryptData, iv) {
  325. let data = {
  326. decryptData: decryptData,
  327. key: this.sessionkey,
  328. iv: iv
  329. };
  330. this.$Request.postJson('/app/Login/selectPhone', data).then(res => {
  331. if (res.code == 0) {
  332. this.phone = res.data.phoneNumber;
  333. this.getWeixinInfo(this.sendDataList);
  334. } else {
  335. uni.showToast({
  336. title: res.msg,
  337. icon: 'none',
  338. duration: 2000
  339. });
  340. }
  341. })
  342. },
  343. //获取个人信息
  344. getWeixinInfo(sendData) {
  345. let that = this;
  346. uni.showLoading({
  347. title: '登录中...'
  348. });
  349. let postData = {
  350. openId: sendData.openId, //小程序openId
  351. unionId: sendData.unionId, //unionId
  352. userName: sendData.userName, //微信名称
  353. avatar: sendData.avatar, //头像
  354. sex: sendData.sex, //性别
  355. phone: that.phone,
  356. inviterCode: sendData.inviterCode
  357. };
  358. that.$Request.postJson('/app/Login/insertWxUser', postData).then(res => {
  359. uni.hideLoading();
  360. if (res.code == 0) {
  361. uni.setStorageSync('token', res.token)
  362. uni.setStorageSync('adminUserId', res.user.adminUserId)
  363. uni.setStorageSync('userName', res.user.userName)
  364. uni.setStorageSync('avatar', res.user.avatar)
  365. uni.setStorageSync('phone', res.user.phone)
  366. uni.setStorageSync('invitationCode', res.user.invitationCode)
  367. uni.setStorageSync('sex', res.user.sex)
  368. uni.setStorageSync('userId', res.user.userId)
  369. uni.setStorageSync('openId', res.user.openId)
  370. uni.setStorageSync('zhiFuBao', res.user.zhiFuBao)
  371. uni.setStorageSync('zhiFuBaoName', res.user.zhiFuBaoName)
  372. // var pages = getCurrentPages();
  373. // var currPage = pages[pages.length - 1] //当前页面
  374. // var prePage = pages[pages.length - 2] //上一个页面
  375. //调用上一页拉取数据的方法
  376. // console.log(prePage.route)
  377. // if (prePage.route == "pages/my/index") {
  378. // prePage.$vm.getUserInfo()
  379. // }
  380. uni.navigateBack();
  381. } else {
  382. uni.showModal({
  383. showCancel: false,
  384. title: '登录失败',
  385. content: res.msg,
  386. });
  387. }
  388. })
  389. },
  390. weixinLo() {
  391. let that = this;
  392. uni.login({
  393. provider: 'weixin',
  394. success: function(loginRes) {
  395. that.$queue.showLoading('正在登录中...');
  396. console.error(loginRes.authResult);
  397. that.$queue.setData('weixinToken', loginRes.authResult.access_token);
  398. that.$queue.setData('unionid', loginRes.authResult.unionid);
  399. that.$queue.setData('weixinOpenid', loginRes.authResult.openid);
  400. that.$Request.postJson('/app/login/loginApp', {
  401. token: loginRes.authResult.access_token,
  402. unionid: loginRes.authResult.unionid,
  403. openid: loginRes.authResult.openid
  404. }).then(res => {
  405. console.log(JSON.stringify(res))
  406. if (res.code === 0) {
  407. if (uni.getSystemInfoSync().platform == "android") {
  408. let clientid = plus.push.getClientInfo().clientid;
  409. that.$Request.postT('/app/login/updateClientId?clientId=' +
  410. clientid + '&userId=' + res.userId).then(res => {
  411. });
  412. }
  413. that.$queue.setData("token", res.uuid);
  414. that.$queue.setData("userId", res.userId);
  415. that.getUserInfo(res.userId, res.token);
  416. } else {
  417. uni.hideLoading();
  418. uni.navigateTo({
  419. url: '/pages/public/wxmobile'
  420. });
  421. }
  422. });
  423. }
  424. });
  425. },
  426. forget() {
  427. uni.navigateTo({
  428. url: '/pages/public/pwd'
  429. });
  430. },
  431. register() {
  432. uni.navigateTo({
  433. url: '/pages/public/loginphone'
  434. });
  435. },
  436. inputChange(e) {
  437. const key = e.currentTarget.dataset.key;
  438. this[key] = e.detail.value;
  439. },
  440. navBack() {
  441. uni.navigateBack();
  442. },
  443. getUserInfo(userId, token) {
  444. this.$Request.postJson('/app/selectUserById?userId=' + userId).then(res => {
  445. if (res.code === 0) {
  446. this.$queue.setData('token', res.data.uuid);
  447. this.$queue.setData('image_url', res.data.imageUrl ? res.data.imageUrl :
  448. '/static/img/common/logo.jpg');
  449. this.$queue.setData('inviterCode', res.data.inviterCode);
  450. this.$queue.setData('invitationCode', res.data.invitationCode);
  451. this.$queue.setData('grade', res.data.grade);
  452. this.$queue.setData('mobile', res.data.mobile);
  453. this.$queue.setData('isInvitation', res.data.isInvitation);
  454. this.$queue.setData('nickName', res.data.nickName ? res.data.nickName : res.data.phone);
  455. this.$queue.setData('gender', parseInt(res.data.gender));
  456. uni.switchTab({
  457. url: '/pages/index/index'
  458. });
  459. } else {
  460. uni.showModal({
  461. showCancel: false,
  462. title: '登录失败',
  463. content: res.msg
  464. });
  465. this.$queue.logout();
  466. }
  467. uni.hideLoading();
  468. });
  469. }
  470. }
  471. };
  472. </script>
  473. <style lang="scss">
  474. page {
  475. height: 100%;
  476. background: #FFF;
  477. }
  478. .footers {
  479. padding-left: 140upx;
  480. margin-top: 32upx;
  481. font-size: 24upx;
  482. color: #666666;
  483. text-align: center;
  484. display: flex;
  485. }
  486. .confirm-btn-weixin {
  487. width: 200px;
  488. height: 42px;
  489. line-height: 42px;
  490. border-radius: 30px;
  491. margin-top: 40upx;
  492. background: -moz-linear-gradient(left, #f15b6c, #e10a07 100%);
  493. background: -webkit-gradient(linear, left top, left right, color-stop(0, #f15b6c), color-stop(100%, #e10a07));
  494. background: -webkit-linear-gradient(left, #f15b6c 0, #e10a07 100%);
  495. background: -o-linear-gradient(left, #f15b6c 0, #e10a07 100%);
  496. background: -ms-linear-gradient(left, #f15b6c 0, #e10a07 100%);
  497. background: linear-gradient(to left, #f15b6c 0, #e10a07 100%);
  498. color: #fff;
  499. font-size: 32upx;
  500. &:after {
  501. border-radius: 60px;
  502. }
  503. }
  504. .confirm-btn {
  505. width: 200px;
  506. height: 42px;
  507. line-height: 42px;
  508. border-radius: 30px;
  509. margin-top: 300upx;
  510. background: -moz-linear-gradient(left, #f15b6c, #e10a07 100%);
  511. background: -webkit-gradient(linear, left top, left right, color-stop(0, #f15b6c), color-stop(100%, #e10a07));
  512. background: -webkit-linear-gradient(left, #f15b6c 0, #e10a07 100%);
  513. background: -o-linear-gradient(left, #f15b6c 0, #e10a07 100%);
  514. background: -ms-linear-gradient(left, #f15b6c 0, #e10a07 100%);
  515. background: linear-gradient(to left, #f15b6c 0, #e10a07 100%);
  516. color: #fff;
  517. font-size: 32upx;
  518. &:after {
  519. border-radius: 60px;
  520. }
  521. }
  522. .headers {
  523. text-align: center;
  524. }
  525. .headers>image {
  526. width: 400upx;
  527. height: 400upx;
  528. }
  529. .footer {
  530. padding-left: 60upx;
  531. margin-top: 48upx;
  532. font-size: 24upx;
  533. color: #666666;
  534. text-align: center;
  535. display: flex;
  536. }
  537. page {
  538. background: #fff;
  539. }
  540. .send-msg {
  541. border-radius: 30px;
  542. color: black;
  543. background: white;
  544. height: 30px;
  545. font-size: 14px;
  546. line-height: 30px;
  547. }
  548. .container {
  549. top: 0;
  550. padding-top: 32upx;
  551. position: relative;
  552. width: 100%;
  553. height: 100%;
  554. overflow: hidden;
  555. // background: #111224;
  556. // color: #FFF;
  557. .mp_wxBox {
  558. .headers {
  559. margin: 35% auto 50rpx;
  560. text-align: center;
  561. border-radius: 60rpx;
  562. width: 650rpx;
  563. height: 300rpx;
  564. line-height: 450rpx;
  565. image {
  566. width: 300rpx;
  567. height: 300rpx;
  568. }
  569. }
  570. .content {
  571. text-align: center;
  572. }
  573. text {
  574. display: block;
  575. color: #9d9d9d;
  576. margin-top: 40rpx;
  577. }
  578. .bottom {
  579. line-height: 80upx;
  580. border-radius: 80upx;
  581. margin: 70rpx 50rpx;
  582. height: 80upx;
  583. font-size: 35rpx;
  584. }
  585. }
  586. }
  587. .wrapper {
  588. position: relative;
  589. z-index: 90;
  590. background: #fff;
  591. padding-bottom: 20px;
  592. }
  593. .input-content {
  594. padding: 0 20px;
  595. }
  596. .confirm-btn {
  597. width: 300px;
  598. height: 42px;
  599. line-height: 42px;
  600. border-radius: 30px;
  601. margin-top: 40px;
  602. background: linear-gradient(to left, #3f5ecb 0, #5074FF 100%);
  603. color: #fff;
  604. // font-size: $font-lg;
  605. &:after {
  606. border-radius: 60px;
  607. }
  608. }
  609. </style>