teacherAdd.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php use common\widgets\UeditorWidget; ?>
  2. <!--百度图片上传-->
  3. <link rel="stylesheet" type="text/css" href="/js/webuploader-0.1.5/webuploader.css">
  4. <script type="text/javascript" src="/js/webuploader-0.1.5/webuploader.js"></script>
  5. <div class="row">
  6. <div class="col-sm-12">
  7. <div class="panel panel-default">
  8. <div class="panel-body">
  9. <form role="form" class="form-horizontal">
  10. <div class="form-group">
  11. <label class="col-sm-2 control-label">名称:</label>
  12. <div class="col-sm-10">
  13. <input type="text" class="form-control" name="teacherName"
  14. value=""
  15. placeholder="">
  16. <p></p>
  17. </div>
  18. </div>
  19. <div class="form-group">
  20. <label class="col-sm-2 control-label">头衔:</label>
  21. <div class="col-sm-10">
  22. <input type="text" class="form-control" name="title"
  23. value=""
  24. placeholder="">
  25. <p></p>
  26. </div>
  27. </div>
  28. <div class="form-group">
  29. <label class="col-sm-2 control-label">头像:</label>
  30. <div class="col-sm-10">
  31. <div>
  32. <img name="teacherAvatar" width="80" src="/images/teacher_default_avatar.jpg" />
  33. </div>
  34. <div id="uploader-demo" style="margin-top:2px;">
  35. <div id="fileList" class="uploader-list"></div>
  36. <div id="filePicker">选择图片</div>
  37. </div>
  38. <input type="hidden" class="form-control" name="avatar" value="" placeholder="">
  39. <p></p>
  40. </div>
  41. </div>
  42. <div class="form-group">
  43. <label class="col-sm-2 control-label" for="field-3">简介:</label>
  44. <div class="col-sm-10">
  45. <?= UeditorWidget::widget(['name' => 'intro','content' => '']); ?>
  46. <p></p>
  47. </div>
  48. </div>
  49. <div class="form-group-separator"></div>
  50. <div class="form-group" style="text-align:center;">
  51. <button type="button" onclick="addSubmit('click',this)" class="btn btn-info btn-single">确认
  52. </button>
  53. <button type="button" onclick="history.go(-1);" class="btn btn-info btn-single">返回</button>
  54. </div>
  55. </form>
  56. </div>
  57. </div>
  58. </div>
  59. </div>
  60. <!-- footer -->
  61. <?php
  62. use backend\widgets\FooterWidget;
  63. ?>
  64. <?= FooterWidget::widget() ?>
  65. <!-- footer -->
  66. <script>
  67. //文章验证流程
  68. var teacherName = $("input[name=teacherName]");
  69. var title = $("input[name=title]");
  70. var avatar = $("input[name=avatar]");
  71. var intro = $("textarea[name=intro]");
  72. var needValidate = [teacherName, avatar, intro];
  73. //修改文章验证流程
  74. addSubmit();
  75. function addSubmit() {
  76. setStats(teacherName, '请填写名称', function (val, mess) {
  77. var info = [false, mess];
  78. if (val.length <= 0) {
  79. info[1] = '请填写名称';
  80. } else if (getLength(val) > 0) {
  81. info[0] = true;
  82. info[1] = '填写正确';
  83. } else {
  84. info[0] = false;
  85. }
  86. return info;
  87. });
  88. setStats(avatar, '请上传头像', function (val, mess) {
  89. var info = [false, mess];
  90. if (val.length <= 0) {
  91. info[1] = '请上传头像';
  92. } else if (getLength(val) > 0) {
  93. info[0] = true;
  94. info[1] = '填写正确';
  95. } else {
  96. info[0] = false;
  97. }
  98. return info;
  99. });
  100. setStats(intro, '请填写简介', function (val, mess) {
  101. var info = [false, mess];
  102. if (getLength(val) > 0) {
  103. info[0] = true;
  104. info[1] = '填写正确';
  105. } else {
  106. info[1] = '请填写简介';
  107. }
  108. return info;
  109. });
  110. if (arguments[0] == 'click') {//用户点击提交
  111. var verifyStatus = true;
  112. for (var m = 0; m < needValidate.length; m++) {
  113. var singleObj = needValidate[m];
  114. if (singleObj.attr('addStatus') == undefined) {
  115. singleObj.blur();
  116. }
  117. if (singleObj.attr('addStatus') == 'no') {
  118. verifyStatus = false;
  119. continue;
  120. }
  121. }
  122. if (verifyStatus) {
  123. var data = "teacherName=" + teacherName.val() + "&title=" + title.val() + "&avatar=" + avatar.val() + "&intro=" + intro.val();
  124. data += "&_csrf=<?= Yii::$app->request->csrfToken ?>";
  125. $.ajax({
  126. type: "POST",
  127. url: '/teach/teacher-save-add',
  128. data: data,
  129. dataType: 'json',
  130. async: false,
  131. success: function (data) {
  132. if (data.code == 'A0001') {
  133. xhPopMsg(data.msg, 1300);//调用定时浮层 XXX毫秒后消失
  134. setTimeout(function () {
  135. window.location.href = '/teach/teacher';
  136. }, 1400);
  137. }
  138. }
  139. });
  140. }
  141. }
  142. }
  143. function setStats(obj, mess, check) {
  144. obj.focus(function () {
  145. $(this).removeAttr('addStatus');
  146. $(this).siblings('p').removeClass('help-error-mention').html('');
  147. $(this).parent().parent().removeClass("has-success").removeClass('has-error');
  148. });
  149. obj.blur(function () {
  150. var objVal = obj.val();
  151. var info = check(objVal, mess);
  152. if (info[0]) {
  153. $(this).attr('addStatus', 'yes');
  154. $(this).siblings('p').removeClass('help-error-mention').html('');
  155. $(this).parent().parent().addClass("has-success").removeClass("has-error");
  156. } else {
  157. $(this).attr('addStatus', 'no');
  158. $(this).siblings('p').addClass('help-error-mention').html(info[1]);
  159. $(this).parent().parent().addClass("has-error").removeClass("has-success");
  160. }
  161. });
  162. }
  163. // 图片上传demo
  164. jQuery(function() {
  165. var $ = jQuery,
  166. $list = $('#fileList'),
  167. // 优化retina, 在retina下这个值是2
  168. ratio = window.devicePixelRatio || 1,
  169. // 缩略图大小
  170. thumbnailWidth = 100 * ratio,
  171. thumbnailHeight = 100 * ratio,
  172. // Web Uploader实例
  173. uploader;
  174. // 初始化Web Uploader
  175. uploader = WebUploader.create({
  176. // 自动上传。
  177. auto: true,
  178. // swf文件路径
  179. swf: '/js/webuploader-0.1.5/Uploader.swf',
  180. //swf: BASE_URL + '/js/Uploader.swf',
  181. // 文件接收服务端。
  182. server: '/teach/upload-teacher-avatar',
  183. // 选择文件的按钮。可选。
  184. // 内部根据当前运行是创建,可能是input元素,也可能是flash.
  185. pick: '#filePicker',
  186. //是否允许重复上传
  187. duplicate :true,
  188. fileSingleSizeLimit: 1*1024*1024,//上传限制1M
  189. fileNumLimit: 5,//每次只能传5张
  190. formData: { "_csrf": '<?= Yii::$app->request->csrfToken?>'},
  191. // 只允许选择文件,可选。
  192. accept: {
  193. title: 'Images',
  194. extensions: 'gif,jpg,jpeg,bmp,png',
  195. mimeTypes: 'image/*'
  196. }
  197. });
  198. // 当有文件添加进来的时候
  199. uploader.on( 'fileQueued', function( file ) {
  200. //这里可以取到图片的宽高
  201. //uploader.makeThumb(file, function (error, src) {
  202. //console.log(file._info.width + '*' + file._info.height)
  203. //uploader.removeFile( file );
  204. //}, 100, 100);
  205. });
  206. // 文件上传过程中创建进度条实时显示。
  207. uploader.on( 'uploadProgress', function( file, percentage ) {
  208. });
  209. // 文件上传成功,给item添加成功class, 用样式标记上传成功。
  210. uploader.on( 'uploadSuccess', function( file, response) {
  211. $("img[name=teacherAvatar]").attr('src','<?= Yii::$app->params['imgUrl']?>'+response.data.url);
  212. $("input[name=avatar]").val(response.data.url);
  213. });
  214. // 文件上传失败,现实上传出错。
  215. uploader.on( 'uploadError', function( file ) {
  216. });
  217. // 完成上传完了,成功或者失败,先删除进度条。
  218. uploader.on( 'uploadComplete', function( file ) {
  219. xhPopMsg('上传成功', 1000);
  220. });
  221. uploader.on("error", function (type) {
  222. if (type == "Q_TYPE_DENIED") {
  223. alert("请上传JPG、PNG、GIF、BMP格式文件");
  224. } else if (type == "F_EXCEED_SIZE") {
  225. alert("单张图片不能超过1M");
  226. }else {
  227. alert("上传出错!请检查后重新上传!错误代码"+type);
  228. }
  229. });
  230. });
  231. </script>