123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- /**
- * Author: luzheng.liu
- * Time: 2020/12/20 16:55
- */
- namespace app\common\service;
- use app\api\exception\ApiException;
- use app\api\model\UserModel;
- use app\api\model\WxUserInfoModel;
- use GuzzleHttp\Client;
- class UserService {
- const BASE_URL = "https://csapi.ahamu.cn/";
- // const APPCODE = "MSJ";
- const APPCODE = "HESHEN";
- const KEY = '533E269CD14DBF46257C09612CE51EC0';
- public function decode($wxCode) {
- $client = new Client();
- $time = time();
- $code = self::APPCODE;
- $key = $this->getSignKey($time);
- $res = $client->request('Post',
- self::BASE_URL . "V1/getOpenInfo?api_code={$code}&request_ts={$time}&signKey={$key}", [
- 'json' => [
- 'code' => $wxCode
- ]
- ]);
- $info = json_decode((string)$res->getBody(), true);
- if ($info['code'] != 200) {
- throw new ApiException($info['msg']);
- }
- return $info['data'];
- }
- public function getPhoneMobile(string $wxCode,string $iv,string $encryptedData) {
- $client = new Client();
- $time = time();
- $code = self::APPCODE;
- $key = $this->getSignKey($time);
- $res = $client->request('Post',
- self::BASE_URL . "V1/getMobileInfo?api_code={$code}&request_ts={$time}&signKey={$key}", [
- 'json' => [
- 'code' => $wxCode,
- 'iv' => $iv,
- 'encryptedData' => $encryptedData
- ]
- ]);
- $info = json_decode((string)$res->getBody(), true);
- if ($info['code'] != 200) {
- throw new ApiException($info['msg']);
- }
- return $info['data']['phoneNumber'];
- }
- public function getSignKey($time) {
- return sha1(md5(base64_encode(self::APPCODE . $time)) . self::KEY);
- }
- /**
- * 添加微信用户信息
- * @param $wxUsrInfo = [
- * 'nickName' => 'Geek',
- * 'gender' => 1,
- * 'language' => 'zh_CN',
- * 'city' => 'Bengbu',
- * 'province' => 'Anhui',
- * 'country' => 'China',
- * 'avatarUrl' => 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTI6J3aB8SFk2qt7xCUGsym18d1ib3F'
- * ]
- * @return array
- */
- public function saveUserInfo(array $wxUsrInfo, string $openId): array {
- $userModel = new UserModel();
- $wxUserInfoModel = new WxUserInfoModel();
- $info = $userModel::where(['open_id' => $openId])->find();
- $saveData = [];
- $now = date('Y-m-d H:i:s');
- !$wxUsrInfo['nickName'] ?: $saveData['nick_name'] = $wxUsrInfo['nickName'];
- !$wxUsrInfo['gender'] ?: $saveData['gender'] = $wxUsrInfo['gender'];
- !$wxUsrInfo['city'] ?: $saveData['city'] = $wxUsrInfo['city'];
- !$wxUsrInfo['province'] ?: $saveData['province'] = $wxUsrInfo['province'];
- !$wxUsrInfo['country'] ?: $saveData['country'] = $wxUsrInfo['country'];
- !$wxUsrInfo['avatarUrl'] ?: $saveData['avatar_url'] = $wxUsrInfo['avatarUrl'];
- $saveData['update_time'] = $now;
- if (empty($info)) {
- $userId = $userModel->insertGetId([
- 'name' => $wxUsrInfo['nickName'] ?: '',
- 'real_name' => $wxUsrInfo['nickName'] ?: '',
- 'avatar' => $wxUsrInfo['avatarUrl'] ?: '',
- 'open_id' => $openId,
- 'join_type' => 1,
- 'sex' => $wxUsrInfo['gender'] ?? 0,
- 'xcx_join_time' => $now,
- 'create_time' => $now
- ]);
- $saveData['open_id'] = $openId;
- $saveData['create_time'] = $now;
- $wxUserInfoModel->insertGetId($saveData);
- return $userModel->getUserInfo($userId);
- }
- $wxUserInfoModel::where(['open_id' => $openId])->update($saveData);
- $userModel::where(['open_id' => $openId])->update(['xcx_join_time' => $now]);
- return $userModel->getUserInfo($info['id']);
- }
- }
|