假设有这么一个场景;之前你们的业务都是基于微信的,整个项目任何操作都是关于微信;比如你是做微信小程序服务平台的,底层代码已经非常非常多了,这个时候假设让你再拓展一个支付宝小程序服务商处理,需要兼容微信;支付宝有工具可以进行微信小程序代码转为支付宝小程序,但是也是有很多东西要修改的。下面说一下如何从底层兼容。
可能任何表都要加一个字段区分支付宝或者小程序,假设增加一个type字段;就默认为0吧,支付宝为1;下面在底层从整个系统区分它们。
①增加一个类,重写底层模型类逻辑。
<?php
namespace app\models;
use yii\db\ActiveQuery;
class MyActiveQuery extends ActiveQuery
{
public $myCondition = [];
public function where($condition, $params = [])
{
if (count($this->myCondition) != 0) {
$newCondition = $this->unsetArray($this->myCondition, $condition);
if (count($newCondition) != 0) {
parent::where($newCondition);
return parent::andWhere($condition, $params);
} else {
return parent::where($condition, $params);
}
} else {
return parent::where($condition, $params);
}
}
public function andWhere($condition, $params = [])
{
if($this->where === null){
if (count($this->myCondition) != 0) {
$newCondition = $this->unsetArray($this->myCondition, $condition);
parent::andWhere($newCondition);
}
}else{
$where = $this->where;
if(isset($where[1])){
$arr = $where[1];
}else{
$arr = $where;
}
$newCondition = $this->unsetArray($arr, $condition);
parent::where($newCondition);
foreach($where as $key=>$value){
if($key <= 1){
continue;
}
parent::andWhere($value);
}
}
return parent::andWhere($condition, $params); // TODO: Change the autogenerated stub
}
/**
* @param $arr
* @param $condition // 四种情况 1、'is_show = 1' 字符串 2、['is_show'=>1] 键值对数组 3、['!=','is_show',1]数组 4、['or',['is_show'=>1],'is_show = 0',['!=','is_show',1]];
* @return array
*/
private function unsetArray($arr, $condition)
{
$newCondition = [];
foreach ($arr as $key => $item) {
if (is_string($condition)) {
if ($this->newStrstr($condition, $key) == false) {
$newCondition[$key] = $item;
}
} else if (is_array($condition)) {
$arr = [$key, $item];
$ok = $this->conditionArray($arr, $condition);
if ($ok) {
$newCondition[$key] = $item;
}
} else {
$newCondition[$key] = $item;
}
}
return $newCondition;
}
private function conditionArray($arr, $condition)
{
$ok = true;
$type = 0;
foreach ($condition as $k => $v) {
if (is_numeric($k)) {
if ($k == 0) {
if (in_array($v, ['or', 'and', 'Or', 'OR', 'AND', 'And'])) {
$type = 0;
} else {
$type = 1;
}
}
} else {
$type = 2;
if ($this->newStrstr($k, $arr[0])) {
$ok = false;
break;
}
}
if ($type == 0) {
if ($k == 0) {
continue;
}
if (is_array($v)) {
$okA = $this->conditionArray($arr, $v);
if ($okA == false) {
$ok = false;
break;
}
} else {
if ($k == 1 && $this->newStrstr($v, $arr[0])) {
$ok = false;
break;
}
}
} elseif ($type == 1) {
if (isset($condition[1]) && is_string($condition[1])) {
if ($this->newStrstr($condition[1], $arr[0])) {
$ok = false;
break;
}
}
}
}
return $ok;
}
private function newStrstr($a, $b)
{
return strstr($a, $b);
}
}
②在任何需要修改的表模型类中增加 重置查询条件
<?php
namespace app\models;
use app\models\common\admin\log\CommonActionLog;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
/**
* This is the model class for table "{{%ys_order}}".
*
* @property integer $id
* @property integer $store_id
* @property integer $user_id
* @property integer $book_shop_id
* @property string $order_no
* @property string $total_price
* @property string $pay_price
* @property string $express_price
* @property string $name
* @property string $mobile
* @property string $offline_time
* @property string $address
* @property string $remark
* @property integer $is_pay
* @property integer $pay_type
* @property integer $pay_time
* @property integer $is_send
* @property integer $send_time
* @property string $express
* @property string $express_no
* @property integer $is_confirm
* @property integer $confirm_time
* @property integer $is_comment
* @property integer $apply_delete
* @property integer $is_refund
* @property integer $addtime
* @property integer $end_time
* @property integer $is_delete
* @property integer $is_price
* @property integer $parent_id
* @property string $first_price
* @property string $second_price
* @property string $third_price
* @property string $content
* @property integer $is_offline
* @property integer $clerk_id
* @property string $address_data
* @property integer $is_cancel
* @property string $offline_qrcode
* @property string $before_update_price
* @property integer $shop_id
* @property integer $parent_id_1
* @property integer $parent_id_2
* @property integer $is_sale
* @property string $words
* @property string $version
* @property integer $is_recycle
* @property string $rebate
* @property string $seller_comments
* @property integer $mch_id
* @property integer $order_union_id
* @property integer $is_transfer
* @property integer $type
* @property string $share_price
* @property integer $is_show
* @property string $callback_phone
* @property string $admin_account
* @property integer $update_time
* @property string $longitude
* @property string $latitude
* @property integer $send_type
* @property string $transaction_id
* @property string $trade_close
* @property string $take_no
*/
class YsOrder extends \yii\db\ActiveRecord
{
/**
* 是否取消(手动):已取消
*/
const IS_DELETE_TRUE = 1;
/**
* 是否取消(手动):未取消
*/
const IS_DELETE_FALSE = 0;
/**
* 是否取消(自动):已取消
*/
const IS_CANCEL_TRUE = 1;
/**
* 是否取消(自动):未取消
*/
const IS_CANCEL_FALSE = 0;
/**
* 是否支付:已支付
*/
const IS_PAY_TRUE = 1;
/**
* 是否支付:未支付
*/
const IS_PAY_FALSE = 0;
/**
* 支付方式:未支付
*/
const PAY_TYPE_UNPAID = 0;
/**
* 支付方式:微信支付
*/
const PAY_TYPE_WECHAT = 1;
/**
* 支付方式:货到付款
*/
const PAY_TYPE_COD = 2;
/**
* 支付方式:余额支付
*/
const PAY_TYPE_BALANCE_PAID = 3;
/**
* 发货状态:已发货
*/
const IS_SEND_TRUE = 1;
/**
* 发货状态:未发货
*/
const IS_SEND_FALSE = 0;
//public $shop_name = '';
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%ys_order}}';
}
/** 以下为重置查询;默认为微信0;这样底层就默认只查微信;你就不会因为每个地方都要去找手动加
* @inheritdoc
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function find()
{
return Yii::createObject(MyActiveQuery::className(), [
get_called_class(), [
'myCondition' => [
'type' => 0
]
]
]);
}
}
③测试效果,是否带上底层查询 和是否可以重写查询条件