person.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <template>
  2. <view class="main">
  3. <!-- #ifdef APP-PLUS -->
  4. <cu-custom bgColor="bg-blue">
  5. <block slot="content">我的</block>
  6. </cu-custom>
  7. <!-- #endif -->
  8. <view class="person-head">
  9. <lsj-upload ref="lsjUpload" childId="upload1" width="160rpx" height="140rpx" :option="option" :debug="false"
  10. :instantly="false" @uploadEnd="onuploadEnd" @change="afterRead">
  11. <view class="cu-avatar xl round margin-left-sm" :style="`background-image:url('${avatar}')`"></view>
  12. </lsj-upload>
  13. <view class="person-head-box">
  14. <view class="person-head-nickname">{{userInfo.name?userInfo.name:''}}</view>
  15. <view class="person-head-username">ID:{{userInfo.loginName?userInfo.loginName:''}}</view>
  16. </view>
  17. </view>
  18. <u-cell-group :border="false" customStyle="padding: 5px">
  19. <u-cell title="签名" icon="home-fill" :iconStyle="{color: '#0081ff'}" isLink
  20. url="/pages/user/setting/signature/signature"></u-cell>
  21. <u-cell title="部门" icon="list-dot" :iconStyle="{color: '#0081ff'}"
  22. :value="userInfo.officeDTO && userInfo.officeDTO.name"></u-cell>
  23. <!-- <u-cell
  24. title="岗位"
  25. icon="account-fill"
  26. :iconStyle="{color: '#0081ff'}"
  27. :value="userInfo.post && userInfo.post.name"
  28. ></u-cell> -->
  29. <u-cell title="角色" icon="man-add-fill" :iconStyle="{color: '#0081ff'}"
  30. :value="userInfo.roleNames?userInfo.roleNames:''"></u-cell>
  31. <u-cell title="电话" icon="phone-fill" :iconStyle="{color: '#0081ff'}"
  32. :value="userInfo.phone?userInfo.phone:''"></u-cell>
  33. <u-cell title="手机号" icon="phone-fill" :iconStyle="{color: '#0081ff'}"
  34. :value="userInfo.mobile?userInfo.mobile:''"></u-cell>
  35. <!-- <u-cell
  36. title="邮箱"
  37. icon="email-fill"
  38. :iconStyle="{color: '#0081ff'}"
  39. :value="userInfo.email"
  40. ></u-cell> -->
  41. <u-cell title="修改密码" icon="edit-pen" isLink :iconStyle="{color: '#e54d42'}"
  42. url="/pages/user/setting/password/password"></u-cell>
  43. </u-cell-group>
  44. <view class="padding-xl">
  45. <u-button type="primary" text="退出登录" @click="outlogin"></u-button>
  46. <!-- <u-gap height="80" bgColor="#fff"></u-gap> -->
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. import {
  52. mapState,
  53. mapMutations,
  54. mapActions
  55. } from 'vuex'
  56. import {
  57. isImageFormat
  58. } from "@/common/util.js"
  59. import BASE_URL from '@/config.js'
  60. import * as $auth from "@/common/auth.js"
  61. import userService from "@/api/sys/userService"
  62. import loginService from "@/api/auth/loginService"
  63. import fileService from "@/api/file/fileService.js"
  64. export default {
  65. name: "person",
  66. computed: mapState({
  67. userInfo: (state) => state.user.userInfo,
  68. avatar: (state) => BASE_URL + state.user.avatar
  69. }),
  70. data() {
  71. return {
  72. files: [],
  73. // 上传接口参数
  74. option: {
  75. url: BASE_URL + '/gwfile/upload?uploadPath=userdir',
  76. name: 'file',
  77. header: {
  78. "token": $auth.getUserToken()
  79. },
  80. },
  81. }
  82. },
  83. methods: {
  84. ...mapActions(['refreshUserInfo']),
  85. /**
  86. * 修改密码
  87. */
  88. toPassword() {
  89. uni.navigateTo({
  90. url: '/pages/user/setting/password/password'
  91. })
  92. },
  93. // 新增图片
  94. afterRead(files) {
  95. uni.showLoading({
  96. title: "更新中"
  97. })
  98. let lists = [...files.values()]
  99. this.files = files
  100. for (let i = 0; i < lists.length; i++) {
  101. if (lists[i].type == 'success') continue;
  102. if (!isImageFormat(lists[i].name)) {
  103. uni.showToast({
  104. title: '请上传图片',
  105. icon: 'error'
  106. })
  107. } else {
  108. if (lists[i].size > 200 * 1024) {
  109. this.compressAPP(lists[i], 150).then(res => {
  110. this.files.get(lists[i].name).file = res
  111. this.upload(lists[i].name)
  112. })
  113. } else {
  114. this.upload(lists[i].name)
  115. }
  116. }
  117. }
  118. },
  119. // app图片压缩
  120. compressAPP(urlData, targetSizeKB, initialQuality = 1.0) {
  121. const maxQuality = 1.0;
  122. const minQuality = 0.0;
  123. const tolerance = 0.01; // 根据需要调整公差
  124. let that = this
  125. return new Promise((resolve, reject) => {
  126. let binarySearch = (min, max) => {
  127. const midQuality = (min + max) / 2;
  128. uni.getImageInfo({
  129. src: urlData.path,
  130. success: function(res) {
  131. let img = new Image()
  132. img.src = res.path
  133. img.onload = function() {
  134. const canvas = document.createElement('canvas');
  135. const ctx = canvas.getContext('2d');
  136. canvas.width = img.width;
  137. canvas.height = img.height;
  138. ctx.clearRect(0, 0, canvas.width, canvas.height);
  139. ctx.drawImage(img, 0, 0, canvas.width, canvas
  140. .height);
  141. // 使用异步的 toBlob 方法
  142. canvas.toBlob(async (blob) => {
  143. const fileSizeKB = blob.size / 1024;
  144. if (Math.abs(fileSizeKB -
  145. targetSizeKB) <
  146. tolerance || max - min <
  147. tolerance) {
  148. // 当前质量足够接近目标大小,使用当前质量解析
  149. const file = new File([blob], urlData
  150. .name, {
  151. type: blob.type,
  152. lastModified: new Date()
  153. .getTime(), // 使用当前时间作为最后修改时间
  154. });
  155. setTimeout(() => {
  156. resolve(file)
  157. }, 300)
  158. } else if (fileSizeKB >
  159. targetSizeKB) {
  160. // 如果文件大小太大,降低质量,继续二分查找
  161. binarySearch(min, midQuality);
  162. } else {
  163. // 如果文件大小太小,增加质量,继续二分查找
  164. binarySearch(midQuality, max);
  165. }
  166. }, urlData.file.type, midQuality);
  167. };
  168. },
  169. fail: function(err) {
  170. resolve(false);
  171. }
  172. });
  173. }
  174. // 开始二分查找
  175. binarySearch(minQuality, maxQuality);
  176. })
  177. },
  178. // APP手动上传
  179. upload(name) {
  180. // name=指定文件名,不指定则上传所有type等于waiting和fail的文件
  181. this.$refs['lsjUpload'].upload(name);
  182. },
  183. onuploadEnd(item) {
  184. console.log(`${item.name}已上传结束,上传状态=${item.type}`);
  185. // 更新当前窗口状态变化的文件
  186. this.files.set(item.name, item);
  187. let file = {
  188. name: item.name,
  189. path: item.responseText
  190. }
  191. // 微信小程序Map对象for循环不显示,所以转成普通数组,
  192. // 如果你用不惯Map对象,也可以像这样转普通数组,组件使用Map主要是避免反复文件去重操作
  193. // #ifdef MP-WEIXIN
  194. this.wxFiles = [...this.files.values()];
  195. // #endif
  196. // 强制更新视图
  197. this.$forceUpdate();
  198. userService.saveInfo({
  199. id: this.userInfo.id,
  200. photo: item.responseText
  201. }).then(() => {
  202. this.refreshUserInfo()
  203. })
  204. uni.hideLoading()
  205. // ---可删除--演示判断是否所有文件均已上传成功
  206. let isAll = [...this.files.values()].find(item => item.type !== 'success');
  207. if (!isAll) {
  208. } else {
  209. console.log(isAll.name + '待上传');
  210. }
  211. },
  212. // upload(filePath) {
  213. // fileService.upload(filePath).then((data) => {
  214. // userService.saveInfo({
  215. // id: this.userInfo.id,
  216. // photo: data
  217. // }).then(() => {
  218. // this.refreshUserInfo()
  219. // })
  220. // })
  221. // },
  222. outlogin() {
  223. uni.showLoading()
  224. loginService.logout().then((data) => {
  225. this.$store.commit('logout');
  226. // uni.clearStorage();
  227. uni.removeStorageSync("WMS-userinfo")
  228. uni.removeStorageSync("WMS-username")
  229. uni.removeStorageSync("dictList")
  230. uni.removeStorageSync("__DC_STAT_UUID")
  231. uni.removeStorageSync("WMS-permission")
  232. uni.removeStorageSync("token")
  233. uni.hideLoading()
  234. uni.reLaunch({
  235. url: '/pages/login/login'
  236. })
  237. })
  238. }
  239. }
  240. }
  241. </script>
  242. <style>
  243. .person-head {
  244. display: flex;
  245. flex-direction: row;
  246. align-items: center;
  247. height: 150px;
  248. padding-left: 20px;
  249. background: #0081ff;
  250. }
  251. .person-head-box {
  252. display: flex;
  253. flex-direction: column;
  254. justify-content: center;
  255. align-items: flex-start;
  256. margin-left: 10px;
  257. }
  258. .person-head-nickname {
  259. font-size: 18px;
  260. font-weight: 500;
  261. color: #fff;
  262. }
  263. .person-head-username {
  264. font-size: 14px;
  265. font-weight: 500;
  266. color: #fff;
  267. }
  268. .person-list {
  269. line-height: 0;
  270. }
  271. .cu-list.card-menu {
  272. overflow: hidden;
  273. margin-right: 5px;
  274. margin-left: 5px;
  275. border-radius: 7px;
  276. }
  277. .cu-list.card-menu.margin-top-20 {
  278. margin-top: -20px;
  279. }
  280. .cu-list.menu>.cu-item .content>uni-view:first-child {
  281. display: -webkit-box;
  282. display: -webkit-flex;
  283. display: flex;
  284. -webkit-box-align: center;
  285. /* -webkit-align-items: center; */
  286. /* align-items: center; */
  287. display: inline-block;
  288. margin-right: 5px;
  289. width: 1.6em;
  290. text-align: center;
  291. }
  292. </style>