add_user.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. {extend name="base"/}
  2. {block name="resources"}
  3. <style>
  4. .form-wrap {margin-top: 0;}
  5. .align-center {text-align: center!important;}
  6. .manage-store {display: none}
  7. .manage-store .store-group {width: 200px;margin: 0 auto}
  8. </style>
  9. {/block}
  10. {block name="main"}
  11. <div class="layui-form form-wrap">
  12. <div class="layui-form-item">
  13. <label class="layui-form-label"><span class="required">*</span>用户名:</label>
  14. <div class="layui-input-block">
  15. <input name="username" type="text" lay-verify="required" placeholder="请输入员工用户名" autocomplete="off" readonly onfocus="this.removeAttribute('readonly');" onblur="this.setAttribute('readonly',true);" class="layui-input len-long">
  16. </div>
  17. </div>
  18. <div class="layui-form-item">
  19. <label class="layui-form-label"><span class="required">*</span>密码:</label>
  20. <div class="layui-input-block">
  21. <input name="password" type="password" lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input len-long" readonly onfocus="this.removeAttribute('readonly');" onblur="this.setAttribute('readonly',true);">
  22. </div>
  23. </div>
  24. <div class="layui-form-item">
  25. <label class="layui-form-label"><span class="required">*</span>再次输入密码:</label>
  26. <div class="layui-input-block">
  27. <input name="repassword" type="password" lay-verify="required|repassword" placeholder="请输入密码" autocomplete="off" class="layui-input len-long" readonly onfocus="this.removeAttribute('readonly');" onblur="this.setAttribute('readonly',true);">
  28. </div>
  29. </div>
  30. <div class="layui-form-item">
  31. <label class="layui-form-label">{if !$cashier_is_exit}<span class="required">*</span>{/if}员工角色:</label>
  32. <div class="layui-input-block len-mid">
  33. <select name="group_id" {if !$cashier_is_exit}lay-verify="groupid"{/if}>
  34. <option value="">选择角色</option>
  35. {foreach $group_list as $group_list_k => $group_list_v}
  36. <option value="{$group_list_v.group_id}">{$group_list_v.group_name}</option>
  37. {/foreach}
  38. </select>
  39. </div>
  40. </div>
  41. {if $store_is_exit}
  42. <div class="layui-form-item">
  43. <label class="layui-form-label">管理门店:</label>
  44. <div class="layui-input-block len-mid">
  45. <button class="layui-btn select-store">选择门店</button>
  46. </div>
  47. </div>
  48. <div class="layui-form-item manage-store">
  49. <label class="layui-form-label"></label>
  50. <div class="layui-input-block" style="width: 600px">
  51. <table class="layui-table" lay-skin="nob">
  52. <colgroup>
  53. <col width="40%">
  54. <col width="40%">
  55. <col width="20%">
  56. </colgroup>
  57. <thead>
  58. <tr>
  59. <th>门店</th>
  60. <th class="align-center">门店角色</th>
  61. <th class="align-center">操作</th>
  62. </tr>
  63. </thead>
  64. <tbody>
  65. </tbody>
  66. </table>
  67. </div>
  68. </div>
  69. {elseif $cashier_is_exit}
  70. <div class="layui-form-item">
  71. <label class="layui-form-label">门店角色:</label>
  72. <div class="layui-input-block len-mid">
  73. <select name="store_group">
  74. <option value="">请选择门店角色</option>
  75. {foreach name="store_user_group" item="vo"}
  76. <option value="{$vo.group_id}">{$vo.group_name}</option>
  77. {/foreach}
  78. </select>
  79. </div>
  80. </div>
  81. <input type="hidden" name="default_store_id" value="{$default_store_id}">
  82. {/if}
  83. <div class="form-row">
  84. <button class="layui-btn" lay-submit lay-filter="save">保存</button>
  85. <button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
  86. </div>
  87. </div>
  88. {if isset($store_user_group)}
  89. <script id="storeGroup" type="text/html">
  90. <select name="store_group" lay-verify="storegroupid" lay-filter="store_group">
  91. <option value="">请选择门店角色</option>
  92. {foreach name="store_user_group" item="vo"}
  93. <option value="{$vo.group_id}">{$vo.group_name}</option>
  94. {/foreach}
  95. </select>
  96. </script>
  97. {/if}
  98. {/block}
  99. {block name="script"}
  100. <script>
  101. var storeGroup = {};
  102. layui.use('form', function() {
  103. var form = layui.form,
  104. repeat_flag = false;//防重复标识
  105. form.render();
  106. /**
  107. * 监听提交
  108. */
  109. form.on('submit(save)', function(data) {
  110. data.field.store = [];
  111. {if $store_is_exit}
  112. $('.manage-store tbody tr').each(function () {
  113. data.field.store.push({
  114. store_id: $(this).attr('data-store'),
  115. group_id: $(this).find('[name="store_group"]').val()
  116. });
  117. })
  118. {elseif $cashier_is_exit}
  119. if (data.field.store_group) {
  120. data.field.store.push({
  121. store_id: data.field.default_store_id,
  122. group_id: data.field.store_group
  123. });
  124. }
  125. {/if}
  126. {if $cashier_is_exit}
  127. if (!data.field.group_id && !data.field.store.length) {
  128. layer.msg('员工角色和门店角色至少需设置一项', {icon: 5});
  129. return;
  130. }
  131. {/if}
  132. data.field.store = JSON.stringify(data.field.store);
  133. if (repeat_flag) return;
  134. repeat_flag = true;
  135. $.ajax({
  136. dataType: 'JSON',
  137. type: 'POST',
  138. url: ns.url("shop/user/addUser"),
  139. data: data.field,
  140. success: function(res){
  141. repeat_flag = false;
  142. if (res.code == 0) {
  143. layer.confirm('添加员工已成功', {
  144. title:'操作提示',
  145. btn: ['返回列表', '继续添加'],
  146. closeBtn: 0,
  147. yes: function(){
  148. location.href = ns.url("shop/user/user")
  149. },
  150. btn2: function() {
  151. location.href = ns.url("shop/user/addUser")
  152. }
  153. })
  154. }else{
  155. layer.msg(res.message);
  156. }
  157. }
  158. });
  159. });
  160. /**
  161. * 表单验证
  162. */
  163. form.verify({
  164. repassword: function (value) {
  165. if (value.length == 0) {
  166. return '请输入密码!';
  167. }
  168. var pw = $("input[name='password']").val();
  169. if(value != pw){
  170. return '两次密码输入不一致!';
  171. }
  172. },
  173. groupid: function (value) {
  174. if(value == 0) {
  175. return '请选择角色!';
  176. }
  177. },
  178. storegroupid: function (value) {
  179. if(!value) {
  180. return '请选择门店角色!';
  181. }
  182. },
  183. });
  184. form.on('select(store_group)', function (data) {
  185. var store_id = $(data.elem).parents('tr').attr('data-store');
  186. storeGroup['store_' + store_id] = data.value;
  187. })
  188. // 选择门店
  189. $('.select-store').click(function () {
  190. var storeId = [];
  191. $('.manage-store tbody tr').each(function () {
  192. storeId.push($(this).attr('data-store'));
  193. })
  194. storeSelect(function (store) {
  195. fetchStore(store);
  196. }, {store_id: storeId.toString()})
  197. })
  198. function fetchStore(store){
  199. if (store.length) {
  200. $('.manage-store').show();
  201. var h = '', group = $('#storeGroup').html();
  202. store.forEach(function (item) {
  203. h += `<tr data-store="`+ item.store_id +`">
  204. <td>`+ item.store_name +`</td>
  205. <td><div class="store-group">`+ group +`</div></td>
  206. <td class="align-center"><a href="javascript:;" class="del">删除</a></td>
  207. </tr>`;
  208. })
  209. $('.manage-store tbody').html(h);
  210. Object.keys(storeGroup).forEach(function (key) {
  211. let group = storeGroup[key], store_id = key.replace('store_', '');
  212. $('.manage-store tr[data-store="'+ store_id +'"] option[value="'+ group +'"]').prop('selected', true);
  213. })
  214. form.render();
  215. } else {
  216. $('.manage-store tbody').html('');
  217. $('.manage-store').hide();
  218. }
  219. }
  220. // 删除门店
  221. $('body').on('click', '.manage-store .del', function () {
  222. let store_id = $(this).parents('tr').attr('data-store')
  223. if (storeGroup['store_' + store_id]) delete storeGroup['store_' + store_id];
  224. $(this).parents('tr').remove();
  225. })
  226. });
  227. function back() {
  228. location.href = ns.url("shop/user/user");
  229. }
  230. </script>
  231. {/block}