login.vue 11 KB

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