familyMemberVindicate.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <template>
  2. <view class="common_page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <cus-header :title="title" bgColor="#FFFFFF"></cus-header>
  4. <view class="form">
  5. <view class="avatar">
  6. <view class="title"><span>*</span>头像</view>
  7. <view class="adfac" style="margin: 24rpx 0 52rpx;">
  8. <view class="imgbox">
  9. <image class="imgbox-avatar" :src="memberInfo?.avatarPath||defaultAvatar" @click="changeAvatar"></image>
  10. </view>
  11. <image class="add-avatar" src="https://oss.familydaf.cn/sxsnfile/20251219/0b4d0f26464945dbae17491f1b529229.png" @click="changeAvatar"></image>
  12. </view>
  13. </view>
  14. <view class="item adfacjb">
  15. <view class="left">个人公益名称</view>
  16. <view class="right">
  17. <input min="3" class="setinp"v-model="memberInfo.welfareName" placeholder-style="color: #A1A4A9;font-size: 28rpx;" placeholder="请输入最少三个字"></input>
  18. </view>
  19. </view>
  20. <view class="item adfacjb">
  21. <view class="left">个人公益口号</view>
  22. <view class="right">
  23. <input class="setinp"v-model="memberInfo.welfareSlogan" placeholder-style="color: #A1A4A9;font-size: 28rpx;" placeholder="请输入个人公益口号"></input>
  24. </view>
  25. </view>
  26. </view>
  27. <view class="form">
  28. <view class="title">成员信息</view>
  29. <view class="pre adfac" style="margin-top: 40rpx;">
  30. <view class="pre-title"><span>*</span>人员类型</view>
  31. <view class="pre-content">
  32. <view style="font-size: 30rpx;color: #252525;text-align: right;">{{memberInfo.personnelType===2?'学生':'成人'}}</view>
  33. </view>
  34. </view>
  35. <view class="pre adfac">
  36. <view class="pre-title"><span>*</span>姓名</view>
  37. <view class="pre-content">
  38. <input v-model="memberInfo.name" style="font-size: 30rpx;color: #252525;text-align: right;" placeholder-style="color: #A1A4A9;font-size: 28rpx;" placeholder="请输入姓名"></input>
  39. </view>
  40. </view>
  41. <view class="pre adfac">
  42. <view class="pre-title"><span>*</span>身份证</view>
  43. <view class="pre-content">
  44. <input v-model="memberInfo.idCard" style="font-size: 30rpx;color: #252525;text-align: right;" placeholder-style="color: #A1A4A9;font-size: 28rpx;" placeholder="请输入身份证"></input>
  45. </view>
  46. </view>
  47. <view class="pre adfac">
  48. <view class="pre-title"><span style="color: #FFFFFF;">*</span>义工号</view>
  49. <view class="pre-content">
  50. <input v-model="memberInfo.volunteerNo" style="font-size: 30rpx;color: #252525;text-align: right;" placeholder-style="color: #A1A4A9;font-size: 28rpx;" placeholder="请输入义工号"></input>
  51. </view>
  52. </view>
  53. <view class="pre adfac">
  54. <view class="pre-title"><span>*</span>性别</view>
  55. <view class="pre-content adfac">
  56. <view class="pre-content-box adfac" @click="changeMember(0,'gender')">
  57. <image v-if="memberInfo.gender==0" src="https://oss.familydaf.cn/sxsnfile/20251218/ef2d43944c7e4cea8c6ebe0b4179f0fc.png"></image>
  58. <image v-else src="https://oss.familydaf.cn/sxsnfile/20251218/a27c9b23939d4d5085ff790ed4bd2d6a.png"></image>
  59. <text>男</text>
  60. </view>
  61. <view class="pre-content-box adfac" @click="changeMember(1,'gender')" style="margin-left: 114rpx;">
  62. <image v-if="memberInfo.gender==1" src="https://oss.familydaf.cn/sxsnfile/20251218/ef2d43944c7e4cea8c6ebe0b4179f0fc.png"></image>
  63. <image v-else src="https://oss.familydaf.cn/sxsnfile/20251218/a27c9b23939d4d5085ff790ed4bd2d6a.png"></image>
  64. <text>女</text>
  65. </view>
  66. </view>
  67. </view>
  68. <view class="pre adfac" v-if="memberInfo.personnelType==2">
  69. <view class="pre-title"><span>*</span>就读学校</view>
  70. <view class="pre-content">
  71. <input v-model="memberInfo.currentSchool" style="font-size: 30rpx;color: #252525;text-align: right;" placeholder-style="color: #A1A4A9;font-size: 28rpx;" placeholder="请输入就读学校"></input>
  72. </view>
  73. </view>
  74. <view class="pre adfac">
  75. <view class="pre-title">会员渠道</view>
  76. <view class="pre-content">
  77. <picker v-if="channelList.length > 0" :range="channelList" range-key="channelName" :value="selectedChannelIndex" @change="onChannelChange">
  78. <view style="font-size: 30rpx;color: #252525;text-align: right;">
  79. {{ memberChannelNames || '请选择渠道' }}
  80. </view>
  81. </picker>
  82. <view v-else style="font-size: 30rpx;color: #A1A4A9;text-align: right;">暂无可用渠道</view>
  83. </view>
  84. </view>
  85. </view>
  86. <view class="btn" @click="handleSave">保存</view>
  87. <qf-image-cropper :width="500" :height="500" :radius="30" @crop="handleCrop" ref="qicRef" v-if="qicShow"></qf-image-cropper>
  88. </view>
  89. </template>
  90. <script setup name="">
  91. import { BaseApi2 } from '../common/api/baseApi';
  92. import CusHeader from '@/components/CusHeader/index.vue'
  93. import { onLoad } from '@dcloudio/uni-app'
  94. import { ref, getCurrentInstance, nextTick, watch } from 'vue'
  95. const { proxy } = getCurrentInstance()
  96. const isValidIdCard = (idCard) => idCard && proxy.$reg.idCard(idCard)
  97. const calcAge = (idCard) => {
  98. if (!isValidIdCard(idCard)) return -1
  99. const birthStr = idCard.substring(6, 14)
  100. const birth = new Date(
  101. parseInt(birthStr.substring(0, 4)),
  102. parseInt(birthStr.substring(4, 6)) - 1,
  103. parseInt(birthStr.substring(6, 8))
  104. )
  105. const today = new Date()
  106. let age = today.getFullYear() - birth.getFullYear()
  107. const m = today.getMonth() - birth.getMonth()
  108. if (m < 0 || (m === 0 && today.getDate() < birth.getDate())) age--
  109. return age
  110. }
  111. const title = ref('添加家庭成员')
  112. const defaultAvatar = ref('https://oss.familydaf.cn/sxsnfile/20251218/3821654e080945998d464f3c3aa64122.png')
  113. const memberInfo = ref({
  114. userId:'',
  115. personnelType:2,
  116. name:'',
  117. idCard:'',
  118. volunteerNo:'',
  119. gender:0,
  120. currentSchool:''
  121. })
  122. const qicShow = ref(false)
  123. // 渠道相关
  124. const channelList = ref([]) // 会员渠道列表(可选项)
  125. const memberChannelNames = ref('') // 成员已加入的渠道名称(逗号分隔)
  126. const selectedChannelId = ref('') // 已选渠道 ID(字符串)
  127. const selectedChannelIndex = ref(-1) // picker 当前选中下标,-1 表示未选
  128. const hasSelectedChannel = ref(false) // 用户是否主动选择过渠道
  129. const turnType = ref('')
  130. const loadChannelList = () => {
  131. return new Promise((resolve) => {
  132. proxy.$api.get('/channel/list', {}).then(({ data: res }) => {
  133. if (res.code !== 0) {
  134. resolve()
  135. return
  136. }
  137. channelList.value = res.data || []
  138. // 若 loadMemberChannel 已先完成并设置了 selectedChannelId,补充同步 index
  139. if (selectedChannelId.value) {
  140. const idx = channelList.value.findIndex(c => String(c.id) === selectedChannelId.value)
  141. if (idx >= 0) selectedChannelIndex.value = idx
  142. }
  143. resolve()
  144. })
  145. })
  146. }
  147. const loadMemberChannel = (memberId) => {
  148. proxy.$api.get('/member/channelList', { memberId }).then(({ data: res }) => {
  149. if (res.code !== 0) return
  150. const list = res.data || []
  151. if (list.length > 0) {
  152. // 显示所有已加入的渠道名称
  153. memberChannelNames.value = list.map(c => c.channelName).join('、')
  154. // 默认选中第一个渠道
  155. selectedChannelId.value = String(list[0].id)
  156. const idx = channelList.value.findIndex(c => String(c.id) === selectedChannelId.value)
  157. if (idx >= 0) selectedChannelIndex.value = idx
  158. }
  159. })
  160. }
  161. const changeMember = (value,key) => {
  162. memberInfo.value[key] = value;
  163. }
  164. // 监听身份证变化,自动设置人员类型(>=18 为成人,<18 为学生)
  165. watch(() => memberInfo.value.idCard, (val) => {
  166. const age = calcAge(val)
  167. if (age >= 0) {
  168. memberInfo.value.personnelType = age >= 18 ? 1 : 2
  169. }
  170. })
  171. const handleSave = async () => {
  172. if(!memberInfo.value.avatarPath) return proxy.$showToast('请上传头像')
  173. if(memberInfo.value.welfareName&&memberInfo.value.welfareName.length<3) return proxy.$showToast('个人公益名称至少三个字')
  174. if(!memberInfo.value.name) return proxy.$showToast('请输入姓名')
  175. if(!proxy.$reg.idCard(memberInfo.value.idCard)) return proxy.$showToast('请输入正确的身份证号')
  176. if(!memberInfo.value.currentSchool&&memberInfo.value.personnelType==2) return proxy.$showToast('请输入就读学校')
  177. memberInfo.value.gender = memberInfo.value.gender??1;
  178. const checkMsgs = ['welfareName','welfareSlogan','name'].map(k=>checkMsg(k));
  179. const tipRes = (await Promise.all(checkMsgs)).filter(item=>item!=='ok');
  180. if(tipRes&&tipRes.length) return proxy.$showModal(tipRes.join('、'))
  181. if(!memberInfo.value.userId) memberInfo.value.userId = JSON.parse(uni.getStorageSync('userInfo')).id;
  182. proxy.$api[memberInfo.value.id?'put':'post']('/core/family/member',memberInfo.value).then(({data:res})=>{
  183. if(res.code!==0) return proxy.$showToast(res.msg)
  184. const memberId = memberInfo.value.id || res.data?.id
  185. // 编辑时(memberInfo.value.id 存在)或用户主动选择了渠道,才调用 setMemberChannel
  186. if (memberId && (hasSelectedChannel.value || memberInfo.value.id)) {
  187. setMemberChannel(memberId) // 异步,不 await,不阻断跳转
  188. }
  189. proxy.$showToast(memberInfo.value.id?'编辑成功':'添加成功')
  190. setTimeout(()=>{
  191. if(turnType.value==='my'){
  192. uni.reLaunch({
  193. url:'/pages/my'
  194. })
  195. return
  196. }
  197. uni.redirectTo({
  198. url:'/pagesMy/familyMember'
  199. })
  200. },1000)
  201. })
  202. }
  203. const onChannelChange = (e) => {
  204. const idx = e.detail.value
  205. selectedChannelIndex.value = idx
  206. selectedChannelId.value = String(channelList.value[idx].id)
  207. memberChannelNames.value = channelList.value[idx].channelName
  208. hasSelectedChannel.value = true
  209. }
  210. const setMemberChannel = (memberId) => {
  211. proxy.$api.post(`/member/setChannels?memberId=${memberId}&channelIds=${selectedChannelId.value}`, {}).then(({ data: res }) => {
  212. if (res.code !== 0) proxy.$showToast(res.msg)
  213. }).catch(() => {
  214. proxy.$showToast('渠道设置失败,请稍后重试')
  215. })
  216. }
  217. const checkMsg = async msg => {
  218. const msgAlert = {
  219. welfareName:'家庭公益名称',
  220. welfareSlogan:'家庭公益口号',
  221. name:'姓名'
  222. }
  223. return new Promise((resolve,reject)=>{
  224. proxy.$api.detectionContent(memberInfo.value[msg]).then(res=>{
  225. resolve(res.code!==0?(`${msgAlert[msg]}:`+res.msg):'ok')
  226. })
  227. })
  228. }
  229. const getMemberInfo = (id) => {
  230. proxy.$api.get(`/core/family/member/${id}`).then(({data:res})=>{
  231. if(res.code!==0) return proxy.$showToast(res.msg)
  232. if(res.data){
  233. memberInfo.value = {...memberInfo.value,...res.data}
  234. // 编辑模式下,如果后端未返回 personnelType,根据 idCard 重新计算
  235. const age = calcAge(memberInfo.value.idCard)
  236. if (age >= 0 && (memberInfo.value.personnelType == null || memberInfo.value.personnelType === '')) {
  237. memberInfo.value.personnelType = age >= 18 ? 1 : 2
  238. }
  239. }
  240. })
  241. }
  242. const changeAvatar = () => {
  243. uni.showActionSheet({
  244. itemList: ['从手机相册选择'],
  245. success: (res) => {
  246. if (res.tapIndex === 0) { // 从手机相册选择
  247. chooseLocalImage();
  248. }
  249. }
  250. });
  251. }
  252. // 从本地选择图片并显示
  253. const chooseLocalImage = () => {
  254. uni.chooseImage({
  255. count: 1, // 只能选择一张
  256. sizeType: ['compressed'], // 压缩图片
  257. sourceType: ['album', 'camera'], // 可以从相册或相机选择
  258. success: (res) => {
  259. const tempFilePaths = res.tempFilePaths;
  260. if (tempFilePaths && tempFilePaths.length > 0) {
  261. proxy.$api.detectionContent(tempFilePaths[0],2).then(res=>{
  262. if(res.code!==0) return proxy.$showToast(res.msg)
  263. qicShow.value = true;
  264. nextTick(()=>{
  265. proxy.$refs.qicRef.initImage(tempFilePaths[0],true)
  266. })
  267. })
  268. }
  269. },
  270. fail: (err) => {
  271. console.error('选择图片失败', err);
  272. }
  273. });
  274. }
  275. const handleCrop = async (e) => {
  276. const result = await uploadFilePromise(e.tempFilePath);
  277. memberInfo.value.avatarPath = result||'';
  278. qicShow.value = false;
  279. }
  280. const uploadFilePromise = (url) => {
  281. return new Promise((resolve, reject) => {
  282. let a = uni.uploadFile({
  283. url: BaseApi2+'/sys/oss/upload',
  284. filePath: url,
  285. name: 'file',
  286. success: (res) => {
  287. setTimeout(() => {
  288. let data = JSON.parse(res.data)
  289. if(data&&data.code===0){
  290. resolve(data.data);
  291. }else proxy.$showToast(data?.msg)
  292. }, 1000);
  293. },
  294. fail: err =>{
  295. resolve('');
  296. }
  297. });
  298. });
  299. };
  300. onLoad(async (options)=>{
  301. turnType.value = options.type;
  302. await loadChannelList()
  303. const id = options?.id;
  304. if(id){
  305. title.value = '编辑家庭成员';
  306. getMemberInfo(id)
  307. loadMemberChannel(id)
  308. }
  309. })
  310. </script>
  311. <style scoped lang="scss">
  312. .common_page{
  313. padding-bottom: 184rpx;
  314. .form{
  315. background: #FFFFFF;
  316. border-radius: 24rpx;
  317. margin-top: 20rpx;
  318. padding: 36rpx 24rpx 0;
  319. .avatar{
  320. .title{
  321. font-family: PingFang-SC, PingFang-SC;
  322. font-weight: bold;
  323. font-size: 30rpx;
  324. color: #252525;
  325. line-height: 42rpx;
  326. label{
  327. font-family: PingFangSC, PingFang SC;
  328. font-weight: 400;
  329. font-size: 30rpx;
  330. color: #F4657A;
  331. line-height: 42rpx;
  332. }
  333. }
  334. .imgbox{
  335. width: 122rpx;
  336. height: 122rpx;
  337. &-avatar{
  338. width: 100%;
  339. height: 100%;
  340. border-radius: 50%;
  341. }
  342. }
  343. .add-avatar{
  344. margin-left: 40rpx;
  345. width: 122rpx;
  346. height: 122rpx;
  347. border-radius: 50%;
  348. }
  349. border-bottom: 1rpx solid #E6E6E6;
  350. }
  351. .item{
  352. height: 90rpx;
  353. box-shadow: inset 0rpx -1rpx 0rpx 0rpx #E6E6E6;
  354. .left{
  355. width: 200rpx;
  356. font-family: PingFangSC, PingFang SC;
  357. font-weight: 400;
  358. font-size: 28rpx;
  359. color: #676775;
  360. line-height: 40rpx;
  361. }
  362. .right{
  363. width: calc(100% - 200rpx);
  364. }
  365. }
  366. .title{
  367. font-family: PingFang-SC, PingFang-SC;
  368. font-weight: bold;
  369. font-size: 36rpx;
  370. color: #151B29;
  371. line-height: 48rpx;
  372. }
  373. .pre{
  374. padding: 25rpx 0;
  375. box-shadow: inset 0rpx -1rpx 0rpx 0rpx #F2F2F2;
  376. &-title{
  377. width: 209rpx;
  378. font-family: PingFangSC, PingFang SC;
  379. font-weight: 400;
  380. font-size: 28rpx;
  381. color: #676775;
  382. line-height: 40rpx;
  383. label{
  384. font-family: PingFangSC, PingFang SC;
  385. font-weight: 400;
  386. font-size: 30rpx;
  387. color: #F4657A;
  388. line-height: 42rpx;
  389. margin-right: 4rpx;
  390. }
  391. }
  392. &-content{
  393. width: calc(100% - 209rpx);
  394. display: flex;
  395. align-items: center;
  396. justify-content: flex-end;
  397. &-box{
  398. margin-left: 80rpx;
  399. image{
  400. width: 28rpx;
  401. height: 28rpx;
  402. }
  403. text{
  404. font-family: PingFangSC, PingFang SC;
  405. font-weight: 400;
  406. font-size: 30rpx;
  407. color: #252525;
  408. line-height: 42rpx;
  409. margin-left: 16rpx;
  410. }
  411. }
  412. }
  413. }
  414. }
  415. .btn{
  416. width: calc(100% - 80rpx);
  417. height: 90rpx;
  418. background: #B7F358;
  419. border-radius: 45rpx;
  420. font-family: PingFang-SC, PingFang-SC;
  421. font-weight: bold;
  422. font-size: 32rpx;
  423. color: #151B29;
  424. line-height: 90rpx;
  425. text-align: center;
  426. letter-spacing: 2rpx;
  427. position: fixed;
  428. left: 40rpx;
  429. bottom: 64rpx;
  430. }
  431. }
  432. </style>