loginByCode.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. this.seconds = 60;
  101. this.getCodeByPhone();
  102. }
  103. },
  104. getCodeByPhone(){
  105. let params = {
  106. mobile: this.baseFormData.userName,
  107. platType: "slb",
  108. mpType: "engineer",
  109. smsType:'Login'
  110. }
  111. const that = this;
  112. request.post('/smsSend/sendSMS', {bean:JSON.stringify(params)}).then(res => {
  113. console.error(res);
  114. if (res&&res.success) {
  115. that.startCountdown();
  116. }else{
  117. uni.showToast({
  118. title: res.msg||'获取失败,请稍后再试',
  119. icon: 'none'
  120. })
  121. }
  122. })
  123. },
  124. startCountdown() {
  125. this.timer = setInterval(() => {
  126. if (this.seconds > 0) {
  127. this.seconds--;
  128. } else {
  129. // 倒计时结束,清除定时器
  130. this.clearTimer();
  131. // 执行倒计时结束后的操作
  132. }
  133. }, 1000);
  134. },
  135. // 清除定时器
  136. clearTimer() {
  137. clearInterval(this.timer);
  138. this.timer = null;
  139. },
  140. showToast() {
  141. uni.showToast({
  142. title: '请先阅读并同意协议',
  143. icon: 'none'
  144. })
  145. wx.vibrateShort();
  146. return false;
  147. },
  148. saveForm(){
  149. if(!this.baseFormData.userName){
  150. uni.showToast({
  151. title: '请输入手机号',
  152. icon: 'none'
  153. })
  154. return false;
  155. }
  156. if(!this.baseFormData.code){
  157. uni.showToast({
  158. title: '请输入验证码',
  159. icon: 'none'
  160. })
  161. return false;
  162. }
  163. let params = {
  164. userName:this.baseFormData.userName,
  165. smCode: this.baseFormData.code,
  166. mpType: "engineer",
  167. }
  168. const that = this;
  169. request.post('/slbSmLogin', params).then(res => {
  170. console.error(res);
  171. if (res&&res.success) {
  172. uni.showToast({
  173. title: '登录成功',
  174. icon: 'none'
  175. })
  176. uni.setStorageSync('loginStatus', 'true');
  177. uni.setStorageSync('userMap', JSON.stringify(res2.resultMap));
  178. uni.setStorageSync('userNo', res2.resultMap.accountName);
  179. that.getUserInfo();
  180. }else{
  181. uni.showToast({
  182. title: res.msg,
  183. icon: 'none'
  184. })
  185. }
  186. })
  187. },
  188. showInfo() {
  189. if(uni.getStorageSync('agreeInfo')){
  190. uni.navigateTo({
  191. url: '/pages/login/info?counted=1'
  192. })
  193. return false;
  194. }else{
  195. uni.navigateTo({
  196. url: '/pages/login/info'
  197. })
  198. }
  199. // uni.navigateTo({
  200. // url:'/pages/webview/web-view?url='+'https://test-oss.lx-device.com/userFeedback/1732866523422nfH.docx',
  201. // })
  202. },
  203. showInfo2() {
  204. uni.navigateTo({
  205. url:'/pages/webview/web-view?url='+'https://test-oss.lx-device.com/userFeedback/1732866629261TEn.docx',
  206. })
  207. },
  208. showLogin(){
  209. uni.redirectTo({
  210. url:'/pages/login/login'
  211. })
  212. },
  213. change(e) {
  214. this.value = e.detail.data.length == 0 ? [] : [0];
  215. console.warn(this.value);
  216. console.log('e:', e);
  217. },
  218. getLxSessionKey() {
  219. const that = this;
  220. uni.login({
  221. success(res) {
  222. console.error(res);
  223. that.getOpenId(res.code);
  224. },
  225. fail(res) {
  226. console.error(res);
  227. uni.hideLoading();
  228. }
  229. });
  230. },
  231. getOpenId(code) {
  232. const that = this;
  233. request.post('/wxma/code2Session', {
  234. code: code,
  235. platType: "slb",
  236. mpType: "engineer",
  237. }).then(res => {
  238. console.error(res);
  239. if (res.success) {
  240. that.lxSessionKey = res.resultMap.lxSessionKey;
  241. that.openId = res.resultMap.openId;
  242. that.unionid = res.resultMap.unionid;
  243. }
  244. })
  245. },
  246. getPhoneNumber(e) {
  247. if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
  248. // wx.showModal({
  249. // title: '提示',
  250. // content: e.detail.errMsg,
  251. // showCancel: false
  252. // })
  253. console.error(e)
  254. return;
  255. }
  256. this.getLxSessionKey();
  257. setTimeout(() => {
  258. this._getPhoneNumber(e)
  259. }, 1000)
  260. },
  261. _getPhoneNumber(e) {
  262. console.warn(e);
  263. let that = this;
  264. if (e.detail.errMsg === 'getPhoneNumber:ok') {
  265. wx.getUserInfo({
  266. success: function(res) {
  267. console.error(res);
  268. res.encryptedData = encodeURIComponent(e.detail.encryptedData);
  269. res.iv = e.detail.iv;
  270. res.lxSessionKey = that.lxSessionKey;
  271. that.getPhone(res)
  272. }
  273. })
  274. } else {
  275. uni.showToast({
  276. icon: 'none',
  277. title: e.detail.code ? '获取成功' : '拒绝了使用微信手机号'
  278. });
  279. }
  280. // if (res.code == 0) {
  281. // uni.showToast({
  282. // title: '绑定成功',
  283. // icon: 'success',
  284. // duration: 2000
  285. // })
  286. // this.$u.vuex('mobile', res.data)
  287. // this.form.mobile = res.data
  288. // } else {
  289. // uni.showModal({
  290. // title: '提示',
  291. // content: res.msg,
  292. // showCancel: false
  293. // })
  294. // }
  295. },
  296. getPhone(prarms) {
  297. let newParams = {};
  298. newParams.signature = prarms.signature;
  299. newParams.rawData = prarms.rawData;
  300. newParams.encryptedData = prarms.encryptedData;
  301. newParams.iv = prarms.iv;
  302. newParams.lxSessionKey = prarms.lxSessionKey;
  303. let that = this;
  304. request.post('/wxma/getWaUserPhone',
  305. newParams
  306. ).then(res => {
  307. console.error(res);
  308. if (res.success) {
  309. this.ZhuceByPhone(res.resultMap.waUserPhoneInfo.phoneNumber);
  310. } else {
  311. uni.showToast({
  312. title: res.msg,
  313. icon: 'none'
  314. })
  315. }
  316. });
  317. console.warn(res);
  318. },
  319. async ZhuceByPhone(phone) {
  320. const that = this;
  321. let params = {
  322. phone: phone,
  323. openid: that.openId,
  324. unionid: that.unionid,
  325. };
  326. that.canSave = false;
  327. const res = await request.post('/wxma/register',
  328. params, {
  329. header: {
  330. 'content-type': 'application/x-www-form-urlencoded',
  331. platType: "slb",
  332. mpType: "engineer",
  333. }
  334. }
  335. );
  336. if (res.success) {
  337. //当前页直接登录
  338. // uni.navigateTo({
  339. // url:'/pages/index/auth'
  340. // })
  341. this.loginAgain();
  342. } else {
  343. uni.showToast({
  344. title: res.msg,
  345. icon: 'none'
  346. })
  347. }
  348. console.warn(res);
  349. },
  350. loginAgain() {
  351. let that = this;
  352. uni.login({
  353. success(res) {
  354. that.loginByCode(res.code);
  355. },
  356. fail(res) {
  357. uni.hideLoading();
  358. }
  359. });
  360. },
  361. loginByCode(frontId) {
  362. const that = this;
  363. uni.setStorageSync('loginStatus', 'false');
  364. wx.getUserInfo({
  365. success: function(res) {
  366. console.error(res);
  367. request.post("/slbMpAutoLogin", {
  368. code: frontId,
  369. appType: 'ma',
  370. encryptedData: res.encryptedData,
  371. iv: res.iv
  372. }, {
  373. login: false,
  374. warn: false,
  375. loading: false
  376. }).then(res2 => {
  377. console.error(res2);
  378. if (res2.success) {
  379. //登录成功
  380. uni.setStorageSync('loginStatus', 'true');
  381. uni.setStorageSync('userMap', JSON.stringify(res2.resultMap));
  382. uni.setStorageSync('userNo', res2.resultMap.accountName);
  383. that.getUserInfo();
  384. } else {
  385. uni.showToast({
  386. title: res2.msg,
  387. icon: 'none'
  388. })
  389. that.canSave = true;
  390. //登录失败,
  391. }
  392. console.error(res2);
  393. });
  394. }
  395. })
  396. },
  397. getUserInfo() {
  398. let that = this;
  399. request.post('/slbWxma/getPersonlInfo', {
  400. }).then(res => {
  401. that.canSave = true;
  402. if (res.success) {
  403. that.personInfo = res.resultMap.userInfo || {};
  404. uni.setStorageSync('userInfo', JSON.stringify(res.resultMap.userInfo));
  405. uni.navigateBack();
  406. }
  407. console.warn(res);
  408. })
  409. },
  410. }
  411. }
  412. </script>
  413. <style lang="scss" scoped>
  414. .isHidden {
  415. visibility: hidden;
  416. }
  417. .template-edit {}
  418. /* 胶囊*/
  419. .tn-custom-nav-bar__back {
  420. width: 60%;
  421. height: 100%;
  422. position: relative;
  423. display: flex;
  424. justify-content: space-evenly;
  425. align-items: center;
  426. box-sizing: border-box;
  427. // background-color: rgba(0, 0, 0, 0.15);
  428. border-radius: 1000rpx;
  429. border: 1rpx solid rgba(255, 255, 255, 0.5);
  430. // color: #FFFFFF;
  431. font-size: 18px;
  432. .icon {
  433. display: block;
  434. flex: 1;
  435. margin: auto;
  436. text-align: center;
  437. }
  438. &:before {
  439. content: " ";
  440. width: 1rpx;
  441. height: 110%;
  442. position: absolute;
  443. top: 22.5%;
  444. left: 0;
  445. right: 0;
  446. margin: auto;
  447. transform: scale(0.5);
  448. transform-origin: 0 0;
  449. pointer-events: none;
  450. box-sizing: border-box;
  451. opacity: 0.7;
  452. background-color: #FFFFFF;
  453. }
  454. }
  455. /* 底部悬浮按钮 start*/
  456. .tn-tabbar-height {
  457. min-height: 100rpx;
  458. height: calc(120rpx + env(safe-area-inset-bottom) / 2);
  459. }
  460. .tn-footerfixed {
  461. position: fixed;
  462. width: 100%;
  463. bottom: calc(30rpx + env(safe-area-inset-bottom));
  464. z-index: 1024;
  465. box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0);
  466. }
  467. /* 底部悬浮按钮 end*/
  468. </style>