add_ywy.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <template>
  2. <view class="page-container">
  3. <view class="form-card">
  4. <view class="form-item">
  5. <text class="form-label">业务员名称</text>
  6. <input
  7. class="form-input"
  8. v-model="formData.name"
  9. placeholder="请输入业务员姓名"
  10. placeholder-class="placeholder"
  11. />
  12. </view>
  13. <view class="form-item">
  14. <text class="form-label">业务员账号</text>
  15. <input
  16. class="form-input"
  17. type="number"
  18. maxlength="11"
  19. v-model="formData.account"
  20. placeholder="请输入业务员账号(手机号)"
  21. placeholder-class="placeholder"
  22. />
  23. </view>
  24. <view class="form-item item-flex" v-if="formData.id != 0">
  25. <text class="form-label">业务员状态</text>
  26. <view class="switch-wrapper">
  27. <text :class="formData.isJob ? 'status-active' : ''">在职</text>
  28. <switch :checked="formData.isJob" @change="statusChange" color="#409eff" />
  29. </view>
  30. </view>
  31. <picker mode="selector" :range="areaList" range-key="area_name" @change="handleAreaChange">
  32. <view class="form-item item-flex">
  33. <text class="form-label">所属区域</text>
  34. <view class="picker-value">
  35. <text :class="{'placeholder': !formData.region}">
  36. {{ formData.region || '请选择' }}
  37. </text>
  38. <uni-icons type="right" size="16" color="#c0c0c0"></uni-icons>
  39. </view>
  40. </view>
  41. </picker>
  42. </view>
  43. <view class="footer-save-button">
  44. <button class="save-btn" @click="submitForm">保 存</button>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import {areaList,addYwy,updateYwy,getYwyByIdDetail} from "@/api/hexiao";
  50. export default {
  51. data() {
  52. return {
  53. // 区域列表数据
  54. areaList: [
  55. ],
  56. areaMap :{},
  57. // 表单数据
  58. formData: {
  59. id:0,
  60. name: '',
  61. account: '',
  62. isJob: true,
  63. region: '', // 用于显示选择的区域名称
  64. regionId: null // 用于提交给后端的区域ID
  65. }
  66. };
  67. },
  68. onLoad(opt) {
  69. // 在实际项目中,您应该在这里通过API获取区域列表
  70. this.fetchAreaList(opt);
  71. },
  72. methods: {
  73. fetchAreaList (opt) {
  74. // 模拟区域列表数据
  75. areaList().then(res=>{
  76. this.areaMap = {};
  77. this.areaList = res.data;
  78. for (let i = 0; i < this.areaList.length; i++) {
  79. let area = this.areaList[i];
  80. this.areaMap [area.id] = area;
  81. }
  82. if(opt.id){
  83. getYwyByIdDetail(opt.id).then(res=>{
  84. let data = res.data;
  85. this.formData = {
  86. id: data.id,
  87. name: data.nick_name,
  88. account: data.tel_,
  89. isJob: data.is_job,
  90. regionId: data.area_id
  91. }
  92. let area = this.areaMap[data.area_id]
  93. this.formData.region = area.area_name;
  94. })
  95. }
  96. })
  97. },
  98. // 监听 Switch 开关变化
  99. statusChange(e) {
  100. this.formData.isJob = e.detail.value;
  101. },
  102. // 关键改动:处理 Picker 的 change 事件
  103. handleAreaChange(e) {
  104. const selectedIndex = e.detail.value; // 获取用户选择的索引
  105. const selectedArea = this.areaList[selectedIndex]; // 根据索引找到对应的区域对象
  106. console.log('选择的区域对象:', selectedArea);
  107. // 更新表单数据
  108. this.formData.region = selectedArea.area_name;
  109. this.formData.regionId = selectedArea.id;
  110. },
  111. // 提交表单
  112. submitForm() {
  113. // --- 手动进行数据校验 ---
  114. if (!this.formData.name) {
  115. uni.showToast({ title: '请输入业务员姓名', icon: 'none' });
  116. return;
  117. }
  118. if (!this.formData.account) {
  119. uni.showToast({ title: '请输入业务员账号', icon: 'none' });
  120. return;
  121. }
  122. if (!/^1[3-9]\d{9}$/.test(this.formData.account)) {
  123. uni.showToast({ title: '请输入正确的手机号码', icon: 'none' });
  124. return;
  125. }
  126. if (!this.formData.regionId) { // 校验ID是否存在
  127. uni.showToast({ title: '请选择所属区域', icon: 'none' });
  128. return;
  129. }
  130. console.log('校验通过,准备提交的数据:', this.formData);
  131. // 执行提交逻辑
  132. uni.showLoading({ title: '正在保存...' });
  133. let data = {
  134. name: this.formData.name,
  135. phone:this.formData.account,
  136. status: this.formData.status,
  137. areaId: this.formData.regionId,
  138. }
  139. if(this.formData.id == 0){
  140. addYwy(data).then(res=>{
  141. uni.hideLoading();
  142. uni.showToast({
  143. title: '保存成功',
  144. icon: 'success'
  145. });
  146. uni.navigateBack();
  147. })
  148. }else{
  149. data.isJob = this.formData.isJob?1:0;
  150. data.ywyId = this.formData.id;
  151. updateYwy(data).then(res=>{
  152. uni.hideLoading();
  153. uni.showToast({
  154. title: '保存成功',
  155. icon: 'success'
  156. });
  157. uni.navigateBack();
  158. })
  159. }
  160. }
  161. }
  162. }
  163. </script>
  164. <style lang="scss" scoped>
  165. /* 所有样式与上一版完全相同,无需改动 */
  166. .page-container {
  167. min-height: 100vh;
  168. background: linear-gradient(to bottom, #e4efff, #f5f6fa 40%);
  169. padding: 30rpx;
  170. box-sizing: border-box;
  171. }
  172. .form-card {
  173. background-color: #ffffff;
  174. border-radius: 20rpx;
  175. padding: 0 40rpx;
  176. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  177. }
  178. .form-item {
  179. padding: 30rpx 0;
  180. border-bottom: 1rpx solid #f0f0f0;
  181. &:last-child {
  182. border-bottom: none;
  183. }
  184. &.item-flex {
  185. display: flex;
  186. justify-content: space-between;
  187. align-items: center;
  188. }
  189. }
  190. .form-label {
  191. font-size: 30rpx;
  192. color: #333;
  193. font-weight: 500;
  194. }
  195. .form-input {
  196. margin-top: 20rpx;
  197. font-size: 28rpx;
  198. color: #333;
  199. }
  200. .placeholder {
  201. color: #c0c4cc;
  202. }
  203. .switch-wrapper {
  204. display: flex;
  205. align-items: center;
  206. font-size: 28rpx;
  207. color: #999;
  208. .status-active {
  209. color: #409eff;
  210. }
  211. switch {
  212. margin-left: 20rpx;
  213. transform: scale(0.8);
  214. }
  215. }
  216. .picker-value {
  217. display: flex;
  218. align-items: center;
  219. font-size: 28rpx;
  220. }
  221. .footer-save-button {
  222. position: fixed;
  223. left: 0;
  224. bottom: 0;
  225. width: 100%;
  226. padding: 20rpx 30rpx;
  227. background-color: #f5f6fa;
  228. box-sizing: border-box;
  229. padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
  230. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  231. }
  232. .save-btn {
  233. background-color: #409eff;
  234. color: #ffffff;
  235. border-radius: 50rpx;
  236. font-size: 32rpx;
  237. height: 90rpx;
  238. line-height: 90rpx;
  239. &::after {
  240. border: none;
  241. }
  242. }
  243. </style>