PresentRegister.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <template>
  2. <div>
  3. <nav-bar path="index" title="上传礼物"/>
  4. <div class='card'>
  5. <h3><strong>礼物匹配</strong></h3>
  6. <p>希望对方的性别是</p>
  7. <van-radio-group v-model="gender">
  8. <van-radio name="1" checked-color="#fd6740" style="float: left; margin-right: 20px">男</van-radio>
  9. <van-radio name="2" checked-color="#fd6740">女</van-radio>
  10. </van-radio-group>
  11. <p>礼物名称</p>
  12. <van-field
  13. placeholder="给你的礼物起个名字吧"
  14. v-model="presentName"
  15. :error="pnError"
  16. @click="pnError=false"
  17. required
  18. maxlength="30"
  19. />
  20. <p>我的礼物丨走心介绍</p>
  21. <van-cell-group>
  22. <van-field
  23. type="textarea"
  24. placeholder="请在此输入你的礼物介绍和礼物故事"
  25. rows="5"
  26. autosize
  27. v-model="desc"
  28. :error="descError"
  29. @click="descError=false"
  30. required
  31. maxlength="300"
  32. />
  33. </van-cell-group>
  34. <p>礼物标签</p>
  35. <van-radio-group v-model="tag">
  36. <div
  37. v-for="i in [0, 1 , 2, 3]"
  38. :key="i"
  39. style="height: 40px"
  40. >
  41. <div
  42. v-for="j in [0, 1, 2, 3]"
  43. :key="j"
  44. style="float: left; width: 25%"
  45. >
  46. <van-radio
  47. v-if="i * 4 + j < tags.length"
  48. :name="i * 4 + j"
  49. checked-color="#fd6740"
  50. >{{tags[i * 4 + j]}}
  51. </van-radio>
  52. </div>
  53. </div>
  54. </van-radio-group>
  55. </div>
  56. <div class="card" style="margin-bottom: 10px">
  57. <p>上传礼物照片</p>
  58. <div style="margin-bottom: 10px">
  59. <add-photo :photoURL="imgUrl" @onRead="onRead"/>
  60. </div>
  61. </div>
  62. <van-button
  63. type="primary"
  64. class="submit_button"
  65. @click="publishPresent"
  66. :loading="loading"
  67. >下一步
  68. </van-button>
  69. </div>
  70. </template>
  71. <script>
  72. import {
  73. Uploader, Icon,
  74. CellGroup, Field,
  75. RadioGroup, Radio,
  76. CheckboxGroup, Checkbox,
  77. Picker,
  78. Button,
  79. Toast
  80. } from 'vant';
  81. import {BasicFunction} from "../connector/basic-service";
  82. import AddPhoto from "../components/addPhoto";
  83. import NavBar from "../components/navBar";
  84. export default {
  85. name: "PresentRegister",
  86. components: {
  87. AddPhoto,
  88. NavBar,
  89. [Uploader.name]: Uploader, [Icon.name]: Icon,
  90. [CellGroup.name]: CellGroup, [Field.name]: Field,
  91. [RadioGroup.name]: RadioGroup, [Radio.name]: Radio,
  92. [CheckboxGroup.name]: CheckboxGroup, [Checkbox.name]: Checkbox,
  93. [Picker.name]: Picker,
  94. [Button.name]: Button
  95. },
  96. data() {
  97. return {
  98. gender: '1',
  99. presentId: '',
  100. presentName: '',
  101. pnError: false,
  102. desc: '',
  103. descError: false,
  104. tags: [
  105. "情怀","复古", "有趣", "学霸", "实用",
  106. "童年", "吃货", "佛系", "土味", "活力",
  107. "洋气", "精致", "轻奢", "其它",
  108. ],
  109. tag: 0,
  110. photo: null,
  111. loading: false
  112. }
  113. },
  114. methods: {
  115. publishPresent() {
  116. Toast.loading({
  117. mask: true,
  118. message: '图片上传较慢,请耐心等待...',
  119. duration: 3000
  120. });
  121. this.loading = true;
  122. // 检验输入
  123. if (isEmojiCharacter(this.presentName)) {
  124. Toast.fail('不支持表情哦');
  125. this.pnError = true;
  126. this.loading = false;
  127. return;
  128. }
  129. else if (this.presentName.length === 0) {
  130. Toast.fail('请填写手机号');
  131. this.pnError = true;
  132. this.loading = false;
  133. return;
  134. }
  135. else if (isEmojiCharacter(this.desc)) {
  136. Toast.fail('不支持表情哦');
  137. this.descError = true;
  138. this.loading = false;
  139. return;
  140. } else if (this.desc.length <= 15) {
  141. this.descError = true;
  142. Toast.fail('请输入至少15个字的礼物介绍');
  143. this.loading = false;
  144. return;
  145. } else if (this.photo === null) {
  146. Toast.fail('请上传礼物照片');
  147. this.loading = false;
  148. return;
  149. }
  150. // 上传礼物信息
  151. let userId = localStorage.getItem("frontend-userid");
  152. let tag = this.tag;
  153. let gender = this.gender;
  154. // console.log('------- photo -------');
  155. // console.log(this.photo);
  156. if(userId == null || userId === undefined){
  157. this.$router.push('login');
  158. return;
  159. }
  160. let that = this;
  161. // 如果没有改变图片,直接上传
  162. if (this.imgUrl && this.imgUrl === this.photo) {
  163. let imgURL = this.imgUrl;
  164. imgURL = '/' + imgURL.split('/')[3] + '/' + imgURL.split('/')[4] + '/' + imgURL.split('/')[5];
  165. let present = [{
  166. goodid: this.presentId,
  167. name: this.presentName,
  168. goodsNo: "0", modelId: "0",
  169. ownerId: userId.toString(),
  170. goodFirstKind: gender,
  171. goodSecondKind: tag.toString(),
  172. sellPrice: "0.0", marketPrice: "0.0", costPrice: "0.0",
  173. upTime: getNowFormatDate(), downTime: getNowFormatDate(), createTime: getNowFormatDate(),
  174. storeNums: "0",
  175. img: imgURL,
  176. isDel: "0", content: "none", keywords: "none",
  177. description: that.desc,
  178. searchWords: "", weight: "0.0", point: "0", unit: "0", brandId: "0",
  179. visit: "0", favorite: "0", sort: "0", listImg: "xx.jpg",
  180. smallImg: "xx.jpg", specArray: "{}", exp: "0"
  181. }];
  182. // console.log(present);
  183. let url = "updateBatch";
  184. if (that.presentId === '') url = "saveBatch";
  185. BasicFunction.get_data("smartGoods/" + url, function (response) {
  186. // console.log("------ Data Rcvd in PresentReg --------");
  187. // console.log(response);
  188. if (response.ret === "10000") {
  189. that.$router.push('share');
  190. } else {
  191. Toast.fail("上传失败,请检查网络");
  192. that.loading = false;
  193. }
  194. }, present);
  195. }
  196. else {
  197. let formData = new FormData();
  198. // formData.append('file', this.photo);
  199. let fileObj = this.photo;
  200. if (fileObj.size / 1024 > 1025) { //大于1M,进行压缩上传
  201. photoCompress(fileObj, {
  202. quality: 0.2
  203. }, function (base64Codes) {
  204. //console.log("压缩后:" + base.length / 1024 + " " + base);
  205. var bl = convertBase64UrlToBlob(base64Codes);
  206. // formData.append('file', fileObj);
  207. console.log(fileObj);
  208. formData.append("file", bl); // 文件对象
  209. console.log(bl);
  210. console.log("file_" + Date.parse(new Date()) + ".jpg");
  211. });
  212. } else { //小于等于1M 原图上传
  213. formData.append('file', this.photo);
  214. }
  215. // 先上传图片
  216. BasicFunction.get_data("fileserver/upload?sign=abcdefg", function (res) {
  217. // console.log('----- upload photo result -----');
  218. // console.log(res);
  219. // if (res.model === undefined || res.model.length === 0) {
  220. // Toast.fail('上传失败,请重试');
  221. // return;
  222. // }
  223. try {
  224. let imgURL = res.model;
  225. if (res.ret === '10000') {
  226. // console.log('img:' + imgURL);
  227. let present = [{
  228. goodid: that.presentId,
  229. name: that.presentName,
  230. goodsNo: "0",
  231. modelId: "0",
  232. ownerId: userId.toString(),
  233. goodFirstKind: gender,
  234. goodSecondKind: tag.toString(),
  235. sellPrice: "0.0",
  236. marketPrice: "0.0",
  237. costPrice: "0.0",
  238. upTime: getNowFormatDate(),
  239. downTime: getNowFormatDate(),
  240. createTime: getNowFormatDate(),
  241. storeNums: "0",
  242. img: imgURL,
  243. isDel: "0",
  244. content: "none",
  245. keywords: "none",
  246. description: that.desc,
  247. searchWords: "",
  248. weight: "0.0",
  249. point: "0",
  250. unit: "0",
  251. brandId: "0",
  252. visit: "0",
  253. favorite: "0",
  254. sort: "0",
  255. listImg: "xx.jpg",
  256. smallImg: "xx.jpg",
  257. specArray: "{}",
  258. exp: "0"
  259. }];
  260. // console.log(present);
  261. let url = "updateBatch";
  262. if (that.presentId === '') url = "saveBatch";
  263. BasicFunction.get_data("smartGoods/" + url, function (response) {
  264. // console.log("------ Data Rcvd in PresentReg --------");
  265. // console.log(response);
  266. if (response.ret === "10000") {
  267. that.$router.push('share');
  268. } else {
  269. Toast.fail("上传失败,请检查网络");
  270. that.loading = false;
  271. }
  272. }, present);
  273. } else {
  274. Toast.fail('上传图片失败,请检查网络');
  275. that.loading = false;
  276. }
  277. } catch (e) {
  278. console.warn(e);
  279. Toast.fail('上传图片失败,请检查网络');
  280. that.loading = false;
  281. }
  282. }, formData);
  283. }
  284. },
  285. onRead: function (e) {
  286. this.photo = e;
  287. }
  288. },
  289. created: function () {
  290. if (!localStorage.getItem('frontend-userid')) {
  291. this.$router.push('login');
  292. return;
  293. }
  294. if(this.$route.query.present) {
  295. let present = this.$route.query.present;
  296. // console.log('----- Present Info -----');
  297. // console.log(present);
  298. this.presentId = present.id;
  299. this.gender = present.gender.toString();
  300. this.presentName = present.name;
  301. this.desc = present.desc_long;
  302. this.tag = present.tags;
  303. this.imgUrl = present.imgUrl;
  304. this.photo = present.imgUrl;
  305. }
  306. }
  307. }
  308. function getNowFormatDate() {
  309. let date = new Date();
  310. let seperator = "-";
  311. let year = date.getFullYear();
  312. let month = date.getMonth() + 1;
  313. let strDate = date.getDate();
  314. let hour = date.getHours();
  315. let minutes = date.getMinutes();
  316. let seconds = date.getSeconds();
  317. if (month >= 1 && month <= 9) {
  318. month = "0" + month;
  319. }
  320. if (strDate >= 0 && strDate <= 9) {
  321. strDate = "0" + strDate;
  322. }
  323. if (hour >= 0 && hour <= 9) {
  324. hour = "0" + hour;
  325. }
  326. if (minutes >= 0 && minutes <= 9) {
  327. minutes = "0" + minutes;
  328. }
  329. if (seconds >= 0 && seconds <= 9) {
  330. seconds = "0" + seconds;
  331. }
  332. let currentdate = year + seperator + month + seperator + strDate;
  333. currentdate = currentdate + ' ' + hour + ':' + minutes + ':' + seconds;
  334. return currentdate;
  335. }
  336. // 判断是否有emoji
  337. function isEmojiCharacter(substring) {
  338. for (var i = 0; i < substring.length; i++) {
  339. var hs = substring.charCodeAt(i);
  340. if (0xd800 <= hs && hs <= 0xdbff) {
  341. if (substring.length > 1) {
  342. var ls = substring.charCodeAt(i + 1);
  343. var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
  344. if (0x1d000 <= uc && uc <= 0x1f77f) {
  345. return true;
  346. }
  347. }
  348. } else if (substring.length > 1) {
  349. var ls = substring.charCodeAt(i + 1);
  350. if (ls == 0x20e3) {
  351. return true;
  352. }
  353. } else {
  354. if (0x2100 <= hs && hs <= 0x27ff) {
  355. return true;
  356. } else if (0x2B05 <= hs && hs <= 0x2b07) {
  357. return true;
  358. } else if (0x2934 <= hs && hs <= 0x2935) {
  359. return true;
  360. } else if (0x3297 <= hs && hs <= 0x3299) {
  361. return true;
  362. } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030
  363. || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b
  364. || hs == 0x2b50) {
  365. return true;
  366. }
  367. }
  368. }
  369. }
  370. /*
  371. 三个参数
  372. file:一个是文件(类型是图片格式),
  373. w:一个是文件压缩的后宽度,宽度越小,字节越小
  374. objDiv:一个是容器或者回调函数
  375. photoCompress()
  376. */
  377. function photoCompress(file, w, objDiv) {
  378. var ready = new FileReader();
  379. /*开始读取指定的Blob对象或File对象中的内容. 当读取操作完成时,readyState属性的值会成为DONE,如果设置了onloadend事件处理程序,则调用之.同时,result属性中将包含一个data: URL格式的字符串以表示所读取文件的内容.*/
  380. ready.readAsDataURL(file);
  381. ready.onload = function () {
  382. var re = this.result;
  383. canvasDataURL(re, w, objDiv)
  384. }
  385. }
  386. function canvasDataURL(path, obj, callback) {
  387. var img = new Image();
  388. img.src = path;
  389. img.onload = function () {
  390. var that = this;
  391. // 默认按比例压缩
  392. var w = that.width,
  393. h = that.height,
  394. scale = w / h;
  395. w = obj.width || w;
  396. h = obj.height || (w / scale);
  397. var quality = 0.7; // 默认图片质量为0.7
  398. //生成canvas
  399. var canvas = document.createElement('canvas');
  400. var ctx = canvas.getContext('2d');
  401. // 创建属性节点
  402. var anw = document.createAttribute("width");
  403. anw.nodeValue = w;
  404. var anh = document.createAttribute("height");
  405. anh.nodeValue = h;
  406. canvas.setAttributeNode(anw);
  407. canvas.setAttributeNode(anh);
  408. ctx.drawImage(that, 0, 0, w, h);
  409. // 图像质量
  410. if (obj.quality && obj.quality <= 1 && obj.quality > 0) {
  411. quality = obj.quality;
  412. }
  413. // quality值越小,所绘制出的图像越模糊
  414. var base64 = canvas.toDataURL('image/jpeg', quality);
  415. // 回调函数返回base64的值
  416. callback(base64);
  417. }
  418. }
  419. /**
  420. * 将以base64的图片url数据转换为Blob
  421. * @param urlData
  422. * 用url方式表示的base64图片数据
  423. */
  424. function convertBase64UrlToBlob(urlData) {
  425. var arr = urlData.split(','), mime = arr[0].match(/:(.*?);/)[1],
  426. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  427. while (n--) {
  428. u8arr[n] = bstr.charCodeAt(n);
  429. }
  430. return new Blob([u8arr], {type: mime});
  431. }
  432. </script>
  433. <style scoped>
  434. p, h3 {
  435. color: #fd6740;
  436. }
  437. h3 {
  438. padding: 5px;
  439. border-bottom: solid 1px #e4e4e4;
  440. }
  441. .card {
  442. background-color: white;
  443. margin: 20px 20px;
  444. border: solid 1px #e4e4e4;
  445. padding: 15px;
  446. text-align: left;
  447. }
  448. .submit_button {
  449. margin-bottom: 10px;
  450. margin-left: auto;
  451. margin-right: auto;
  452. background-color: #fd6740;
  453. border-color: #fd6740;
  454. width: 85%;
  455. }
  456. </style>