login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <!--
  2. 退役军人端: 通过退役军人userID获取退役军人userSig等信息,再通过判断permission获取哪一个教师拥有这个咨询权限,再进行登录后直接跳转到聊天页面
  3. 教师端: 通过教师userID获取教师userSig等信息,再进行登录跳转到聊天记录页面
  4. -->
  5. <template>
  6. <view class="login">
  7. <view class="login-logo">
  8. <u-image src="../../static/logo.png" mode="aspectFill" width="260" height="260" />
  9. </view>
  10. <view class="login-form">
  11. <u-form :model="form" ref="uForm">
  12. <u-form-item label="用户名" label-width="140" prop="userName">
  13. <u-input v-model="form.userName" placeholder="" border="" :disabled="isDisable"></u-input>
  14. </u-form-item>
  15. <!-- <u-form-item label="用户名" label-width="140" prop="userName">
  16. <u-input v-model="form.userName" placeholder="请输入用户名" border="" :disabled="isDisable"></u-input>
  17. </u-form-item> -->
  18. <!-- <u-form-item label="用户密码" label-width="140" prop="userSig">
  19. <u-input v-model="form.userSig" placeholder="请输入用户密码" border="" type="password" :disabled="isDisable"></u-input>
  20. </u-form-item> -->
  21. <u-form-item :border-bottom="false">
  22. <u-button type="primary" @click="handleLogin">登录</u-button>
  23. </u-form-item>
  24. </u-form>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. import userList from '../../config/tim/user.js'
  30. export default {
  31. data() {
  32. return {
  33. isDisable: false,
  34. form: {
  35. userID: '', // 用户id
  36. userName: '', // 用户名
  37. userSig: '', // 用户标签
  38. toUserId: '', // 接收消息用户id
  39. permission: '', // 教师权限 用于筛选不用类型下的教师
  40. classId: '', // 班级id 用于筛选该班级下的教师
  41. type: '' // 用来区分教师端(teacher)和退役军人移动端(retire)
  42. },
  43. rules: {
  44. userID: [{
  45. required: true,
  46. message: '请输入用户ID',
  47. trigger: ['blur']
  48. }],
  49. userName: [{
  50. required: true,
  51. message: '请输入用户名',
  52. trigger: ['blur']
  53. }],
  54. userSig: [{
  55. required: true,
  56. message: '请输入用户密码',
  57. trigger: ['blur']
  58. }]
  59. }
  60. }
  61. },
  62. onLoad(page) {
  63. /**
  64. * userID 用户id
  65. * userName 用户名
  66. * userSig 用户标签
  67. * toUserId 接收消息用户id
  68. * permission 教师权限 用于筛选不用类型下的教师
  69. * classId 班级id 用于筛选该班级下的教师
  70. * type 用来区分教师端(teacher)和退役军人移动端(retire)
  71. */
  72. if (page.userID) {
  73. this.isDisable = true
  74. this.form.userID = page?.userID
  75. this.form.type = page?.type
  76. this.form.userName = page?.userName
  77. this.form.userSig = page?.userSig
  78. this.form.permission = page?.permission
  79. this.form.classId = page?.classId
  80. this.autoLogin();
  81. } else {
  82. uni.showToast({
  83. title: '参数丢失',
  84. icon: 'none'
  85. })
  86. }
  87. },
  88. onShow() {},
  89. methods: {
  90. /**
  91. * 自动登录
  92. */
  93. autoLogin() {
  94. uni.showLoading({
  95. mask: true,
  96. title: '正在登录中...'
  97. })
  98. if (this.form.type === 'teacher') {
  99. setTimeout(() => {
  100. this.getTeacherInfo(this.form.userID);
  101. }, 100)
  102. } else if (this.form.type === 'retire') {
  103. setTimeout(() => {
  104. this.getRetireInfo(this.form.userID);
  105. }, 100)
  106. } else {
  107. uni.showToast({
  108. title: '参数丢失',
  109. icon: 'none'
  110. })
  111. }
  112. },
  113. /**
  114. * 获取个人信息
  115. */
  116. getMyProfile() {
  117. this.tim.getMyProfile().then(res => {
  118. console.log('res', res)
  119. }).catch(err => {
  120. console.log('err', err)
  121. this.autoLogin();
  122. })
  123. },
  124. /**
  125. * 登录按钮
  126. */
  127. handleLogin() {
  128. this.$refs.uForm.validate(valid => {
  129. if (valid) {
  130. this.autoLogin();
  131. }
  132. return false;
  133. })
  134. },
  135. /**
  136. * 登录TIM
  137. */
  138. loginTim(userInfo) {
  139. this.tim.login({
  140. userID: this.form.userID,
  141. userSig: this.form.userSig
  142. }).then(res => {
  143. // 登录成功后 更新登录状态
  144. this.$store.commit('toggleIsLogin', true);
  145. // 自己平台的用户基础信息
  146. uni.setStorageSync('userInfo', JSON.stringify(userInfo))
  147. // tim 返回的用户信息
  148. uni.setStorageSync('userTIMInfo', JSON.stringify(res.data));
  149. this.updateUserInfo(userInfo);
  150. if (this.form.toUserId) {
  151. setTimeout(() => {
  152. uni.hideLoading()
  153. this.$store.commit('createConversationActive', this.form.toUserId)
  154. this.$u.route({
  155. url: 'pages/chat/chat'
  156. })
  157. }, 300)
  158. } else {
  159. setTimeout(() => {
  160. uni.hideLoading();
  161. this.$u.route({
  162. url: '/pages/index/index'
  163. })
  164. }, 300)
  165. }
  166. }).catch(err => {
  167. uni.showToast({
  168. icon: 'none',
  169. title: '用户不存在',
  170. duration: 2000
  171. })
  172. })
  173. },
  174. updateUserInfo(userInfo) {
  175. // 将已经登陆的用户信息 提交到IM中
  176. this.tim.updateMyProfile({
  177. nick: userInfo.username,
  178. avatar: userInfo.avatar,
  179. gender: this.$TIM.TYPES.GENDER_MALE,
  180. selfSignature: '暂无个性签名',
  181. allowType: this.$TIM.TYPES.ALLOW_TYPE_ALLOW_ANY
  182. }).then((res) => {
  183. console.log('提交资料成功')
  184. }).catch((err) => {
  185. console.warn('updateMyProfile error:', err); // 更新资料失败的相关信息
  186. });
  187. },
  188. /**
  189. * 获取老师信息
  190. * @param { String } teacherId
  191. */
  192. getTeacherInfo(teacherId) {
  193. uni.request({
  194. url: this.config.baseUrl + '/im/getSignatureInfo',
  195. data: {
  196. id: teacherId,
  197. type: 2
  198. },
  199. success: (res) => {
  200. console.log('教师信息:', res.data)
  201. if (res.data.code === 200) {
  202. this.form.userSig = res.data.data.signature
  203. this.form.userName = res.data.data.username
  204. this.importUser(teacherId, 2, res.data.data)
  205. } else {
  206. uni.showToast({
  207. title: res.data.msg,
  208. icon: 'error'
  209. })
  210. uni.hideLoading()
  211. }
  212. },
  213. fail: (err) => {
  214. uni.showToast({
  215. title: '获取教师信息失败!',
  216. icon: 'none'
  217. })
  218. uni.hideLoading()
  219. }
  220. });
  221. },
  222. /**
  223. * 获取退役军人信息
  224. * @param {String} retireId
  225. */
  226. getRetireInfo(retireId) {
  227. uni.request({
  228. url: this.config.baseUrl + '/im/getSignatureInfo',
  229. data: {
  230. id: retireId,
  231. type: 1
  232. },
  233. success: (res) => {
  234. console.log('退役军人信息:', res.data);
  235. if (res.data.code === 200) {
  236. this.form.userSig = res.data.data.signature
  237. this.form.userName = res.data.data.username
  238. if (this.form.classId) {
  239. this.getClassTeacherByClassId(this.form.classId, res.data.data)
  240. } else {
  241. this.getConsultJurisdiction(this.form.permission, res.data.data)
  242. }
  243. } else {
  244. uni.showToast({
  245. title: res.data.msg,
  246. icon: 'error'
  247. })
  248. uni.hideLoading()
  249. }
  250. },
  251. fail: (err) => {
  252. uni.showToast({
  253. title: '获取退役军人信息失败!',
  254. icon: 'none'
  255. })
  256. uni.hideLoading()
  257. }
  258. });
  259. },
  260. /**
  261. * 通过班级id筛选老师
  262. * @param {Object} classId
  263. * @param {Object} userInfo
  264. */
  265. getClassTeacherByClassId(classId, userInfo) {
  266. uni.request({
  267. url: this.config.baseUrl + '/im/getTeacherInfoByClassId',
  268. data: {
  269. classId: classId
  270. },
  271. success: (res) => {
  272. console.log('班级教师信息:', res.data);
  273. if (res.data.code === 200) {
  274. this.form.toUserId = res.data.data?.teacherId
  275. if (this.form.toUserId) {
  276. this.importUser(this.form.toUserId, 2, userInfo)
  277. } else {
  278. uni.showToast({
  279. title: '未找到相关班级老师!',
  280. icon: 'none'
  281. })
  282. }
  283. } else {
  284. uni.showToast({
  285. title: res.data.msg,
  286. icon: 'error'
  287. })
  288. uni.hideLoading()
  289. }
  290. },
  291. fail: (err) => {
  292. uni.showToast({
  293. title: '获取退役军人信息失败!',
  294. icon: 'none'
  295. })
  296. uni.hideLoading()
  297. }
  298. });
  299. },
  300. /**
  301. * 获取拥有该权限的教师
  302. * @param { String } permission
  303. */
  304. getConsultJurisdiction(permission, userInfo) {
  305. uni.request({
  306. url: this.config.baseUrl + '/im/getTeacherInfoByAuth',
  307. data: {
  308. auth: permission
  309. },
  310. success: (res) => {
  311. console.log('拥有权限的教师:', res.data);
  312. if (res.data.code === 200) {
  313. if (res.data.data) {
  314. this.form.toUserId = res.data.data.teacherId
  315. this.importUser(res.data.data.teacherId, 2, userInfo)
  316. } else {
  317. uni.showToast({
  318. title: '该咨询目前未分配教师!',
  319. icon: 'none'
  320. })
  321. }
  322. } else {
  323. uni.showToast({
  324. title: res.msg,
  325. icon: 'error'
  326. })
  327. uni.hideLoading()
  328. }
  329. },
  330. fail: (err) => {
  331. uni.showToast({
  332. title: '获取教师权限失败!',
  333. icon: 'none'
  334. })
  335. uni.hideLoading()
  336. }
  337. });
  338. },
  339. /**
  340. * 导入用户
  341. * @param { String } id 导入用户id
  342. * @param { Number } type 1-退役军人 2-教师 3-企业
  343. * @param { Object } userInfo 登录用户信息
  344. */
  345. importUser(id, type, userInfo) {
  346. uni.request({
  347. url: this.config.baseUrl + '/im/importUser',
  348. method: 'POST',
  349. data: {
  350. id,
  351. type
  352. },
  353. success: (res) => {
  354. console.log('注册:', res.data);
  355. if (res.data.code === 200) {
  356. this.loginTim(userInfo);
  357. } else {
  358. uni.showToast({
  359. title: res.data.msg || res.data.message,
  360. icon: 'error'
  361. })
  362. }
  363. },
  364. fail: (err) => {
  365. uni.showToast({
  366. title: '注册失败!',
  367. icon: 'none'
  368. })
  369. uni.hideLoading()
  370. }
  371. });
  372. },
  373. /**
  374. * 登录trtc calling
  375. */
  376. loginTrtc() {
  377. this.trtcCalling.login({
  378. sdkAppID: this.config.sdkAppId,
  379. userID: this.form.userID,
  380. userSig: this.form.userSig
  381. }).then(res => {
  382. this.tim.login({
  383. userID: this.form.userID,
  384. userSig: this.form.userSig
  385. }).then(res => {
  386. // 登录成功后 更新登录状态
  387. this.$store.commit('toggleIsLogin', true);
  388. // 自己平台的用户基础信息
  389. uni.setStorageSync('userInfo', JSON.stringify(userInfo))
  390. // tim 返回的用户信息
  391. uni.setStorageSync('userTIMInfo', JSON.stringify(res.data));
  392. setTimeout(() => {
  393. uni.hideLoading()
  394. this.$u.route({
  395. url: '/pages/index/index'
  396. })
  397. }, 1000)
  398. }).catch(err => {
  399. uni.showToast({
  400. icon: 'none',
  401. title: '用户不存在',
  402. duration: 2000
  403. })
  404. })
  405. }).catch(err => {
  406. console.log(err)
  407. })
  408. }
  409. },
  410. onReady() {
  411. this.$refs.uForm.setRules(this.rules);
  412. }
  413. }
  414. </script>
  415. <style lang="scss" scoped>
  416. .login {
  417. &-logo {
  418. width: 260rpx;
  419. height: 260rpx;
  420. margin: 100rpx auto;
  421. }
  422. &-form {
  423. width: calc(100% - 200rpx);
  424. margin: 0 auto;
  425. }
  426. }
  427. </style>