login.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <template>
  2. <view class="container">
  3. <view class="login-box">
  4. <text style="color: #838383; font-size: 25rpx;">您还没有登录</text>
  5. <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="wx-login-btn" v-if="checked">
  6. 点击授权注册登录
  7. </button>
  8. <button class="wx-login-btn" v-if="!checked" @tap="showNotice=true">
  9. 点击授权注册登录
  10. </button>
  11. <view class="" @click="changeState">
  12. <image src="https://mall.zhaijieshi.cc/file/wx-huawang/dian1.png" mode="" v-if="!checked" class="icon-img"></image>
  13. <image src="https://mall.zhaijieshi.cc/file/wx-huawang/dian2.png" mode="" v-else class="icon-img"></image>
  14. <text>我已阅读并同意</text>
  15. <text @tap="showProtocal" style="margin: 10rpx;color:#d77958 ;">《隐私协议》</text>
  16. </view>
  17. </view>
  18. <view class="notice" v-if="showNotice">
  19. 请阅读并勾选上方协议
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. var api = require('../../../config/api.js');
  25. var util = require('../../../utils/util.js');
  26. var user = require('../../../utils/user.js');
  27. var app = getApp();
  28. export default {
  29. data() {
  30. return {
  31. username: '',
  32. mobile: '',
  33. code: '',
  34. type: '',
  35. checked: false,
  36. showNotice:false
  37. };
  38. },
  39. onLoad: function(options) {
  40. // 页面初始化 options为页面跳转所带来的参数
  41. // 页面渲染完成
  42. if (options.reg != null) {
  43. uni.showModal({
  44. title: '提示',
  45. content: '授权过期,请重新授权',
  46. showCancel: false
  47. });
  48. }
  49. if (options.type && options.type != null && options.type != '') {
  50. this.setData({
  51. type: options.type
  52. });
  53. }
  54. },
  55. onReady: function() {},
  56. onShow: async function() {
  57. // 页面显示
  58. let that = this;
  59. await that.wxloginCode();
  60. if (this.type == 1) {
  61. //切换账号不自动登录
  62. return;
  63. }
  64. // uni.showLoading({
  65. // title: '授权中'
  66. // });
  67. let r_userid = uni.getStorageSync('r_userid');
  68. if (r_userid == null || r_userid == '') {
  69. r_userid = 0;
  70. }
  71. util.request(api.AuthRegister, {
  72. code: that.code,
  73. r_userid: r_userid
  74. })
  75. .then(function(res) {
  76. if (res.errno === 0) {
  77. //存储用户信息
  78. app.globalData.hasLogin = true;
  79. app.globalData.userInfo = res.data.userInfo;
  80. uni.setStorageSync('userInfo', res.data.userInfo);
  81. uni.setStorageSync('token', res.data.token);
  82. that.getdefalutUserAddress();
  83. uni.showToast({
  84. title: '授权成功',
  85. icon: 'none'
  86. });
  87. uni.navigateBack({
  88. delta: 1
  89. });
  90. }
  91. uni.hideLoading();
  92. that.wxloginCode();
  93. })
  94. .catch(function(err) {
  95. uni.hideLoading();
  96. that.wxloginCode();
  97. });
  98. },
  99. onHide: function() {
  100. // 页面隐藏
  101. },
  102. onUnload: function() {
  103. // 页面关闭
  104. },
  105. methods: {
  106. changeState(){
  107. this.setData({
  108. checked:!this.checked
  109. });
  110. if(this.checked==true){
  111. this.showNotice=false
  112. }
  113. },
  114. //点击查看隐私协议
  115. showProtocal() {
  116. uni.navigateTo({
  117. url: "/pages/auth/login/protocol/protocol"
  118. })
  119. },
  120. async wxloginCode() {
  121. let that = this;
  122. await user.login().then((res) => {
  123. console.info(res);
  124. that.setData({
  125. code: res.code
  126. });
  127. });
  128. },
  129. getdefalutUserAddress() {
  130. //登陆拿用户默认地址,如果没有就拿系统默认地址
  131. //如果有默认地址,但是不在门店服务区域里
  132. util.request(api.OrderBookDefaultAddress, {}, 'get').then((res) => {
  133. if (res.errno === 0) {
  134. let address = res.data;
  135. if(address.length>0){
  136. var city = address.city.indexOf('市辖') != -1 ? address.province : address.city;
  137. //通过jz_shop的shop_no关联服务力分配的门店id,拿实际的jz_shop的门店id
  138. var addresInfo = {
  139. city: city,
  140. lng: address.lng,
  141. lat: address.lat,
  142. shopId: address.shopId,
  143. address: address.addressDetail,
  144. shopName: address.shopName
  145. };
  146. uni.setStorageSync('addressInfo', addresInfo);
  147. }else{
  148. return
  149. }
  150. } else {
  151. //无默认地址时 拿系统的
  152. this.getdefaultCityShop();
  153. }
  154. });
  155. },
  156. getdefaultCityShop() {
  157. let that = this;
  158. util.request(api.getDefaultCityShop, {}, 'get').then((res) => {
  159. if (res.errno === 0) {
  160. uni.setStorageSync('addressInfo', res.data);
  161. }
  162. });
  163. },
  164. getPhoneNumber: function(e) {
  165. if(this.checked){
  166. this.setData({
  167. showNotice:false
  168. })
  169. var ivObj = e.detail.iv;
  170. var telObj = e.detail.encryptedData;
  171. var that = this;
  172. let r_userid = uni.getStorageSync('r_userid');
  173. if (r_userid == null || r_userid == '') {
  174. r_userid = 0;
  175. }
  176. console.log('请求授权登录:code:' + that.code + 'encryptedData,' + telObj + 'iv,' + ivObj + 'r_userid,' +
  177. r_userid);
  178. //------执行Login---------
  179. util.request(api.AuthRegister, {
  180. code: that.code,
  181. encryptedData: telObj,
  182. iv: ivObj,
  183. r_userid: r_userid
  184. }).then(function(res) {
  185. if (res.errno === 0) {
  186. //存储用户信息
  187. app.globalData.hasLogin = true;
  188. app.globalData.userInfo = res.data.userInfo;
  189. uni.setStorageSync('userInfo', res.data.userInfo);
  190. uni.setStorageSync('token', res.data.token);
  191. that.getdefalutUserAddress();
  192. uni.showToast({
  193. title: '授权注册成功',
  194. icon: 'success',
  195. duration: 2000,
  196. complete: function() {
  197. setTimeout(function() {
  198. uni.navigateBack({
  199. delta: 1
  200. });
  201. }, 2000);
  202. }
  203. });
  204. } else {
  205. console.info(res);
  206. uni.showToast({
  207. title: '授权失败请重试',
  208. icon: 'error',
  209. duration: 3000
  210. });
  211. }
  212. });
  213. //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
  214. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  215. //用户点击拒绝
  216. uni.showModal({
  217. title: '警告',
  218. content: '您点击了拒绝授权,部分功能无法使用!!!',
  219. showCancel: false,
  220. confirmText: '确定',
  221. success: function(res) {
  222. // 用户没有授权成功,不需要改变 isHide 的值
  223. if (res.confirm) {
  224. uni.switchTab({
  225. url: '/pages/index/index'
  226. });
  227. }
  228. }
  229. });
  230. }
  231. }else{
  232. this.setData({
  233. showNotice:true
  234. })
  235. }
  236. },
  237. wxLogin: function(e) {
  238. //console.log(e);
  239. if (e.detail.userInfo == null) {
  240. app.globalData.hasLogin = false;
  241. return;
  242. }
  243. app.globalData.hasLogin = false;
  244. uni.setStorageSync('userInfo', null);
  245. user.checkLogin().catch(() => {
  246. user.loginByWeixin(e.detail.userInfo)
  247. .then((res) => {
  248. console.log(res);
  249. app.globalData.hasLogin = true;
  250. uni.navigateBack({
  251. delta: 1
  252. });
  253. })
  254. .catch((err) => {
  255. app.globalData.hasLogin = false;
  256. console.log(err);
  257. uni.switchTab({
  258. url: '/pages/index/index'
  259. });
  260. });
  261. });
  262. }
  263. }
  264. };
  265. </script>
  266. <style>
  267. page {
  268. height: 100%;
  269. width: 100%;
  270. background-color: #f7f7f7;
  271. }
  272. .login-box {
  273. width: 100%;
  274. height: auto;
  275. overflow: hidden;
  276. padding: 0 40rpx;
  277. margin-top: 200rpx;
  278. align-items: center;
  279. text-align: center;
  280. }
  281. .login-box text {
  282. text-align: center;
  283. }
  284. .img {
  285. width: 50rpx;
  286. height: 50rpx;
  287. }
  288. .wx-login-btn {
  289. margin: 60rpx 0 40rpx 0;
  290. height: 96rpx;
  291. line-height: 96rpx;
  292. font-size: 30rpx;
  293. border-radius: 6rpx;
  294. width: 90%;
  295. color: #fff;
  296. right: 0;
  297. display: flex;
  298. justify-content: center;
  299. align-items: center;
  300. position: flex;
  301. bottom: 0;
  302. left: 0;
  303. padding: 0;
  304. margin-left: 5%;
  305. text-align: center;
  306. /* padding-left: -5rpx; */
  307. border-radius: 10rpx;
  308. letter-spacing: 3rpx;
  309. /*background-color: #242a48;*/
  310. background-color: #d77958;
  311. }
  312. .account-login-btn {
  313. width: 90%;
  314. margin: 0 auto;
  315. color: #fff;
  316. font-size: 30rpx;
  317. height: 96rpx;
  318. line-height: 96rpx;
  319. right: 0;
  320. display: flex;
  321. justify-content: center;
  322. align-items: center;
  323. position: flex;
  324. bottom: 0;
  325. left: 0;
  326. border-radius: 0;
  327. padding: 0;
  328. margin-left: 5%;
  329. text-align: center;
  330. border-top-left-radius: 50rpx;
  331. border-bottom-left-radius: 50rpx;
  332. border-top-right-radius: 50rpx;
  333. border-bottom-right-radius: 50rpx;
  334. letter-spacing: 3rpx;
  335. background-image: linear-gradient(to right, #9a9ba1 0%, #9a9ba1 100%);
  336. }
  337. .wechat {
  338. width: 80rpx;
  339. height: 80rpx;
  340. margin-right: 15rpx;
  341. }
  342. .form-text {
  343. margin-top: 100rpx;
  344. text-align: center;
  345. }
  346. .form-item {
  347. position: relative;
  348. height: 96rpx;
  349. border-bottom: 1px solid #d9d9d9;
  350. }
  351. .form-item .username,
  352. .form-item .password,
  353. .form-item .mobile,
  354. .form-item .code {
  355. position: absolute;
  356. top: 26rpx;
  357. left: 0;
  358. display: block;
  359. width: 100%;
  360. height: 44rpx;
  361. color: #333;
  362. font-size: 30rpx;
  363. }
  364. .form-item-code {
  365. margin-top: 32rpx;
  366. height: auto;
  367. overflow: hidden;
  368. width: 100%;
  369. float: left;
  370. width: 350rpx;
  371. }
  372. .form-item-code .form-item {
  373. float: left;
  374. width: 350rpx;
  375. }
  376. .form-item-getPhone {
  377. margin-top: 18rpx;
  378. height: auto;
  379. float: right;
  380. /*border: 1px solid #242a48;*/
  381. border-radius: 6rpx;
  382. color: #242a48;
  383. /*background: green;*/
  384. font-size: 30rpx;
  385. }
  386. .van-checkbox__label{
  387. margin-left: 18rpx!important;
  388. margin-top: 60rpx!important;
  389. }
  390. .van-checkbox__icon-wrap{
  391. line-height: 20rpx!important;
  392. margin-top: 72rpx!important;
  393. margin-left: 150rpx!important;
  394. }
  395. .notice{
  396. width: 415rpx;
  397. height: 82rpx;
  398. background: #4C4C4C;
  399. border-radius: 22rpx;
  400. font-weight: 400;
  401. font-family: PingFangSC, PingFang SC;
  402. font-size: 31rpx;
  403. color: rgba(255,255,255,0.9);
  404. line-height: 82rpx;
  405. text-align: center;
  406. font-style: normal;
  407. margin-left: 168rpx;
  408. margin-top: 150rpx;
  409. }
  410. .icon-img{
  411. width: 22rpx;
  412. height: 22rpx;
  413. display: inline-block;
  414. margin-right: 10rpx;
  415. }
  416. </style>