myInfo.vue 6.2 KB

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