123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <?php
- namespace app\common\model;
- use app\common\library\Redis;
- use app\main\constants\PayConstents;
- use think\Cache;
- use think\Log;
- use think\Model;
- use app\main\constants\ApiConstants;
- class Wxpay extends Model
- {
- /**
- * 微信支付域名host列表
- */
- const CACHE_KEY_PAYHOST = 'PAYHOST';
- /**
- * 平台默认支付域名 + platform_id
- */
- const CACHE_KEY_DEFAULT = 'WXP:D:';
- /**
- * 微信支付域名信息缓存 + id
- */
- const CACHE_KEY_ID = 'WXP:';
- /**
- * 微信支付域名信息缓存 + host
- */
- const CACHE_KEY_HOST = 'WXP:H:';
- // 表名
- protected $table = 'wxpay';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- 'status_text',
- 'fufen_text',
- 'isdefault_text',
- 'is_closure_text',
- ];
- /**
- * 删除全部缓存
- * @param $id
- * @param null $host
- * @param null $platform_id
- */
- public function delCacheAll($id,$host = null,$platform_id = null){
- $redis = Redis::instance();
- //删除ID
- $redis->del(self::CACHE_KEY_ID.$id);
- //Host没有时取ID的
- if(empty($host)){
- $host = $this->where('id',$id)->value('pay_host');
- }
- //平台Id没有事取ID的
- if(empty($platform_id)){
- $platform_id = $this->where('id',$id)->value('platform_id');
- }
- //删除Host
- $redis->del(self::CACHE_KEY_HOST.$host);
- //删除平台ID
- $redis->del(self::CACHE_KEY_DEFAULT.$platform_id);
- //删除支付列表
- $redis->del(self::CACHE_KEY_PAYHOST);
- }
- /**
- * 删除Wxpay平台缓存
- * @param null $platform_id
- */
- public function delCacheByPlatformId($platform_id){
- $redis = Redis::instance();
- //删除平台ID
- $redis->del(self::CACHE_KEY_DEFAULT.$platform_id);
- }
- /**
- * 删除Wxpay平台缓存
- */
- public function delCachePayHost(){
- $redis = Redis::instance();
- //删除平台ID
- $redis->del(self::CACHE_KEY_PAYHOST);
- }
- /**
- * 删除Wxpay平台缓存
- * @param null $host
- */
- public function delCacheByHost($host){
- $redis = Redis::instance();
- //删除平台ID
- $redis->del(self::CACHE_KEY_HOST.$host);
- }
- /**
- * 获取支付信息
- * @param $platform_id
- * @param null $id 表id
- * @return array|null
- */
- public function getInfo($platform_id,$id=null){
- if(empty($platform_id)) {
- return null;
- }
- $redis = Redis::instance();
- if(empty($id)){ //获取当前平台下默认的支付信息
- $modulename = request()->module();
- if($modulename == 'admin' && PHP_SAPI != 'cli') { //如果是后台
- $arr = $this->where(['platform_id'=>$platform_id,'isdefault'=>'1','status'=>1])->find();
- if($arr){
- $arr = $arr->toArray();
- }
- }else{
- $key = self::CACHE_KEY_DEFAULT.$platform_id;
- if($redis->exists($key)){
- $arr = $redis->hgetall($key);
- }else{
- $arr = $this->where(['platform_id'=>$platform_id,'isdefault'=>'1','status'=>1])->find();
- if($arr){
- $arr = $arr->toArray();
- $redis->hmset($key,$arr);
- $redis->expire($key,86400);
- }
- }
- }
- }else{ //获取指定的支付信息
- $modulename = request()->module();
- if($modulename == 'admin' && PHP_SAPI != 'cli' ) { //如果是后台
- $arr = $this->where('id',$id)->find();
- if($arr){
- $arr = $arr->toArray();
- }
- }else{
- $arr = $this->getInfoById($id);
- }
- }
- return $arr;
- }
- /**
- * 通过支付域名去相关支付信息
- * @param $domain
- * @return array|null
- * @throws \think\Exception
- */
- public function getInfoByDomain($domain){
- $info = $this->where('pay_host',$domain)->find();
- if(!empty($info)){
- return $info->toArray();
- }else{
- return null;
- }
- }
- /**
- * 根据host获取支付域名信息
- * @param $host
- * @return array|false|\PDOStatement|string|Model
- */
- public function getInfoByHost($host){
- $redis = Redis::instance();
- $key = self::CACHE_KEY_HOST.$host;
- if($redis->exists($key)){
- $payhost = $redis->hgetall($key);
- }else{
- if($payhost = $this->where('pay_host',$host)->find()){
- $payhost = $payhost->toArray();
- $redis->hmset($key,$payhost);
- $redis->expire($key,3600);
- }
- }
- return $payhost;
- }
- public function getInfoById($id){
- $redis = Redis::instance();
- $key = self::CACHE_KEY_ID.$id;
- $wxpay = null;
- if (Cache::has($key)) {
- Log::info("获取缓存:$key filecache命中");
- $wxpay = Cache::get($key);
- }
- if (!$wxpay) {
- if ($redis->exists($key)) {
- $wxpay = $redis->hgetall($key);
- Log::info("获取缓存:$key redis命中");
- Cache::set($key, $wxpay, 10);
- }
- }
- if (!$wxpay) {
- $wxpay = $this->where('id', $id)->find();
- if ($wxpay) {
- Log::info("获取缓存:$key mysql命中");
- $wxpay = $wxpay->toArray();
- $redis->hmset($key, $wxpay);
- $redis->expire($key, 3600);
- }
- }
- return $wxpay;
- }
- public function getListByBusiness($business){
- $redis = Redis::instance();
- $key = 'WPLS:'.intval($business);
- if($redis->exists($key)){
- $result = json_decode($redis->get($key), true);
- }else{
- $result = $this->where("find_in_set($business,business_line)")->where('status','1')->select();
- if($result){
- $result = collection($result)->toArray();
- }
- $redis->setex($key, 60, json_encode($result, JSON_UNESCAPED_UNICODE));
- }
- return $result;
- }
- //获取所有可用的支付域名 全部支付域名
- public function getHosts(){
- $redis = Redis::instance();
- if ($ret = $redis->get(self::CACHE_KEY_PAYHOST)) {
- $res = json_decode($ret, true);
- }else{
- $res = collection($this->column('pay_host'))->toArray();
- $redis->setex(self::CACHE_KEY_PAYHOST,86400,json_encode($res));
- }
- return $res;
- }
- public function platform()
- {
- return $this->belongsTo('Platform', 'platform_id')->setEagerlyType(0);
- }
- /**
- * 获取平台支付列表
- * @param $business 业务线
- * @param $platform_id
- * @param null $ophost_id 根据OpHost域名过滤
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getListFormatByPlatformId($business = null,$platform_id = null,$ophost_id = null){
- $result = [];
- $map['status'] = '1';
- if($platform_id){
- $map['platform_id'] = ['in',$platform_id];
- }
- if($business !== null){
- $map[] = ['exp',"FIND_IN_SET({$business},business_line)"];
- }
- if($ophost_id){
- $host = model('Ophost')->where('id',$ophost_id)->value('host');
- $map['pay_host'] = ['like','%'.$host];
- }
- if($list = $this->where($map)->select()){
- foreach($list as $val){
- $platform_name = model('Platform')->where('id',$val['platform_id'])->value('name');
- array_push($result,['fufen'=>$val['fufen'],'id'=>$val['id'],'platform_id'=>$val['platform_id'],'text'=>$platform_name.'--'.$val['name'].'--'.$val['p_desc']]);
- }
- }
- return $result;
- }
- public function getStatusList()
- {
- return ['0' => __('Status 0'),'1' => __('Status 1')];
- }
- public function getFufenList()
- {
- return ['0' => __('Fufen 0'),'1' => __('Fufen 1')];
- }
- public function getIsdefaultList()
- {
- return ['0' => __('Isdefault 0'),'1' => __('Isdefault 1')];
- }
- public function getPaymentMethodList()
- {
- return ApiConstants::$allPaymentMethods;
- }
- public function getIsclosureList()
- {
- return ['0' => __('IsClosure 0'),'1' => __('IsClosure 1')];
- }
- public function getStatusTextAttr($value, $data)
- {
- $value = $value ? $value : $data['status'];
- $list = $this->getStatusList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getFufenTextAttr($value, $data)
- {
- $value = $value ? $value : $data['fufen'];
- $list = $this->getFufenList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getIsdefaultTextAttr($value, $data)
- {
- $value = $value ? $value : $data['isdefault'];
- $list = $this->getIsdefaultList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getIsClosureTextAttr($value, $data)
- {
- $value = $value ? $value : $data['is_closure'];
- $list = $this->getIsclosureList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getQuartetIsSplit()
- {
- return ApiConstants::$quartetIsSplit;
- }
- //获取点众分账子账户
- public function getDzPamlpaySubaccountList()
- {
- return model('palmpay_sub_account')->alias('psub')->join('palmpay_company pc','pc.id=psub.palmpay_company_id')->where('psub.sub_account_type',PayConstents::PALMPAY_ACCOUNT_DZ)->field('psub.id,psub.nickname,pc.name')->select();
- }
- public function getQuartetIsSplitList()
- {
- return ['1' => __('Quartet_is_split 1'),'2' => __('Quartet_is_split 2')];
- }
- }
|