add_ywy.vue 6.1 KB

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