| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use app\constants\OptionSetting;
- use app\jobs\storeSync\DiyCommon;
- use app\utils\Validator;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- use yii\helpers\Json;
- use yii\validators\StringValidator;
- /**
- * class Option
- * @package app\modules\common\models
- * @property integer $store_id
- * @property string $name
- * @property string $value
- * @property integer $updated_at
- * @property string $group
- */
- class Option extends ActiveRecord
- {
- CONST OPTOPN_KEY = 'alipay_config';
- CONST ALIPAY_CONFIG_CERT = 'alipay_config_cert';
- CONST CLOUD_STORE_KEY = -2;
- CONST PROMOTER_GROUP_NAME = 'promoter';
- CONST PROMOTER_SETTING_NAME = 'promoter_setting';
- CONST SHARER_SETTING_NAME = 'sharer_setting';
- public static function takeName()
- {
- return "{%option}";
- }
- public function behaviors()
- {
- return [
- [
- // 自动更新更新时间
- 'class' => 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;
- }
- }
|