login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <template>
  2. <view style="padding: 16px;font-size: 14px;background-color: #f7f7f7;min-height: 100vh;">
  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. unionid: '',
  52. canSave:true,
  53. phoneNum:'',
  54. wxFontSize: 17,
  55. phoneData: null
  56. }
  57. },
  58. onLoad() {
  59. const appBaseInfo = wx.getAppBaseInfo();
  60. this.wxFontSize = uni.getStorageSync('fontSize')||appBaseInfo.fontSizeSetting||17;
  61. console.error(wx.getAccountInfoSync().miniProgram.envVersion);
  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.reLaunch({
  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. if(wx.getAccountInfoSync()&&wx.getAccountInfoSync().miniProgram.envVersion=='develop'){
  171. res.encryptedData = encodeURIComponent(encodeURIComponent(e.detail.encryptedData));
  172. res.iv = encodeURIComponent(encodeURIComponent(e.detail.iv));
  173. }else{
  174. res.encryptedData = (encodeURIComponent(e.detail.encryptedData));
  175. res.iv = (encodeURIComponent(e.detail.iv));
  176. }
  177. res.lxSessionKey = that.lxSessionKey;
  178. that.getPhone(res)
  179. }
  180. })
  181. } else {
  182. uni.showToast({
  183. icon: 'none',
  184. title: e.detail.code ? '获取成功' : '拒绝了使用微信手机号'
  185. });
  186. }
  187. },
  188. getPhone(prarms) {
  189. let newParams = {};
  190. newParams.signature = prarms.signature;
  191. newParams.rawData = prarms.rawData;
  192. newParams.encryptedData = prarms.encryptedData;
  193. newParams.iv = prarms.iv;
  194. newParams.lxSessionKey = prarms.lxSessionKey;
  195. let that = this;
  196. request.post('/wxma/getWaUserPhone',
  197. newParams
  198. ).then(res => {
  199. console.error(res);
  200. if (res&&res.success) {
  201. that.phoneNum = res.resultMap.waUserPhoneInfo.phoneNumber;
  202. that.ZhuceByPhone(res.resultMap.waUserPhoneInfo.phoneNumber);
  203. } else {
  204. uni.showToast({
  205. title: res.msg,
  206. icon: 'none'
  207. })
  208. }
  209. });
  210. },
  211. async ZhuceByPhone(phone) {
  212. const that = this;
  213. let params = {
  214. phone: phone,
  215. openid: that.openId,
  216. unionid: that.unionid,
  217. };
  218. that.canSave = false;
  219. const res = await request.post('/wxma/register',
  220. params, {
  221. header: {
  222. 'content-type': 'application/x-www-form-urlencoded',
  223. platType: "slb",
  224. mpType: "engineer",
  225. }
  226. }
  227. );
  228. if (res.success) {
  229. //当前页直接登录
  230. // uni.navigateTo({
  231. // url:'/pages/index/auth'
  232. // })
  233. uni.showToast({
  234. title: '请稍后',
  235. icon: 'none'
  236. })
  237. this.loginAgain();
  238. } else {
  239. uni.showToast({
  240. title: res.msg,
  241. icon: 'none'
  242. })
  243. }
  244. console.warn(res);
  245. },
  246. //退出登录时,先自动登录一次,看用户绑定过没有,没有才去注册.
  247. loginFirst(){
  248. let that = this;
  249. uni.showToast({
  250. title: '请稍后',
  251. icon: 'none'
  252. })
  253. that.canSave = false;
  254. uni.login({
  255. success(res) {
  256. that.loginByCodeOnce(res.code);
  257. },
  258. fail(res) {
  259. uni.hideLoading();
  260. }
  261. });
  262. },
  263. loginAgain() {
  264. let that = this;
  265. uni.login({
  266. success(res) {
  267. that.loginByCode(res.code);
  268. },
  269. fail(res) {
  270. uni.hideLoading();
  271. }
  272. });
  273. },
  274. loginByCodeOnce(frontId) {
  275. const that = this;
  276. uni.setStorageSync('loginStatus', 'false');
  277. wx.getUserInfo({
  278. success: function(res) {
  279. console.error(res);
  280. request.post("/slbMpAutoLogin", {
  281. code: frontId,
  282. appType: 'ma',
  283. encryptedData: res.encryptedData,
  284. iv: res.iv
  285. }, {
  286. login: false,
  287. warn: false,
  288. loading: false
  289. }).then(res2 => {
  290. console.error(res2);
  291. if (res2.success) {
  292. //登录成功
  293. uni.setStorageSync('loginStatus', 'true');
  294. uni.setStorageSync('userMap', JSON.stringify(res2.resultMap));
  295. uni.setStorageSync('userNo', res2.resultMap.accountName);
  296. uni.removeStorageSync('loginOut');
  297. that.getUserInfo();
  298. } else {
  299. //
  300. that.getLxSessionKey(that.phoneData);
  301. // that.ZhuceByPhone(that.phoneNum)
  302. // that.canSave = true;
  303. //登录失败,
  304. }
  305. console.error(res2);
  306. });
  307. }
  308. })
  309. },
  310. loginByCode(frontId) {
  311. const that = this;
  312. uni.setStorageSync('loginStatus', 'false');
  313. wx.getUserInfo({
  314. success: function(res) {
  315. console.error(res);
  316. request.post("/slbMpAutoLogin", {
  317. code: frontId,
  318. appType: 'ma',
  319. encryptedData: res.encryptedData,
  320. iv: res.iv
  321. }, {
  322. login: false,
  323. warn: false,
  324. loading: false
  325. }).then(res2 => {
  326. if (res2.success) {
  327. //登录成功
  328. uni.setStorageSync('loginStatus', 'true');
  329. uni.setStorageSync('userMap', JSON.stringify(res2.resultMap));
  330. uni.setStorageSync('userNo', res2.resultMap.accountName);
  331. uni.removeStorageSync('loginOut');
  332. that.getUserInfo();
  333. } else {
  334. //
  335. uni.showToast({
  336. title: res2.msg,
  337. icon: 'none'
  338. })
  339. that.canSave = true;
  340. //登录失败,
  341. }
  342. console.error(res2);
  343. });
  344. }
  345. })
  346. },
  347. getUserInfo() {
  348. let that = this;
  349. request.post('/slbWxma/getPersonlInfo', {
  350. }).then(res => {
  351. that.canSave = true;
  352. if (res&&res.success) {
  353. that.personInfo = res.resultMap.userInfo || {};
  354. uni.setStorageSync('userInfo', JSON.stringify(res.resultMap.userInfo));
  355. that.goBack();
  356. }else{
  357. uni.showToast({
  358. title: '登录超时,请再试一次',
  359. icon: 'none'
  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>