add.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <template>
  2. <!-- 新增账号 -->
  3. <div class="accountPerPage">
  4. <s-header :name="pageTitle" :noback="false"></s-header>
  5. <div class="accountPerBox">
  6. <van-form @submit="onSubmit" class="modern-form">
  7. <!-- 表单字段 -->
  8. <van-field
  9. v-model="username"
  10. :label="$t('accountPer.usernameLabel')"
  11. :placeholder="$t('accountPer.usernamePlaceholder')"
  12. left-icon="user-o"
  13. class="modern-field"
  14. :rules="[
  15. {
  16. pattern: /^(?:[a-zA-Z][a-zA-Z0-9]*|[\w.-]+@[\w.-]+\.[a-zA-Z]{2,})$/,
  17. message: $t('register.usernameRequired'),
  18. },
  19. ]"
  20. />
  21. <van-field
  22. v-model="password"
  23. type="password"
  24. :label="$t('accountPer.passwordLabel')"
  25. :placeholder="passwordPlaceholder"
  26. left-icon="lock"
  27. class="modern-field"
  28. />
  29. <van-field
  30. v-model="name"
  31. :label="$t('accountPer.nameLabel')"
  32. :placeholder="$t('accountPer.namePlaceholder')"
  33. left-icon="contact"
  34. class="modern-field"
  35. :rules="[
  36. { required: true, message: $t('accountPer.namePlaceholder') },
  37. ]"
  38. />
  39. <!-- <van-field v-model="phone" :label="$t('accountPer.phoneLabel')" :placeholder="$t('accountPer.phonePlaceholder')"
  40. left-icon="phone" class="modern-field"
  41. :rules="[{ required: true, message: $t('accountPer.phonePlaceholder') }]" /> -->
  42. <!-- 类型选择 -->
  43. <van-field name="radio" v-if="user.type === 0" class="radio-field">
  44. <template #input>
  45. <van-radio-group v-model="type" class="radio-group">
  46. <van-radio name="1" class="radio-item">公司人员</van-radio>
  47. <van-radio name="4" class="radio-item">营销经理</van-radio>
  48. </van-radio-group>
  49. </template>
  50. </van-field>
  51. <!-- 角色选择 -->
  52. <van-field
  53. v-model="roleText"
  54. is-link
  55. readonly
  56. :label="$t('accountPer.roleLabel')"
  57. :placeholder="$t('accountPer.rolePlaceholder')"
  58. left-icon="friends-o"
  59. class="modern-field"
  60. @click="roleShow = true"
  61. :rules="[
  62. { required: true, message: $t('accountPer.rolePlaceholder') },
  63. ]"
  64. />
  65. <!-- 启用开关 -->
  66. <van-field class="switch-field">
  67. <template #input>
  68. <div class="switch-container">
  69. <van-switch v-model="isEnabled" size="20px" />
  70. <span class="switch-label">{{
  71. $t("accountPer.startOrNot")
  72. }}</span>
  73. </div>
  74. </template>
  75. </van-field>
  76. <!-- 设备管理 -->
  77. <van-field
  78. v-model="cofficentForm.equipmentNames"
  79. :label="$t('accountPer.manageMachinesLabel')"
  80. :placeholder="$t('accountPer.manageMachinesPlaceholder')"
  81. left-icon="cluster-o"
  82. class="modern-field"
  83. readonly
  84. clearable
  85. @click="busiEquipInpClk"
  86. :rules="[
  87. {
  88. required: true,
  89. message: $t('accountPer.manageMachinesPlaceholder'),
  90. },
  91. ]"
  92. >
  93. <template #right-icon>
  94. <div class="field-icons">
  95. <van-icon
  96. v-if="cofficentForm.equipmentNames"
  97. name="clear"
  98. @click="
  99. cofficentForm.equipmentNames = '';
  100. equipmentIds = '';
  101. "
  102. />
  103. <van-icon name="arrow" @click="busiEquipInpClk" />
  104. </div>
  105. </template>
  106. </van-field>
  107. <!-- 操作按钮 -->
  108. <div class="action-buttons">
  109. <van-button
  110. round
  111. type="primary"
  112. native-type="submit"
  113. class="submit-btn"
  114. >
  115. {{ $t("accountPer.submit") }}
  116. </van-button>
  117. <van-button
  118. v-if="pageTitle === $t('accountPer.modifyAccount')"
  119. round
  120. type="danger"
  121. class="delete-btn"
  122. @click="deleteLoginUserFun"
  123. >
  124. {{ $t("accountPer.delete") }}
  125. </van-button>
  126. </div>
  127. </van-form>
  128. </div>
  129. <van-popup v-model:show="roleShow" round position="bottom">
  130. <van-cascader
  131. v-model="roleList"
  132. :title="$t('accountPer.rolePlaceholder')"
  133. :options="roleOptions"
  134. @close="roleShow = false"
  135. @finish="onRoleFinish"
  136. />
  137. </van-popup>
  138. <!-- 机器弹窗 -->
  139. <kSelectPop
  140. @selconfirm="selEquipConfirm"
  141. ref="kSelectPopEquipRef"
  142. :selOptions="selEquipOptions"
  143. @searchData="searchEquData"
  144. :selTitleAndName="['name', 'id']"
  145. ></kSelectPop>
  146. </div>
  147. </template>
  148. <script>
  149. // 导入选择组件
  150. import kSelectPop from "../../components/commom/kSelectPop/index.vue";
  151. import { onMounted, reactive, toRefs, ref } from "vue";
  152. import sHeader from "../../components/SimpleHeader";
  153. import {
  154. showFailToast,
  155. showSuccessToast,
  156. showToast,
  157. showNotify,
  158. showConfirmDialog,
  159. } from "vant";
  160. import {
  161. getSysRoleList,
  162. getEquipmentList,
  163. addLoginUser,
  164. updataLoginUser,
  165. deleteLoginUser,
  166. } from "../../service/accountPar/index";
  167. import { getLoginUser } from "../../common/js/utils";
  168. import md5 from "js-md5";
  169. import { useRouter } from "vue-router";
  170. import { useI18n } from "vue-i18n";
  171. export default {
  172. components: { sHeader, kSelectPop },
  173. setup() {
  174. const { t } = useI18n();
  175. const router = useRouter();
  176. const user = getLoginUser();
  177. const roleShow = ref(false);
  178. const roleText = ref("");
  179. const roleOptions = ref([]);
  180. const onRoleFinish = ({ selectedOptions }) => {
  181. roleShow.value = false;
  182. roleText.value = selectedOptions[0].text;
  183. };
  184. const equipmentIdShow = ref(false);
  185. const equipmentIdText = ref("");
  186. const equipmentIdsOptions = ref([]);
  187. const onEquipmentIdFinish = ({ selectedOptions }) => {
  188. equipmentIdShow.value = false;
  189. equipmentIdText.value = selectedOptions[0].text;
  190. };
  191. const passwordPlaceholder = ref("");
  192. const passwordPattern = ref(/^(?=.*[a-zA-Z])(?=.*\d).{10,}$/);
  193. const havePassword = ref(true);
  194. const isUserName = ref(false);
  195. let addParams = reactive({
  196. username: "",
  197. password: "",
  198. name: "",
  199. phone: "",
  200. type: user.type === 0 ? "1" : "",
  201. isEnabled: true,
  202. roleList: "",
  203. equipmentIds: "",
  204. companyType: "0",
  205. });
  206. const cofficentForm = ref({
  207. equipmentNames: "",
  208. });
  209. const pageTitle = ref("");
  210. onMounted(async () => {
  211. let accoutPerSet = localStorage.getItem("accoutPerSet");
  212. if (accoutPerSet) {
  213. pageTitle.value = t("accountPer.modifyAccount");
  214. accoutPerSet = JSON.parse(accoutPerSet);
  215. addParams.id = accoutPerSet.id;
  216. addParams.username = accoutPerSet.username;
  217. isUserName.value = true;
  218. addParams.name = accoutPerSet.name;
  219. addParams.phone = accoutPerSet.phone;
  220. addParams.isEnabled = accoutPerSet.isEnabled;
  221. addParams.roleList = accoutPerSet.roleList[0];
  222. console.log(addParams.roleList);
  223. addParams.equipmentIds = accoutPerSet.equipmentIds;
  224. havePassword.value = false;
  225. passwordPlaceholder.value = t("accountPer.editPasswordPlaceholder");
  226. } else {
  227. pageTitle.value = t("accountPer.addAccount");
  228. passwordPlaceholder.value = t("register.passwordPlaceholder");
  229. havePassword.value = true;
  230. }
  231. getSysRoleListFun();
  232. getEquipmentListFun();
  233. });
  234. // 获取角色下拉列表
  235. const getSysRoleListFun = async () => {
  236. const { data } = await getSysRoleList({ adminId: user.id });
  237. if (data.code === "00000") {
  238. console.log();
  239. roleOptions.value = data.data
  240. .filter((item) => item.label !== "商家")
  241. .map((item) => ({
  242. text: item.label,
  243. value: item.value,
  244. }));
  245. if (pageTitle.value === t("accountPer.modifyAccount")) {
  246. roleText.value = roleOptions.value.filter(
  247. (v) => v.value === addParams.roleList
  248. )[0].text;
  249. console.log(roleOptions.value);
  250. }
  251. } else {
  252. showFailToast(data.message);
  253. }
  254. };
  255. const searchEquData = (e) => {
  256. selEquipOptions.value = e;
  257. };
  258. // 获取机器下拉
  259. const getEquipmentListFun = async () => {
  260. const { data } = await getEquipmentList({ adminId: user.id });
  261. if (data.code === "00000") {
  262. if (data.data.length > 0) {
  263. // 将id转成字符串类型,解决选中问题
  264. data.data.forEach((item) => {
  265. item.id = item.id + "";
  266. });
  267. }
  268. selEquipOptions.value = data.data;
  269. if (addParams.equipmentIds != null) {
  270. const list = addParams.equipmentIds.split(",");
  271. const listName = [];
  272. // 根据id找到对应的name
  273. selEquipOptions.value.forEach((item) => {
  274. list.forEach((item1) => {
  275. if (item.id === item1) {
  276. listName.push(item.name);
  277. }
  278. });
  279. });
  280. cofficentForm.value.equipmentNames = listName.join(",");
  281. }
  282. } else {
  283. showFailToast(data.message);
  284. }
  285. };
  286. const onSubmit = async () => {
  287. let params = { adminId: user.id };
  288. if (addParams.username === "admin") {
  289. showNotify({ type: "danger", message: t("register.A0201") });
  290. return false;
  291. }
  292. if (addParams.password != "") {
  293. if (!passwordPattern.value.test(addParams.password)) {
  294. showNotify(t("register.passwordPlaceholder"));
  295. return false;
  296. }
  297. }
  298. params = Object.assign({ adminId: user.id }, addParams);
  299. if (params.password !== "") {
  300. params.password = md5(params.password);
  301. }
  302. params.roleList = [params.roleList];
  303. if (pageTitle.value === t("accountPer.modifyAccount")) {
  304. const { data } = await updataLoginUser(params);
  305. if (data.code === "00000") {
  306. showSuccessToast(t("accountPer.successfullyModifiedTheAccount"));
  307. router.replace("/accountPer");
  308. } else {
  309. showFailToast(t("accountPer.failedToModifyTheAccount"), data.message);
  310. }
  311. } else {
  312. const { data } = await addLoginUser(params);
  313. if (data.code === "00000") {
  314. showSuccessToast(t("accountPer.accountAddedSuccessfully"));
  315. router.replace("/accountPer");
  316. } else if (data.code === "A0201") {
  317. showToast(t("accountPer.accountNameExists"));
  318. } else {
  319. showFailToast(t("accountPer.failedToAddAccount"), data.message);
  320. }
  321. }
  322. };
  323. const deleteLoginUserFun = () => {
  324. showConfirmDialog({
  325. title: t("user.tips"),
  326. message: t("accountPer.delTips"),
  327. }).then(async () => {
  328. const params = {
  329. adminId: user.id,
  330. id: addParams.id,
  331. };
  332. const { data } = await deleteLoginUser(params);
  333. if (data.code === "00000") {
  334. showSuccessToast(t("accountPer.successfullyDeletedTheAccount"));
  335. setTimeout(() => {
  336. router.go(0);
  337. }, 1000);
  338. } else {
  339. showFailToast(t("accountPer.failedToDeleteTheAccount"), data.message);
  340. }
  341. });
  342. };
  343. // 点击机器列表
  344. const busiEquipInpClk = () => {
  345. if (cofficentForm.value.equipmentNames) {
  346. let list = cofficentForm.value.equipmentNames.split(",");
  347. kSelectPopEquipRef.value.selPopOpen(list);
  348. } else {
  349. kSelectPopEquipRef.value.selPopOpen();
  350. }
  351. };
  352. // 机器选择弹窗
  353. const selEquipOptions = ref([]);
  354. const kSelectPopEquipRef = ref(null);
  355. // 机器弹窗的确定按钮
  356. const selEquipConfirm = (e, e1) => {
  357. if (e.length > 0) {
  358. cofficentForm.value.equipmentNames = e.join(",");
  359. addParams.equipmentIds = e1.join(",");
  360. } else {
  361. cofficentForm.value.equipmentNames = "";
  362. addParams.equipmentIds = "";
  363. }
  364. kSelectPopEquipRef.value.selPopClose();
  365. };
  366. return {
  367. user,
  368. pageTitle,
  369. roleShow,
  370. roleText,
  371. roleOptions,
  372. onRoleFinish,
  373. equipmentIdShow,
  374. equipmentIdText,
  375. equipmentIdsOptions,
  376. onEquipmentIdFinish,
  377. ...toRefs(addParams),
  378. onSubmit,
  379. deleteLoginUserFun,
  380. selEquipOptions,
  381. kSelectPopEquipRef,
  382. selEquipConfirm,
  383. cofficentForm,
  384. busiEquipInpClk,
  385. havePassword,
  386. passwordPlaceholder,
  387. passwordPattern,
  388. isUserName,
  389. searchEquData,
  390. };
  391. },
  392. };
  393. </script>
  394. <style lang="less" scoped>
  395. @import "../../common/style/common.less";
  396. @primary-color: #4d6add;
  397. @danger-color: #ff6b6b;
  398. .modern-form {
  399. padding: 16px;
  400. .modern-field {
  401. margin-bottom: 16px;
  402. border-radius: 8px;
  403. background: #f8f9ff;
  404. :deep(.van-field__label) {
  405. color: #666;
  406. font-size: 14px;
  407. }
  408. :deep(.van-field__control) {
  409. color: #333;
  410. font-size: 14px;
  411. }
  412. :deep(.van-field__left-icon) {
  413. color: @primary-color;
  414. margin-right: 8px;
  415. }
  416. }
  417. .radio-field {
  418. :deep(.van-field__body) {
  419. padding: 8px 0;
  420. }
  421. }
  422. .radio-group {
  423. display: flex;
  424. gap: 24px;
  425. .radio-item {
  426. margin: 0;
  427. :deep(.van-radio__label) {
  428. color: #333;
  429. }
  430. }
  431. }
  432. .switch-field {
  433. :deep(.van-field__body) {
  434. padding: 8px 0;
  435. }
  436. .switch-container {
  437. display: flex;
  438. align-items: center;
  439. .switch-label {
  440. margin-left: 12px;
  441. color: #333;
  442. font-size: 14px;
  443. }
  444. }
  445. }
  446. .action-buttons {
  447. margin-top: 24px;
  448. display: flex;
  449. flex-direction: column;
  450. gap: 16px;
  451. .submit-btn {
  452. background: @primary-color;
  453. width: 100%;
  454. height: 44px;
  455. }
  456. .delete-btn {
  457. background: @danger-color;
  458. width: 100%;
  459. height: 44px;
  460. }
  461. }
  462. .field-icons {
  463. display: flex;
  464. align-items: center;
  465. gap: 8px;
  466. .van-icon {
  467. color: #c0c4cc;
  468. font-size: 18px;
  469. }
  470. }
  471. }
  472. </style>