UserService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/12/20 16:55
  5. */
  6. namespace app\common\service;
  7. use app\api\exception\ApiException;
  8. use app\api\model\UserModel;
  9. use app\api\model\WxUserInfoModel;
  10. use GuzzleHttp\Client;
  11. class UserService {
  12. const BASE_URL = "https://csapi.ahamu.cn/";
  13. // const APPCODE = "MSJ";
  14. const APPCODE = "HESHEN";
  15. const KEY = '533E269CD14DBF46257C09612CE51EC0';
  16. public function decode($wxCode) {
  17. $client = new Client();
  18. $time = time();
  19. $code = self::APPCODE;
  20. $key = $this->getSignKey($time);
  21. $res = $client->request('Post',
  22. self::BASE_URL . "V1/getOpenInfo?api_code={$code}&request_ts={$time}&signKey={$key}", [
  23. 'json' => [
  24. 'code' => $wxCode
  25. ]
  26. ]);
  27. $info = json_decode((string)$res->getBody(), true);
  28. if ($info['code'] != 200) {
  29. throw new ApiException($info['msg']);
  30. }
  31. return $info['data'];
  32. }
  33. public function getPhoneMobile(string $wxCode,string $iv,string $encryptedData) {
  34. $client = new Client();
  35. $time = time();
  36. $code = self::APPCODE;
  37. $key = $this->getSignKey($time);
  38. $res = $client->request('Post',
  39. self::BASE_URL . "V1/getMobileInfo?api_code={$code}&request_ts={$time}&signKey={$key}", [
  40. 'json' => [
  41. 'code' => $wxCode,
  42. 'iv' => $iv,
  43. 'encryptedData' => $encryptedData
  44. ]
  45. ]);
  46. $info = json_decode((string)$res->getBody(), true);
  47. if ($info['code'] != 200) {
  48. throw new ApiException($info['msg']);
  49. }
  50. return $info['data']['phoneNumber'];
  51. }
  52. public function getSignKey($time) {
  53. return sha1(md5(base64_encode(self::APPCODE . $time)) . self::KEY);
  54. }
  55. /**
  56. * 添加微信用户信息
  57. * @param $wxUsrInfo = [
  58. * 'nickName' => 'Geek',
  59. * 'gender' => 1,
  60. * 'language' => 'zh_CN',
  61. * 'city' => 'Bengbu',
  62. * 'province' => 'Anhui',
  63. * 'country' => 'China',
  64. * 'avatarUrl' => 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTI6J3aB8SFk2qt7xCUGsym18d1ib3F'
  65. * ]
  66. * @return array
  67. */
  68. public function saveUserInfo(array $wxUsrInfo, string $openId): array {
  69. $userModel = new UserModel();
  70. $wxUserInfoModel = new WxUserInfoModel();
  71. $info = $userModel::where(['open_id' => $openId])->find();
  72. $saveData = [];
  73. $now = date('Y-m-d H:i:s');
  74. !$wxUsrInfo['nickName'] ?: $saveData['nick_name'] = $wxUsrInfo['nickName'];
  75. !$wxUsrInfo['gender'] ?: $saveData['gender'] = $wxUsrInfo['gender'];
  76. !$wxUsrInfo['city'] ?: $saveData['city'] = $wxUsrInfo['city'];
  77. !$wxUsrInfo['province'] ?: $saveData['province'] = $wxUsrInfo['province'];
  78. !$wxUsrInfo['country'] ?: $saveData['country'] = $wxUsrInfo['country'];
  79. !$wxUsrInfo['avatarUrl'] ?: $saveData['avatar_url'] = $wxUsrInfo['avatarUrl'];
  80. $saveData['update_time'] = $now;
  81. if (empty($info)) {
  82. $userId = $userModel->insertGetId([
  83. 'name' => $wxUsrInfo['nickName'] ?: '',
  84. 'real_name' => $wxUsrInfo['nickName'] ?: '',
  85. 'avatar' => $wxUsrInfo['avatarUrl'] ?: '',
  86. 'open_id' => $openId,
  87. 'join_type' => 1,
  88. 'sex' => $wxUsrInfo['gender'] ?? 0,
  89. 'xcx_join_time' => $now,
  90. 'create_time' => $now
  91. ]);
  92. $saveData['open_id'] = $openId;
  93. $saveData['create_time'] = $now;
  94. $wxUserInfoModel->insertGetId($saveData);
  95. return $userModel->getUserInfo($userId);
  96. }
  97. $wxUserInfoModel::where(['open_id' => $openId])->update($saveData);
  98. $userModel::where(['open_id' => $openId])->update(['xcx_join_time' => $now]);
  99. return $userModel->getUserInfo($info['id']);
  100. }
  101. }