xhUserAlipayService.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace common\services;
  3. use common\components\util;
  4. use Yii;
  5. use common\components\stringUtil;
  6. use common\components\dict;
  7. use common\components\wxUtil;
  8. use common\models\xhUserAlipay;
  9. class xhUserAlipayService
  10. {
  11. public static function replaceInfo($arr)
  12. {
  13. $sjId = $arr['sjId'];
  14. $alipayId = $arr['alipayId'];
  15. $alipayAccount = $arr['alipayAccount'];
  16. $alipay = xhUserAlipay::find()->where(['sjId' => $sjId, 'alipayId' => $alipayId])->one();
  17. if (empty($alipay)) {
  18. $alipay = self::add(['sjId' => $sjId, 'alipayId' => $alipayId, 'alipayAccount' => $alipayAccount, 'addTime' => time(), 'createTime' => date("Y-m-d H:i:s")]);
  19. }
  20. if (empty($alipay['userId'])) {
  21. $user = xhUserService::generateUserByAlipay($alipayId, $sjId);
  22. $userId = $user['id'];
  23. xhUserAlipay::updateByCondition(['sjId' => $sjId, 'alipayId' => $alipayId], ['userId' => $userId]);
  24. }else{
  25. $user = xhUserService::getById($alipay['userId']);
  26. }
  27. return $user;
  28. }
  29. public static function getByIds($alipayId, $sjId)
  30. {
  31. $user = xhUserAlipay::find()->where(['alipayId' => $alipayId, 'sjId' => $sjId])->asArray()->one();
  32. return $user;
  33. }
  34. public static function add($data)
  35. {
  36. $return = xhUserAlipay::add($data);
  37. $sjId = $return['sjId'];
  38. $alipayId = $return['alipayId'];
  39. $cacheKey = dict::getCacheKey('xhUserAlipay') . '_' . $sjId . '_' . $alipayId;
  40. $params = [$cacheKey];
  41. if (!empty($return)) {
  42. foreach ($return as $key => $val) {
  43. $params[] = $key;
  44. $params[] = $val;
  45. }
  46. }
  47. Yii::$app->redis->executeCommand('HMSET', $params);
  48. return $return;
  49. }
  50. public static function updateByIds($alipayId, $sjId, $data)
  51. {
  52. xhUserAlipay::updateByCondition(['alipayId' => $alipayId, 'sjId' => $sjId], $data);
  53. self::getByIds($alipayId, $sjId);
  54. $cacheKey = dict::getCacheKey('xhUserAlipay') . '_' . $sjId . '_' . $alipayId;
  55. $params = [$cacheKey];
  56. if (!empty($data)) {
  57. foreach ($data as $key => $val) {
  58. $params[] = $key;
  59. $params[] = $val;
  60. }
  61. }
  62. Yii::$app->redis->executeCommand('HMSET', $params);
  63. }
  64. public static function getAlipayAccount($alipayId, $sjId)
  65. {
  66. $user = self::getByIds($alipayId, $sjId);
  67. $account = isset($user['alipayAccount']) ? $user['alipayAccount'] : '';
  68. return $account;
  69. }
  70. }