common.js 3.3 KB
/*
 * @Description: 省市组件
 * @Author: huyue
 * @Date: 2022-04-08 17:00:25
 * @LastEditors: huyue
 * @LastEditTime: 2022-04-11 17:01:39
 */

layui.define(['form', 'laydate', 'carousel', 'util'], function (exports) {
  const { form, laydate, carousel, util, $, cache } = layui
  exports('common', {
    /**
     * 生成下拉选
     * @param config:参数
     *      id:dom的id
     * 			string:单个dom
     * 			Array:多个dom
     *      url:获取地址
     *      value:<option>的value名
     *      text:<option>text
     * 			getValue:自定义传输值
     * 			getText:自定义显示文本
     *      param:url请求的额外参数
     *      initData:默认option
     *			link:需要联动的下拉选的dom id
     *			select:选中事件
     *	@return <option/>文本
     */
    getSelect(config) {
      let f = {
          id: undefined,
          url: undefined,
          name: '',
          value: 'value',
          text: 'text',
          getValue: function (v) {
            return v[this.value]
          },
          getText: function (t) {
            return t[this.text]
          },
          param: null,
          initData: [{ adcode: '', name: '请选择' }],
          link: false,
          select: false,
          data: [],
          isFirst: true,
          ...config
        },
        that = this,
        isArray = Array.isArray(f.id)
      if (f.isFirst) {
        if (isArray) {
          f.id.forEach(id => {
            $('#' + id).attr('lay-filter', 'commonSelect' + id)
            $('#' + id).attr('name', f.name)
          })
        } else {
          $('#' + f.id).attr('lay-filter', 'commonSelect' + f.id)
          $('#' + f.id).attr('name', f.name)
        }
      }

      // getProvinceList()
      //绑定选中事件
      if (f.select) {
        if (isArray) {
          f.id.forEach(id => {
            form.on('select(commonSelect' + id + ')', function (data) {
              f.select(data)
            })
          })
        } else {
          form.on('select(commonSelect' + f.id + ')', function (data) {
            f.select(data)
          })
        }
      }
      //生成option
      function getOptions() {
        // if (!f.link) {
        //   f.data = that.provinceInfoList
        // }
        let h = ''
        $.each(f.initData, function (i, d) {
          h += '<option value="' + d.adcode + '">' + d.name + '</option>'
        })
        $.each(f.data, function (i, d) {
          h += '<option value="' + f.getValue(d) + '">' + f.getText(d) + '</option>'
        })
        if (isArray) {
          f.id.forEach(d => {
            let a = $('#' + d)
            a.empty()
            a.append(h)
          })
        } else {
          let a = $('#' + f.id)
          a.empty()
          a.append(h)
        }
        form.render('select')
        return h
      }
      function getProvinceList() {
        var ajax = layui.$.ajax
        ajax({
          // url: 'https://oss-config.test.glsx.com.cn/mock/5be17454f31545347559d499/config/glsx-oss-account-config',
          url: 'http://oss-config.dev.glsx.com.cn/mock/617ba1df1072f92988f05a14/account/provinceList',
          async: false,
          success: function (res) {
            that.provinceInfoList = res.provinceInfoList
          }
        })
      }
      return getOptions()
    }
  })
})