login.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // index.ts
  2. import { ConstsData } from "../../utils/const";
  3. import { loginType } from "../../utils/loginType"
  4. import { httpUtil } from "../../utils/restful";
  5. Page({
  6. data: {
  7. login_check_tab: 0,
  8. loginType: 0,
  9. showAccPwd: true,
  10. showUpdatePassWordLayout: false,
  11. //短信验证码登录参数
  12. inputPhoneNum: '',
  13. inputVCodeNum: '',
  14. inputPhonePwdLoginNum: '',
  15. inputPwdText: '',
  16. hasGetVCode: false,
  17. getVCodeText: "获取验证码",
  18. getvcodeInter: -5
  19. },
  20. onLoad(opention: any) {
  21. console.log("opention.loginType:", opention.loginType)
  22. this.setData({
  23. login_check_tab: 0,
  24. loginType: opention.loginType
  25. })
  26. },
  27. // 点击短信验证码登录
  28. clickPhoneNumLogin: function (event: any) {
  29. console.log("短信验证码登录")
  30. console.log(event)
  31. this.setData({
  32. login_check_tab: 0,
  33. // inputPhoneNum: '',
  34. // inputPhonePwdLoginNum: ''
  35. })
  36. },
  37. // 点击账号密码登录
  38. clicAccountPasswordLogin: function (event: any) {
  39. console.log("账号密码登录")
  40. console.log(event)
  41. this.setData({
  42. login_check_tab: 1,
  43. // inputPhoneNum: '',
  44. // inputPhonePwdLoginNum: ''
  45. })
  46. },
  47. // 获取验证码
  48. clickGetVCode: function (event: any) {
  49. console.log("账号密码登录")
  50. if (this.data.hasGetVCode) {
  51. this.showToast("请勿频繁点击")
  52. return;
  53. }
  54. if (!this.data.inputPhoneNum) {
  55. this.showToast("请输入手机号")
  56. return;
  57. }
  58. let that = this;
  59. let time = 60;
  60. this.data.getvcodeInter = setInterval(function () {
  61. if (time <= 1) {
  62. clearInterval(that.data.getvcodeInter)
  63. that.setData({
  64. hasGetVCode: false,
  65. getVCodeText: '获取验证码',
  66. getvcodeInter: -5
  67. })
  68. return;
  69. }
  70. time--;
  71. that.setData({
  72. getVCodeText: time.toString(),
  73. hasGetVCode: true
  74. })
  75. }, 1000)
  76. let params = {
  77. mobileNo: this.data.inputPhoneNum
  78. }
  79. httpUtil.wxGet(httpUtil.interfaces.getVerifyCode, params).then((res) => {
  80. console.log("获取验证码成功:", res)
  81. }).catch((res) => {
  82. console.log("获取验证码失败:", res)
  83. })
  84. },
  85. //账号密码登录——---------------------------
  86. showAccountPwd: function (event: any) {
  87. if (this.data.showAccPwd) {
  88. this.setData({
  89. showAccPwd: false
  90. })
  91. } else {
  92. this.setData({
  93. showAccPwd: true
  94. })
  95. }
  96. },
  97. //显示修改密码的框
  98. showUpdatePwdLayout: function (event: any) {
  99. if (this.data.showUpdatePassWordLayout) {
  100. this.setData({
  101. showUpdatePassWordLayout: false
  102. })
  103. } else {
  104. this.setData({
  105. showUpdatePassWordLayout: true
  106. })
  107. }
  108. },
  109. //跳转注册界面
  110. toRegisterPage: function () {
  111. wx.navigateTo({
  112. url: '../register/register',
  113. })
  114. },
  115. //判断是短信登录还是账号密码登录进行相应登录
  116. toTabIndex: function () {
  117. switch (Number(this.data.login_check_tab)) {
  118. case 0:
  119. //短信验证码登录
  120. this.phoneVCodeLogin();
  121. break;
  122. case 1:
  123. //账号密码登录
  124. this.pwdPhoneLogin();
  125. break;
  126. }
  127. // this.toNextPage();
  128. },
  129. //短信验证码登录
  130. phoneVCodeLogin: function () {
  131. if (!this.data.inputPhoneNum) {
  132. this.showToast('请输入手机号')
  133. return;
  134. }
  135. if (!this.data.inputVCodeNum) {
  136. this.showToast('请输入验证码')
  137. }
  138. let params = {
  139. mobile: this.data.inputPhoneNum,
  140. verifyCode: this.data.inputVCodeNum
  141. }
  142. httpUtil.wxPost(httpUtil.interfaces.phoneRegister, params).then((res: any) => {
  143. if (res.data.success) {
  144. console.log("登录成功:", res)
  145. httpUtil.httpData.userId = res.data.data.id;
  146. ConstsData.AppData.myInfoData = res.data.data
  147. this.toNextPage();
  148. } else {
  149. console.log("登录失败:", res)
  150. this.showToast(res.data.message)
  151. }
  152. }).catch((res) => {
  153. console.log("登录失败:", res)
  154. })
  155. },
  156. //账号密码登录
  157. pwdPhoneLogin: function () {
  158. if (!this.data.inputPhonePwdLoginNum) {
  159. this.showToast('请输入手机号')
  160. return;
  161. }
  162. if (!this.data.inputPwdText) {
  163. this.showToast('请输入密码')
  164. }
  165. let params = {
  166. password: this.data.inputPwdText,
  167. userName: this.data.inputPhonePwdLoginNum
  168. }
  169. httpUtil.wxPost(httpUtil.interfaces.pwdPhoneLogin, params).then((res: any) => {
  170. console.log("res:", res.data)
  171. if (res.data.success) {
  172. console.log("res.data.data:", res.data.data.id)
  173. httpUtil.httpData.userId = res.data.data.id
  174. ConstsData.AppData.myInfoData = res.data.data
  175. this.toNextPage()
  176. } else {
  177. this.showToast(res.data.message)
  178. }
  179. }).catch((res) => {
  180. console.log(res)
  181. })
  182. },
  183. bindPhoneInput: function (event: any) {
  184. this.setData({
  185. inputPhoneNum: event.detail.value
  186. })
  187. },
  188. bindPwdLoginPhoneInput: function (event: any) {
  189. this.setData({
  190. inputPhonePwdLoginNum: event.detail.value
  191. })
  192. },
  193. bindVCodeInput: function (event: any) {
  194. this.setData({
  195. inputVCodeNum: event.detail.value
  196. })
  197. },
  198. bindPwdInput: function (event: any) {
  199. this.setData({
  200. inputPwdText: event.detail.value
  201. })
  202. },
  203. //登录成功后跳转界面
  204. toNextPage: function () {
  205. let toUrl = '';
  206. switch (Number(this.data.loginType)) {
  207. case loginType.Teacher:
  208. //教师(去教师的index页面)
  209. toUrl = "../teacher/index/index"
  210. break;
  211. case loginType.Installer:
  212. //安装员(去安装员的index页面)
  213. toUrl = "../installer/index/index"
  214. break;
  215. case loginType.Repairman:
  216. //维修员(去维修员的index页面)
  217. toUrl = "../repairman/index/index"
  218. break;
  219. case loginType.IT:
  220. //IT管理员(去IT管理员的index页面)
  221. toUrl = "../itadministrator/index/index"
  222. break;
  223. }
  224. wx.navigateTo({
  225. url: toUrl,
  226. })
  227. },
  228. showToast: function (message: string) {
  229. wx.showToast({
  230. title: message,
  231. icon: 'none'
  232. })
  233. },
  234. onUnload() {
  235. if (this.data.getvcodeInter != -5) {
  236. clearInterval(this.data.getvcodeInter)
  237. }
  238. }
  239. })