login.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. <template>
  2. <!-- <view class="container">
  3. <view class="login-box">
  4. <text>您还没有登录</text>
  5. <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" class="wx-login-btn">
  6. 抖音用户手机号授权登录</button>
  7. </view>
  8. </view> -->
  9. <view class="background">
  10. <view class="content-wrap">
  11. <view class="content">
  12. <image src="/static/images/new/logo.png" mode="aspectFit"></image>
  13. <view class="no-login">10秒快速预约 鲸致服务到家</view>
  14. <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="dy-login-btn">授权手机号一键登录</button>
  15. <!-- <view style="text-align: center;margin-top: 5px;color:#FE2B54;font-size: 12px;">解锁更多精彩,登录开始探索</view> -->
  16. </view>
  17. <!-- <image src="https://mall.zhaijieshi.cc/file/wx-dy/icon-close.png" mode="aspectFit" class="close-btn" bindtap="closeLogin"></image> -->
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. var api = require('../../../config/api.js');
  23. var util = require('../../../utils/util.js');
  24. var user = require('../../../utils/user.js');
  25. var app = getApp();
  26. export default {
  27. data() {
  28. return {
  29. username: '',
  30. mobile: '',
  31. code: '',
  32. inputTel: '',
  33. navUrl: ''
  34. };
  35. },
  36. onLoad: function (options) {
  37. // 页面初始化 options为页面跳转所带来的参数
  38. // 页面渲染完成
  39. if (options.reg != null) {
  40. uni.showModal({
  41. title: '提示',
  42. content: '授权过期,请重新授权',
  43. showCancel: false
  44. });
  45. }
  46. if (options.navUrl) {
  47. console.info(unescape(options.navUrl));
  48. this.setData({
  49. navUrl: unescape(options.navUrl)
  50. });
  51. }
  52. // wx.login({
  53. // force: true,
  54. // success(res) {
  55. // console.log(`login 调用成功${res.code} ${res.anonymousCode}`);
  56. // console.log(res);
  57. // },
  58. // fail(res) {
  59. // console.log(`login 调用失败`);
  60. // },
  61. // });
  62. },
  63. onReady: function () {},
  64. onShow: function () {
  65. // 页面显示
  66. let that = this;
  67. user.login().then((res) => {
  68. console.info(res);
  69. that.setData({
  70. code: res.code
  71. });
  72. });
  73. },
  74. onHide: function () {
  75. // 页面隐藏
  76. },
  77. onUnload: function () {
  78. // 页面关闭
  79. },
  80. methods: {
  81. getdefalutUserAddress() {
  82. //登陆拿用户默认地址,如果没有就拿系统默认地址
  83. //如果有默认地址,但是不在门店服务区域里
  84. util.request(api.OrderBookDefaultAddress, {}, 'get').then((res) => {
  85. if (res.errno === 0) {
  86. let address = res.data;
  87. var city = address.city.indexOf('市辖') != -1 ? address.province : address.city;
  88. //通过jz_shop的shop_no关联服务力分配的门店id,拿实际的jz_shop的门店id
  89. var addresInfo = {
  90. city: city,
  91. lng: address.lng,
  92. lat: address.lat,
  93. shopId: address.shopId,
  94. address: address.addressDetail,
  95. shopName: address.shopName
  96. };
  97. // that.setData({
  98. // addressInfo: res.data
  99. // })
  100. uni.setStorageSync('addressInfo', addresInfo);
  101. // that.getIndexData(); //加载商品
  102. } else {
  103. //无默认地址时 拿系统的
  104. this.getdefaultCityShop();
  105. }
  106. });
  107. },
  108. getdefaultCityShop() {
  109. let that = this;
  110. util.request(api.getDefaultCityShop, {}, 'get').then((res) => {
  111. if (res.errno === 0) {
  112. // that.setData({
  113. // addressInfo: res.data
  114. // })
  115. uni.setStorageSync('addressInfo', res.data);
  116. // that.getIndexData();
  117. }
  118. // else {
  119. // util.showErrorToast(res.errmsg);
  120. // }
  121. });
  122. },
  123. bindInputTel(e) {
  124. console.log(e.detail.value);
  125. this.setData({
  126. inputTel: e.detail.value
  127. });
  128. },
  129. goLogin() {
  130. if (this.inputTel == null || this.inputTel == '') {
  131. uni.showToast({
  132. title: '请输入账号登录',
  133. icon: 'error',
  134. duration: 1000
  135. });
  136. return;
  137. }
  138. let that = this;
  139. util.request(api.AuthRegister, {
  140. tel: this.inputTel
  141. }).then(function (res) {
  142. if (res.errno === 0) {
  143. //存储用户信息
  144. app.globalData.hasLogin = true;
  145. app.globalData.userInfo = res.data.userInfo;
  146. uni.setStorageSync('userInfo', res.data.userInfo);
  147. uni.setStorageSync('token', res.data.token);
  148. uni.setStorageSync('openId', res.data.openId);
  149. that.getdefalutUserAddress();
  150. uni.showToast({
  151. title: '登录成功',
  152. icon: 'success',
  153. duration: 2000,
  154. complete: function () {
  155. setTimeout(function () {
  156. uni.navigateBack({
  157. delta: 1,
  158. fail(res) {
  159. console.info(res);
  160. uni.switchTab({
  161. url: 'pages/index/index'
  162. });
  163. }
  164. });
  165. }, 2000);
  166. }
  167. });
  168. }
  169. });
  170. },
  171. getPhoneNumber: function (e) {
  172. console.log(e);
  173. // #ifdef MP-TOUTIAO||MP-WEIXIN
  174. this.authRegister(e);
  175. // #endif
  176. // #ifdef MP-ALIPAY
  177. this.authLogin(e);
  178. //#endif
  179. },
  180. authRegister(e){
  181. var ivObj = e.detail.iv;
  182. var telObj = e.detail.encryptedData;
  183. if (ivObj == undefined || telObj == undefined) {
  184. uni.showToast({
  185. title: '获取授权失败',
  186. icon: 'error',
  187. duration: 2000
  188. });
  189. return;
  190. }
  191. var that = this;
  192. let r_userid = uni.getStorageSync('r_userid');
  193. if (r_userid == null || r_userid == '') {
  194. r_userid = 0;
  195. }
  196. console.log('请求授权登录:code:' + that.code + 'encryptedData,' + telObj + 'iv,' + ivObj + 'r_userid,' + r_userid);
  197. //------执行Login---------
  198. util.request(api.AuthRegister, {
  199. code: that.code,
  200. encryptedData: telObj,
  201. iv: ivObj,
  202. r_userid: r_userid
  203. }).then(function (res) {
  204. if (res.errno === 0) {
  205. //存储用户信息
  206. app.globalData.hasLogin = true;
  207. app.globalData.userInfo = res.data.userInfo;
  208. uni.setStorageSync('userInfo', res.data.userInfo);
  209. uni.setStorageSync('token', res.data.token);
  210. uni.setStorageSync('openId', res.data.openId);
  211. that.getdefalutUserAddress();
  212. uni.showToast({
  213. title: '授权注册成功',
  214. icon: 'success',
  215. duration: 2000,
  216. complete: function () {
  217. setTimeout(function () {
  218. if (that.navUrl) {
  219. uni.navigateTo({
  220. url: that.navUrl
  221. });
  222. } else {
  223. uni.navigateBack({
  224. delta: 1,
  225. fail(res) {
  226. console.info(res);
  227. uni.switchTab({
  228. url: '/pages/index/index'
  229. });
  230. }
  231. });
  232. }
  233. }, 1000);
  234. }
  235. });
  236. } else {
  237. console.info(res);
  238. user.login().then((res) => {
  239. console.info(res);
  240. that.setData({
  241. code: res.code
  242. });
  243. });
  244. uni.showToast({
  245. title: '授权失败请重试',
  246. icon: 'error',
  247. duration: 3000
  248. });
  249. }
  250. });
  251. //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
  252. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  253. //用户点击拒绝
  254. uni.showModal({
  255. title: '警告',
  256. content: '您点击了拒绝授权,部分功能无法使用!!!',
  257. showCancel: false,
  258. confirmText: '确定',
  259. success: function (res) {
  260. // 用户没有授权成功,不需要改变 isHide 的值
  261. if (res.confirm) {
  262. uni.switchTab({
  263. url: '/pages/index/index'
  264. });
  265. }
  266. }
  267. });
  268. }
  269. },
  270. authLogin(e){
  271. var telObj = e.detail.encryptedData;
  272. if (telObj == undefined) {
  273. uni.showToast({
  274. title: '获取授权失败',
  275. icon: 'error',
  276. duration: 2000
  277. });
  278. return;
  279. }
  280. var that = this;
  281. let r_userid = uni.getStorageSync('r_userid');
  282. if (r_userid == null || r_userid == '') {
  283. r_userid = 0;
  284. }
  285. console.log('请求授权登录:code:' + that.code + 'encryptedData,' + telObj + 'r_userid,' + r_userid);
  286. //------执行Login---------
  287. util.request(api.AuthLogin, {
  288. code: that.code,
  289. encryptData: telObj,
  290. r_userid: r_userid
  291. }).then(function (res) {
  292. if (res.errno === 0) {
  293. //存储用户信息
  294. app.globalData.hasLogin = true;
  295. app.globalData.userInfo = res.data.userInfo;
  296. uni.setStorageSync('userInfo', res.data.userInfo);
  297. uni.setStorageSync('token', res.data.token);
  298. uni.setStorageSync('openId', res.data.openId);
  299. that.getdefalutUserAddress();
  300. uni.showToast({
  301. title: '授权注册成功',
  302. icon: 'success',
  303. duration: 2000,
  304. complete: function () {
  305. setTimeout(function () {
  306. if (that.navUrl) {
  307. uni.navigateTo({
  308. url: that.navUrl
  309. });
  310. } else {
  311. uni.navigateBack({
  312. delta: 1,
  313. fail(res) {
  314. console.info(res);
  315. uni.switchTab({
  316. url: '/pages/index/index'
  317. });
  318. }
  319. });
  320. }
  321. }, 1000);
  322. }
  323. });
  324. } else {
  325. console.info(res);
  326. user.login().then((res) => {
  327. console.info(res);
  328. that.setData({
  329. code: res.code
  330. });
  331. });
  332. uni.showToast({
  333. title: '授权失败请重试',
  334. icon: 'error',
  335. duration: 3000
  336. });
  337. }
  338. });
  339. //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
  340. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  341. //用户点击拒绝
  342. uni.showModal({
  343. title: '警告',
  344. content: '您点击了拒绝授权,部分功能无法使用!!!',
  345. showCancel: false,
  346. confirmText: '确定',
  347. success: function (res) {
  348. // 用户没有授权成功,不需要改变 isHide 的值
  349. if (res.confirm) {
  350. uni.switchTab({
  351. url: '/pages/index/index'
  352. });
  353. }
  354. }
  355. });
  356. }
  357. },
  358. closeLogin() {
  359. uni.navigateBack({
  360. delta: 1
  361. });
  362. },
  363. wxLogin: function (e) {
  364. //console.log(e);
  365. if (e.detail.userInfo == null) {
  366. app.globalData.hasLogin = false;
  367. return;
  368. }
  369. app.globalData.hasLogin = false;
  370. uni.setStorageSync('userInfo', null);
  371. user.checkLogin().catch(() => {
  372. user.loginByWeixin(e.detail.userInfo)
  373. .then((res) => {
  374. console.log(res);
  375. app.globalData.hasLogin = true;
  376. uni.navigateBack({
  377. delta: 1
  378. });
  379. })
  380. .catch((err) => {
  381. app.globalData.hasLogin = false;
  382. console.log(err);
  383. uni.switchTab({
  384. url: '/pages/index/index'
  385. });
  386. });
  387. });
  388. }
  389. }
  390. };
  391. </script>
  392. <style>
  393. page {
  394. height: 100%;
  395. width: 100%;
  396. background-color: #f7f7f7;
  397. }
  398. .login-box {
  399. width: 100%;
  400. height: auto;
  401. overflow: hidden;
  402. padding: 0 40rpx;
  403. margin-top: 200rpx;
  404. align-items: center;
  405. text-align: center;
  406. }
  407. .login-box text {
  408. text-align: center;
  409. }
  410. .img {
  411. width: 50rpx;
  412. height: 50rpx;
  413. }
  414. .wx-login-btn {
  415. margin: 60rpx 0 40rpx 0;
  416. height: 96rpx;
  417. line-height: 96rpx;
  418. font-size: 30rpx;
  419. border-radius: 6rpx;
  420. width: 90%;
  421. color: #fff;
  422. right: 0;
  423. display: flex;
  424. justify-content: center;
  425. align-items: center;
  426. position: flex;
  427. bottom: 0;
  428. left: 0;
  429. padding: 0;
  430. margin-left: 5%;
  431. text-align: center;
  432. /* padding-left: -5rpx; */
  433. border-radius: 6rpx;
  434. letter-spacing: 3rpx;
  435. /*background-color: #242a48;*/
  436. background-color: #242a48;
  437. }
  438. .account-login-btn {
  439. width: 90%;
  440. margin: 0 auto;
  441. color: #fff;
  442. font-size: 30rpx;
  443. height: 96rpx;
  444. line-height: 96rpx;
  445. right: 0;
  446. display: flex;
  447. justify-content: center;
  448. align-items: center;
  449. position: flex;
  450. bottom: 0;
  451. left: 0;
  452. border-radius: 0;
  453. padding: 0;
  454. margin-left: 5%;
  455. text-align: center;
  456. /* padding-left: -5rpx; */
  457. border-top-left-radius: 50rpx;
  458. border-bottom-left-radius: 50rpx;
  459. border-top-right-radius: 50rpx;
  460. border-bottom-right-radius: 50rpx;
  461. letter-spacing: 3rpx;
  462. background-image: linear-gradient(to right, #9a9ba1 0%, #9a9ba1 100%);
  463. }
  464. .wechat {
  465. width: 80rpx;
  466. height: 80rpx;
  467. margin-right: 15rpx;
  468. }
  469. .form-text {
  470. margin-top: 100rpx;
  471. text-align: center;
  472. }
  473. .form-item {
  474. position: relative;
  475. height: 96rpx;
  476. border-bottom: 1px solid #d9d9d9;
  477. }
  478. .form-item .username,
  479. .form-item .password,
  480. .form-item .mobile,
  481. .form-item .code {
  482. position: absolute;
  483. top: 26rpx;
  484. left: 0;
  485. display: block;
  486. width: 100%;
  487. height: 44rpx;
  488. color: #333;
  489. font-size: 30rpx;
  490. }
  491. .form-item-code {
  492. margin-top: 32rpx;
  493. height: auto;
  494. overflow: hidden;
  495. width: 100%;
  496. float: left;
  497. width: 350rpx;
  498. }
  499. .form-item-code .form-item {
  500. float: left;
  501. width: 350rpx;
  502. }
  503. .form-item-getPhone {
  504. margin-top: 18rpx;
  505. height: auto;
  506. float: right;
  507. /*border: 1px solid #242a48;*/
  508. border-radius: 6rpx;
  509. color: #242a48;
  510. /*background: green;*/
  511. font-size: 30rpx;
  512. }
  513. .background {
  514. width: 750rpx;
  515. height: 100vh;
  516. background: #ffffff;
  517. display: flex;
  518. /* align-items: center; */
  519. justify-content: center;
  520. text-align: center;
  521. }
  522. .content {
  523. /* width: 100%; */
  524. height: 480rpx;
  525. /* background: #FFFFFF;
  526. border-radius: 20rpx;
  527. position: relative; */
  528. padding-top: 250rpx;
  529. }
  530. .content image {
  531. width: 212rpx;
  532. height: 212rpx;
  533. /* margin-bottom: 10px; */
  534. }
  535. .no-login {
  536. height: 40rpx;
  537. line-height: 40rpx;
  538. font-size: 34rpx;
  539. letter-spacing: 3rpx;
  540. /* font-weight: bold; */
  541. color: #333333;
  542. text-align: center;
  543. margin: 20px 0 50px 0;
  544. }
  545. .close-btn {
  546. width: 70rpx;
  547. height: 70rpx;
  548. margin: 30rpx 254rpx 0;
  549. }
  550. button.dy-login-btn {
  551. width: 680rpx;
  552. height: 102rpx;
  553. line-height: 102rpx;
  554. background: #00b9ff;
  555. border-radius: 102rpx;
  556. color: #ffffff;
  557. font-size: 34rpx;
  558. border: none;
  559. outline: none;
  560. position: static;
  561. margin: 0 auto;
  562. letter-spacing: 3rpx;
  563. }
  564. .dy-login-btn::after {
  565. border: none;
  566. }
  567. </style>