login.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 (event: any) {
  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 (event: any) {
  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 (event: any) {
  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. this.toNextPage()
  202. } else {
  203. this.showToast(res.data.message)
  204. }
  205. }).catch((res) => {
  206. console.log(res)
  207. })
  208. },
  209. bindPhoneInput: function (event: any) {
  210. this.setData({
  211. inputPhoneNum: event.detail.value
  212. })
  213. },
  214. bindPwdLoginPhoneInput: function (event: any) {
  215. this.setData({
  216. inputPhonePwdLoginNum: event.detail.value
  217. })
  218. },
  219. bindVCodeInput: function (event: any) {
  220. this.setData({
  221. inputVCodeNum: event.detail.value
  222. })
  223. },
  224. bindPwdInput: function (event: any) {
  225. this.setData({
  226. inputPwdText: event.detail.value
  227. })
  228. },
  229. choseType: function (arr: any[]) {
  230. arr.forEach((item: any) => {
  231. switch (Number(item)) {
  232. case 1:
  233. //安装员
  234. this.setData({
  235. installerEnable: true
  236. })
  237. //如果是安装员,又从安装员进来的
  238. if (this.data.loginType == loginType.Installer) {
  239. this.toNextPage();
  240. } else {
  241. console.log("没有权限")
  242. wx.navigateBack()
  243. }
  244. break;
  245. case 2:
  246. //IT管理员
  247. this.setData({
  248. itEnable: true
  249. })
  250. if (this.data.loginType == loginType.Installer) {
  251. this.toNextPage();
  252. } else {
  253. console.log("没有权限")
  254. wx.navigateBack()
  255. }
  256. break;
  257. case 3:
  258. //维修员
  259. this.setData({
  260. repairmanEnable: true
  261. })
  262. if (this.data.loginType == loginType.Repairman) {
  263. this.toNextPage();
  264. } else {
  265. console.log("没有权限")
  266. wx.navigateBack()
  267. }
  268. break;
  269. }
  270. });
  271. },
  272. //登录成功后跳转界面
  273. toNextPage: function () {
  274. let toUrl = '';
  275. switch (Number(this.data.loginType)) {
  276. case loginType.Teacher:
  277. //教师(去教师的index页面)
  278. toUrl = "../teacher/index/index"
  279. break;
  280. case loginType.Installer:
  281. //安装员(去安装员的index页面)
  282. toUrl = "../installer/index/index"
  283. break;
  284. case loginType.Repairman:
  285. //维修员(去维修员的index页面)
  286. toUrl = "../repairman/index/index"
  287. break;
  288. case loginType.IT:
  289. //IT管理员(去IT管理员的index页面)
  290. toUrl = "../itadministrator/index/index"
  291. break;
  292. }
  293. wx.navigateTo({
  294. url: toUrl,
  295. })
  296. },
  297. showToast: function (message: string) {
  298. wx.showToast({
  299. title: message,
  300. icon: 'none'
  301. })
  302. },
  303. onUnload() {
  304. if (this.data.getvcodeInter != -5) {
  305. clearInterval(this.data.getvcodeInter)
  306. }
  307. }
  308. })