updateUser.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <template>
  2. <view class="uni-app">
  3. <view class="status-bar" />
  4. <view v-if="!showImageCropper" class="main-container">
  5. <wk-nav-bar title="个人信息">
  6. <!-- #ifndef MP-WEIXIN -->
  7. <button
  8. class="button white-btn"
  9. @click="handleSave">
  10. 保存
  11. </button>
  12. <!-- #endif -->
  13. </wk-nav-bar>
  14. <view class="container">
  15. <view class="scroll-content">
  16. <view class="avatar-box">
  17. <view class="box-title">
  18. 头像
  19. </view>
  20. <view class="box-cell">
  21. <wk-avatar
  22. :name="userInfo.realname"
  23. :avatar="userInfo.img"
  24. :preview="false"
  25. class="avatar" />
  26. <text class="wk wk-arrow-right icon" @click="handlToEditAvatar" />
  27. </view>
  28. </view>
  29. <wk-form ref="form" :fields="fieldArr" />
  30. </view>
  31. </view>
  32. <!-- #ifdef MP-WEIXIN -->
  33. <view class="footer-btn-group">
  34. <button class="button" @click="handleSave">
  35. 保存
  36. </button>
  37. </view>
  38. <!-- #endif -->
  39. <uni-popup ref="popupOptions">
  40. <view class="pop-wrapper">
  41. <view
  42. v-for="(item, index) in popOptions"
  43. :key="index"
  44. class="pop-item"
  45. @click.stop="handlePopChoose(item)">
  46. {{ item.label }}
  47. </view>
  48. </view>
  49. </uni-popup>
  50. </view>
  51. <view v-else class="cropper-wrapper">
  52. <invinbg-image-cropper
  53. :src="tempFilePath"
  54. @confirm="handleCropperConfirm"
  55. @cancel="handleCropperCancel" />
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. import { AdminQueryLoginUser, AdminSetUser } from 'API/admin'
  61. import InvinbgImageCropper from './invinbg-image-cropper/index.vue'
  62. import { isEmail } from '@/utils/lib.js'
  63. import { BASE_URL_FN } from '@/config.js'
  64. import { mapMutations } from 'vuex'
  65. import Fields from '@/utils/fields.js'
  66. export default {
  67. name: 'PersonalUser',
  68. components: {
  69. InvinbgImageCropper
  70. },
  71. data() {
  72. return {
  73. userInfo: {},
  74. popOptions: [
  75. { label: '修改头像', value: 'update' }
  76. ],
  77. fieldArr: [
  78. new Fields({
  79. name: '姓名',
  80. fieldName: 'realname',
  81. formType: 'text',
  82. isNull: 1
  83. }),
  84. new Fields({
  85. name: '性别',
  86. fieldName: 'sex',
  87. formType: 'select',
  88. setting: [
  89. { label: '男', value: 1 },
  90. { label: '女', value: 2 }
  91. ]
  92. }),
  93. new Fields({
  94. name: '邮箱',
  95. fieldName: 'email',
  96. formType: 'email'
  97. }),
  98. new Fields({
  99. name: '岗位',
  100. fieldName: 'post',
  101. formType: 'text'
  102. }),
  103. new Fields({
  104. name: '手机',
  105. fieldName: 'mobile',
  106. formType: 'mobile',
  107. disabled: true
  108. }),
  109. new Fields({
  110. name: '部门',
  111. fieldName: 'deptName',
  112. formType: 'text',
  113. disabled: true
  114. })
  115. ],
  116. showImageCropper: false,
  117. tempFilePath: '',
  118. cropFilePath: ''
  119. }
  120. },
  121. onLoad() {
  122. this.getUserInfo()
  123. },
  124. methods: {
  125. ...mapMutations({
  126. setUserInfo: 'user/SET_USER_INFO'
  127. }),
  128. getUserInfo() {
  129. AdminQueryLoginUser().then(res => {
  130. this.userInfo = res
  131. this.setUserInfo(res)
  132. this.$nextTick(() => {
  133. if (this.$refs.form) {
  134. this.$refs.form.setForm({
  135. ...res,
  136. sex: res.sex ? [{ label: this.getSex(res.sex), value: res.sex }] : []
  137. })
  138. }
  139. })
  140. }).catch()
  141. },
  142. getSex(sex) {
  143. if (isNaN(Number(sex))) return sex
  144. if (Number(sex) === 1) return '男'
  145. else if (Number(sex) === 2) return '女'
  146. return ''
  147. },
  148. /**
  149. * 去修改头像
  150. */
  151. handlToEditAvatar() {
  152. this.$refs.popupOptions.open()
  153. },
  154. handlePopChoose() {
  155. this.$refs.popupOptions.close()
  156. const that = this
  157. uni.chooseImage({
  158. count: 1, // 默认9
  159. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  160. sourceType: ['album'], // 从相册选择
  161. success: (res) => {
  162. that.showImageCropper = true
  163. that.$nextTick(function() {
  164. that.tempFilePath = res.tempFilePaths.shift()
  165. })
  166. }
  167. })
  168. },
  169. handleCropperConfirm(e) {
  170. console.log('confirm: ', e)
  171. this.showImageCropper = false
  172. this.tempFilePath = ''
  173. this.cropFilePath = e.detail.tempFilePath;
  174. const that = this
  175. const appid = uni.getStorageSync('appid') || ''
  176. const header = {
  177. 'Admin-Token': uni.getStorageSync('token') || ''
  178. }
  179. if (appid) {
  180. header.k = appid
  181. }
  182. uni.uploadFile({
  183. url: BASE_URL_FN() + 'adminUser/updateImg',
  184. fileType: 'image',
  185. name: 'file',
  186. header,
  187. filePath: that.cropFilePath,
  188. success: res => {
  189. that.$refreshAndToPrev(that, false)
  190. that.getUserInfo()
  191. },
  192. fail: (error) => {
  193. console.log('err', error)
  194. }
  195. })
  196. },
  197. handleCropperCancel() {
  198. this.showImageCropper = false
  199. this.$nextTick(() => {
  200. if (this.$refs.form) {
  201. this.$refs.form.setForm({
  202. ...this.userInfo,
  203. sex: this.userInfo.sex ? [{ label: this.getSex(this.userInfo.sex), value: this.userInfo.sex }] : []
  204. })
  205. }
  206. })
  207. },
  208. /**
  209. * 保存
  210. */
  211. handleSave() {
  212. this.$refs.form.getForm().then(form => {
  213. form = form.entity
  214. delete form.mobile
  215. delete form.deptName
  216. form.userId = this.userInfo.userId
  217. form.username = this.userInfo.username
  218. console.log('save: ', form)
  219. AdminSetUser(form).then(res => {
  220. this.getUserInfo()
  221. this.$toast('保存成功')
  222. this.$refreshAndToPrev(this)
  223. }).catch()
  224. }).catch(() => {})
  225. }
  226. }
  227. }
  228. </script>
  229. <style scoped lang="scss">
  230. .main-container {
  231. .container {
  232. flex: 1;
  233. width: 100%;
  234. overflow: hidden;
  235. .scroll-content {
  236. width: 100%;
  237. height: 100%;
  238. overflow: auto;
  239. }
  240. .avatar-box {
  241. background-color: white;
  242. padding: 20rpx 30rpx;
  243. .box-title {
  244. font-size: 30rpx;
  245. }
  246. .box-cell {
  247. margin-top: 20rpx;
  248. @include left;
  249. .avatar {
  250. width: 80rpx;
  251. height: 80rpx;
  252. }
  253. .icon {
  254. flex: 1;
  255. font-size: 28rpx;
  256. color: $light;
  257. text-align: right;
  258. margin-left: 10rpx;
  259. }
  260. }
  261. }
  262. }
  263. }
  264. .cropper-wrapper {
  265. flex: 1;
  266. width: 100%;
  267. }
  268. </style>