formDiy.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <div class="diy_form">
  3. <div v-for="(item,h) in diydata" :key="h">
  4. <el-form-item v-if="item.type == 'diyinput'" :label="item.data.tp_name">
  5. <el-input :placeholder="item.data.placeholder || '请填写'" v-model="item.value">
  6. </el-input>
  7. </el-form-item>
  8. <el-form-item v-if="item.type == 'diytextarea'" :label="item.data.tp_name">
  9. <el-input type="textarea" maxlength="100" :placeholder="item.data.placeholder" v-model="item.value">
  10. </el-input>
  11. </el-form-item>
  12. <el-form-item v-if="item.type == 'diycheckbox'" :label="item.data.tp_name">
  13. <div v-for="(ck,i) in item.data.tp_text" :key='i'>
  14. <el-checkbox-group v-model="item.value">
  15. <el-checkbox :label="ck">{{ck}}</el-checkbox>
  16. </el-checkbox-group>
  17. </div>
  18. </el-form-item>
  19. <el-form-item v-if="item.type == 'diypwd'" :label="item.data.tp_name===''?'密码':`${item.data.tp_name}`">
  20. <el-input :placeholder="item.data.placeholder" v-model="item.value">
  21. </el-input>
  22. </el-form-item>
  23. <el-form-item v-if="item.type == 'diyusername'" :label="item.data.tp_name===''?'账号':`${item.data.tp_name}`">
  24. <el-input :placeholder="item.data.placeholder" v-model="item.value">
  25. </el-input>
  26. </el-form-item>
  27. <el-form-item v-if="item.type == 'diyselect'" :label="item.data.tp_name">
  28. <el-select v-model="item.value" placeholder="请选择">
  29. <el-option v-for="(sitem,i) in item.data.tp_text" :key="i" :label="sitem" :value="sitem">
  30. </el-option>
  31. </el-select>
  32. </el-form-item>
  33. <el-form-item v-if="item.type == 'diyradio'" :label="item.data.tp_name">
  34. <el-radio-group v-model="item.value">
  35. <el-radio :label="ritem" v-for="(ritem,i) in item.data.tp_text" :key="i">{{ ritem }}</el-radio>
  36. </el-radio-group>
  37. </el-form-item>
  38. <el-form-item v-if="item.type == 'diydate'" :label="item.data.tp_name">
  39. <el-date-picker v-model="item.value" type="date" placeholder="请选择日期" value-format="yyyy-MM-dd">
  40. </el-date-picker>
  41. </el-form-item>
  42. <el-form-item v-if="item.type == 'diycity'" :label="item.data.tp_name">
  43. <el-cascader :options="district" v-model="item.value" :props="diyform_props"></el-cascader>
  44. </el-form-item>
  45. <el-form-item v-if="item.type == 'diyimg'" :label="item.data.tp_name">
  46. <div class="flex" style="flex-wrap:wrap;width:360px;">
  47. <div style="margin-right:20px;margin-bottom:20px;position:relative;" class="avatar"
  48. v-for="(val, index) in item[item.name + 'Value2']" :key="index">
  49. <img src="~/assets/images/icon/delete.png" style="position:absolute;right:-10px;top:-10px;cursor:pointer;"
  50. @click="removeImg_1(item, index)"/>
  51. <img :src="val.url" style="width:100%;height:100%;">
  52. </div>
  53. <div @click="chooseUpload(item.name)">
  54. <el-upload :disabled="item[item.name + 'Length'].length >= item.data.tp_max" class="avatar-uploader"
  55. :on-success="diySuccess" :action="uploadUrl" :show-file-list="false">
  56. <i class="el-icon-plus avatar-uploader-icon"></i>
  57. </el-upload>
  58. </div>
  59. </div>
  60. </el-form-item>
  61. </div>
  62. </div>
  63. </template>
  64. <script>
  65. import District from "~/static/js/gov_province_city_area_id";
  66. export default {
  67. name: 'formDiy',
  68. props: {
  69. diydata: Array
  70. },
  71. data() {
  72. return {
  73. uploadUrl: '',
  74. district: [],
  75. diyform_props: {
  76. value: "n",
  77. children: "c",
  78. label: "n"
  79. },
  80. };
  81. },
  82. mounted() {
  83. if(!window.localStorage.getItem('provinceData')) {
  84. this._initAddressInfo();
  85. }else {
  86. this.district = JSON.parse(window.localStorage.getItem('provinceData'));
  87. }
  88. this.uploadUrl = this.fun.getRealUrl("upload.uploadPic", {});
  89. },
  90. methods: {
  91. _initAddressInfo() {
  92. this.fun.$get("member.member-address.address", {}, "正在获取").then(res => {
  93. if (res.result == 1) {
  94. let provinces = [];
  95. let citys = [];
  96. res.data.province.map((province) => {
  97. provinces.push({
  98. "v": province.id,
  99. "n": province.areaname,
  100. "c": []
  101. })
  102. });
  103. res.data.city.map((city) => {
  104. citys.push({
  105. "parentid": city.parentid,
  106. "v": city.id,
  107. "n": city.areaname,
  108. "c": []
  109. })
  110. });
  111. res.data.district.map((district) => {
  112. citys.map((city) => {
  113. if (district.parentid == city.v) {
  114. city.c.push({
  115. "v": district.id,
  116. "n": district.areaname
  117. })
  118. }
  119. });
  120. });
  121. citys.map((city) => {
  122. provinces.map((province) => {
  123. if (city.parentid == province.v) {
  124. province.c.push({
  125. "v": city.v,
  126. "n": city.n,
  127. "c": city.c
  128. })
  129. }
  130. })
  131. });
  132. provinces.map((province) => {
  133. if (province.c.length == 0) {
  134. province.c = ""
  135. } else {
  136. province.c.map((c) => {
  137. if (c.c.length == 0) {
  138. c.c = ""
  139. }
  140. })
  141. }
  142. });
  143. this.district = provinces;
  144. if(provinces) {
  145. window.localStorage.setItem('provinceData',JSON.stringify(provinces));
  146. }
  147. }
  148. });
  149. },
  150. removeImg_1(item, delIndex) {
  151. console.log(item);
  152. var index = 0;
  153. var real_name = "";
  154. for (let i = 0; i < this.diydata.length; i++) {
  155. if (item.name == this.diydata[i].name) {
  156. real_name = this.diydata[i].name;
  157. console.log(real_name);
  158. index = i;
  159. break;
  160. }
  161. }
  162. this.diydata[index][real_name + "Value1"].splice(delIndex, 1);
  163. this.diydata[index][real_name + "Value2"].splice(delIndex, 1);
  164. this.diydata[index][real_name + "Length"] = this.diydata[index][
  165. real_name + "Value1"
  166. ].length;
  167. },
  168. chooseUpload(name) {
  169. this.upload_name = name;
  170. console.log(this.upload_name);
  171. return true;
  172. },
  173. diySuccess(res, file) {
  174. var That = this;
  175. var real_length = 0;
  176. var max_length = 0;
  177. var real_list1 = [];
  178. var real_list2 = [];
  179. var index = 0;
  180. var real_name = "";
  181. for (let i = 0; i < this.diydata.length; i++) {
  182. if (this.upload_name == this.diydata[i].name) {
  183. real_name = this.diydata[i].name;
  184. console.log(real_name);
  185. index = i;
  186. max_length = this.diydata[i].data.tp_max;
  187. real_list1 = this.diydata[i][real_name + "Value1"];
  188. real_list2 = this.diydata[i][real_name + "Value2"];
  189. console.log(this.diydata[i][real_name + "Value1"]);
  190. break;
  191. }
  192. }
  193. if (
  194. this.diydata[index][real_name + "Length"] ==
  195. this.diydata[index].data.tp_max ||
  196. this.diydata[index][real_name + "Length"] >=
  197. this.diydata[index].data.tp_max
  198. ) {
  199. console.log(this.diydata[index].data.tp_max);
  200. this.$message.error("图片数量已达到上限");
  201. return false;
  202. }
  203. this.diydata[index][real_name + "Length"]++;
  204. this.diydata[index][real_name + "Value1"].push({
  205. url: res.data.img
  206. });
  207. this.diydata[index][real_name + "Value2"].push({
  208. url: res.data.img_url
  209. });
  210. },
  211. postDiy(member_id) {
  212. var that = this;
  213. var formData = [];
  214. var formObject = {};
  215. this.diydata.forEach(item => {
  216. if (item.type == "diycity") {
  217. if (item.value.length && item.value.length > 0) {
  218. item.value = item.value.join(",");
  219. }
  220. }
  221. if (item.type == "diyimg") {
  222. var arr = [];
  223. for (let i = 0; i < item[item.name + "Value1"].length; i++) {
  224. arr.push(item[item.name + "Value1"][i].url);
  225. }
  226. formObject[item.name] = arr;
  227. } else {
  228. formObject[item.name] = item.value;
  229. }
  230. });
  231. formData.push(formObject);
  232. console.log(formData);
  233. console.log("i miss");
  234. let json = {
  235. form_data: formData,
  236. form_id: this.form_id,
  237. member_id: member_id,//商城注册页面时需要的参数
  238. form_type: "register"//商城注册页面时需要的参数
  239. };
  240. console.log(json);
  241. this.fun
  242. .$post(
  243. "plugin.diyform.api.diy-form.save-diy-form-data",
  244. json,
  245. "提交中..."
  246. )
  247. .then(response => {
  248. if (response.result == 1) {
  249. that.$message.success(response.msg);
  250. that.$emit('diySuccess', true)
  251. } else {
  252. that.$message.error(response.msg);
  253. }
  254. });
  255. },
  256. //验证参数
  257. validation() {
  258. let isValidation = true;
  259. let flag = true;
  260. this.diydata.some(item => {
  261. if (item.data.tp_must == 1 && !item.value && item.type != "diyimg") {
  262. console.log(item.data.tp_name, item.value);
  263. isValidation = false;
  264. this.$emit('pausepost', isValidation)
  265. this.$message.error(item.data.tp_name + "必须填写哦");
  266. return true;
  267. }
  268. if (item.data.tp_must == 1 && item.type == "diyimg") {
  269. if (item[item.name + "Length"] <= 0) {
  270. isValidation = false;
  271. this.$emit('pausepost', isValidation)
  272. this.$message.error(item.data.tp_name + "必须填写哦");
  273. return true;
  274. }
  275. }
  276. if (item.type == "diycheckbox" && item.value.length == 0) {
  277. isValidation = false;
  278. this.$emit('pausepost', isValidation)
  279. this.$message.error(item.data.tp_name + "必须填写哦");
  280. return true;
  281. } else {
  282. this.$emit('pausepost', true)
  283. }
  284. });
  285. },
  286. }
  287. };
  288. </script>
  289. <style lang="scss" scoped>
  290. </style>