myInfo.vue 6.7 KB

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