xhWxOpenService.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\stringUtil;
  5. use common\components\configDict;
  6. use common\models\xhWxOpen;
  7. class xhWxOpenService {
  8. public static function getById($id=0)
  9. {
  10. $defaultId = configDict::getConfig('openId');
  11. $id = empty($id) ? $defaultId : $id;
  12. $preKey = configDict::getCacheKey('wxOpen');
  13. $cacheKey = $preKey.$id;
  14. $cacheData = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  15. $data = [];
  16. $list = [];
  17. if(!empty($cacheData)){
  18. if(!empty($cacheData)){
  19. $x = 0;
  20. foreach($cacheData as $cKey => $cVal){
  21. if($cKey%2 == 0){
  22. $list[$x]['key'] = $cVal;
  23. }else{
  24. $list[$x]['value'] = $cVal;
  25. $x++;
  26. }
  27. }
  28. foreach($list as $lKey => $lVal){
  29. $data[$lVal['key']] = $lVal['value'];
  30. }
  31. }
  32. }else{
  33. $condition = ['id' => $id];
  34. $merchant = xhWxOpen::getByCondition($condition);
  35. if(!empty($merchant)){
  36. $arr = [$cacheKey];
  37. foreach($merchant as $key => $val){
  38. $arr[] = $key;
  39. $arr[] = $val;
  40. }
  41. Yii::$app->redis->executeCommand('HMSET', $arr);
  42. }
  43. $data = $merchant;
  44. }
  45. return $data;
  46. }
  47. public static function updateById($id, $data)
  48. {
  49. xhWxOpen::updateById($id, $data);
  50. self::refresh($id);
  51. }
  52. public static function refresh($id)
  53. {
  54. $preKey = configDict::getCacheKey('wxOpen');
  55. $cacheKey = $preKey.$id;
  56. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  57. self::getById($id);
  58. }
  59. public static function add($data)
  60. {
  61. $data = xhWxOpen::add($data);
  62. $id = $data['id'];
  63. self::refresh($id);
  64. return $data;
  65. }
  66. /**
  67. * 取平台相应的商家
  68. */
  69. public static function getMerchant()
  70. {
  71. $wxOpenId = configDict::getConfig('openId');
  72. $wxOpen = xhWxOpenService::getById($wxOpenId);
  73. $merchantId = $wxOpen['merchantId'];
  74. $merchant = xhMerchantService::getById($merchantId);
  75. return $merchant;
  76. }
  77. }