login.ts 6.6 KB

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