account.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. (function (app) {
  2. app.controller('loginCtrl', ["$scope", "userService", "storage", "$state", "msg", "$http", "util","$timeout",
  3. function ($scope, userService, storage, $state, msg, $http, util, $timeout) {
  4. $scope.vm = {
  5. mobile: '',
  6. verify_code: '',
  7. waitSeconds: "获取验证码"
  8. };
  9. $scope.login = function () {
  10. msg.loading('登录中...');
  11. userService.login($scope.vm.mobile, $scope.vm.verify_code).then(function (result) {
  12. msg.hide();
  13. debugger;
  14. storage.setObject('user', result.data.data.user);
  15. storage.set('token', result.data.data.token);
  16. $http.defaults.headers.common["Authorization"] = 'Bearer ' + result.data.data.token;
  17. $state.go('app.home');
  18. }, function (erro) {
  19. msg.hide();
  20. msg.error(erro.data.message);
  21. });
  22. }
  23. //获取验证码
  24. $scope.getVerifyCode = function () {
  25. if (!util.isMobile($scope.vm.mobile)) {
  26. msg.text('请输入正确的手机号');
  27. return;
  28. }
  29. $scope.vm.waitSeconds = "正在发送";
  30. userService.getVerifyCode($scope.vm.mobile).then(function (result) {
  31. wait(50);
  32. $scope.returncode = result.data;
  33. }, function (error) {
  34. $scope.vm.waitSeconds = "获取验证码";
  35. wait(0);
  36. msg.error(error.data.Message);
  37. });
  38. };
  39. var wait = function (seconds) {
  40. if (seconds > 0) {
  41. $scope.vm.waitSeconds = "" + seconds + "秒";
  42. } else {
  43. $scope.vm.waitSeconds = "获取验证码";
  44. }
  45. $timeout(function () {
  46. if (seconds >= 1)
  47. wait(seconds - 1);
  48. }, 1000);
  49. };
  50. }]);
  51. })(angular.module('app.controllers'));