account.js 1.9 KB

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