SkuMessages.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Utils
  2. import { createNamespace } from '../../utils';
  3. import { isEmail } from '../../utils/validate/email';
  4. import { isNumeric } from '../../utils/validate/number'; // Components
  5. import Cell from '../../cell';
  6. import Field from '../../field';
  7. import SkuImgUploader from './SkuImgUploader';
  8. import SkuDateTimeField from './SkuDateTimeField';
  9. var _createNamespace = createNamespace('sku-messages'),
  10. createComponent = _createNamespace[0],
  11. bem = _createNamespace[1],
  12. t = _createNamespace[2];
  13. export default createComponent({
  14. props: {
  15. messageConfig: Object,
  16. goodsId: [Number, String],
  17. messages: {
  18. type: Array,
  19. default: function _default() {
  20. return [];
  21. }
  22. }
  23. },
  24. data: function data() {
  25. return {
  26. messageValues: this.resetMessageValues(this.messages)
  27. };
  28. },
  29. watch: {
  30. messages: function messages(val) {
  31. this.messageValues = this.resetMessageValues(val);
  32. }
  33. },
  34. methods: {
  35. resetMessageValues: function resetMessageValues(messages) {
  36. var messageConfig = this.messageConfig;
  37. var _messageConfig$initia = messageConfig.initialMessages,
  38. initialMessages = _messageConfig$initia === void 0 ? {} : _messageConfig$initia;
  39. return (messages || []).map(function (message) {
  40. return {
  41. value: initialMessages[message.name] || ''
  42. };
  43. });
  44. },
  45. getType: function getType(message) {
  46. if (+message.multiple === 1) {
  47. return 'textarea';
  48. }
  49. if (message.type === 'id_no') {
  50. return 'text';
  51. }
  52. return message.datetime > 0 ? 'datetime' : message.type;
  53. },
  54. getMessages: function getMessages() {
  55. var messages = {};
  56. this.messageValues.forEach(function (item, index) {
  57. messages["message_" + index] = item.value;
  58. });
  59. return messages;
  60. },
  61. getCartMessages: function getCartMessages() {
  62. var _this = this;
  63. var messages = {};
  64. this.messageValues.forEach(function (item, index) {
  65. var message = _this.messages[index];
  66. messages[message.name] = item.value;
  67. });
  68. return messages;
  69. },
  70. getPlaceholder: function getPlaceholder(message) {
  71. var type = +message.multiple === 1 ? 'textarea' : message.type;
  72. var map = this.messageConfig.placeholderMap || {};
  73. return message.placeholder || map[type] || t("placeholder." + type);
  74. },
  75. validateMessages: function validateMessages() {
  76. var values = this.messageValues;
  77. for (var i = 0; i < values.length; i++) {
  78. var value = values[i].value;
  79. var message = this.messages[i];
  80. if (value === '') {
  81. // 必填字段的校验
  82. if (String(message.required) === '1') {
  83. var textType = t(message.type === 'image' ? 'upload' : 'fill');
  84. return textType + message.name;
  85. }
  86. } else {
  87. if (message.type === 'tel' && !isNumeric(value)) {
  88. return t('invalid.tel');
  89. }
  90. if (message.type === 'mobile' && !/^\d{6,20}$/.test(value)) {
  91. return t('invalid.mobile');
  92. }
  93. if (message.type === 'email' && !isEmail(value)) {
  94. return t('invalid.email');
  95. }
  96. if (message.type === 'id_no' && (value.length < 15 || value.length > 18)) {
  97. return t('invalid.id_no');
  98. }
  99. }
  100. }
  101. },
  102. /**
  103. * The phone number copied from IOS mobile phone address book
  104. * will add spaces and invisible Unicode characters
  105. * which cannot pass the /^\d+$/ verification
  106. * so keep numbers and dots
  107. */
  108. getFormatter: function getFormatter(message) {
  109. return function formatter(value) {
  110. if (message.type === 'mobile' || message.type === 'tel') {
  111. return value.replace(/[^\d.]/g, '');
  112. }
  113. return value;
  114. };
  115. },
  116. getExtraDesc: function getExtraDesc(message) {
  117. var h = this.$createElement;
  118. var extraDesc = message.extraDesc;
  119. if (extraDesc) {
  120. return h("div", {
  121. "class": bem('extra-message')
  122. }, [extraDesc]);
  123. }
  124. },
  125. genMessage: function genMessage(message, index) {
  126. var _this2 = this;
  127. var h = this.$createElement;
  128. if (message.type === 'image') {
  129. return h(Cell, {
  130. "key": this.goodsId + "-" + index,
  131. "attrs": {
  132. "title": message.name,
  133. "required": String(message.required) === '1',
  134. "valueClass": bem('image-cell-value')
  135. },
  136. "class": bem('image-cell')
  137. }, [h(SkuImgUploader, {
  138. "attrs": {
  139. "maxSize": this.messageConfig.uploadMaxSize,
  140. "uploadImg": this.messageConfig.uploadImg,
  141. "customUpload": this.messageConfig.customUpload
  142. },
  143. "model": {
  144. value: _this2.messageValues[index].value,
  145. callback: function callback($$v) {
  146. _this2.$set(_this2.messageValues[index], "value", $$v);
  147. }
  148. }
  149. }), h("div", {
  150. "class": bem('image-cell-label')
  151. }, [t('imageLabel')])]);
  152. } // 时间和日期使用的vant选择器
  153. var isDateOrTime = ['date', 'time'].indexOf(message.type) > -1;
  154. if (isDateOrTime) {
  155. return h(SkuDateTimeField, {
  156. "attrs": {
  157. "label": message.name,
  158. "required": String(message.required) === '1',
  159. "placeholder": this.getPlaceholder(message),
  160. "type": this.getType(message)
  161. },
  162. "key": this.goodsId + "-" + index,
  163. "model": {
  164. value: _this2.messageValues[index].value,
  165. callback: function callback($$v) {
  166. _this2.$set(_this2.messageValues[index], "value", $$v);
  167. }
  168. }
  169. });
  170. }
  171. return h("div", {
  172. "class": bem('cell-block')
  173. }, [h(Field, {
  174. "attrs": {
  175. "maxlength": "200",
  176. "center": !message.multiple,
  177. "label": message.name,
  178. "required": String(message.required) === '1',
  179. "placeholder": this.getPlaceholder(message),
  180. "type": this.getType(message),
  181. "formatter": this.getFormatter(message),
  182. "border": false
  183. },
  184. "key": this.goodsId + "-" + index,
  185. "model": {
  186. value: _this2.messageValues[index].value,
  187. callback: function callback($$v) {
  188. _this2.$set(_this2.messageValues[index], "value", $$v);
  189. }
  190. }
  191. }), this.getExtraDesc(message)]);
  192. }
  193. },
  194. render: function render() {
  195. var h = arguments[0];
  196. return h("div", {
  197. "class": bem()
  198. }, [this.messages.map(this.genMessage)]);
  199. }
  200. });