login.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <!--
  2. ## 代码风格与命名规范问题
  3. 1. 全局变量滥用 :代码中使用了 _this 全局变量(第50行),这是一种非常过时且危险的做法。在Vue组件中,应当使用箭头函数或 bind 方法来保持正确的 this 上下文,而不是依赖外部变量。这种写法会导致作用域混乱,增加调试难度。
  4. 2. 命名不一致 :方法命名风格混乱,有的使用驼峰命名(如 onGetUserProfile ),有的使用下划线风格(如 changMobileLogin ),还有的使用混合风格(如 wechatLogin )。
  5. 3. 硬编码问题 :代码中存在大量硬编码值,如第203行的 appid: 'wxf651ba4b0025640a' 和第204行的 main_body_id: '1' 。这些值应该通过配置文件或环境变量管理。
  6. 4. 注释不规范 :注释风格不一致,有的使用 // ,有的使用 /* */ ,且部分注释与代码逻辑不符。例如第104行的注释 "切换微信登录" 与方法实现不符,该方法只是简单地设置 ismobile=false 。
  7. ## 逻辑结构与流程问题
  8. 1. 状态管理混乱 :组件中同时使用了 $db (本地存储)、 $store (Vuex)和组件内部 data 来管理状态,导致状态管理逻辑分散,难以追踪。
  9. 2. 生命周期钩子使用不当 :在 mounted 钩子中仅仅赋值 _this = this ,这是完全多余的操作。而在 onShow 钩子中,混合了用户信息检查和微信登录逻辑,职责不清晰。
  10. 3. 条件渲染逻辑复杂 :模板中使用了 v-if="ismobile" 和微信小程序条件编译 #ifdef MP-WEIXIN 混合控制登录方式显示,逻辑嵌套过深,可读性差。
  11. 4. 导航逻辑不一致 :登录成功后,根据 redirect 参数的存在与否,使用了不同的导航方法: $common.navigateTo 、 uni.navigateBack 和 uni.switchTab ,逻辑分散且不一致。
  12. ## 错误处理与安全性问题
  13. 1. 错误处理不完善 :微信登录失败时(第246-263行),虽然有错误处理,但手机登录失败时(第167-175行)只是简单显示错误信息,没有更详细的错误分类和处理。
  14. 2. 密码安全问题 :密码输入框(第14行)使用了简单的 :password="true" ,但没有任何密码强度检测或加密措施。
  15. 3. 网络请求错误处理缺失 : $api.login 和 $api.third 调用中没有 catch 块来处理网络请求失败的情况,可能导致程序在网络异常时崩溃。
  16. 4. 数据验证不足 :虽然有简单的手机号和密码非空验证,但没有更严格的格式验证(如手机号格式正则验证)。
  17. ## 性能与可维护性问题
  18. 1. 重复代码 :登录成功后的存储逻辑(第144-152行和第212-224行)在手机登录和微信登录中重复出现,应当提取为公共方法。
  19. 2. 冗余代码 :第265-274行的 uni.login 代码被注释掉,属于死代码,应当移除。
  20. 3. 依赖外部库不当 :代码中使用了 $common 、 $api 、 $db 等全局依赖,但没有明确的导入语句,增加了代码的耦合度和维护难度。
  21. 4. 样式与逻辑混合 :模板中直接使用了内联样式(如第40行的 :style="{width:'160rpx',height: '40rpx'}" ),违反了样式与逻辑分离的原则。
  22. 5. 微信登录逻辑过时 :使用了 uni.getUserProfile 方法,但根据微信小程序的最新政策,这种登录方式已经过时,应当使用更现代的登录流程。
  23. -->
  24. <template>
  25. <view class="body">
  26. <u-navbar title="登录" bgColor="rgba(255,255,255,0.3)" :leftIconSize="0" titleStyle="font-weight:bold;color:#7a5831"></u-navbar>
  27. <block>
  28. <view v-if="ismobile" class="login-bg">
  29. <view class="login-card">
  30. <view class="login-head">
  31. <image class="logoimg" :src="baseLogo" mode="widthFix"></image>
  32. </view>
  33. <view class="login-input login-margin-b">
  34. <input type="number" v-model="username" placeholder="请输入手机号" />
  35. </view>
  36. <view class="login-input">
  37. <input :password="true" v-model="password" placeholder="请输入密码(6-16位)" />
  38. </view>
  39. <view class="cu-bar btn-group margin-top">
  40. <button class="cu-btn bg-orange shadow-blur round" :loading="loading" @tap="login">{{ loading ? '登录中...' : '登 录' }}</button>
  41. </view>
  42. <view class="flex justify-center">
  43. <view class="text-gray text-sm margin-top-xl" @tap="register">注册新账户</view>
  44. </view>
  45. </view>
  46. </view>
  47. <!-- #ifdef MP-WEIXIN -->
  48. <view v-else class="logView">
  49. <view class="logbt">
  50. <view class="login-head">
  51. <image class="logoimg" :src="baseLogo" mode="widthFix"></image>
  52. </view>
  53. <view class="loginTitile"><text decode="true">请点击微信登录,并授权获取公开信息, 登录后您将获得更多权益</text></view>
  54. <view class="btn-container">
  55. <view @click="onGetUserProfile" style="background-color: #cd5e3c; width: 200rpx; border-radius: 80rpx" class="cu-btn bg-orange shadow-blur round">
  56. <text class="cuIcon-lightauto"></text>
  57. 微信登录
  58. </view>
  59. <view style="background-color: #dcdcdc; color: #000; width: 200rpx; border-radius: 80rpx; margin-left: 30rpx" class="cu-btn bg-orange shadow-blur round" @click="back()">
  60. <text class="cuIcon-lightauto"></text>
  61. 暂不登录
  62. </view>
  63. </view>
  64. </view>
  65. <view class="text-gray text-sm margin-top-xl" :style="{width:'160rpx',height: '40rpx'}" @click="changMobileLogin()">
  66. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  67. </view>
  68. </view>
  69. <!-- #endif -->
  70. </block>
  71. </view>
  72. </template>
  73. <script>
  74. var _this;
  75. import { baseLogo } from '../../config/config.js';
  76. export default {
  77. data() {
  78. return {
  79. loading: false,
  80. user: [],
  81. username: '',
  82. password: '',
  83. class_id: '',
  84. ismobile: false,
  85. group_id: 1,
  86. code: '',
  87. baseLogo: baseLogo,
  88. redirect: ''
  89. };
  90. },
  91. mounted() {
  92. _this = this;
  93. },
  94. onLoad(e) {
  95. console.log(e);
  96. if (e.redirect) {
  97. this.redirect = e.redirect;
  98. }
  99. },
  100. onShow() {
  101. this.user = this.$common.userInfo();
  102. console.log('this.user: ', this.user);
  103. if (typeof this.user == 'undefined' || this.user == '' || this.user == null) {
  104. } else {
  105. this.$common.navigateTo('index');
  106. }
  107. // #ifdef MP-WEIXIN
  108. this.wxLogin();
  109. // #endif
  110. },
  111. methods: {
  112. wxLogin() {
  113. wx.login({
  114. success: (res) => {
  115. this.code = res.code;
  116. console.log('code' + res);
  117. },
  118. fail: function (error) {
  119. console.log('login failed ' + error);
  120. }
  121. });
  122. },
  123. //返回首页
  124. back(){
  125. uni.switchTab({
  126. url: '/pages/wenWuPage/wenWuPage',
  127. })
  128. },
  129. //切换微信登录
  130. wechatLogin(){
  131. _this.ismobile=false;
  132. },
  133. //切换手机登录
  134. changMobileLogin(){
  135. _this.ismobile=true;
  136. },
  137. register() {
  138. this.$common.navigateTo('register');
  139. },
  140. login() {
  141. _this.loading = true;
  142. if (_this.username == '' || _this.username.length < 11) {
  143. uni.showToast({
  144. icon: 'none',
  145. title: '请输入正确的手机号'
  146. });
  147. _this.loading = false;
  148. return;
  149. }
  150. if (_this.password == '') {
  151. uni.showToast({
  152. icon: 'none',
  153. title: '请输入密码'
  154. });
  155. _this.loading = false;
  156. return;
  157. }
  158. _this.$api.login(
  159. {
  160. account: _this.username,
  161. password: _this.password
  162. },
  163. (data) => {
  164. //console.log(data);
  165. if (data.code == 1) {
  166. _this.loading = false;
  167. //console.log(data);
  168. try {
  169. _this.$db.set('upload', 1);
  170. _this.$db.set('login', 1);
  171. _this.$db.set('token', data.data.userinfo.token);
  172. _this.$db.set('user', data.data.userinfo);
  173. _this.$db.set('auth', data.data.auth);
  174. _this.$store.commit('setAccessToken', data.data.userinfo.token);
  175. _this.$store.commit('setActivityId', 0);
  176. } catch (e) {}
  177. _this.$common.successToShow(data.msg, function () {
  178. // _this.$common.navigateTo('index');
  179. if (_this.redirect != '') {
  180. console.log(_this.redirect);
  181. if (_this.redirect.indexOf('%2F') > 0) {
  182. _this.redirect = '/' + unescape(_this.redirect);
  183. }
  184. _this.$common.navigateTo(_this.redirect);
  185. return;
  186. }
  187. uni.navigateBack();
  188. });
  189. } else {
  190. _this.loading = false;
  191. console.log('$api.login失败:', data);
  192. uni.showToast({
  193. duration: 1500,
  194. icon: 'none',
  195. title: data.msg
  196. });
  197. }
  198. }
  199. );
  200. },
  201. onGetUserProfile() {
  202. // uni.showLoading({
  203. // title:"正在登录中..."
  204. // })
  205. var platform = 'wechat';
  206. var that = this;
  207. var fid = uni.getStorageSync('parentid') ? uni.getStorageSync('parentid') : '';
  208. uni.getUserProfile({
  209. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  210. success: (res) => {
  211. console.log(res, '55');
  212. var invite_id = _this.$db.get('invite_id');
  213. if (!invite_id) invite_id = 0;
  214. console.log(invite_id);
  215. _this.$api.third(
  216. {
  217. code: _this.code,
  218. platform: platform,
  219. encrypted_data: res.encryptedData,
  220. iv: res.iv,
  221. raw_data: res.rawData,
  222. signature: res.signature,
  223. invite_id: invite_id,
  224. appid: 'wxf651ba4b0025640a',
  225. main_body_id: '1'
  226. },
  227. (data) => {
  228. console.log(data, '登录成功');
  229. // console.log(data.data.userinfo)
  230. var res = data.data;
  231. if (data.code == 1) {
  232. this.$common.successToShow('登录成功!');
  233. try {
  234. this.$db.set('upload', 1);
  235. this.$db.set('login', 1);
  236. this.$db.set('auth', res.auth);
  237. this.$db.set('user', res.userinfo);
  238. this.$db.set('token', res.auth.token);
  239. this.$db.set('mainBodyUserInfo', res.mainBodyUserInfo);
  240. _this.$store.commit('accessToken', data.data.userinfo.token);
  241. _this.$db.set('setActivityId', 9);
  242. _this.$db.set('activityId', 9);
  243. this.$store.commit('setAccessToken', res.auth.token);
  244. this.$store.commit('setActivityId', 9);
  245. this.$store.commit('activityId', 9);
  246. } catch (e) {
  247. console.log('e: ', e);
  248. }
  249. uni.switchTab({
  250. url: '/pages/index/index'
  251. });
  252. if (this.redirect != '') {
  253. console.log(this.redirect);
  254. if (this.redirect.indexOf('%2F') > 0) {
  255. this.redirect = '/' + unescape(this.redirect);
  256. }
  257. this.$common.navigateTo(this.redirect);
  258. return;
  259. }
  260. uni.navigateBack();
  261. } else {
  262. _this.wxLogin();
  263. }
  264. }
  265. );
  266. },
  267. fail: (res) => {
  268. console.log('res登录失败: ', res);
  269. _this.wxLogin(); //重新获取登录code
  270. uni.hideLoading();
  271. if (res.errMsg == 'getUserInfo:cancel' || res.errMsg == 'getUserInfo:fail auth deny') {
  272. uni.showModal({
  273. title: '用户授权失败',
  274. showCancel: false,
  275. content: '请点击重新授权,如果未弹出授权,请尝试长按删除小程序,重新进入!',
  276. success: function (res) {
  277. if (res.confirm) {
  278. console.log('用户点击确定');
  279. uni.navigateBack();
  280. }
  281. }
  282. });
  283. }
  284. }
  285. });
  286. // uni.login({
  287. // success: loginRes => {
  288. // uni.hideLoading();
  289. // console.log('第一次登录'+loginRes.code)
  290. // if (loginRes.code && loginRes.code!='') {
  291. // console.log('2222222222222222222')
  292. // }
  293. // }
  294. // })
  295. }
  296. }
  297. };
  298. </script>
  299. <style>
  300. page {
  301. background: #fff;
  302. }
  303. .content {
  304. height: 100%;
  305. }
  306. .logView {
  307. display: flex;
  308. align-items: center;
  309. justify-content: center;
  310. flex-direction: column;
  311. align-items: center;
  312. /* 垂直居中 */
  313. width: 100%;
  314. position: fixed;
  315. left: 50%;
  316. top: 50%;
  317. transform: translate(-50%, -50%);
  318. }
  319. .logbt {
  320. display: flex;
  321. align-items: center;
  322. justify-content: center;
  323. flex-direction: column;
  324. align-items: center;
  325. /* 垂直居中 */
  326. width: 100%;
  327. background: none;
  328. border: none !important;
  329. }
  330. .logbt:after {
  331. border: none !important;
  332. }
  333. .logbt .logoimg {
  334. width: 200rpx;
  335. height: 200rpx;
  336. display: block;
  337. }
  338. .logbt .wechatimg {
  339. width: 150rpx;
  340. height: 150rpx;
  341. display: block;
  342. }
  343. .loginTitile {
  344. padding: 50rpx;
  345. font-size: 28rpx;
  346. color: #787878;
  347. line-height: 1.3;
  348. text-align: center;
  349. }
  350. .loginBtn {
  351. width: 300rpx;
  352. height: 70rpx;
  353. line-height: 70rpx;
  354. color: #fff;
  355. background: #2562a1;
  356. border-radius: 10rpx;
  357. border: none;
  358. }
  359. image {
  360. width: 100rpx;
  361. height: 100rpx;
  362. }
  363. .mobileLogin {
  364. background: none;
  365. color: #999;
  366. text-align: center;
  367. margin: 40rpx auto;
  368. border: none;
  369. font-size: 26rpx;
  370. }
  371. .landing[type='primary'] {
  372. height: 84rpx;
  373. line-height: 84rpx;
  374. border-radius: 44rpx;
  375. font-size: 32rpx;
  376. /* background: linear-gradient(left, #86B5F4, #4790EF); */
  377. background-color: #ffbc32;
  378. }
  379. .login-btn {
  380. padding: 10rpx 20rpx;
  381. margin-top: 60rpx;
  382. }
  383. .login-function {
  384. overflow: auto;
  385. padding: 20rpx 20rpx 30rpx 20rpx;
  386. }
  387. .login-forget {
  388. float: left;
  389. font-size: 26rpx;
  390. color: #999;
  391. }
  392. .login-register {
  393. color: #666;
  394. float: right;
  395. font-size: 26rpx;
  396. }
  397. .login-input input {
  398. background: #f2f5f6;
  399. font-size: 28rpx;
  400. padding: 10rpx 25rpx;
  401. height: 80rpx;
  402. line-height: 80rpx;
  403. border-radius: 40rpx;
  404. }
  405. .login-margin-b {
  406. margin-bottom: 25rpx;
  407. }
  408. .login-input {
  409. padding: 10rpx 20rpx;
  410. }
  411. .login-head {
  412. font-size: 34rpx;
  413. text-align: center;
  414. padding: 25rpx 10rpx 55rpx 10rpx;
  415. }
  416. .login-head image {
  417. width: 200rpx;
  418. }
  419. .login-card {
  420. background: #fff;
  421. border-radius: 12rpx;
  422. padding: 10rpx 25rpx;
  423. position: relative;
  424. margin-top: 120rpx;
  425. }
  426. .login-bg {
  427. height: 100%;
  428. padding: 25rpx;
  429. }
  430. .btn-container {
  431. display: flex;
  432. flex-direction: row;
  433. align-items: center;
  434. justify-content: center;
  435. margin-top: 20rpx;
  436. }
  437. </style>