myInfo.vue 6.6 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" >
  7. <view class="user-title">头像</view>
  8. <view v-if="!data.images" @click="openFilePicker">默认图</view>
  9. <!-- <img v-else src="" @click="openFilePicker">-->
  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.userName" placeholder="请输入手机号" />
  25. <icon :style="{ backgroundImage: 'url(' + data.jtIcon + ')' }"></icon>
  26. </view>
  27. <!-- 证件号 -->
  28. <view class="form-label-input">
  29. <view class="phone-form-label"><text class="form-label-require">*</text>证件号</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. userName: '',
  69. userId: null,
  70. images: null,
  71. jtIcon: '',
  72. })
  73. onLoad((options) => {
  74. data.from = options.from || 'shouye';
  75. data.jtIcon= cacheManager.get('projectImg').nav_bar_jt_bottom;
  76. defaultImage1.url= cacheManager.get('projectImg').user_default_img;
  77. initPage();
  78. })
  79. function openFilePicker(){
  80. filePicker.value.upload()
  81. }
  82. function handleSelect(e) {
  83. console.log('eee',e)
  84. doUploadImage(e)
  85. }
  86. function genderSelect(genderValue){
  87. data.gender = genderValue;
  88. }
  89. function doUploadImage(params) {
  90. const filePath = params.tempFiles[0];
  91. const fileUrl = params.tempFilePaths;
  92. try {
  93. const suffix = filePath.file.name.split('.').pop();
  94. let req = {
  95. prefix: 'resource/',
  96. suffix: suffix,
  97. }
  98. uni.showLoading({
  99. title: '上传中...',
  100. mask: true
  101. });
  102. getAliyunPolicy(req).then(res => {
  103. if (res.code == 0) {
  104. const policyData = res.data;
  105. const formData = {
  106. key: policyData.key,
  107. policy: policyData.policy,
  108. signature: policyData.signature,
  109. OSSAccessKeyId: policyData.accessid,
  110. success_action_status: '200',
  111. }
  112. uni.uploadFile({
  113. url: policyData.uploadUrl,
  114. filePath: fileUrl[0],
  115. name: 'file',
  116. formData: formData,
  117. success(uploadRes) {
  118. if (uploadRes.statusCode === 200) {
  119. console.log('uploadRes',uploadRes)
  120. data.icon = `${policyData.downloadUrl}/${policyData.key}`;
  121. console.log( `${policyData.downloadUrl}/${policyData.key}`)
  122. uni.showToast({
  123. title: '上传成功',
  124. icon: 'success'
  125. });
  126. // data.images = { url: data.icon, extname: 'png', name: 'touxiang.png' }
  127. data.images = data.icon
  128. } else {
  129. uni.showToast({
  130. icon: 'none',
  131. title: '上传失败',
  132. });
  133. uni.hideLoading();
  134. }
  135. },
  136. fail(err) {
  137. uni.showToast({
  138. icon: 'none',
  139. title: '上传失败',
  140. });
  141. uni.hideLoading();
  142. }
  143. });
  144. } else {
  145. uni.showToast({
  146. title: '获取凭证失败',
  147. });
  148. uni.hideLoading();
  149. }
  150. })
  151. } catch (error) {
  152. uni.showToast({
  153. title: '上传失败',
  154. icon: 'none'
  155. });
  156. uni.hideLoading();
  157. }
  158. }
  159. function handleUpdate() {
  160. let arr = [];
  161. if (!data.realName) {
  162. arr.push('姓名');
  163. }
  164. if (!data.userName) {
  165. arr.push('电话');
  166. }
  167. if (!data.icon) {
  168. arr.push('头像');
  169. }
  170. if (!data.idcard) {
  171. arr.push('证件号');
  172. }
  173. if (!data.realName || !data.userName||!data.icon||!data.idcard) {
  174. uni.showToast({
  175. icon: 'none',
  176. title: `请完善${arr.join('、')}信息!`
  177. })
  178. return;
  179. }
  180. const opt = {
  181. gender: data.gender,
  182. icon: data.icon,
  183. idcard: data.idcard,
  184. realName: data.realName,
  185. userId: data.userId,
  186. userName: data.userName,
  187. }
  188. myApi.getMineUpdate(opt).then(res => {
  189. if (res.data) {
  190. uni.showToast({
  191. title: '更新成功'
  192. })
  193. handleBack();
  194. }
  195. })
  196. }
  197. function handleBack() {
  198. const pages = getCurrentPages();
  199. if (pages.length>1) {
  200. uni.navigateBack()
  201. } else {
  202. /* if (data.from == 'my') {
  203. uni.redirectTo({
  204. url: '/pages/client/my/index'
  205. })
  206. } else if (data.from == 'kaoshi') {
  207. uni.redirectTo({
  208. url: '/pages/client/ShouYe/shouye'
  209. })
  210. } else {
  211. uni.redirectTo({
  212. url: '/pages/client/my/index'
  213. })
  214. } */
  215. history.back();
  216. }
  217. }
  218. function initPage() {
  219. myApi.getMineUser().then(res => {
  220. const {gender,icon,idcard,realName,userName,userId} = res.data;
  221. data.gender = res.data.gender;
  222. data.icon = res.data.icon;
  223. data.idcard = res.data.idcard;
  224. data.realName = res.data.realName;
  225. data.userName = res.data.userName;
  226. data.userId = res.data.userId;
  227. data.images = data.icon?{ url: data.icon, extname: 'png', name: 'touxiang.png' }:defaultImage1.value
  228. })
  229. }
  230. </script>
  231. <style>
  232. </style>