心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mylayui.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. layui.use(['element','upload','laydate','form'], function(){
  2. var element = layui.element;
  3. var upload = layui.upload;
  4. var laydate = layui.laydate;
  5. var form = layui.form;
  6. //获取hash来切换选项卡,假设当前地址的hash为lay-id对应的值
  7. var layid = location.hash.replace(/^#tab=/, '');
  8. element.tabChange('tab', layid); //假设当前地址为:http://a.com#test1=222,那么选项卡会自动切换到“发送消息”这一项
  9. //监听Tab切换,以改变地址hash值
  10. element.on('tab(tab)', function(){
  11. var clayid=this.getAttribute('lay-id');
  12. if(clayid){
  13. location.hash = 'tab='+ clayid;
  14. $('.page').find('a').each(function(index,element){//避免tab翻页问题
  15. var url=$(this).attr('href');
  16. if(url.indexOf('tab=')==-1){
  17. $(this).attr('href', url+'#tab='+ clayid);
  18. }else{
  19. $(this).attr('href', url.replace(/tab=[\w]+/, 'tab='+ clayid));
  20. }
  21. });
  22. }
  23. });
  24. //跳转
  25. form.on('select(tourl)', function(data){
  26. window.location.href= data.value;
  27. });
  28. //提示
  29. $(".tips").on("mouseover",function(){
  30. layer.tips($(this).data('content'), this);
  31. })
  32. //用户登录验证
  33. form.on('submit(login-submit)', function(data){
  34. var form = $("#dologin");
  35. var url = form.attr('action');
  36. var username = form.find("#username").val();
  37. var password = form.find("#password").val();
  38. var checkcode = form.find("#checkcode").val();
  39. var formcheck = form.find("#formcheck").val();
  40. $.ajax({
  41. type: 'POST',
  42. url: url,
  43. dataType: 'json',
  44. data: {
  45. username: username,
  46. password: password,
  47. checkcode: checkcode,
  48. formcheck: formcheck
  49. },
  50. success: function (response, status) {
  51. if (response.code == 1) {
  52. layer.msg("登录成功!", {icon: 1});
  53. window.location.href = response.data;
  54. } else {
  55. form.find("#checkcode").val("");
  56. $('#codeimg').click();//更新验证码
  57. layer.msg("登录失败:" + response.data, {icon: 5});
  58. }
  59. },
  60. error:function(xhr,status,error){
  61. layer.msg("登录请求发生错误!", {icon: 5});
  62. $('#note').html('登录请求发生错误,您可按照如下方式排查:<br>1、试着删除根目录下runtime目录,刷新页面重试;<br>2、检查系统会话文件存储目录是否具有写入权限;<br>3、检查服务器环境pathinfo及伪静态规则配置;');
  63. }
  64. });
  65. return false;
  66. });
  67. var sitedir=$('#sitedir').data('sitedir');
  68. var uploadurl = $("#preurl").data('preurl')+'/index/upload';
  69. //执行单图片实例
  70. var uploadInst = upload.render({
  71. elem: '.upload' //绑定元素
  72. ,url: uploadurl //上传接口
  73. ,field: 'upload' //字段名称
  74. ,multiple: false //多文件上传
  75. ,accept: 'images' //接收文件类型 images(图片)、file(所有文件)、video(视频)、audio(音频)
  76. ,acceptMime: 'image/*'
  77. ,before: function(obj){
  78. //判断是否需要加水印
  79. if($(this.item).hasClass('watermark')){
  80. uploadInst.config.url=uploadurl+'/watermark/1';//改变URL
  81. }
  82. layer.load(); //上传loading
  83. }
  84. ,done: function(res){
  85. var item = this.item;
  86. var des=$(item).data('des');
  87. layer.closeAll('loading'); //关闭loading
  88. if(res.code==1){
  89. $('#'+des).val(res.data[0]);
  90. $('#'+des+'_box').html("<dl><dt><img src='"+sitedir+res.data[0]+"' data-url='"+res.data[0]+"' ></dt><dd>删除</dd></dl>");
  91. layer.msg('上传成功!');
  92. }else{
  93. layer.msg('上传失败:'+res.data);
  94. }
  95. }
  96. ,error: function(){
  97. layer.closeAll('loading'); //关闭loading
  98. layer.msg('上传发生错误!');
  99. }
  100. });
  101. //执行多图片上传实例
  102. var files='';
  103. var html='';
  104. var html2='';
  105. var uploadsInst = upload.render({
  106. elem: '.uploads' //绑定元素
  107. ,url: uploadurl //上传接口
  108. ,field: 'upload' //字段名称
  109. ,multiple: true//多文件上传
  110. ,accept: 'images' //接收文件类型 images(图片)、file(所有文件)、video(视频)、audio(音频)
  111. ,acceptMime: 'image/*'
  112. ,before: function(obj){
  113. //判断是否需要加水印
  114. if($(this.item).hasClass('watermark')){
  115. uploadsInst.config.url=uploadurl+'/watermark/1';//改变URL
  116. }
  117. layer.load(); //上传loading
  118. }
  119. ,done: function(res){
  120. if(res.code==1){
  121. if(files){
  122. files+=','+res.data[0];
  123. }else{
  124. files+=res.data[0];
  125. }
  126. html += "<dl><dt><img src='"+sitedir+res.data[0]+"' data-url='"+res.data[0]+"'></dt><dd>删除</dd>" +
  127. "<dt><input type='text' name='picstitle[]' style='width:95%' /></dt>"+
  128. "</dl>";
  129. html2 += "<dl><dt><img src='"+sitedir+res.data[0]+"' data-url='"+res.data[0]+"'></dt><dd>删除</dd>" + "</dl>";
  130. }else{
  131. layer.msg('有文件上传失败:'+res.data);
  132. }
  133. }
  134. ,allDone: function(obj){
  135. var item = this.item;
  136. var des=$(item).data('des');
  137. layer.closeAll('loading'); //关闭loading
  138. if(files!=''){
  139. if($('#'+des).val()){
  140. $('#'+des).val($('#'+des).val()+','+files);
  141. }else{
  142. $('#'+des).val(files);
  143. }
  144. if(des=='pics'){
  145. $('#'+des+'_box').append(html);
  146. }else{
  147. $('#'+des+'_box').append(html2);
  148. }
  149. layer.msg('成功上传'+obj.successful+'个文件!');
  150. files='';
  151. html='';
  152. html2='';
  153. }else{
  154. layer.msg('全部上传失败!');
  155. }
  156. }
  157. ,error: function(){
  158. layer.closeAll('loading'); //关闭loading
  159. layer.msg('上传发生错误!');
  160. }
  161. });
  162. //图片页面删除功能
  163. $('.pic').on("click",'dl dd',function(){
  164. var id=$(this).parents('.pic').attr('id');
  165. var url=$(this).siblings('dt').find('img').data('url');
  166. var input=$('#'+id.replace('_box',''));
  167. var value = input.val();
  168. value = value.replace(url,'');
  169. value = value.replace(/^,/, '');
  170. value = value.replace(/,$/, '');
  171. value = value.replace(/,,/, ',');
  172. input.val(value);
  173. $(this).parents('dl').remove();
  174. });
  175. //执行附件上传实例
  176. var uploadFileInst = upload.render({
  177. elem: '.file' //绑定元素
  178. ,url: uploadurl //上传接口
  179. ,field: 'upload' //字段名称
  180. ,multiple: false //多文件上传
  181. ,accept: 'file' //接收文件类型 images(图片)、file(所有文件)、video(视频)、audio(音频)
  182. ,before: function(obj){
  183. layer.load(); //上传loading
  184. }
  185. ,done: function(res){
  186. var item = this.item;
  187. var des=$(item).data('des');
  188. layer.closeAll('loading'); //关闭loading
  189. if(res.code==1){
  190. $('#'+des).val(res.data[0]);
  191. layer.msg('上传成功!');
  192. }else{
  193. layer.msg('上传失败:'+res.data);
  194. }
  195. }
  196. ,error: function(){
  197. layer.closeAll('loading'); //关闭loading
  198. layer.msg('上传发生错误!');
  199. }
  200. });
  201. //使用多日期控件
  202. useLayDateMultiple('year','year');
  203. useLayDateMultiple('month','month');
  204. useLayDateMultiple('time','time');
  205. useLayDateMultiple('date','date');
  206. useLayDateMultiple('datetime','datetime');
  207. //选择模型切换模板
  208. form.on('select(model)', function(data){
  209. var elem = data.elem;
  210. var type = $(elem).find("option:selected").data('type');
  211. var listtpl = $(elem).find("option:selected").data('listtpl');
  212. var contenttpl = $(elem).find("option:selected").data('contenttpl');
  213. $(elem).parents('form').find("#type").val(type);
  214. addOptionValue("listtpl",listtpl,listtpl);
  215. addOptionValue("contenttpl",contenttpl,contenttpl);
  216. $(elem).parents('form').find("#listtpl").val(listtpl);
  217. $(elem).parents('form').find("#contenttpl").val(contenttpl);
  218. form.render(null, 'sort');
  219. });
  220. });
  221. //日期控件函数
  222. function useLayDateMultiple(cls,type) {
  223. layui.use('laydate', function() {
  224. var laydate = layui.laydate;
  225. lay('.' + cls).each(function() {
  226. laydate.render({
  227. elem : this,
  228. type : type,
  229. });
  230. });
  231. });
  232. }