login.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. var ivObj = e.detail.iv;
  174. var telObj = e.detail.encryptedData;
  175. if (ivObj == undefined || telObj == undefined) {
  176. uni.showToast({
  177. title: '获取授权失败',
  178. icon: 'error',
  179. duration: 2000
  180. });
  181. return;
  182. }
  183. var that = this;
  184. let r_userid = uni.getStorageSync('r_userid');
  185. if (r_userid == null || r_userid == '') {
  186. r_userid = 0;
  187. }
  188. console.log('请求授权登录:code:' + that.code + 'encryptedData,' + telObj + 'iv,' + ivObj + 'r_userid,' + r_userid);
  189. //------执行Login---------
  190. util.request(api.AuthRegister, {
  191. code: that.code,
  192. encryptedData: telObj,
  193. iv: ivObj,
  194. r_userid: r_userid
  195. }).then(function (res) {
  196. if (res.errno === 0) {
  197. //存储用户信息
  198. app.globalData.hasLogin = true;
  199. app.globalData.userInfo = res.data.userInfo;
  200. uni.setStorageSync('userInfo', res.data.userInfo);
  201. uni.setStorageSync('token', res.data.token);
  202. uni.setStorageSync('openId', res.data.openId);
  203. that.getdefalutUserAddress();
  204. uni.showToast({
  205. title: '授权注册成功',
  206. icon: 'success',
  207. duration: 2000,
  208. complete: function () {
  209. setTimeout(function () {
  210. if (that.navUrl) {
  211. uni.navigateTo({
  212. url: that.navUrl
  213. });
  214. } else {
  215. uni.navigateBack({
  216. delta: 1,
  217. fail(res) {
  218. console.info(res);
  219. uni.switchTab({
  220. url: '/pages/index/index'
  221. });
  222. }
  223. });
  224. }
  225. }, 1000);
  226. }
  227. });
  228. } else {
  229. console.info(res);
  230. user.login().then((res) => {
  231. console.info(res);
  232. that.setData({
  233. code: res.code
  234. });
  235. });
  236. uni.showToast({
  237. title: '授权失败请重试',
  238. icon: 'error',
  239. duration: 3000
  240. });
  241. }
  242. });
  243. //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
  244. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  245. //用户点击拒绝
  246. uni.showModal({
  247. title: '警告',
  248. content: '您点击了拒绝授权,部分功能无法使用!!!',
  249. showCancel: false,
  250. confirmText: '确定',
  251. success: function (res) {
  252. // 用户没有授权成功,不需要改变 isHide 的值
  253. if (res.confirm) {
  254. uni.switchTab({
  255. url: '/pages/index/index'
  256. });
  257. }
  258. }
  259. });
  260. }
  261. },
  262. closeLogin() {
  263. uni.navigateBack({
  264. delta: 1
  265. });
  266. },
  267. wxLogin: function (e) {
  268. //console.log(e);
  269. if (e.detail.userInfo == null) {
  270. app.globalData.hasLogin = false;
  271. return;
  272. }
  273. app.globalData.hasLogin = false;
  274. uni.setStorageSync('userInfo', null);
  275. user.checkLogin().catch(() => {
  276. user.loginByWeixin(e.detail.userInfo)
  277. .then((res) => {
  278. console.log(res);
  279. app.globalData.hasLogin = true;
  280. uni.navigateBack({
  281. delta: 1
  282. });
  283. })
  284. .catch((err) => {
  285. app.globalData.hasLogin = false;
  286. console.log(err);
  287. uni.switchTab({
  288. url: '/pages/index/index'
  289. });
  290. });
  291. });
  292. }
  293. }
  294. };
  295. </script>
  296. <style>
  297. page {
  298. height: 100%;
  299. width: 100%;
  300. background-color: #f7f7f7;
  301. }
  302. .login-box {
  303. width: 100%;
  304. height: auto;
  305. overflow: hidden;
  306. padding: 0 40rpx;
  307. margin-top: 200rpx;
  308. align-items: center;
  309. text-align: center;
  310. }
  311. .login-box text {
  312. text-align: center;
  313. }
  314. .img {
  315. width: 50rpx;
  316. height: 50rpx;
  317. }
  318. .wx-login-btn {
  319. margin: 60rpx 0 40rpx 0;
  320. height: 96rpx;
  321. line-height: 96rpx;
  322. font-size: 30rpx;
  323. border-radius: 6rpx;
  324. width: 90%;
  325. color: #fff;
  326. right: 0;
  327. display: flex;
  328. justify-content: center;
  329. align-items: center;
  330. position: flex;
  331. bottom: 0;
  332. left: 0;
  333. padding: 0;
  334. margin-left: 5%;
  335. text-align: center;
  336. /* padding-left: -5rpx; */
  337. border-radius: 6rpx;
  338. letter-spacing: 3rpx;
  339. /*background-color: #242a48;*/
  340. background-color: #242a48;
  341. }
  342. .account-login-btn {
  343. width: 90%;
  344. margin: 0 auto;
  345. color: #fff;
  346. font-size: 30rpx;
  347. height: 96rpx;
  348. line-height: 96rpx;
  349. right: 0;
  350. display: flex;
  351. justify-content: center;
  352. align-items: center;
  353. position: flex;
  354. bottom: 0;
  355. left: 0;
  356. border-radius: 0;
  357. padding: 0;
  358. margin-left: 5%;
  359. text-align: center;
  360. /* padding-left: -5rpx; */
  361. border-top-left-radius: 50rpx;
  362. border-bottom-left-radius: 50rpx;
  363. border-top-right-radius: 50rpx;
  364. border-bottom-right-radius: 50rpx;
  365. letter-spacing: 3rpx;
  366. background-image: linear-gradient(to right, #9a9ba1 0%, #9a9ba1 100%);
  367. }
  368. .wechat {
  369. width: 80rpx;
  370. height: 80rpx;
  371. margin-right: 15rpx;
  372. }
  373. .form-text {
  374. margin-top: 100rpx;
  375. text-align: center;
  376. }
  377. .form-item {
  378. position: relative;
  379. height: 96rpx;
  380. border-bottom: 1px solid #d9d9d9;
  381. }
  382. .form-item .username,
  383. .form-item .password,
  384. .form-item .mobile,
  385. .form-item .code {
  386. position: absolute;
  387. top: 26rpx;
  388. left: 0;
  389. display: block;
  390. width: 100%;
  391. height: 44rpx;
  392. color: #333;
  393. font-size: 30rpx;
  394. }
  395. .form-item-code {
  396. margin-top: 32rpx;
  397. height: auto;
  398. overflow: hidden;
  399. width: 100%;
  400. float: left;
  401. width: 350rpx;
  402. }
  403. .form-item-code .form-item {
  404. float: left;
  405. width: 350rpx;
  406. }
  407. .form-item-getPhone {
  408. margin-top: 18rpx;
  409. height: auto;
  410. float: right;
  411. /*border: 1px solid #242a48;*/
  412. border-radius: 6rpx;
  413. color: #242a48;
  414. /*background: green;*/
  415. font-size: 30rpx;
  416. }
  417. .background {
  418. width: 750rpx;
  419. height: 100vh;
  420. background: #ffffff;
  421. display: flex;
  422. /* align-items: center; */
  423. justify-content: center;
  424. text-align: center;
  425. }
  426. .content {
  427. /* width: 100%; */
  428. height: 480rpx;
  429. /* background: #FFFFFF;
  430. border-radius: 20rpx;
  431. position: relative; */
  432. padding-top: 250rpx;
  433. }
  434. .content image {
  435. width: 212rpx;
  436. height: 212rpx;
  437. /* margin-bottom: 10px; */
  438. }
  439. .no-login {
  440. height: 40rpx;
  441. line-height: 40rpx;
  442. font-size: 34rpx;
  443. letter-spacing: 3rpx;
  444. /* font-weight: bold; */
  445. color: #333333;
  446. text-align: center;
  447. margin: 20px 0 50px 0;
  448. }
  449. .close-btn {
  450. width: 70rpx;
  451. height: 70rpx;
  452. margin: 30rpx 254rpx 0;
  453. }
  454. button.dy-login-btn {
  455. width: 680rpx;
  456. height: 102rpx;
  457. line-height: 102rpx;
  458. background: #00b9ff;
  459. border-radius: 102rpx;
  460. color: #ffffff;
  461. font-size: 34rpx;
  462. border: none;
  463. outline: none;
  464. position: static;
  465. margin: 0 auto;
  466. letter-spacing: 3rpx;
  467. }
  468. .dy-login-btn::after {
  469. border: none;
  470. }
  471. </style>