TimestampBehavior::class, 'value' => time() ] ]; } public function rules() { return [ [['store_id'], 'integer'], [['name',], 'required'], [['value'], 'string'], [['name','group'], 'string', 'max' => 255], [['updated_at','created_at'], 'safe'] ]; } public function attributeLabels() { return [ 'store_id' => 'store ID', 'name' => '设置名称', 'value' => '值', 'group' => '类型', 'updated_at' => '更新时间' ]; } /** * 获取配置数据 * @param $name * @param int $store_id * @param string $group * @param null $default * @return array|ActiveRecord|ActiveRecord[]|null */ public static function get ($name, $store_id = null, $group = '', $default = null) { if ($store_id === null) { $store_id = get_store_id(); } $key = $name.'_'.$store_id.'_'.$group.'_option'; /*if (\Yii::$app->cache->exists($key)) { return \Yii::$app->cache->get($key); }*/ $query = self::find()->where([ 'name' => $name, 'store_id' => $store_id, ])->select(['name','group','value','updated_at']); if ($group) { $query->andWhere(['group' => $group]); } if (is_array($name)) { $data = $query->asArray()->all(); $res = $data ?: []; } else { $data = $query->asArray()->one(); $res = $data ?: ['value' => $default]; } \Yii::$app->cache->set($key,$res,3); return $res; } /** * 设置配置数据 * @param $name | sting or array // 配置名称 * @param $value | string or array // 配置数据 * @param int $store_id // storeId * @param string $group // 配置类型 * @return bool * @throws \yii\db\Exception */ public static function set ($name, $value, $store_id = null, $group = '') { if ($store_id === null) { $store_id = get_store_id(); } if (is_array($name) || is_array($value)) { if (!is_array($name) || !is_array($value) || count($name) != count($value) || count($name) == 0) { return false; } $t = \Yii::$app->db->beginTransaction(); foreach ($name as $k => $v) { $option = self::findOne(['name' => $v, 'store_id' => $store_id, 'group' => $group]); if (!$option) { $option = new self(); $option->group = $group; $option->store_id = $store_id; } $option->name = $v; $option->value = is_array($value[$k]) ? Json::encode($value[$k]) : '' . $value[$k]; if (!$option->save()) { $t->rollBack(); return false; } } $t->commit(); return true; } else { $option = self::findOne(['name' => $name, 'store_id' => $store_id, 'group' => $group]); if (!$option) { $option = new self(); $option->group = $group; $option->store_id = $store_id; } $option->name = $name; $option->value = '' . $value; if (!$option->save()) { \Yii::error($option->errors); return false; } return true; } } public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub if (!$insert) { if (in_array($this->name, ['share_basic_setting', 'share_money_setting'])) { (new DiyCommon)->JobBehaviors($this->store_id, StoreSyncExtLog::TYPE_SHARE_CONFIG, [$this->name]); } } } /** * 根据类获取数据 * @param $group * @param $store_id * @return array */ public static function getGroup ($group, $store_id = null) { if ($store_id === null) { $store_id = get_store_id(); } $group_setting = OptionSetting::getOptionSetting(); $group_arr = $group_setting[$group]['list'] ?: []; $data = []; foreach ($group_arr as $val) { $group_data = self::findOne(['store_id' => $store_id, 'group' => $group, 'name' => $val['name']]); if ($val['type'] == 'checkbox') { if ($group_data) { $attr = $val['default']; $data_val = Json::decode($group_data->value,true); foreach ($attr as $k => &$v) { $v = isset($data_val[$k]) ? $data_val[$k] : $v; } } } if ($val['type'] == 'address_info') { if ($group_data) { $attr = Json::decode($group_data->value,true); } } $data[] = [ 'name' => $group_data ? $group_data->name : $val['name'], 'text' => $val['text'], 'group' => $group_data ? $group_data->group : $group, 'type' => $val['type'], 'size' => isset($val['size']) ? $val['size'] : '', 'title' => isset($val['title']) ? $val['title'] : '', 'required' => $val['required'], 'muted' => isset($val['muted']) ? $val['muted'] : '', 'value' => $group_data ? (($val['type'] == 'checkbox' || $val['type'] == 'address_info') ? $attr : $group_data->value ) : (is_int($val['default']) ? "$val[default]" : $val['default'] ), 'updated_at' => $group_data ? $group_data->updated_at : 0, 'unit' => isset($val['unit']) ? $val['unit'] : '', 'select_list' => isset($val['select_list']) ? $val['select_list'] : [] ]; } return $data; } /** * 根据类型批量设置数据 * @param $group * @param $set_data * @param int $store_id * @return bool */ public static function setGroup ($group, $set_data, $store_id = null) { if ($store_id === null) { $store_id = get_store_id(); } foreach ($set_data as $val) { if ($val['name']) { $group_data = self::findOne(['store_id' => $store_id, 'group' => $group, 'name' => $val['name']]); if (!$group_data) { $group_data = new self(); $group_data->store_id = $store_id; $group_data->group = $group; } $group_data->name = $val['name']; if ($val['type'] == 'checkbox' || $val['type'] == 'address_info') { $group_data->value = Json::encode($val['value']); }else { $group_data->value = "{$val['value']}"; } if (!$group_data->save()) { return false; } } } return true; } /** * 验证保存信息 * @param $data * @param $group * @return array */ public static function verifyData ($data, $group) { $default = OptionSetting::getOptionSetting(); if (empty($data) || empty($group) || empty($default[$group])) { return [ 'code' => 1, 'msg' => '数据格式错误:'.Json::encode(['data' => $data ,'group' => $group,'default_group' =>$default[$group]]) ]; } $validArr = []; foreach ($default[$group]['list'] as $val) { foreach ($data as $v) { if ($v['name'] == $val['name']) { if ($val['required'] && empty($v['value'])) { return [ 'code' => 1, 'msg' => $val['text'].'为必填' ]; } switch ($val['type']) { case 'text': $validArr[] = ['string', $v['value'], [ 'max' => 255 ], $v['text'].'最大长度255' ]; break; case 'number': $validArr[] = ['number', $v['value'], [ 'max' => 9999999999,'min' => 0 ], $v['text'].'请填写0~9999999999范围数字']; break; case 'checkbox': break; case 'radio': $redio_arr = [0, 1]; $v['select_list'] = isset($v['select_list']) && is_array($v['select_list']) ? $v['select_list'] : []; if (count($v['select_list']) > 0) { $select_arr = []; foreach($v['select_list'] as $select_val) { $select_arr[] = $select_val['value']; } $redio_arr = $select_arr; } $validArr[] = ['radio', $v['value'], $redio_arr, $v['text'].'参数错误']; break; case 'textarea': $validArr[] = ['string', $v['value'], [ 'max' => 2000 ], $v['text'].'最大长度2000' ]; break; case 'image': break; case 'mobile': $validArr[] = ['phone', $v['value'], [], $v['text'].'格式错误']; break; default: break; } } } } if ($group == 'share') { return ['code' => 0, 'msg' => 'success']; } if ($validArr) { $valid = new Validator($validArr); } if (!$valid->success && $validArr) { $text = $valid->success !== true ? $valid->error : ''; return [ 'code' => 1, 'msg' => $text ? $text : '数据格式错误', 'validArr' => $default[$group]['list'] ]; } } /** * 获取充值设置 * @return array */ public static function getRechargeOption() { $name = [ 'recharge_wallet_status', 'recharge_custom_status', 'recharge_pic_url', 'recharge_ad_pic_url', 'recharge_page_url', 'recharge_p_pic_url', 'recharge_help', ]; $data = self::get($name, get_store_id(), 'recharge'); $data = array_column($data, null, 'name'); $balance = [ 'status' => $data['recharge_wallet_status']['value'], 'pic_url' => $data['recharge_pic_url']['value'], 'ad_pic_url' => $data['recharge_ad_pic_url']['value'], 'page_url' => $data['recharge_page_url']['value'], 'p_pic_url' => $data['recharge_p_pic_url']['value'], 'help' => $data['recharge_help']['value'], 'type' => $data['recharge_custom_status']['value'], ]; return $balance; } /** * 获取saas微信设置 * @return array */ public static function getSaasWechat2() { $name = [ 'sp_appid', 'sp_mch_id', 'sp_key', 'platform_appid', 'platform_mch_id', 'platform_apiclient_cert', 'platform_apiclient_key', 'sp_name' ]; $data = self::get($name, 0, 'saas'); $data = array_column($data, null, 'name'); $result = [ 'sp_appid' => $data['sp_appid']['value'], 'sp_mch_id' => $data['sp_mch_id']['value'], 'sp_key' => $data['sp_key']['value'], 'sub_appid' => $data['platform_appid']['value'], 'mch_id' => $data['platform_mch_id']['value'], 'sp_apiclient_cert' => $data['platform_apiclient_cert']['value'], 'sp_apiclient_key' => $data['platform_apiclient_key']['value'], 'sp_name' => $data['sp_name']['value'] ]; return $result; } /** * 获取saas微信设置 * @return array */ public static function getSaasWechat() { $name = [ 'sp_appid', 'sp_mch_id', 'sp_key', 'sp_apiclient_cert', 'sp_apiclient_key', 'sp_name' ]; $data = self::get($name, 0, 'saas'); $data = array_column($data, null, 'name'); $result = [ 'sp_appid' => $data['sp_appid']['value'], 'sp_mch_id' => $data['sp_mch_id']['value'], 'sp_key' => $data['sp_key']['value'], 'sp_apiclient_cert' => $data['sp_apiclient_cert']['value'], 'sp_apiclient_key' => $data['sp_apiclient_key']['value'], 'sp_name' => $data['sp_name']['value'] ]; return $result; } /** * 获取saas微信设置 * @return array */ public static function getStoreShare() { $store_share = self::get('store_share', 0, 'saas')['value']; if (empty($store_share)) { return []; } $store_share = Json::decode($store_share); return $store_share; } /** * 获取支付宝第三方应用配置 * @return array */ public static function getSaasAlipay() { $name = [ 'alipay_appid', 'alipay_public_key', 'alipay_app_public_key', 'alipay_app_private_key', 'alipay_user_id', 'alipay_name' ]; $data = self::get($name, 0, 'saas'); $data = array_column($data, null, 'name'); $result = [ 'app_id' => $data['alipay_appid']['value'], 'alipay_public_key' => $data['alipay_public_key']['value'], 'app_public_key' => $data['alipay_app_public_key']['value'], 'app_private_key' => $data['alipay_app_private_key']['value'], 'user_id' => $data['alipay_user_id']['value'], 'name' => $data['alipay_name']['value'] ]; return $result; } /** * 获取配置数据 * @param $name * @param int $store_id * @param string $group * @param null $default * @return array */ public static function getAlipayConfig () { $res = self::findOne([ 'name' => 'alipay_config', 'group' => 'alipay', 'store_id' => get_store_id() ]); if ($res) { return Json::decode($res->value); } return []; } public static function getFoodBookConfig($store_id = null) { $store_id = $store_id ?: get_store_id(); $key = 'food_book_info_' . $store_id; if ($res = cache()->get($key)) { return $res; } $food_book = self::get('food_book', $store_id, 'store')['value']; if (empty($food_book)) { return []; } $food_book = Json::decode($food_book); cache()->set($key, $food_book, 3600); return $food_book; } /** * 获取saas微信设置 * @return array */ public static function getSaasPlatformWechat() { $name = [ 'platform_key', 'platform_appid', ]; $data = self::get($name, 0, 'saas'); $data = array_column($data, null, 'name'); $result = [ 'key' => $data['platform_key']['value'], 'appid' => $data['platform_appid']['value'], ]; return $result; } public static function getSaasPlatformWechat2() { $name = [ 'platform_key', 'platform_appid', ]; $data = self::get($name, UserStringCodePlus::Serial_Code_Collaboration, 'saas'); $data = array_column($data, null, 'name'); $result = [ 'key' => $data['platform_key']['value'], 'appid' => $data['platform_appid']['value'], ]; return $result; } /** * 获取saas微信设置 * @return array */ public static function getSaasPlatformMchWechat() { $name = [ 'platform_mch_key', 'platform_mch_appid', ]; $data = self::get($name, 0, 'saas'); $data = array_column($data, null, 'name'); $result = [ 'key' => $data['platform_mch_key']['value'], 'appid' => $data['platform_mch_appid']['value'], ]; return $result; } /** * 获取联盟端服务号微信设置 * @return array */ public static function getSaasPlatformMchWechatFuwu() { $name = [ 'platform_mch_wechat_appid', 'platform_mch_wechat_secret', ]; $data = self::get($name, 0, 'saas'); $data = array_column($data, null, 'name'); $result = [ 'key' => $data['platform_mch_wechat_secret']['value'], 'appid' => $data['platform_mch_wechat_appid']['value'], ]; return $result; } /** * 获取saas抖音设置 * @return array */ public static function getSaasPlatformBytedance() { $name = [ 'bytedance_platform_key', 'bytedance_platform_appid', 'bytedance_platform_salt', 'bytedance_platform_token', ]; $data = self::get($name, 0, 'saas'); $data = array_column($data, null, 'name'); $result = [ 'key' => $data['bytedance_platform_key']['value'], 'appid' => $data['bytedance_platform_appid']['value'], 'salt' => $data['bytedance_platform_salt']['value'], 'token' => $data['bytedance_platform_token']['value'], ]; return $result; } /** * 获取saas抖音设置 * @return array */ public static function getDeliveryConfig() { $res = [ 'sf' => [ 'sf_app_key' => '', 'sf_app_secret' => '', 'sf_shop_no' => '', ], 'dd' => [ 'dd_app_key' => '', 'dd_app_secret' => '', 'dd_shop_no' => '', ], 'ss' => [ 'ss_app_key' => '', 'ss_app_secret' => '', 'ss_shop_no' => '', ], 'mt' => [ 'mt_app_key' => '', 'mt_app_secret' => '', 'mt_shop_no' => '', ], 'uu' => [ 'uu_app_key' => '', 'uu_app_secret' => '', 'uu_shop_no' => '', ] ]; $sf_data = self::get(['sf_app_key', 'sf_app_secret', 'sf_shop_no'], get_store_id(), 'store'); $dd_data = self::get(['dd_app_key', 'dd_app_secret', 'dd_shop_no'], get_store_id(), 'store'); $ss_data = self::get(['ss_app_key', 'ss_app_secret', 'ss_shop_no'], get_store_id(), 'store'); $mt_data = self::get(['mt_app_key', 'mt_app_secret', 'mt_shop_no'], get_store_id(), 'store'); $uu_data = self::get(['uu_app_key', 'uu_app_secret', 'uu_shop_no'], get_store_id(), 'store'); if (!empty($sf_data)) { $sf_data = array_column($sf_data, null, 'name'); $res['sf'] = [ 'sf_app_key' => $sf_data['sf_app_key']['value'], 'sf_app_secret' => $sf_data['sf_app_secret']['value'], 'sf_shop_no' => $sf_data['sf_shop_no']['value'], ]; } if (!empty($dd_data)) { $dd_data = array_column($dd_data, null, 'name'); $res['dd'] = [ 'dd_app_key' => $dd_data['dd_app_key']['value'], 'dd_app_secret' => $dd_data['dd_app_secret']['value'], 'dd_shop_no' => $dd_data['dd_shop_no']['value'], ]; } if (!empty($ss_data)) { $ss_data = array_column($ss_data, null, 'name'); $res['ss'] = [ 'ss_app_key' => $ss_data['ss_app_key']['value'], 'ss_app_secret' => $ss_data['ss_app_secret']['value'], 'ss_shop_no' => $ss_data['ss_shop_no']['value'], ]; } if (!empty($mt_data)) { $mt_data = array_column($mt_data, null, 'name'); $res['mt'] = [ 'mt_app_key' => $mt_data['mt_app_key']['value'], 'mt_app_secret' => $mt_data['mt_app_secret']['value'], 'mt_shop_no' => $mt_data['mt_shop_no']['value'], ]; } if (!empty($uu_data)) { $uu_data = array_column($uu_data, null, 'name'); $res['uu'] = [ 'uu_app_key' => $uu_data['uu_app_key']['value'], 'uu_app_secret' => $uu_data['uu_app_secret']['value'], 'uu_shop_no' => $uu_data['uu_shop_no']['value'], ]; } return $res; } public static function getPrintOrderSetting(){ $res = [ 'note_show' => 0, 'note' => '', 'qrcode_show' => 0, 'not_pay_notice' => '', 'pay_notice' => '' ]; $result = self::get(['note_show', 'note', 'qrcode_show','not_pay_notice','pay_notice'], get_store_id(), 'store'); $result = array_column($result, null, 'name'); if(!empty($result)){ $res['note_show'] = (int)$result['note_show']['value']; $res['note'] = $result['note']['value']; $res['qrcode_show'] = (int)$result['qrcode_show']['value']; $res['not_pay_notice'] = $result['not_pay_notice']['value']; $res['pay_notice'] = $result['pay_notice']['value']; } return $res; } public static function getShareSaleSetting($store_id){ $setting = self::get(OptionSetting::SHARE_STRING_CODE_SALE_SETTING, $store_id, OptionSetting::SHARE_GROUP_NAME, '{}'); $setting = $setting ? Json::decode($setting['value']) : []; return $setting; } public static function getShareDefaultSetting($store_id){ $setting = self::get(OptionSetting::SHARE_STRING_CODE_DEFAULT_SETTING, $store_id, OptionSetting::SHARE_GROUP_NAME, '{}'); $setting = $setting ? Json::decode($setting['value']) : []; return $setting; } public static function getDecode($name,$store_id,$group){ $setting = self::get($name, $store_id, $group, '{}'); $setting = $setting ? Json::decode($setting['value']) : []; return $setting; } }