loginByCode.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <template>
  2. <view style="padding: 16px;font-size: 14px;">
  3. <tn-nav-bar fixed alpha customBack>
  4. <view slot="back" class='tn-custom-nav-bar__back' @click="goBack">
  5. <text class='icon tn-icon-left'></text>
  6. </view>
  7. </tn-nav-bar>
  8. <view style="text-align: center;padding: 80px 0">
  9. <image src="../../static/logo.png" style="width: 110px; height: 100px;"></image>
  10. <view style="margin: 8px 0;"><text style="font-size: 22px;font-weight: bold;">速立保</text></view>
  11. </view>
  12. <uni-forms ref="baseForm" :modelValue="baseFormData">
  13. <uni-forms-item label="手机号" required>
  14. <uni-easyinput type="number" v-model="baseFormData.userName" :clearable="false" placeholder="请输入手机号" />
  15. </uni-forms-item>
  16. <uni-forms-item label="验证码" required>
  17. <uni-easyinput v-model="baseFormData.code" :clearable="false" placeholder="请输入验证码" >
  18. <template #right>
  19. <view @click="getCode" style="margin-right: 4px;background: #ddd;padding: 4px 10px;border-radius: 14px;">{{seconds?seconds+'s后重新获取':'获取验证码'}}</view>
  20. </template>
  21. </uni-easyinput>
  22. </uni-forms-item>
  23. </uni-forms>
  24. <view :class="!agreeValue?'':'isHidden'" style="background: #00000088;
  25. display: inline;
  26. padding: 4px 10px;
  27. border-radius: 12px;
  28. border-bottom-left-radius: 0;
  29. color: #fff;font-size: 12px;margin-left: 12px;">
  30. 请先阅读并同意协议
  31. </view>
  32. <view style="margin-top:4px">
  33. <tn-checkbox v-model="agreeValue" activeColor="#1d60b1" name="选项1">
  34. 阅读并同意
  35. </tn-checkbox>
  36. <view style="display: inline;font-size: 15px;" @click="showInfo()">
  37. <view style="color: #1d60b1;display: inline;">《用户服务协议》</view>
  38. <view style="display: inline;color: #1d60b1;" @click="showInfo2()">《隐私政策》</view>
  39. </view>
  40. </view>
  41. <view class="" hover-class="button-hover" style="margin-top: 24px;" v-if="agreeValue">
  42. <button :disabled="!canSave" style="border-radius: 50rpx;width: 100%;background-color: #1d60b1;" type="primary" @click="saveForm"
  43. @getphonenumber="getPhoneNumber">登录</button>
  44. </view>
  45. <view class="" hover-class="button-hover" style="margin-top: 24px;" v-if="!agreeValue">
  46. <button :disabled="!canSave" style="border-radius: 50rpx;width: 100%;background-color: #1d60b1;" type="primary" @click="showToast">登录</button>
  47. </view>
  48. <view @click="showLogin" style="text-align: center;margin-top: 24px;">微信一键登录</view>
  49. </view>
  50. </template>
  51. <script>
  52. import request from '../../utils/request';
  53. export default {
  54. data() {
  55. return {
  56. agreeValue: false,
  57. value: [],
  58. range: [{
  59. "value": 0,
  60. "text": ""
  61. }],
  62. baseFormData:{
  63. userName:'',
  64. code:''
  65. },
  66. lxSessionKey: '',
  67. openId: '',
  68. unionid: '',
  69. canSave:true,
  70. seconds:0,
  71. }
  72. },
  73. onLoad() {
  74. // this.getLxSessionKey()
  75. },
  76. onShow(){
  77. if(uni.getStorageSync('agreeInfo')){
  78. this.agreeValue = true;
  79. }
  80. },
  81. beforeDestroy() {
  82. // 组件销毁前清除定时器
  83. this.clearTimer();
  84. },
  85. methods: {
  86. goBack() {
  87. uni.navigateBack();
  88. },
  89. getCode(){
  90. if(!this.baseFormData.userName){
  91. uni.showToast({
  92. title: '请输入手机号',
  93. icon: 'none'
  94. })
  95. return false;
  96. }
  97. if(this.seconds>0){
  98. return false;
  99. }else{
  100. if(!this.agreeValue){
  101. uni.showToast({
  102. title: '请先阅读并同意协议',
  103. icon: 'none'
  104. })
  105. wx.vibrateShort();
  106. return false;
  107. }
  108. this.seconds = 60;
  109. this.getCodeByPhone();
  110. }
  111. },
  112. getCodeByPhone(){
  113. let params = {
  114. mobile: this.baseFormData.userName,
  115. platType: "slb",
  116. mpType: "engineer",
  117. smsType:'Login'
  118. }
  119. const that = this;
  120. request.post('/smsSend/sendSMS', {bean:JSON.stringify(params)}).then(res => {
  121. console.error(res);
  122. if (res&&res.success) {
  123. that.startCountdown();
  124. }else{
  125. uni.showToast({
  126. title: res.msg||'获取失败,请稍后再试',
  127. icon: 'none'
  128. })
  129. }
  130. })
  131. },
  132. startCountdown() {
  133. this.timer = setInterval(() => {
  134. if (this.seconds > 0) {
  135. this.seconds--;
  136. } else {
  137. // 倒计时结束,清除定时器
  138. this.clearTimer();
  139. // 执行倒计时结束后的操作
  140. }
  141. }, 1000);
  142. },
  143. // 清除定时器
  144. clearTimer() {
  145. clearInterval(this.timer);
  146. this.timer = null;
  147. },
  148. showToast() {
  149. uni.showToast({
  150. title: '请先阅读并同意协议',
  151. icon: 'none'
  152. })
  153. wx.vibrateShort();
  154. return false;
  155. },
  156. saveForm(){
  157. if(!this.baseFormData.userName){
  158. uni.showToast({
  159. title: '请输入手机号',
  160. icon: 'none'
  161. })
  162. return false;
  163. }
  164. if(!this.baseFormData.code){
  165. uni.showToast({
  166. title: '请输入验证码',
  167. icon: 'none'
  168. })
  169. return false;
  170. }
  171. let params = {
  172. userName:this.baseFormData.userName,
  173. smCode: this.baseFormData.code,
  174. mpType: "engineer",
  175. }
  176. const that = this;
  177. request.post('/slbSmLogin', params).then(res => {
  178. console.error(res);
  179. if (res&&res.success) {
  180. uni.showToast({
  181. title: '登录成功',
  182. icon: 'none'
  183. })
  184. uni.setStorageSync('loginStatus', 'true');
  185. uni.setStorageSync('userMap', JSON.stringify(res.resultMap));
  186. uni.setStorageSync('userNo', res.resultMap.accountName);
  187. that.getUserInfo();
  188. }else{
  189. uni.showToast({
  190. title: res.msg,
  191. icon: 'none'
  192. })
  193. }
  194. })
  195. },
  196. showInfo() {
  197. if(uni.getStorageSync('agreeInfo')){
  198. uni.navigateTo({
  199. url: '/pages/login/info?counted=1'
  200. })
  201. return false;
  202. }else{
  203. uni.navigateTo({
  204. url: '/pages/login/info'
  205. })
  206. }
  207. // uni.navigateTo({
  208. // url:'/pages/webview/web-view?url='+'https://test-oss.lx-device.com/userFeedback/1732866523422nfH.docx',
  209. // })
  210. },
  211. showInfo2() {
  212. uni.navigateTo({
  213. url:'/pages/webview/web-view?url='+encodeURIComponent('http://slb-m.dev.ml1993.com/webview?title=隐私协议&url='+'https://test-oss.lx-device.com/userFeedback/1734308677563sj9.docx'),
  214. })
  215. },
  216. showLogin(){
  217. uni.redirectTo({
  218. url:'/pages/login/login'
  219. })
  220. },
  221. change(e) {
  222. this.value = e.detail.data.length == 0 ? [] : [0];
  223. console.warn(this.value);
  224. console.log('e:', e);
  225. },
  226. getLxSessionKey() {
  227. const that = this;
  228. uni.login({
  229. success(res) {
  230. console.error(res);
  231. that.getOpenId(res.code);
  232. },
  233. fail(res) {
  234. console.error(res);
  235. uni.hideLoading();
  236. }
  237. });
  238. },
  239. getOpenId(code) {
  240. const that = this;
  241. request.post('/wxma/code2Session', {
  242. code: code,
  243. platType: "slb",
  244. mpType: "engineer",
  245. }).then(res => {
  246. console.error(res);
  247. if (res.success) {
  248. that.lxSessionKey = res.resultMap.lxSessionKey;
  249. that.openId = res.resultMap.openId;
  250. that.unionid = res.resultMap.unionid;
  251. }
  252. })
  253. },
  254. getPhoneNumber(e) {
  255. if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
  256. // wx.showModal({
  257. // title: '提示',
  258. // content: e.detail.errMsg,
  259. // showCancel: false
  260. // })
  261. console.error(e)
  262. return;
  263. }
  264. this.getLxSessionKey();
  265. setTimeout(() => {
  266. this._getPhoneNumber(e)
  267. }, 1000)
  268. },
  269. _getPhoneNumber(e) {
  270. console.warn(e);
  271. let that = this;
  272. if (e.detail.errMsg === 'getPhoneNumber:ok') {
  273. wx.getUserInfo({
  274. success: function(res) {
  275. console.error(res);
  276. res.encryptedData = encodeURIComponent(e.detail.encryptedData);
  277. res.iv = e.detail.iv;
  278. res.lxSessionKey = that.lxSessionKey;
  279. that.getPhone(res)
  280. }
  281. })
  282. } else {
  283. uni.showToast({
  284. icon: 'none',
  285. title: e.detail.code ? '获取成功' : '拒绝了使用微信手机号'
  286. });
  287. }
  288. // if (res.code == 0) {
  289. // uni.showToast({
  290. // title: '绑定成功',
  291. // icon: 'success',
  292. // duration: 2000
  293. // })
  294. // this.$u.vuex('mobile', res.data)
  295. // this.form.mobile = res.data
  296. // } else {
  297. // uni.showModal({
  298. // title: '提示',
  299. // content: res.msg,
  300. // showCancel: false
  301. // })
  302. // }
  303. },
  304. getPhone(prarms) {
  305. let newParams = {};
  306. newParams.signature = prarms.signature;
  307. newParams.rawData = prarms.rawData;
  308. newParams.encryptedData = prarms.encryptedData;
  309. newParams.iv = prarms.iv;
  310. newParams.lxSessionKey = prarms.lxSessionKey;
  311. let that = this;
  312. request.post('/wxma/getWaUserPhone',
  313. newParams
  314. ).then(res => {
  315. console.error(res);
  316. if (res.success) {
  317. this.ZhuceByPhone(res.resultMap.waUserPhoneInfo.phoneNumber);
  318. } else {
  319. uni.showToast({
  320. title: res.msg,
  321. icon: 'none'
  322. })
  323. }
  324. });
  325. },
  326. async ZhuceByPhone(phone) {
  327. const that = this;
  328. let params = {
  329. phone: phone,
  330. openid: that.openId,
  331. unionid: that.unionid,
  332. };
  333. that.canSave = false;
  334. const res = await request.post('/wxma/register',
  335. params, {
  336. header: {
  337. 'content-type': 'application/x-www-form-urlencoded',
  338. platType: "slb",
  339. mpType: "engineer",
  340. }
  341. }
  342. );
  343. if (res.success) {
  344. //当前页直接登录
  345. // uni.navigateTo({
  346. // url:'/pages/index/auth'
  347. // })
  348. this.loginAgain();
  349. } else {
  350. uni.showToast({
  351. title: res.msg,
  352. icon: 'none'
  353. })
  354. }
  355. console.warn(res);
  356. },
  357. loginAgain() {
  358. let that = this;
  359. uni.login({
  360. success(res) {
  361. that.loginByCode(res.code);
  362. },
  363. fail(res) {
  364. uni.hideLoading();
  365. }
  366. });
  367. },
  368. loginByCode(frontId) {
  369. const that = this;
  370. uni.setStorageSync('loginStatus', 'false');
  371. wx.getUserInfo({
  372. success: function(res) {
  373. console.error(res);
  374. request.post("/slbMpAutoLogin", {
  375. code: frontId,
  376. appType: 'ma',
  377. encryptedData: res.encryptedData,
  378. iv: res.iv
  379. }, {
  380. login: false,
  381. warn: false,
  382. loading: false
  383. }).then(res2 => {
  384. console.error(res2);
  385. if (res2.success) {
  386. //登录成功
  387. uni.setStorageSync('loginStatus', 'true');
  388. uni.setStorageSync('userMap', JSON.stringify(res2.resultMap));
  389. uni.setStorageSync('userNo', res2.resultMap.accountName);
  390. that.getUserInfo();
  391. } else {
  392. uni.showToast({
  393. title: res2.msg,
  394. icon: 'none'
  395. })
  396. that.canSave = true;
  397. //登录失败,
  398. }
  399. console.error(res2);
  400. });
  401. }
  402. })
  403. },
  404. getUserInfo() {
  405. let that = this;
  406. request.post('/slbWxma/getPersonlInfo', {
  407. }).then(res => {
  408. that.canSave = true;
  409. if (res.success) {
  410. that.personInfo = res.resultMap.userInfo || {};
  411. uni.setStorageSync('userInfo', JSON.stringify(res.resultMap.userInfo));
  412. uni.navigateBack();
  413. }
  414. console.warn(res);
  415. })
  416. },
  417. }
  418. }
  419. </script>
  420. <style lang="scss" scoped>
  421. .isHidden {
  422. visibility: hidden;
  423. }
  424. .template-edit {}
  425. /* 胶囊*/
  426. .tn-custom-nav-bar__back {
  427. width: 60%;
  428. height: 100%;
  429. position: relative;
  430. display: flex;
  431. justify-content: space-evenly;
  432. align-items: center;
  433. box-sizing: border-box;
  434. // background-color: rgba(0, 0, 0, 0.15);
  435. border-radius: 1000rpx;
  436. border: 1rpx solid rgba(255, 255, 255, 0.5);
  437. // color: #FFFFFF;
  438. font-size: 18px;
  439. .icon {
  440. display: block;
  441. flex: 1;
  442. margin: auto;
  443. text-align: center;
  444. }
  445. &:before {
  446. content: " ";
  447. width: 1rpx;
  448. height: 110%;
  449. position: absolute;
  450. top: 22.5%;
  451. left: 0;
  452. right: 0;
  453. margin: auto;
  454. transform: scale(0.5);
  455. transform-origin: 0 0;
  456. pointer-events: none;
  457. box-sizing: border-box;
  458. opacity: 0.7;
  459. background-color: #FFFFFF;
  460. }
  461. }
  462. /* 底部悬浮按钮 start*/
  463. .tn-tabbar-height {
  464. min-height: 100rpx;
  465. height: calc(120rpx + env(safe-area-inset-bottom) / 2);
  466. }
  467. .tn-footerfixed {
  468. position: fixed;
  469. width: 100%;
  470. bottom: calc(30rpx + env(safe-area-inset-bottom));
  471. z-index: 1024;
  472. box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0);
  473. }
  474. /* 底部悬浮按钮 end*/
  475. </style>