login.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // index.ts
  2. import { ConstsData } from "../../utils/const";
  3. import { loginType } from "../../utils/loginType"
  4. import { httpUtil } from "../../utils/restful";
  5. import { storage } from "../../utils/storageUtil";
  6. Page({
  7. data: {
  8. login_check_tab: 0,
  9. loginType: 0,
  10. showAccPwd: true,
  11. showUpdatePassWordLayout: false,
  12. //短信验证码登录参数
  13. inputPhoneNum: '',
  14. inputVCodeNum: '',
  15. inputPhonePwdLoginNum: '',
  16. inputPwdText: '',
  17. hasGetVCode: false,
  18. getVCodeText: "获取验证码",
  19. getvcodeInter: -5,
  20. teacherEnable: false,
  21. installerEnable: false,
  22. repairmanEnable: false,
  23. itEnable: false
  24. },
  25. onLoad(opention: any) {
  26. console.log("opention.loginType:", opention.loginType)
  27. this.setData({
  28. login_check_tab: 0,
  29. loginType: opention.loginType
  30. })
  31. },
  32. // 点击短信验证码登录
  33. clickPhoneNumLogin: function (event: any) {
  34. console.log("短信验证码登录")
  35. console.log(event)
  36. this.setData({
  37. login_check_tab: 0,
  38. // inputPhoneNum: '',
  39. // inputPhonePwdLoginNum: ''
  40. })
  41. },
  42. // 点击账号密码登录
  43. clicAccountPasswordLogin: function (event: any) {
  44. console.log("账号密码登录")
  45. console.log(event)
  46. this.setData({
  47. login_check_tab: 1,
  48. // inputPhoneNum: '',
  49. // inputPhonePwdLoginNum: ''
  50. })
  51. },
  52. // 获取验证码
  53. clickGetVCode: function () {
  54. console.log("账号密码登录")
  55. if (this.data.hasGetVCode) {
  56. this.showToast("请勿频繁点击")
  57. return;
  58. }
  59. if (!this.data.inputPhoneNum) {
  60. this.showToast("请输入手机号")
  61. return;
  62. }
  63. let that = this;
  64. let time = 60;
  65. this.data.getvcodeInter = setInterval(function () {
  66. if (time <= 1) {
  67. clearInterval(that.data.getvcodeInter)
  68. that.setData({
  69. hasGetVCode: false,
  70. getVCodeText: '获取验证码',
  71. getvcodeInter: -5
  72. })
  73. return;
  74. }
  75. time--;
  76. that.setData({
  77. getVCodeText: time.toString() + "秒",
  78. hasGetVCode: true
  79. })
  80. }, 1000)
  81. let params = {
  82. mobileNo: this.data.inputPhoneNum
  83. }
  84. httpUtil.wxGet(httpUtil.interfaces.getVerifyCode, params).then((res) => {
  85. console.log("获取验证码成功:", res)
  86. }).catch((res) => {
  87. console.log("获取验证码失败:", res)
  88. })
  89. },
  90. //账号密码登录——---------------------------
  91. showAccountPwd: function () {
  92. if (this.data.showAccPwd) {
  93. this.setData({
  94. showAccPwd: false
  95. })
  96. } else {
  97. this.setData({
  98. showAccPwd: true
  99. })
  100. }
  101. },
  102. //显示修改密码的框
  103. showUpdatePwdLayout: function () {
  104. if (this.data.showUpdatePassWordLayout) {
  105. this.setData({
  106. showUpdatePassWordLayout: false
  107. })
  108. } else {
  109. this.setData({
  110. showUpdatePassWordLayout: true
  111. })
  112. }
  113. },
  114. //跳转注册界面
  115. toRegisterPage: function () {
  116. wx.navigateTo({
  117. url: '../register/register',
  118. })
  119. },
  120. //判断是短信登录还是账号密码登录进行相应登录
  121. toTabIndex: function () {
  122. switch (Number(this.data.login_check_tab)) {
  123. case 0:
  124. //短信验证码登录
  125. this.phoneVCodeLogin();
  126. break;
  127. case 1:
  128. //账号密码登录
  129. this.pwdPhoneLogin();
  130. break;
  131. }
  132. // this.toNextPage();
  133. },
  134. //短信验证码登录
  135. phoneVCodeLogin: function () {
  136. if (!this.data.inputPhoneNum) {
  137. this.showToast('请输入手机号')
  138. return;
  139. }
  140. if (!this.data.inputVCodeNum) {
  141. this.showToast('请输入验证码')
  142. }
  143. let params = {
  144. mobile: this.data.inputPhoneNum,
  145. verifyCode: this.data.inputVCodeNum
  146. }
  147. httpUtil.wxPost(httpUtil.interfaces.phoneRegister, params).then((res: any) => {
  148. if (res.data.success) {
  149. console.log("登录成功:", res)
  150. httpUtil.httpData.userId = res.data.data.id;
  151. ConstsData.AppData.myInfoData = res.data.data
  152. storage.saveStorage(storage.storageKey.userId, res.data.data.id).then((res) => {
  153. console.log("保存成功:", res)
  154. }).catch((res) => {
  155. console.log("保存失败:", res)
  156. })
  157. if (ConstsData.AppData.myInfoData.type == 3) {
  158. this.setData({
  159. teacherEnable: true
  160. })
  161. //是老师,并且点击进入的也是老师
  162. if (Number(this.data.loginType) == loginType.Teacher) {
  163. this.toNextPage()
  164. }
  165. } else {
  166. if (!res.data.data.role) {
  167. let arr = res.data.data.role.split(",")
  168. this.choseType(arr)
  169. }
  170. }
  171. } else {
  172. console.log("登录失败:", res)
  173. this.showToast(res.data.message)
  174. }
  175. }).catch((res) => {
  176. console.log("登录失败:", res)
  177. })
  178. },
  179. //账号密码登录
  180. pwdPhoneLogin: function () {
  181. if (!this.data.inputPhonePwdLoginNum) {
  182. this.showToast('请输入手机号')
  183. return;
  184. }
  185. if (!this.data.inputPwdText) {
  186. this.showToast('请输入密码')
  187. }
  188. let params = {
  189. password: this.data.inputPwdText,
  190. userName: this.data.inputPhonePwdLoginNum
  191. }
  192. httpUtil.wxPost(httpUtil.interfaces.pwdPhoneLogin, params).then((res: any) => {
  193. if (res.data.success) {
  194. httpUtil.httpData.userId = res.data.data.id
  195. ConstsData.AppData.myInfoData = res.data.data
  196. storage.saveStorage(storage.storageKey.userId, res.data.data.id).then((res) => {
  197. console.log("保存成功:", res)
  198. }).catch((res) => {
  199. console.log("保存失败:", res)
  200. })
  201. if (Number(ConstsData.AppData.myInfoData.type) == 3) {
  202. //走了3里面
  203. this.setData({
  204. teacherEnable: true
  205. })
  206. //是老师,并且点击进入的也是老师
  207. if (Number(this.data.loginType) == loginType.Teacher) {
  208. this.toNextPage()
  209. } else {
  210. this.goBack()
  211. }
  212. } else {
  213. console.log("走了不是3")
  214. if (!res.data.data.role) {
  215. let arr = res.data.data.role.split(",")
  216. this.choseType(arr)
  217. } else {
  218. this.goBack()
  219. wx.navigateBack()
  220. }
  221. }
  222. } else {
  223. this.showToast(res.data.message)
  224. }
  225. }).catch((res) => {
  226. console.log(res)
  227. })
  228. },
  229. bindPhoneInput: function (event: any) {
  230. this.setData({
  231. inputPhoneNum: event.detail.value
  232. })
  233. },
  234. bindPwdLoginPhoneInput: function (event: any) {
  235. this.setData({
  236. inputPhonePwdLoginNum: event.detail.value
  237. })
  238. },
  239. bindVCodeInput: function (event: any) {
  240. this.setData({
  241. inputVCodeNum: event.detail.value
  242. })
  243. },
  244. bindPwdInput: function (event: any) {
  245. this.setData({
  246. inputPwdText: event.detail.value
  247. })
  248. },
  249. choseType: function (arr: any[]) {
  250. arr.forEach((item: any) => {
  251. switch (Number(item)) {
  252. case 1:
  253. //安装员
  254. this.setData({
  255. installerEnable: true
  256. })
  257. //如果是安装员,又从安装员进来的
  258. if (this.data.loginType == loginType.Installer) {
  259. this.toNextPage();
  260. } else {
  261. this.goBack()
  262. }
  263. break;
  264. case 2:
  265. //IT管理员
  266. this.setData({
  267. itEnable: true
  268. })
  269. if (this.data.loginType == loginType.Installer) {
  270. this.toNextPage();
  271. } else {
  272. this.goBack()
  273. }
  274. break;
  275. case 3:
  276. //维修员
  277. this.setData({
  278. repairmanEnable: true
  279. })
  280. if (this.data.loginType == loginType.Repairman) {
  281. this.toNextPage();
  282. } else {
  283. this.goBack()
  284. }
  285. break;
  286. }
  287. });
  288. },
  289. //登录成功后跳转界面
  290. toNextPage: function () {
  291. let toUrl = '';
  292. switch (Number(this.data.loginType)) {
  293. case loginType.Teacher:
  294. //教师(去教师的index页面)
  295. toUrl = "../teacher/index/index"
  296. break;
  297. case loginType.Installer:
  298. //安装员(去安装员的index页面)
  299. toUrl = "../installer/index/index"
  300. break;
  301. case loginType.Repairman:
  302. //维修员(去维修员的index页面)
  303. toUrl = "../repairman/index/index"
  304. break;
  305. case loginType.IT:
  306. //IT管理员(去IT管理员的index页面)
  307. toUrl = "../itadministrator/index/index"
  308. break;
  309. }
  310. wx.navigateTo({
  311. url: toUrl,
  312. })
  313. },
  314. goBack: function () {
  315. wx.showToast({
  316. title: '身份选择错误',
  317. icon: 'none',
  318. duration: 1000
  319. })
  320. setTimeout(function () {
  321. wx.navigateBack()
  322. }, 1000)
  323. },
  324. showToast: function (message: string) {
  325. wx.showToast({
  326. title: message,
  327. icon: 'none'
  328. })
  329. },
  330. onUnload() {
  331. if (this.data.getvcodeInter != -5) {
  332. clearInterval(this.data.getvcodeInter)
  333. }
  334. }
  335. })