account.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (function (app) {
  2. app.controller('wechatLoginCtrl', ["$scope","$timeout", "userService","$ionicNavBarDelegate", "storage", "$state", "msg", "$http", "util","$timeout",
  3. function ($scope,$timeout, userService,$ionicNavBarDelegate, storage, $state, msg, $http, util, $timeout) {
  4. //$ionicNavBarDelegate.showBackButton(false);
  5. $scope.loginbywx = function(){
  6. var scope = "snsapi_userinfo", state = "_" + (+new Date());
  7. Wechat.auth(scope, state, function (response) {
  8. alert("response:"+JSON.stringify(response.code));
  9. //response.code;
  10. if(response.code){
  11. userService.loginbywx(response.code).then(function(result){
  12. alert("loginbywx success: " + JSON.stringify(result));
  13. msg.hide();
  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. $scope.user=storage.getObject('user');
  18. $state.go('app.home');
  19. },function(error){
  20. alert("loginbywx Failed: " + JSON.stringify(error));
  21. $state.go("loginAccount",{wechat:$scope.wecaht});
  22. });
  23. }
  24. }, function (error) {
  25. console("Failed: " + JSON.stringify(error));
  26. });
  27. //$state.go("loginAccount");
  28. };
  29. }]);
  30. app.controller('loginCtrl', ["$scope", "userService","$stateParams","$ionicNavBarDelegate", "storage", "$state", "msg", "$http", "util","$timeout",
  31. function ($scope, userService,$stateParams,$ionicNavBarDelegate, storage, $state, msg, $http, util, $timeout) {
  32. //$ionicNavBarDelegate.showBackButton(false);
  33. $scope.wecaht = $stateParams.wechat;
  34. $scope.vm = {
  35. mobile: '',
  36. verify_code: '',
  37. waitSeconds: "获取验证码"
  38. };
  39. $scope.login = function () {
  40. msg.loading('登录中...');
  41. userService.login($scope.vm.mobile, $scope.vm.verify_code,$scope.wechat).then(function (result) {
  42. //userService.login($scope.vm.mobile, $scope.vm.verify_code,124).then(function (result) {
  43. msg.hide();
  44. storage.setObject('user', result.data.data.user);
  45. storage.set('token', result.data.data.token);
  46. $http.defaults.headers.common["Authorization"] = 'Bearer ' + result.data.data.token;
  47. $scope.user=storage.getObject('user');
  48. $state.go('app.home');
  49. }, function (error) {
  50. // msg.hide();
  51. // msg.error(error.data.message);
  52. });
  53. };
  54. //获取验证码
  55. $scope.getVerifyCode = function () {
  56. // if (!util.isMobile($scope.vm.mobile)) {
  57. // msg.text('请输入正确的手机号');
  58. // return;
  59. // }
  60. if ($scope.vm.waitSeconds!="获取验证码") {
  61. msg.text('请稍后再试', true);
  62. return;
  63. }
  64. $scope.vm.waitSeconds = "正在发送";
  65. userService.getVerifyCode($scope.vm.mobile).then(function (result) {
  66. wait(50);
  67. // $scope.returncode = result.data;
  68. if(result.data.data.verify_code)msg.success(result.data.data.verify_code);
  69. }, function (error) {
  70. $scope.vm.waitSeconds = "获取验证码";
  71. wait(0);
  72. // msg.error(error.data.message);
  73. });
  74. };
  75. var wait = function (seconds) {
  76. if (seconds > 0) {
  77. $scope.vm.waitSeconds = "" + seconds + "秒";
  78. } else {
  79. $scope.vm.waitSeconds = "获取验证码";
  80. }
  81. $timeout(function () {
  82. if (seconds >= 1)
  83. wait(seconds - 1);
  84. }, 1000);
  85. };
  86. $scope.clear = function(){
  87. document.getElementById("tel").value = "";
  88. }
  89. }]);
  90. })(angular.module('app.controllers'));