main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. $(function () {
  2. // 打开登录框
  3. $('.login_btn').click(function () {
  4. $('.login_form_con').show();
  5. })
  6. // 点击关闭按钮关闭登录框或者注册框
  7. $('.shutoff').click(function () {
  8. $(this).closest('form').hide();
  9. })
  10. // 隐藏错误
  11. $(".login_form #mobile").focus(function () {
  12. $("#login-mobile-err").hide();
  13. });
  14. $(".login_form #password").focus(function () {
  15. $("#login-password-err").hide();
  16. });
  17. $(".register_form #mobile").focus(function () {
  18. $("#register-mobile-err").hide();
  19. });
  20. $(".register_form #imagecode").focus(function () {
  21. $("#register-image-code-err").hide();
  22. });
  23. $(".register_form #smscode").focus(function () {
  24. $("#register-sms-code-err").hide();
  25. });
  26. $(".register_form #password").focus(function () {
  27. $("#register-password-err").hide();
  28. });
  29. // 点击输入框,提示文字上移(*)
  30. $('.form_group').on('click', function () {
  31. $(this).children('input').focus()
  32. })
  33. $('.form_group input').on('focusin', function () {
  34. $(this).siblings('.input_tip').animate({'top': -5, 'font-size': 12}, 'fast')
  35. $(this).parent().addClass('hotline');
  36. })
  37. // 输入框失去焦点,如果输入框为空,则提示文字下移
  38. $('.form_group input').on('blur focusout', function () {
  39. $(this).parent().removeClass('hotline');
  40. var val = $(this).val();
  41. if (val == '') {
  42. $(this).siblings('.input_tip').animate({'top': 22, 'font-size': 14}, 'fast');
  43. }
  44. })
  45. // 打开注册框
  46. $('.register_btn').click(function () {
  47. $('.register_form_con').show();
  48. generateImageCode()
  49. })
  50. // 登录框和注册框切换
  51. $('.to_register').click(function () {
  52. $('.login_form_con').hide();
  53. $('.register_form_con').show();
  54. generateImageCode()
  55. })
  56. // 登录框和注册框切换
  57. $('.to_login').click(function () {
  58. $('.login_form_con').show();
  59. $('.register_form_con').hide();
  60. })
  61. // 根据地址栏的hash值来显示用户中心对应的菜单
  62. var sHash = window.location.hash;
  63. if (sHash != '') {
  64. var sId = sHash.substring(1);
  65. var oNow = $('.' + sId);
  66. var iNowIndex = oNow.index();
  67. $('.option_list li').eq(iNowIndex).addClass('active').siblings().removeClass('active');
  68. oNow.show().siblings().hide();
  69. }
  70. // 用户中心菜单切换
  71. var $li = $('.option_list li');
  72. var $frame = $('#main_frame');
  73. $li.click(function () {
  74. if ($(this).index() == 5) {
  75. $('#main_frame').css({'height': 900});
  76. }
  77. else {
  78. $('#main_frame').css({'height': 660});
  79. }
  80. $(this).addClass('active').siblings().removeClass('active');
  81. $(this).find('a')[0].click()
  82. })
  83. // TODO 登录表单提交
  84. $(".login_form_con").submit(function (e) {
  85. e.preventDefault()
  86. var mobile = $(".login_form #mobile").val()
  87. var password = $(".login_form #password").val()
  88. if (!mobile) {
  89. $("#login-mobile-err").show();
  90. return;
  91. }
  92. if (!password) {
  93. $("#login-password-err").show();
  94. return;
  95. }
  96. // 发起登录请求
  97. var params = {
  98. "mobile": mobile,
  99. "password": password,
  100. }
  101. $.ajax({
  102. url: "/passport/login",
  103. method: "post",
  104. data: JSON.stringify(params),
  105. contentType: "application/json",
  106. headers:{
  107. "X-CSRFToken":getCookie("csrf_token")
  108. },
  109. success: function (resp) {
  110. if (resp.errno == "0") {
  111. // 刷新当前界面
  112. location.reload();
  113. } else {
  114. $("#login-password-err").html(resp.errmsg)
  115. $("#login-password-err").show()
  116. }
  117. }
  118. })
  119. })
  120. // TODO 注册按钮点击
  121. $(".register_form_con").submit(function (e) {
  122. // 阻止默认提交操作
  123. e.preventDefault()
  124. // 取到用户输入的内容
  125. var mobile = $("#register_mobile").val()
  126. var smscode = $("#smscode").val()
  127. var password = $("#register_password").val()
  128. if (!mobile) {
  129. $("#register-mobile-err").show();
  130. return;
  131. }
  132. if (!smscode) {
  133. $("#register-sms-code-err").show();
  134. return;
  135. }
  136. if (!password) {
  137. $("#register-password-err").html("请填写密码!");
  138. $("#register-password-err").show();
  139. return;
  140. }
  141. if (password.length < 6) {
  142. $("#register-password-err").html("密码长度不能少于6位");
  143. $("#register-password-err").show();
  144. return;
  145. }
  146. // 发起注册请求
  147. var params = {
  148. "mobile": mobile,
  149. "smscode": smscode,
  150. "password": password,
  151. }
  152. $.ajax({
  153. url: "/passport/register",
  154. type: "post",
  155. data: JSON.stringify(params),
  156. contentType: "application/json",
  157. headers:{
  158. "X-CSRFToken":getCookie("csrf_token")
  159. },
  160. success: function (resp) {
  161. if (resp.errno == "0") {
  162. // 刷新当前界面
  163. location.reload()
  164. } else {
  165. $("#register-password-err").html(resp.errmsg)
  166. $("#register-password-err").show()
  167. }
  168. }
  169. })
  170. })
  171. });
  172. //TODO 退出登录
  173. function logout() {
  174. $.ajax({
  175. //设置url
  176. url: "/passport/logout",
  177. type: "post",
  178. headers: {
  179. "X-CSRFToken": getCookie("csrf_token")
  180. },
  181. success: function (resp) {
  182. if (resp.errno == "0") {
  183. location.reload()
  184. } else {
  185. }
  186. }
  187. })
  188. }
  189. var imageCodeId = ""
  190. // TODO 生成一个图片验证码的编号,并设置页面中图片验证码img标签的src属性
  191. function generateImageCode() {
  192. // 1.生成一个编号
  193. // 严谨则使用uuid保证编号唯一,不严谨则可使用时间戳
  194. imageCodeId = generateUUID()
  195. // 2.拼接验证码地址
  196. var imageCodeUrl = "/passport/image_code?code_id=" + imageCodeId
  197. // 3.设置页面中图片验证码img标签的src属性
  198. $(".get_pic_code").attr("src", imageCodeUrl)
  199. }
  200. // 发送短信验证码
  201. function sendSMSCode() {
  202. // 校验参数,保证输入框有数据填写
  203. $(".get_code").removeAttr("onclick");
  204. var mobile = $("#register_mobile").val();
  205. if (!mobile) {
  206. $("#register-mobile-err").html("请填写正确的手机号!");
  207. $("#register-mobile-err").show();
  208. $(".get_code").attr("onclick", "sendSMSCode();");
  209. return;
  210. }
  211. var imageCode = $("#imagecode").val();
  212. if (!imageCode) {
  213. $("#image-code-err").html("请填写验证码!");
  214. $("#image-code-err").show();
  215. $(".get_code").attr("onclick", "sendSMSCode();");
  216. return;
  217. }
  218. // TODO 发送短信验证码
  219. var params = {
  220. "mobile": mobile,
  221. "image_code": imageCode,
  222. "image_code_id": imageCodeId
  223. }
  224. $.ajax({
  225. // 请求地址
  226. url: "/passport/smscode",
  227. // 请求方式
  228. method: "POST",
  229. // 请求内容
  230. data: JSON.stringify(params),
  231. // 请求内容的数据类型
  232. contentType: "application/json",
  233. // 响应数据的格式
  234. dataType: "json",
  235. headers:{
  236. "X-CSRFToken":getCookie("csrf_token")
  237. },
  238. success: function (resp) {
  239. if (resp.errno == "0") {
  240. // 倒计时60秒,60秒后允许用户再次点击发送短信验证码的按钮
  241. var num = 60;
  242. // 设置一个计时器
  243. var t = setInterval(function () {
  244. if (num === 1) {
  245. // 如果计时器到最后,清除计时器对象
  246. clearInterval(t)
  247. // 将点击获取验证码的按钮展示的文本回复成原始文本
  248. $(".get_code").html("获取验证码")
  249. // 将点击按钮的onclick事件函数恢复回去
  250. $(".get_code").attr("onclick", "sendSMSCode();");
  251. } else {
  252. num -= 1;
  253. // 展示倒计时信息
  254. $(".get_code").html(num + "秒")
  255. }
  256. }, 1000)
  257. } else {
  258. // 表示后端出现了错误,可以将错误信息展示到前端页面中
  259. $("#register-sms-code-err").html(resp.errmsg);
  260. $("#register-sms-code-err").show();
  261. // 将点击按钮的onclick事件函数恢复回去
  262. $(".get_code").attr("onclick", "sendSMSCode();")
  263. // 如果错误码是4004,代表验证码错误,重新生成验证码
  264. if (resp.errno == "4004") {
  265. generateImageCode()
  266. }
  267. }
  268. },
  269. })
  270. }
  271. // 调用该函数模拟点击左侧按钮
  272. function fnChangeMenu(n) {
  273. var $li = $('.option_list li');
  274. if (n >= 0) {
  275. $li.eq(n).addClass('active').siblings().removeClass('active');
  276. // 执行 a 标签的点击事件
  277. $li.eq(n).find('a')[0].click()
  278. }
  279. }
  280. // 一般页面的iframe的高度是660
  281. // 新闻发布页面iframe的高度是900
  282. function fnSetIframeHeight(num) {
  283. var $frame = $('#main_frame');
  284. $frame.css({'height': num});
  285. }
  286. function getCookie(name) {
  287. var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
  288. return r ? r[1] : undefined;
  289. }
  290. function generateUUID() {
  291. var d = new Date().getTime();
  292. if (window.performance && typeof window.performance.now === "function") {
  293. d += performance.now(); //use high-precision timer if available
  294. }
  295. var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  296. var r = (d + Math.random() * 16) % 16 | 0;
  297. d = Math.floor(d / 16);
  298. return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  299. });
  300. return uuid;
  301. }