upBasic.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <mobile-frame>
  3. <view class="info">
  4. <view class="one">
  5. <uni-forms ref="form" :modelValue="form" :rules="rules" label-width="auto">
  6. <uni-forms-item label="头像" name="icon">
  7. <upload :list="icon" name="icon" :count="1" @uplSuc="uplSuc" @uplDel="uplDel"></upload>
  8. </uni-forms-item>
  9. <uni-forms-item label="用户名" name="name">
  10. <uni-easyinput type="text" v-model="form.name" placeholder="请输入用户名" />
  11. </uni-forms-item>
  12. <!-- <uni-forms-item label="手机号" name="phone">
  13. <uni-easyinput type="text" v-model="form.phone" placeholder="请输入手机号" disabled />
  14. </uni-forms-item>
  15. <uni-forms-item label="电子邮箱" name="email">
  16. <uni-easyinput type="text" v-model="form.email" placeholder="请输入电子邮箱" disabled />
  17. </uni-forms-item> -->
  18. <uni-forms-item label="性别" name="gender">
  19. <picker @change="genderChange" name="gender" :value="form.gender" :range="genderList" range-key="label">
  20. <view class="uni-input">{{form.zhGender||'请选择性别'}}</view>
  21. </picker>
  22. </uni-forms-item>
  23. <uni-forms-item label="生日" name="birth">
  24. <picker mode="date" :value="form.birth" name="birth" @change="bindDateChange">
  25. <view class="uni-input">{{form.birth||'请选择日期'}}</view>
  26. </picker>
  27. </uni-forms-item>
  28. </uni-forms>
  29. <view class="btn">
  30. <button type="primary" @click="onSubmit('form')" size="small">提交</button>
  31. </view>
  32. </view>
  33. </view>
  34. </mobile-frame>
  35. </template>
  36. <script>
  37. import upload from '@/components/upload/index.vue';
  38. export default {
  39. components: {
  40. upload
  41. },
  42. data() {
  43. return {
  44. frameStyle: {
  45. useBar: false
  46. },
  47. form: {},
  48. rules: {
  49. name: {
  50. rules: [{
  51. required: true,
  52. errorMessage: '请输入用户名',
  53. }]
  54. },
  55. phone: {
  56. rules: [{
  57. required: true,
  58. errorMessage: '请输入手机号',
  59. }]
  60. },
  61. },
  62. genderList: [
  63. // {
  64. // label: '保密',
  65. // value: '0'
  66. // },
  67. // {
  68. // label: '男',
  69. // value: '1'
  70. // },
  71. // {
  72. // label: '女',
  73. // value: '2'
  74. // },
  75. ],
  76. icon: []
  77. };
  78. },
  79. onLoad: function(e) {
  80. const that = this;
  81. that.searchOther();
  82. that.watchLogin();
  83. },
  84. methods: {
  85. async searchOther(){
  86. const that = this;
  87. let res;
  88. res = await that.$api(`/dictData`, 'GET', {
  89. code: "gender"
  90. });
  91. if (res.errcode == '0') {
  92. that.$set(that, `genderList`, res.data)
  93. }
  94. },
  95. watchLogin() {
  96. const that = this;
  97. uni.getStorage({
  98. key: 'token',
  99. success: async (res) => {
  100. let user = that.$jwt(res.data);
  101. if (user) {
  102. const arr = await that.$api(`/user/${user.id}`, 'GET');
  103. if (arr.errcode == '0') {
  104. let gender = that.genderList.find(i => i.value == arr.data
  105. .gender)
  106. if (gender) arr.data.zhGender = gender.label;
  107. that.$set(that, `form`, arr.data)
  108. that.$set(that, `icon`, arr.data.icon)
  109. } else {
  110. uni.showToast({
  111. title: arr.errmsg,
  112. icon: 'error',
  113. duration: 2000
  114. });
  115. }
  116. }
  117. },
  118. fail: (err) => {}
  119. })
  120. },
  121. toPath(e) {
  122. if (e && e.route) uni.redirectTo({
  123. url: `/${e.route}`
  124. })
  125. },
  126. // 图片上传
  127. uplSuc(e) {
  128. const that = this;
  129. that.$set(that, `${e.name}`, [...that[e.name], e.data]);
  130. },
  131. // 图片删除
  132. uplDel(e) {
  133. const that = this;
  134. let data = that[e.name];
  135. let arr = data.filter((i, index) => index != e.data.index);
  136. that.$set(that, `${e.name}`, arr)
  137. },
  138. // 选择性别
  139. genderChange(e) {
  140. const that = this;
  141. let data = that.genderList[e.detail.value];
  142. if (data) {
  143. that.$set(that.form, `gender`, data.value);
  144. that.$set(that.form, `zhGender`, data.label);
  145. }
  146. },
  147. // 选择日期
  148. bindDateChange(e) {
  149. const that = this;
  150. that.$set(that.form, `birth`, e.detail.value);
  151. },
  152. // 返回
  153. back() {
  154. uni.navigateBack({
  155. delta: 1
  156. })
  157. },
  158. // 提交保存
  159. onSubmit(ref) {
  160. const that = this;
  161. that.$refs[ref].validate().then(async params => {
  162. params.icon = that.icon
  163. const arr = await that.$api(`/user/${that.form.id}`, 'POST', params);
  164. if (arr.errcode == '0') {
  165. uni.showToast({
  166. title: `维护信息成功`,
  167. icon: 'success',
  168. });
  169. that.back();
  170. } else {
  171. uni.showToast({
  172. title: arr.errmsg,
  173. })
  174. }
  175. })
  176. },
  177. }
  178. }
  179. </script>
  180. <style lang="scss">
  181. .info {
  182. display: flex;
  183. flex-direction: column;
  184. width: 100vw;
  185. height: 100vh;
  186. .one {
  187. padding: 2vw;
  188. .uni-input {
  189. border: #f1f1ff 1px solid;
  190. padding: 2vw 2vw;
  191. border-radius: 1vw;
  192. }
  193. .btn {
  194. text-align: center;
  195. button {
  196. margin: 0 2vw 2vw 2vw;
  197. background-color: var(--ff0Color);
  198. color: var(--fffColor);
  199. }
  200. .name {
  201. color: var(--f85Color);
  202. font-size: var(--font14Size);
  203. }
  204. }
  205. }
  206. }
  207. .uni-forms-item {
  208. margin-bottom: 6vw !important;
  209. display: flex;
  210. flex-direction: row;
  211. }
  212. </style>