login.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * 登录
  3. **/
  4. (function($, owner) {
  5. /**
  6. * 用户登录
  7. **/
  8. owner.login = function(loginInfo, callback) {
  9. callback = callback || $.noop;
  10. loginInfo = loginInfo || {};
  11. loginInfo.account = loginInfo.account || '';
  12. loginInfo.password = loginInfo.password || '';
  13. if (loginInfo.account.length < 3) {
  14. return callback('账号最短为 3 个字符');
  15. }
  16. if (loginInfo.password.length < 4) {
  17. return callback('密码最短为 4 个字符');
  18. }
  19. $.ajax('http://v2.xinnongbaohe.com/web/application/passport/?login.action?json=1',
  20. {
  21. data:
  22. {
  23. username:loginInfo.account,
  24. password:loginInfo.password,
  25. project:'front'
  26. },
  27. dataType:'text',//服务器返回json格式数据
  28. timeout:10000,//超时时间设置为10秒;
  29. type:'post',//HTTP请求类型
  30. success:function(r)
  31. {
  32. var t = JSON.parse(r);
  33. if(t.status == 1)
  34. {
  35. return owner.createState(t.account, callback);
  36. }
  37. else
  38. {
  39. return callback('用户名或密码错误');
  40. }
  41. },
  42. error:function(xhr,type,errorThrown)
  43. {
  44. alert(type);
  45. }
  46. });
  47. };
  48. owner.createState = function(name, callback) {
  49. var state = owner.getState();
  50. state.account = name;
  51. state.token = "tokenv2123456789";
  52. owner.setState(state);
  53. return callback();
  54. };
  55. /**
  56. * 新用户注册
  57. **/
  58. owner.reg = function(regInfo, callback) {
  59. callback = callback || $.noop;
  60. regInfo = regInfo || {};
  61. regInfo.account = regInfo.account || '';
  62. regInfo.password = regInfo.password || '';
  63. if (regInfo.account.length < 5) {
  64. return callback('用户名最短需要 5 个字符');
  65. }
  66. if (regInfo.password.length < 6) {
  67. return callback('密码最短需要 6 个字符');
  68. }
  69. if (!checkEmail(regInfo.email)) {
  70. return callback('邮箱地址不合法');
  71. }
  72. var users = JSON.parse(localStorage.getItem('$usersv2') || '[]');
  73. users.push(regInfo);
  74. localStorage.setItem('$usersv2', JSON.stringify(users));
  75. return callback();
  76. };
  77. /**
  78. * 获取当前状态
  79. **/
  80. owner.getState = function() {
  81. var stateText = localStorage.getItem('$statev5') || "{}";
  82. return JSON.parse(stateText);
  83. };
  84. /**
  85. * 设置当前状态
  86. **/
  87. owner.setState = function(state) {
  88. state = state || {};
  89. localStorage.setItem('$statev5', JSON.stringify(state));
  90. //var settings = owner.getSettings();
  91. //settings.gestures = '';
  92. //owner.setSettings(settings);
  93. };
  94. var checkEmail = function(email) {
  95. email = email || '';
  96. return (email.length > 3 && email.indexOf('@') > -1);
  97. };
  98. /**
  99. * 找回密码
  100. **/
  101. owner.forgetPassword = function(email, callback) {
  102. callback = callback || $.noop;
  103. if (!checkEmail(email)) {
  104. return callback('邮箱地址不合法');
  105. }
  106. return callback(null, '新的随机密码已经发送到您的邮箱,请查收邮件。');
  107. };
  108. /**
  109. * 获取应用本地配置
  110. **/
  111. owner.setSettings = function(settings) {
  112. settings = settings || {};
  113. localStorage.setItem('$settings', JSON.stringify(settings));
  114. }
  115. /**
  116. * 设置应用本地配置
  117. **/
  118. owner.getSettings = function() {
  119. var settingsText = localStorage.getItem('$settings') || "{}";
  120. return JSON.parse(settingsText);
  121. }
  122. /**
  123. * 获取本地是否安装客户端
  124. **/
  125. owner.isInstalled = function(id) {
  126. if (id === 'qihoo' && mui.os.plus) {
  127. return true;
  128. }
  129. if (mui.os.android) {
  130. var main = plus.android.runtimeMainActivity();
  131. var packageManager = main.getPackageManager();
  132. var PackageManager = plus.android.importClass(packageManager)
  133. var packageName = {
  134. "qq": "com.tencent.mobileqq",
  135. "weixin": "com.tencent.mm",
  136. "sinaweibo": "com.sina.weibo"
  137. }
  138. try {
  139. return packageManager.getPackageInfo(packageName[id], PackageManager.GET_ACTIVITIES);
  140. } catch (e) {}
  141. } else {
  142. switch (id) {
  143. case "qq":
  144. var TencentOAuth = plus.ios.import("TencentOAuth");
  145. return TencentOAuth.iphoneQQInstalled();
  146. case "weixin":
  147. var WXApi = plus.ios.import("WXApi");
  148. return WXApi.isWXAppInstalled()
  149. case "sinaweibo":
  150. var SinaAPI = plus.ios.import("WeiboSDK");
  151. return SinaAPI.isWeiboAppInstalled()
  152. default:
  153. break;
  154. }
  155. }
  156. }
  157. }(mui, window.app = {}));