login.vue 11 KB

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