modify.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <template>
  2. <!-- 注册 -->
  3. <view class="app" :style="'width: '+boxWidth+'px;height: '+boxHeight+'px'">
  4. <navbar title="修改信息" leftUrl="/pages/mine/index" :showLeft="true" :bgColor="'transparent'" :place="false">
  5. </navbar>
  6. <view class="main" v-if="userInfo.id">
  7. <view class="form-box">
  8. <view class="form">
  9. <uv-form class="login-form" labelPosition="left" :errorType="'toast'" :model="formData"
  10. :rules="rules" ref="form" label-width="2.25rem">
  11. <view class="form-item">
  12. <uv-image class="avatar"
  13. :src="userInfo.avatar? userInfo.avatar:'/static/images/icon-avatar.png'"
  14. mode="aspectFill" width="6rem" circle height="6rem" shape="circle"
  15. errorIcon="/static/images/icon-avatar.png">
  16. </uv-image>
  17. </view>
  18. <view class="form-item">
  19. <uv-form-item prop="realname" clearable label="昵称" label-width="3.25rem">
  20. <uv-input v-model="formData.nickname" type="text" border="none" placeholder="请输入昵称"
  21. inputAlign="right" clearable maxlength="30">
  22. </uv-input>
  23. </uv-form-item>
  24. </view>
  25. <view class="upload-box">
  26. <uv-form-item prop="formData.avatar" labelWidth="0">
  27. <uv-upload class="upload" :action="uploadConfig.url" :header="uploadConfig.header"
  28. :accept="'image'" @afterRead="afterRead" imageMode="aspectFit" :width="90"
  29. :height="90" :file-list="fileList" name="avatar" :maxCount="1" :deletable="false"
  30. :sizeType="['compressed']">
  31. <image class="img" :src="formData.avatar||'/static/images/icon-upload.png'"
  32. mode="aspectFill">
  33. </image>
  34. <view class="text">
  35. 仅支持JPG、JPEG头像
  36. </view>
  37. </uv-upload>
  38. </uv-form-item>
  39. </view>
  40. <view class="form-submit">
  41. <uv-button class="register" throttleTime="1200" size="normal" shape="circle"
  42. :customStyle="{height:'3.25rem',fontWeight: '500',marginBottom: '.75rem'}"
  43. color="linear-gradient(to right, var(--button-color), var(--button-color1))"
  44. :loading="submitLoading" loadingText="保存中..." @click="submit">保存
  45. </uv-button>
  46. </view>
  47. </uv-form>
  48. </view>
  49. </view>
  50. </view>
  51. <showModal ref="uvModal"></showModal>
  52. <uv-loading-icon class="loading" :show="loading" textSize="30rpx" color="var(--color)"
  53. :customStyle="{position:'absolute',top:'50%',left:0,right:0}"></uv-loading-icon>
  54. </view>
  55. </template>
  56. <script>
  57. import {
  58. getToken,
  59. } from '../../config/cache';
  60. import navbar from "../../components/navbar.vue"
  61. import showModal from "../../components/showModal.vue"
  62. export default {
  63. components: {
  64. navbar,
  65. showModal
  66. },
  67. data() {
  68. return {
  69. id: 0,
  70. loading: false,
  71. submitLoading: false,
  72. type: '',
  73. boxHeight: 800,
  74. boxWidth: 360,
  75. showLang: false,
  76. lang: 'zh-cn',
  77. leftIconStyle: {
  78. width: '1.5rem',
  79. height: '1.5rem'
  80. },
  81. userInfo: {
  82. id: 0,
  83. },
  84. formData: {
  85. nickname: '',
  86. avatar: '',
  87. },
  88. rules: {
  89. nickname: [{
  90. required: true,
  91. message: '请填写昵称',
  92. // blur和change事件触发检验
  93. trigger: ['blur', 'change'],
  94. }],
  95. },
  96. fileList: [],
  97. uploadConfig: {
  98. url: this.baseUrl + '/api/v1/upload/image',
  99. header: {
  100. Authorization: "Bearer " + getToken()
  101. }
  102. },
  103. }
  104. },
  105. onLoad(e) {
  106. const windowInfo = uni.getWindowInfo();
  107. this.boxHeight = windowInfo.windowHeight
  108. this.boxWidth = windowInfo.windowWidth
  109. this.getInfo();
  110. },
  111. onHide() {
  112. this.$refs.uvModal.close();
  113. },
  114. onPullDownRefresh() {
  115. uni.stopPullDownRefresh()
  116. this.getInfo(true)
  117. },
  118. methods: {
  119. loadImage(eve) {
  120. console.log(666)
  121. eve.target.src = '/static/images/icon-avatar.png'
  122. },
  123. // 用户信息
  124. async getInfo(refresh = false) {
  125. this.loading = true;
  126. this.userInfo = await this.$request.apiGetUserInfo(this, refresh);
  127. this.loading = false;
  128. if (this.userInfo.id > 0) {
  129. this.formData = {
  130. nickname: this.userInfo.nickname,
  131. avatar: '',
  132. }
  133. }
  134. },
  135. // 提交
  136. submit() {
  137. this.$refs.form.validate().then(res => {
  138. this._submit();
  139. }).catch(errors => {
  140. })
  141. },
  142. // 提交处理
  143. async _submit() {
  144. let _this = this
  145. if (!this.formData.nickname) {
  146. uni.showToast({
  147. title: '请填写昵称',
  148. icon: 'none'
  149. })
  150. return false;
  151. }
  152. _this.loading = true
  153. _this.submitLoading = true
  154. const res = await this.$request.api('/user/modify', this.formData);
  155. _this.loading = false
  156. _this.submitLoading = false
  157. if (res.success == true) {
  158. this.userInfo.avatar = this.formData.avatar;
  159. this.formData.avatar = '';
  160. this.fileList = [];
  161. uni.showToast({
  162. title: res.msg,
  163. icon: 'none'
  164. })
  165. } else {
  166. let msg = typeof(res.msg) != 'undefined' ? res.msg : '请求失败'
  167. uni.showToast({
  168. title: msg,
  169. icon: 'none'
  170. })
  171. }
  172. },
  173. // 预读图片
  174. async afterRead(event) {
  175. console.log(event);
  176. let lists = [].concat(event.file)
  177. const result = await this.uploadFilePromise(event.name, lists[0].url)
  178. },
  179. uploadFilePromise(name, url) {
  180. let _this = this
  181. _this.loading = true
  182. return new Promise((resolve, reject) => {
  183. let system = {
  184. uuid: uni.getSystemInfoSync().deviceId,
  185. sys_time: (Date.parse(new Date()) / 1000),
  186. };
  187. let a = uni.uploadFile({
  188. url: _this.uploadConfig.url,
  189. filePath: url,
  190. header: _this.uploadConfig.header,
  191. name: 'file',
  192. formData: {
  193. group: 'avatar',
  194. system: system,
  195. },
  196. success: (res) => {
  197. _this.loading = false
  198. let result = JSON.parse(res.data)
  199. if (result.success == true) {
  200. _this.formData[name] = result.data.url
  201. _this.fileList[0] = {
  202. url: result.data.url,
  203. success: 'success',
  204. }
  205. resolve(result.data)
  206. } else {
  207. uni.showToast({
  208. title: result.msg,
  209. icon: 'none'
  210. })
  211. }
  212. },
  213. });
  214. })
  215. },
  216. }
  217. }
  218. </script>
  219. <style lang="scss" scoped>
  220. page {
  221. height: 100%;
  222. width: 100%;
  223. }
  224. .app {
  225. height: 100%;
  226. }
  227. .main {
  228. margin: 4rem .5rem;
  229. .form-box {
  230. position: relative;
  231. z-index: 2;
  232. height: 100%;
  233. .form {
  234. position: relative;
  235. z-index: 2;
  236. padding: 1rem 1rem 1rem;
  237. ::v-deep .uv-form-item {
  238. background-color: #fff;
  239. padding: .3125rem 1.25rem;
  240. border-radius: 10rem;
  241. margin-bottom: 1.5rem;
  242. border-width: 0;
  243. }
  244. .uv-border-bottom::after {
  245. border-width: 0;
  246. }
  247. .uv-form-item--left__content__label {
  248. color: #060518;
  249. }
  250. .form-item {
  251. margin: .75rem 0;
  252. border-radius: 1rem;
  253. justify-content: center;
  254. // margin-bottom: 1.25rem;
  255. .avatar {
  256. width: 6rem;
  257. height: 6rem;
  258. margin: .75rem auto;
  259. border-radius: 10rem;
  260. display: block;
  261. border: .025rem solid #86cdff;
  262. }
  263. ::v-deep .uv-form-item {
  264. padding: 0.3125rem .75rem;
  265. }
  266. }
  267. .upload-box {
  268. display: flex;
  269. flex-direction: row;
  270. align-items: center;
  271. justify-content: space-between;
  272. .uv-form-item {
  273. background: none;
  274. padding: 0rem;
  275. margin-bottom: 0rem;
  276. border: none;
  277. ::v-deep .uv-form-item__body {
  278. padding: 0;
  279. width: 100%;
  280. * {
  281. width: 100%;
  282. flex-direction: column;
  283. }
  284. .upload {
  285. .img {
  286. width: 6.5rem;
  287. height: 6.5rem;
  288. border-radius: .5rem;
  289. }
  290. .text {
  291. width: auto;
  292. margin: .5rem 0;
  293. text-align: center;
  294. font-size: .625rem;
  295. color: var(--text-color);
  296. }
  297. }
  298. }
  299. }
  300. .uv-form-item:first-child {
  301. margin-right: 1rem;
  302. }
  303. }
  304. .form-submit {
  305. margin: 1.25rem 2rem 1.25rem;
  306. }
  307. }
  308. }
  309. }
  310. </style>