upBasic.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. <view class="icon">
  8. <view class="icon_1" v-if="form.icon&&form.icon.length>0">
  9. <image class="image" v-for="i in form.icon" :key="i" :src="i.url" mode=""></image>
  10. <uni-icons class="del" type="close" size="30" color="#ff0000" @click="iconDel()">
  11. </uni-icons>
  12. </view>
  13. <view class="icon_2" v-else>
  14. <button open-type="chooseAvatar" @chooseavatar="chooseavatar">上传头像</button>
  15. </view>
  16. </view>
  17. </uni-forms-item>
  18. <uni-forms-item label="用户名" name="name">
  19. <input class="input" type="nickname" :value="form.name" placeholder="请输入用户名" @input="nameInput">
  20. <!-- <uni-easyinput type="nick" v-model="form.name" placeholder="请输入用户名" /> -->
  21. </uni-forms-item>
  22. <uni-forms-item label="性别" name="gender">
  23. <picker @change="genderChange" name="gender" :value="form.gender" :range="genderList"
  24. range-key="label">
  25. <view class="uni-input">{{form.zhGender||'请选择性别'}}</view>
  26. </picker>
  27. </uni-forms-item>
  28. <uni-forms-item label="生日" name="birth">
  29. <picker mode="date" :value="form.birth" name="birth" @change="bindDateChange">
  30. <view class="uni-input">{{form.birth||'请选择日期'}}</view>
  31. </picker>
  32. </uni-forms-item>
  33. </uni-forms>
  34. <view class="btn">
  35. <button type="primary" @click="onSubmit('form')" size="small">提交</button>
  36. </view>
  37. </view>
  38. </view>
  39. </mobile-frame>
  40. </template>
  41. <script>
  42. export default {
  43. components: {},
  44. data() {
  45. return {
  46. frameStyle: {
  47. useBar: false
  48. },
  49. form: {
  50. icon: []
  51. },
  52. rules: {
  53. name: {
  54. rules: [{
  55. required: true,
  56. errorMessage: '请输入用户名',
  57. }]
  58. },
  59. phone: {
  60. rules: [{
  61. required: true,
  62. errorMessage: '请输入手机号',
  63. }]
  64. },
  65. },
  66. genderList: [],
  67. };
  68. },
  69. onLoad: async function(e) {
  70. const that = this;
  71. await that.searchOther();
  72. that.watchLogin();
  73. },
  74. methods: {
  75. async searchOther() {
  76. const that = this;
  77. let res;
  78. res = await that.$api(`/dictData`, 'GET', {
  79. code: "gender"
  80. });
  81. if (res.errcode == '0') {
  82. that.$set(that, `genderList`, res.data)
  83. }
  84. },
  85. watchLogin() {
  86. const that = this;
  87. uni.getStorage({
  88. key: 'token',
  89. success: async (res) => {
  90. let user = that.$jwt(res.data);
  91. if (user) {
  92. const arr = await that.$api(`/user/${user.id}`, 'GET');
  93. if (arr.errcode == '0') {
  94. let gender = that.genderList.find(i => i.value == arr.data
  95. .gender)
  96. if (gender) arr.data.zhGender = gender.label;
  97. that.$set(that, `form`, arr.data)
  98. } else {
  99. uni.showToast({
  100. title: arr.errmsg,
  101. icon: 'error',
  102. duration: 2000
  103. });
  104. }
  105. }
  106. },
  107. fail: (err) => {}
  108. })
  109. },
  110. toPath(e) {
  111. if (e && e.route) uni.redirectTo({
  112. url: `/${e.route}`
  113. })
  114. },
  115. // 删除头像
  116. iconDel() {
  117. const that = this;
  118. that.$set(that.form, `icon`, [])
  119. },
  120. // 头像上传
  121. async chooseavatar(e) {
  122. const that = this;
  123. let avatarUrl = e.detail.avatarUrl;
  124. let serverUrl = that.$config.serverUrl;
  125. const res = await that.$apifile(`/point/shopOne/upload`, 'file', avatarUrl, 'file');
  126. if (res.errcode == '0') {
  127. let obj = {
  128. name: res.name,
  129. uri: res.uri,
  130. url: serverUrl + res.uri
  131. }
  132. that.$set(that.form, `icon`, [obj])
  133. } else {
  134. uni.showToast({
  135. title: res.errmsg,
  136. icon: 'none'
  137. })
  138. }
  139. },
  140. // 选择,输入昵称
  141. nameInput(e) {
  142. const that = this;
  143. that.$set(that.form, `name`, e.detail.value);
  144. },
  145. // 选择性别
  146. genderChange(e) {
  147. const that = this;
  148. let data = that.genderList[e.detail.value];
  149. if (data) {
  150. that.$set(that.form, `gender`, data.value);
  151. that.$set(that.form, `zhGender`, data.label);
  152. }
  153. },
  154. // 选择日期
  155. bindDateChange(e) {
  156. const that = this;
  157. that.$set(that.form, `birth`, e.detail.value);
  158. },
  159. // 返回
  160. back() {
  161. uni.navigateBack({
  162. delta: 1
  163. })
  164. },
  165. // 提交保存
  166. onSubmit(ref) {
  167. const that = this;
  168. that.$refs[ref].validate().then(async params => {
  169. uni.getStorage({
  170. key: 'shop',
  171. success: async function(res) {
  172. parmas.shop = res.data
  173. }
  174. })
  175. const arr = await that.$api(`/user/${that.form.id}`, 'POST', params);
  176. if (arr.errcode == '0') {
  177. uni.showToast({
  178. title: `维护信息成功`,
  179. icon: 'success',
  180. });
  181. that.back();
  182. } else {
  183. uni.showToast({
  184. title: arr.errmsg,
  185. })
  186. }
  187. })
  188. },
  189. }
  190. }
  191. </script>
  192. <style lang="scss">
  193. .info {
  194. display: flex;
  195. flex-direction: column;
  196. width: 100vw;
  197. height: 100vh;
  198. .one {
  199. padding: 2vw;
  200. .uni-input {
  201. border: #f1f1ff 1px solid;
  202. padding: 2vw 2vw;
  203. border-radius: 1vw;
  204. }
  205. .input {
  206. border: #f1f1ff 1px solid;
  207. padding: 1.5vw 1vw;
  208. border-radius: 5px;
  209. }
  210. .btn {
  211. text-align: center;
  212. button {
  213. margin: 0 2vw 2vw 2vw;
  214. background-color: var(--fFB1Color);
  215. color: var(--fffColor);
  216. }
  217. .name {
  218. color: var(--f85Color);
  219. font-size: var(--font14Size);
  220. }
  221. }
  222. .icon {
  223. display: flex;
  224. .icon_1 {
  225. position: relative;
  226. .image {
  227. width: 30vw;
  228. height: 30vw;
  229. }
  230. .del {
  231. position: absolute;
  232. top: 0;
  233. left: 22vw;
  234. }
  235. }
  236. .icon_2 {
  237. text-align: left;
  238. button {
  239. width: 30vw;
  240. padding: 0vw 1vw;
  241. font-size: 14px;
  242. }
  243. }
  244. }
  245. }
  246. }
  247. .uni-forms-item {
  248. margin-bottom: 6vw !important;
  249. display: flex;
  250. flex-direction: row;
  251. }
  252. </style>