login.vue 11 KB

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