myInfo.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <view class="my-info-page">
  3. <!-- 导航区域 -->
  4. <customNavbarVue title="个人信息" :show-back-btn="true" @back="handleBack"></customNavbarVue>
  5. <!-- 头像 -->
  6. <view class="user-img-box" @click="openFilePicker">
  7. <view class="user-title">头像</view>
  8. <img :src="data.images" class="user-img" v-if="data.images">
  9. <view class="user-add-box" v-else ></view>
  10. <uni-file-picker ref="filePicker" v-show="false" return-type="object" v-model="data.images" :del-icon="false" mode="grid" disable-preview :auto-upload="false"
  11. @select="handleSelect" file-mediatype="image" class="phone-file-picker user-file-picker">
  12. </uni-file-picker>
  13. <icon class="user-jt-icon" :style="{ backgroundImage: 'url(' + data.jtIcon + ')' }"></icon>
  14. </view>
  15. <!-- 姓名 -->
  16. <view class="form-label-input">
  17. <view class="phone-form-label"><text class="form-label-require">*</text>姓名</view>
  18. <input v-model="data.realName" placeholder="请输入姓名" />
  19. <icon :style="{ backgroundImage: 'url(' + data.jtIcon + ')' }"></icon>
  20. </view>
  21. <!-- 证件号 -->
  22. <view class="form-label-input">
  23. <view class="phone-form-label" @click="idCardChange">
  24. <text class="form-label-require">*</text>
  25. <text v-if="data.idtype ==1">身份证号</text>
  26. <text v-if="data.idtype ==2">护照号</text>
  27. <icon @click="idCardChange" class="change-icon">切换</icon>
  28. </view>
  29. <input v-model="data.idcard" placeholder="请输入证件号" />
  30. <icon :style="{ backgroundImage: 'url(' + data.jtIcon + ')' }"></icon>
  31. </view>
  32. <!-- 电话 -->
  33. <view></view>
  34. <!-- 性别 -->
  35. <view class="form-label-radio">
  36. <view class="phone-form-label"><text class="form-label-require">*</text>性别</view>
  37. <view class="form-radio-group">
  38. <view class="form-radio-item" :class="{genderActive: data.gender===1}" @click="genderSelect(1)">男</view>
  39. <view class="form-radio-item" :class="{genderActive: data.gender===2}" @click="genderSelect(2)">女</view>
  40. </view>
  41. </view>
  42. <button type="default" class="phone-green-btn info-btn" @click="handleUpdate">修改资料</button>
  43. </view>
  44. </template>
  45. <script setup>
  46. import customNavbarVue from "@/components/custom-navbar/custom-navbar.vue";
  47. import * as myApi from "@/api/my.js";
  48. import {getAliyunPolicy} from "@/api/jiazheng.js"
  49. import {ref,reactive} from "vue"
  50. import {onLoad} from "@dcloudio/uni-app"
  51. import cacheManager from '@/utils/cacheManager.js';
  52. const filePicker = ref(null)
  53. const defaultImage1 = ref({
  54. url:'', extname: 'png', name: 'moren.png'
  55. })
  56. const imageStyles = ref({
  57. width: '50rpx',
  58. height: '50rpx',
  59. background: 'red'
  60. });
  61. const data = reactive({
  62. from: 'shouye', // my | kaoshi | shouye
  63. gender: 0,
  64. icon: '',
  65. realName: '',
  66. idcard: '',
  67. userId: null,
  68. images: null,
  69. jtIcon: '',
  70. idtype: 1
  71. })
  72. onLoad((options) => {
  73. data.from = options.from || 'shouye';
  74. data.jtIcon= cacheManager.get('projectImg').nav_bar_jt_bottom;
  75. defaultImage1.url= cacheManager.get('projectImg').user_default_img;
  76. initPage();
  77. })
  78. function idCardChange() {
  79. data.idtype = data.idtype == 1 ? 2 : 1;
  80. }
  81. function openFilePicker(){
  82. filePicker.value.chooseFiles()
  83. }
  84. function handleSelect(e) {
  85. console.log('eee',e)
  86. doUploadImage(e)
  87. }
  88. function genderSelect(genderValue){
  89. data.gender = genderValue;
  90. }
  91. function doUploadImage(params) {
  92. const filePath = params.tempFiles[0];
  93. const fileUrl = params.tempFilePaths;
  94. try {
  95. const suffix = filePath.file.name.split('.').pop();
  96. let req = {
  97. prefix: 'resource/',
  98. suffix: suffix,
  99. }
  100. uni.showLoading({
  101. title: '上传中...',
  102. mask: true
  103. });
  104. getAliyunPolicy(req).then(res => {
  105. if (res.code == 0) {
  106. const policyData = res.data;
  107. const formData = {
  108. key: policyData.key,
  109. policy: policyData.policy,
  110. signature: policyData.signature,
  111. OSSAccessKeyId: policyData.accessid,
  112. success_action_status: '200',
  113. }
  114. uni.uploadFile({
  115. url: policyData.uploadUrl,
  116. filePath: fileUrl[0],
  117. name: 'file',
  118. formData: formData,
  119. success(uploadRes) {
  120. if (uploadRes.statusCode === 200) {
  121. console.log('uploadRes',uploadRes)
  122. data.icon = `${policyData.downloadUrl}/${policyData.key}`;
  123. console.log( `${policyData.downloadUrl}/${policyData.key}`)
  124. uni.showToast({
  125. title: '上传成功',
  126. icon: 'success'
  127. });
  128. // data.images = { url: data.icon, extname: 'png', name: 'touxiang.png' }
  129. data.images = data.icon
  130. } else {
  131. uni.showToast({
  132. icon: 'none',
  133. title: '上传失败',
  134. });
  135. uni.hideLoading();
  136. }
  137. },
  138. fail(err) {
  139. uni.showToast({
  140. icon: 'none',
  141. title: '上传失败',
  142. });
  143. uni.hideLoading();
  144. }
  145. });
  146. } else {
  147. uni.showToast({
  148. title: '获取凭证失败',
  149. });
  150. uni.hideLoading();
  151. }
  152. })
  153. } catch (error) {
  154. uni.showToast({
  155. title: '上传失败',
  156. icon: 'none'
  157. });
  158. uni.hideLoading();
  159. }
  160. }
  161. function handleUpdate() {
  162. let arr = [];
  163. if (!data.realName) {
  164. arr.push('姓名');
  165. }
  166. if (!data.icon) {
  167. arr.push('头像');
  168. }
  169. if (!data.idcard) {
  170. arr.push('证件号');
  171. }
  172. if (!data.realName ||!data.icon||!data.idcard) {
  173. uni.showToast({
  174. icon: 'none',
  175. title: `请完善${arr.join('、')}信息!`
  176. })
  177. return;
  178. }
  179. const opt = {
  180. gender: data.gender,
  181. icon: data.icon,
  182. idcard: data.idcard,
  183. realName: data.realName,
  184. userId: data.userId,
  185. idtype: data.idtype
  186. }
  187. myApi.getMineUpdate(opt).then(res => {
  188. if (res.data) {
  189. uni.showToast({
  190. title: '更新成功'
  191. })
  192. handleBack();
  193. }
  194. })
  195. }
  196. function handleBack() {
  197. const pages = getCurrentPages();
  198. if (pages.length>1) {
  199. uni.navigateBack()
  200. } else {
  201. /* if (data.from == 'my') {
  202. uni.redirectTo({
  203. url: '/pages/client/my/index'
  204. })
  205. } else if (data.from == 'kaoshi') {
  206. uni.redirectTo({
  207. url: '/pages/client/ShouYe/shouye'
  208. })
  209. } else {
  210. uni.redirectTo({
  211. url: '/pages/client/my/index'
  212. })
  213. } */
  214. history.back();
  215. }
  216. }
  217. function initPage() {
  218. myApi.getMineUser().then(res => {
  219. const {gender,icon,idcard,realName,userId} = res.data;
  220. data.gender = res.data.gender;
  221. data.icon = res.data.icon;
  222. data.idcard = res.data.idcard;
  223. data.realName = res.data.realName;
  224. data.userId = res.data.userId;
  225. // data.images = data.icon?{ url: data.icon, extname: 'png', name: 'touxiang.png' }:defaultImage1.value
  226. data.images = data.icon?data.icon:defaultImage1.value
  227. })
  228. }
  229. </script>
  230. <style>
  231. </style>