useredit.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <view>
  3. <cu-custom bgColor="bg-gradual-pink" :isBack="true">
  4. <block slot="backText">返回</block>
  5. <block slot="content">编辑资料</block>
  6. </cu-custom>
  7. <form class="user-edit-form">
  8. <view class="cu-form-group">
  9. <view class="title">姓名</view>
  10. <input placeholder="请输入姓名" name="input" v-model="myFormData.realname"></input>
  11. </view>
  12. <view class="cu-form-group">
  13. <view class="title">用户名</view>
  14. <input placeholder="用户名" name="input" v-model="myFormData.username" disabled></input>
  15. </view>
  16. <view class="cu-form-group">
  17. <view class="title">头像</view>
  18. <view class="grid col-4 grid-square flex-sub">
  19. <view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
  20. <image :src="imgList[index]" mode="aspectFill"></image>
  21. <view class="cu-tag bg-red radius" @tap.stop="DelImg" :data-index="index">
  22. <text class='cuIcon-close'></text>
  23. </view>
  24. </view>
  25. <view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
  26. <text class='cuIcon-cameraadd'></text>
  27. </view>
  28. </view>
  29. </view>
  30. <view class="cu-form-group margin-top">
  31. <view class="title">性别</view>
  32. <switch class='switch-sex' @change="SwitchC" :class="switchC?'checked':''" :checked="switchC?true:false"></switch>
  33. </view>
  34. <view class="cu-form-group margin-top">
  35. <view class="title">手机号码</view>
  36. <input placeholder="输入手机号码" name="input" v-model="myFormData.phone"></input>
  37. <view class="cu-capsule radius">
  38. <view class='cu-tag bg-blue '>
  39. +86
  40. </view>
  41. <view class="cu-tag line-blue">
  42. 中国大陆
  43. </view>
  44. </view>
  45. </view>
  46. <view class="cu-form-group">
  47. <view class="title">邮箱</view>
  48. <input placeholder="输入邮箱" name="input" v-model="myFormData.email"></input>
  49. </view>
  50. <view class="padding flex flex-direction">
  51. <button class="cu-btn bg-blue lg" @click="onSubmit">提交</button>
  52. </view>
  53. </form>
  54. </view>
  55. </template>
  56. <script>
  57. export default {
  58. data() {
  59. return {
  60. index: -1,
  61. switchC: true,
  62. imgList: [],
  63. uploadUrl: "/sys/common/upload",
  64. myFormData: {
  65. avatar: "",
  66. realname: "",
  67. username: "",
  68. sex: 1,
  69. birthday: "",
  70. orgCode: "",
  71. workNo: "",
  72. phone: "",
  73. email: "",
  74. id: "",
  75. },
  76. };
  77. },
  78. onLoad: function (option) {
  79. console.log("this.$Route.query", this.$Route.query);
  80. let query = this.$Route.query;
  81. if (query) {
  82. this.myFormData = query;
  83. if (this.myFormData.sex == "女") {
  84. this.switchC = false;
  85. } else if (this.myFormData.sex == "男") {
  86. this.switchC = true;
  87. }
  88. if (this.myFormData.avatar) {
  89. this.imgList = [this.myFormData.avatar];
  90. }
  91. if (!this.myFormData.birthday) {
  92. this.myFormData.birthday = "无";
  93. }
  94. if (this.myFormData.identity == "普通成员") {
  95. this.myFormData.identity = 1;
  96. } else if (this.myFormData.identity == "上级") {
  97. this.myFormData.identity = 2;
  98. }
  99. if (this.myFormData.status == "正常") {
  100. this.myFormData.status = 1;
  101. } else if (this.myFormData.status == "冻结") {
  102. this.myFormData.status = 2;
  103. }
  104. this.Avatar = this.myFormData.avatar;
  105. Object.keys(this.myFormData).map((key) => {
  106. if (this.myFormData[key] == "无") {
  107. this.myFormData[key] = "";
  108. }
  109. });
  110. console.log("this.myFormData", this.myFormData);
  111. }
  112. },
  113. methods: {
  114. onSubmit() {
  115. let myForm = this.myFormData;
  116. let checkPhone = new RegExp(/^[1]([3-9])[0-9]{9}$/);
  117. let checkEmail =
  118. /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
  119. console.log("myForm", myForm);
  120. if (!myForm.phone || myForm.phone.length == 0) {
  121. this.$tip.alert("请输入手机号");
  122. return false;
  123. }
  124. if (!checkPhone.test(myForm.phone)) {
  125. this.$tip.alert("请输入正确的手机号");
  126. return false;
  127. }
  128. if (!checkEmail.test(myForm.email)) {
  129. this.$tip.alert("请输入正确的邮箱地址");
  130. return false;
  131. }
  132. this.myFormData.id = this.$store.getters.userid;
  133. if (this.switchC) {
  134. this.myFormData.sex = 1;
  135. } else {
  136. this.myFormData.sex = 2;
  137. }
  138. console.log("myform", this.myFormData);
  139. this.$tip.loading();
  140. this.$http
  141. .put("/sys/user/appEdit", this.myFormData)
  142. .then((res) => {
  143. console.log(res);
  144. this.$tip.loaded();
  145. if (res.data.success) {
  146. this.$tip.toast("提交成功");
  147. this.$Router.replace({ name: "userdetail" });
  148. /* uni.navigateTo({
  149. url: '/pages/user/userdetail'
  150. }) */
  151. }
  152. })
  153. .catch(() => {
  154. this.$tip.loaded();
  155. this.$tip.error("提交失败");
  156. });
  157. },
  158. DateChange(e) {
  159. this.myFormData.birthday = e.detail.value;
  160. },
  161. SwitchC(e) {
  162. this.switchC = e.detail.value;
  163. },
  164. ChooseImage() {
  165. var that = this;
  166. uni.chooseImage({
  167. count: 4, //默认9
  168. sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
  169. sourceType: ["album"], //从相册选择
  170. success: (res) => {
  171. that.$http
  172. .upload(that.$config.apiUrl + that.uploadUrl, {
  173. filePath: res.tempFilePaths[0],
  174. name: "file",
  175. })
  176. .then((res) => {
  177. that.myFormData.avatar = res.data.message;
  178. })
  179. .catch((err) => {
  180. that.$tip.error(err.data.message);
  181. });
  182. this.imgList = res.tempFilePaths;
  183. },
  184. });
  185. },
  186. ViewImage(e) {
  187. uni.previewImage({
  188. urls: this.imgList,
  189. current: e.currentTarget.dataset.url,
  190. });
  191. },
  192. DelImg(e) {
  193. uni.showModal({
  194. title: "召唤师",
  195. content: "确定要删除这段回忆吗?",
  196. cancelText: "再看看",
  197. confirmText: "再见",
  198. success: (res) => {
  199. if (res.confirm) {
  200. this.imgList.splice(e.currentTarget.dataset.index, 1);
  201. }
  202. },
  203. });
  204. },
  205. },
  206. };
  207. </script>
  208. <style>
  209. .user-edit-form {
  210. margin-top: 50rpx;
  211. }
  212. .user-edit-form .cu-form-group .title {
  213. min-width: calc(4em + 15px);
  214. flex-shrink: 0;
  215. }
  216. </style>