main.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var baseUrl = "http://twong.h/api/";
  2. function get(api, data, callback) {
  3. http.ajax({
  4. type: "GET",
  5. data: data,
  6. url: baseUrl + api,
  7. success: function(result) {
  8. callback(result);
  9. },
  10. error: function() {
  11. callback();
  12. }
  13. });
  14. }
  15. function post(api, data, callback) {
  16. http.ajax({
  17. type: "POST",
  18. data: JSON.stringify(data),
  19. url: baseUrl + api,
  20. success: function(result) {
  21. callback(result);
  22. },
  23. error: function() {
  24. callback();
  25. }
  26. });
  27. }
  28. function getQueryVariable(variable) {
  29. var query = window.location.search.substring(1);
  30. var vars = query.split("&");
  31. for (var i = 0; i < vars.length; i++) {
  32. var pair = vars[i].split("=");
  33. if (pair[0] == variable) {
  34. return pair[1];
  35. }
  36. }
  37. return false;
  38. }
  39. function getKey(callback) {
  40. get("verify_code", {}, function(result) {
  41. if (result.status === 200) {
  42. callback(result.data);
  43. }
  44. });
  45. }
  46. function show(text, callback) {
  47. var d = dialog({
  48. content: text,
  49. skin: "dialog-content"
  50. });
  51. d.show();
  52. setTimeout(function () {
  53. d.close().remove();
  54. if(callback != null) callback();
  55. }, 2000);
  56. }
  57. function verify(phone) {
  58. getKey(function(result) {
  59. var data = {
  60. code: "",
  61. key: result.key,
  62. phone: phone,
  63. type: "register"
  64. };
  65. post("register/verify", data, function(result) {
  66. if (result.status !== 200) {
  67. show(result.msg);
  68. } else {
  69. show("短信发送成功!");
  70. }
  71. });
  72. });
  73. }
  74. function register(phone, passwd, code) {
  75. console.log(phone, passwd, code);
  76. if (phone.trim() === "") {
  77. console.log("phone is null !");
  78. return;
  79. }
  80. var sp = getQueryVariable("sp");
  81. sp = sp === false ? null : sp;
  82. console.log(sp);
  83. var data = {
  84. account: phone,
  85. captcha: code,
  86. password: passwd,
  87. spread: sp
  88. };
  89. post("register", data, function(result) {
  90. console.log(result);
  91. if (result.status === 200) {
  92. show("注册成功!<br />正在跳转,请稍后...", function () {
  93. document.location.href = "http://localhost:8080/#/login";
  94. });
  95. } else {
  96. show(result.msg);
  97. }
  98. });
  99. }