Wxpay.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use app\main\constants\PayConstents;
  5. use think\Cache;
  6. use think\Log;
  7. use think\Model;
  8. use app\main\constants\ApiConstants;
  9. class Wxpay extends Model
  10. {
  11. /**
  12. * 微信支付域名host列表
  13. */
  14. const CACHE_KEY_PAYHOST = 'PAYHOST';
  15. /**
  16. * 平台默认支付域名 + platform_id
  17. */
  18. const CACHE_KEY_DEFAULT = 'WXP:D:';
  19. /**
  20. * 微信支付域名信息缓存 + id
  21. */
  22. const CACHE_KEY_ID = 'WXP:';
  23. /**
  24. * 微信支付域名信息缓存 + host
  25. */
  26. const CACHE_KEY_HOST = 'WXP:H:';
  27. // 表名
  28. protected $table = 'wxpay';
  29. // 自动写入时间戳字段
  30. protected $autoWriteTimestamp = 'int';
  31. // 定义时间戳字段名
  32. protected $createTime = 'createtime';
  33. protected $updateTime = 'updatetime';
  34. // 追加属性
  35. protected $append = [
  36. 'status_text',
  37. 'fufen_text',
  38. 'isdefault_text',
  39. 'is_closure_text',
  40. ];
  41. /**
  42. * 删除全部缓存
  43. * @param $id
  44. * @param null $host
  45. * @param null $platform_id
  46. */
  47. public function delCacheAll($id,$host = null,$platform_id = null){
  48. $redis = Redis::instance();
  49. //删除ID
  50. $redis->del(self::CACHE_KEY_ID.$id);
  51. //Host没有时取ID的
  52. if(empty($host)){
  53. $host = $this->where('id',$id)->value('pay_host');
  54. }
  55. //平台Id没有事取ID的
  56. if(empty($platform_id)){
  57. $platform_id = $this->where('id',$id)->value('platform_id');
  58. }
  59. //删除Host
  60. $redis->del(self::CACHE_KEY_HOST.$host);
  61. //删除平台ID
  62. $redis->del(self::CACHE_KEY_DEFAULT.$platform_id);
  63. //删除支付列表
  64. $redis->del(self::CACHE_KEY_PAYHOST);
  65. }
  66. /**
  67. * 删除Wxpay平台缓存
  68. * @param null $platform_id
  69. */
  70. public function delCacheByPlatformId($platform_id){
  71. $redis = Redis::instance();
  72. //删除平台ID
  73. $redis->del(self::CACHE_KEY_DEFAULT.$platform_id);
  74. }
  75. /**
  76. * 删除Wxpay平台缓存
  77. */
  78. public function delCachePayHost(){
  79. $redis = Redis::instance();
  80. //删除平台ID
  81. $redis->del(self::CACHE_KEY_PAYHOST);
  82. }
  83. /**
  84. * 删除Wxpay平台缓存
  85. * @param null $host
  86. */
  87. public function delCacheByHost($host){
  88. $redis = Redis::instance();
  89. //删除平台ID
  90. $redis->del(self::CACHE_KEY_HOST.$host);
  91. }
  92. /**
  93. * 获取支付信息
  94. * @param $platform_id
  95. * @param null $id 表id
  96. * @return array|null
  97. */
  98. public function getInfo($platform_id,$id=null){
  99. if(empty($platform_id)) {
  100. return null;
  101. }
  102. $redis = Redis::instance();
  103. if(empty($id)){ //获取当前平台下默认的支付信息
  104. $modulename = request()->module();
  105. if($modulename == 'admin' && PHP_SAPI != 'cli') { //如果是后台
  106. $arr = $this->where(['platform_id'=>$platform_id,'isdefault'=>'1','status'=>1])->find();
  107. if($arr){
  108. $arr = $arr->toArray();
  109. }
  110. }else{
  111. $key = self::CACHE_KEY_DEFAULT.$platform_id;
  112. if($redis->exists($key)){
  113. $arr = $redis->hgetall($key);
  114. }else{
  115. $arr = $this->where(['platform_id'=>$platform_id,'isdefault'=>'1','status'=>1])->find();
  116. if($arr){
  117. $arr = $arr->toArray();
  118. $redis->hmset($key,$arr);
  119. $redis->expire($key,86400);
  120. }
  121. }
  122. }
  123. }else{ //获取指定的支付信息
  124. $modulename = request()->module();
  125. if($modulename == 'admin' && PHP_SAPI != 'cli' ) { //如果是后台
  126. $arr = $this->where('id',$id)->find();
  127. if($arr){
  128. $arr = $arr->toArray();
  129. }
  130. }else{
  131. $arr = $this->getInfoById($id);
  132. }
  133. }
  134. return $arr;
  135. }
  136. /**
  137. * 通过支付域名去相关支付信息
  138. * @param $domain
  139. * @return array|null
  140. * @throws \think\Exception
  141. */
  142. public function getInfoByDomain($domain){
  143. $info = $this->where('pay_host',$domain)->find();
  144. if(!empty($info)){
  145. return $info->toArray();
  146. }else{
  147. return null;
  148. }
  149. }
  150. /**
  151. * 根据host获取支付域名信息
  152. * @param $host
  153. * @return array|false|\PDOStatement|string|Model
  154. */
  155. public function getInfoByHost($host){
  156. $redis = Redis::instance();
  157. $key = self::CACHE_KEY_HOST.$host;
  158. if($redis->exists($key)){
  159. $payhost = $redis->hgetall($key);
  160. }else{
  161. if($payhost = $this->where('pay_host',$host)->find()){
  162. $payhost = $payhost->toArray();
  163. $redis->hmset($key,$payhost);
  164. $redis->expire($key,3600);
  165. }
  166. }
  167. return $payhost;
  168. }
  169. public function getInfoById($id){
  170. $redis = Redis::instance();
  171. $key = self::CACHE_KEY_ID.$id;
  172. $wxpay = null;
  173. if (Cache::has($key)) {
  174. Log::info("获取缓存:$key filecache命中");
  175. $wxpay = Cache::get($key);
  176. }
  177. if (!$wxpay) {
  178. if ($redis->exists($key)) {
  179. $wxpay = $redis->hgetall($key);
  180. Log::info("获取缓存:$key redis命中");
  181. Cache::set($key, $wxpay, 10);
  182. }
  183. }
  184. if (!$wxpay) {
  185. $wxpay = $this->where('id', $id)->find();
  186. if ($wxpay) {
  187. Log::info("获取缓存:$key mysql命中");
  188. $wxpay = $wxpay->toArray();
  189. $redis->hmset($key, $wxpay);
  190. $redis->expire($key, 3600);
  191. }
  192. }
  193. return $wxpay;
  194. }
  195. public function getListByBusiness($business){
  196. $redis = Redis::instance();
  197. $key = 'WPLS:'.intval($business);
  198. if($redis->exists($key)){
  199. $result = json_decode($redis->get($key), true);
  200. }else{
  201. $result = $this->where("find_in_set($business,business_line)")->where('status','1')->select();
  202. if($result){
  203. $result = collection($result)->toArray();
  204. }
  205. $redis->setex($key, 60, json_encode($result, JSON_UNESCAPED_UNICODE));
  206. }
  207. return $result;
  208. }
  209. //获取所有可用的支付域名 全部支付域名
  210. public function getHosts(){
  211. $redis = Redis::instance();
  212. if ($ret = $redis->get(self::CACHE_KEY_PAYHOST)) {
  213. $res = json_decode($ret, true);
  214. }else{
  215. $res = collection($this->column('pay_host'))->toArray();
  216. $redis->setex(self::CACHE_KEY_PAYHOST,86400,json_encode($res));
  217. }
  218. return $res;
  219. }
  220. public function platform()
  221. {
  222. return $this->belongsTo('Platform', 'platform_id')->setEagerlyType(0);
  223. }
  224. /**
  225. * 获取平台支付列表
  226. * @param $business 业务线
  227. * @param $platform_id
  228. * @param null $ophost_id 根据OpHost域名过滤
  229. * @return array
  230. * @throws \think\db\exception\DataNotFoundException
  231. * @throws \think\db\exception\ModelNotFoundException
  232. * @throws \think\exception\DbException
  233. */
  234. public function getListFormatByPlatformId($business = null,$platform_id = null,$ophost_id = null){
  235. $result = [];
  236. $map['status'] = '1';
  237. if($platform_id){
  238. $map['platform_id'] = ['in',$platform_id];
  239. }
  240. if($business !== null){
  241. $map[] = ['exp',"FIND_IN_SET({$business},business_line)"];
  242. }
  243. if($ophost_id){
  244. $host = model('Ophost')->where('id',$ophost_id)->value('host');
  245. $map['pay_host'] = ['like','%'.$host];
  246. }
  247. if($list = $this->where($map)->select()){
  248. foreach($list as $val){
  249. $platform_name = model('Platform')->where('id',$val['platform_id'])->value('name');
  250. array_push($result,['fufen'=>$val['fufen'],'id'=>$val['id'],'platform_id'=>$val['platform_id'],'text'=>$platform_name.'--'.$val['name'].'--'.$val['p_desc']]);
  251. }
  252. }
  253. return $result;
  254. }
  255. public function getStatusList()
  256. {
  257. return ['0' => __('Status 0'),'1' => __('Status 1')];
  258. }
  259. public function getFufenList()
  260. {
  261. return ['0' => __('Fufen 0'),'1' => __('Fufen 1')];
  262. }
  263. public function getIsdefaultList()
  264. {
  265. return ['0' => __('Isdefault 0'),'1' => __('Isdefault 1')];
  266. }
  267. public function getPaymentMethodList()
  268. {
  269. return ApiConstants::$allPaymentMethods;
  270. }
  271. public function getIsclosureList()
  272. {
  273. return ['0' => __('IsClosure 0'),'1' => __('IsClosure 1')];
  274. }
  275. public function getStatusTextAttr($value, $data)
  276. {
  277. $value = $value ? $value : $data['status'];
  278. $list = $this->getStatusList();
  279. return isset($list[$value]) ? $list[$value] : '';
  280. }
  281. public function getFufenTextAttr($value, $data)
  282. {
  283. $value = $value ? $value : $data['fufen'];
  284. $list = $this->getFufenList();
  285. return isset($list[$value]) ? $list[$value] : '';
  286. }
  287. public function getIsdefaultTextAttr($value, $data)
  288. {
  289. $value = $value ? $value : $data['isdefault'];
  290. $list = $this->getIsdefaultList();
  291. return isset($list[$value]) ? $list[$value] : '';
  292. }
  293. public function getIsClosureTextAttr($value, $data)
  294. {
  295. $value = $value ? $value : $data['is_closure'];
  296. $list = $this->getIsclosureList();
  297. return isset($list[$value]) ? $list[$value] : '';
  298. }
  299. public function getQuartetIsSplit()
  300. {
  301. return ApiConstants::$quartetIsSplit;
  302. }
  303. //获取点众分账子账户
  304. public function getDzPamlpaySubaccountList()
  305. {
  306. 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();
  307. }
  308. public function getQuartetIsSplitList()
  309. {
  310. return ['1' => __('Quartet_is_split 1'),'2' => __('Quartet_is_split 2')];
  311. }
  312. }