login.vue 15 KB

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