| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- /**
- * @package merchant
- *
- * @author xaboy
- * @day 2020/6/18
- *
- *
- */
- namespace app\controller\api\user;
- use app\common\dao\user\UnionUsersDao;
- use app\common\repositories\article\AgreementRepository;
- use app\common\repositories\merchant\order\OrderMerchantRepository;
- use app\common\repositories\user\UnionUsersRepository;
- use crmeb\basic\BaseController;
- use app\common\repositories\user\UserRepository;
- use crmeb\services\MiniProgramService;
- use think\App;
- use think\Exception;
- use think\facade\Cache;
- use think\facade\Db;
- use think\facade\Log;
- class UserMp extends BaseController
- {
- public $repository;
- private $appid;
- private $secret;
- public function __construct(App $app, UserRepository $repository)
- {
- parent::__construct($app);
- $this->repository = $repository;
- //点餐小程序
- // $this->appid = 'wx10669a2eca032216';
- // $this->secret = '0e71d5c636fdb9bf1c79c403db70f739';
- // $this->appid = 'wx7b4d857deb0447f3';
- // $this->secret = '643efbd7f3cd6398df1e7adb441afceb';
- $this->appid = 'wx9082e5ac2fdb513f';
- $this->secret = 'e334c41e1c4aa962f65d1882092fbda6';
- }
- public function getUserUnionid(){
- $data = $this->request->params(['code','source']);
- if(!$data['code'] || $data['code'] == '' || $data['code'] =='undefined') {
- return app('json')->fail("异常操作");
- }
- // $appid = 'wx7b4d857deb0447f3';
- // $secret = '643efbd7f3cd6398df1e7adb441afceb';
- $appid = 'wx9082e5ac2fdb513f';
- $secret = 'e334c41e1c4aa962f65d1882092fbda6';
- $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$data['code']."&grant_type=authorization_code";
- $get = curlGet($url);
- if(!$get){
- return app('json')->fail($url);
- }
- $get['cache_key'] = md5(time() . $data['code']);
- try {
- Cache::set('mp_code_' . $get['cache_key'], $get['session_key'], 86400);
- }catch (\Exception $exception) {
- Log::error($exception->getMessage());
- }
- return app('json')->success($get);
- }
- public function getPhoneToBind(){
- $data = $this->request->params([
- 'cache_key',
- 'iv',
- 'encryptedData',
- 'third_user_id',
- 'source'
- ]);
- $session_key = Cache::get('mp_code_' . $data['cache_key']);
- $appid = $this -> appid;
- $secret = $this -> secret;
- if($data['source'] && $data['source'] == 'cxb'){
- $appid = env('WECHAT_MP.APPID');
- $secret = env('WECHAT_MP.SECRET');
- }
- //解密获取用户信息urldecode
- $returnCode = $this -> decryptData($session_key,$data['encryptedData'],$data['iv'],$userInfo,$appid);
- if($returnCode != 0){
- return app('json')->fail($returnCode);
- }
- $userInfo = json_decode($userInfo,true);
- if (!$userInfo || !isset($userInfo['purePhoneNumber'])) return app('json')->fail('手机号码获取失败');
- $phone = $userInfo['purePhoneNumber'];
- $repository = app()->make(UserRepository::class);
- $user = $repository->thirdBinding($phone, false,'MP',$data['third_user_id'],1,"http://". $this->request->host(true));//2222
- if($user){
- $userRepository = app()->make(UserRepository::class);
- $userInfo1 = $userRepository->getOne(['uid' => $user['uid']]);
- //绑定线下充值金额
- $unionUsersRepository = app()->make(UnionUsersRepository::class);
- $orderMerchantRepository = app()->make(OrderMerchantRepository::class);
- $notBind = $unionUsersRepository->getAllNotBind($data['third_user_id']);
- if ($notBind) {
- $orderMerchantRepository->doBindUser($userInfo1, 1);
- $userAccount = Db::name("UserAccount");
- $userAccount -> insert(['type'=>1,'uid'=>$user['uid'],'nickname'=>$notBind['nickname'],'avater'=>$notBind['avatar']]);
- }
- $user = $repository->accountByUser($userInfo['purePhoneNumber']);
- $tokenInfo = $repository->createToken($user);
- $repository->loginAfter($user);
- return app('json')->success($repository->returnToken($user, $tokenInfo));
- }else{
- return app('json')->fail("绑定失败");
- }
- }
- public function getUser(){
- $data = $this->request->params(['unionid','nickname','avatar']);
- if(!$data['unionid'] || $data['unionid'] == '' || $data['unionid'] =='undefined'){
- return app('json')->fail("异常操作");
- }
- $repository = app()->make(UserRepository::class);
- $result = $repository ->wechatCheck($data['unionid']);
- if(isset($result['code']) && $result['code'] == 200){
- //用户已注册并绑定了手机号码
- $user = $repository->accountByUser($result['user']['account']);
- $tokenInfo = $repository->createToken($user);
- $repository->loginAfter($user);
- return app('json')->success($repository->returnToken($user, $tokenInfo));
- }
- else{
- try {
- if($data['avatar'] && $data['avatar'] != ''){
- $data['avatar'] = str_replace('"', '', $data['avatar']);
- }
- Db::transaction(function () use ($data,$repository) {
- $unionData = [
- 'unionid' => $data['unionid'],
- 'nickname' => $data['nickname'] ? : time(),
- 'avatar' => $data['avatar'] ? : 'https://cdn.bjtdba.com/vod/image/2025-01-21/20250121180855764.png',
- 'is_lock' => 1,
- 'lock_time' => date('Y-m-d H:i:s'),
- 'balance' => 0,
- 'give_money' => 0,
- 'pension' => 0,
- 'type' => 1,
- ];
- $unionUsersDao = app()->make(UnionUsersDao::class);
- $unionUsersRepository = app()->make(UnionUsersRepository::class);
- $unionUser = $unionUsersRepository->getWhere(['unionid' => $data['unionid']]);
- if (!$unionUser) {
- $unionUsersDao->create($unionData);
- } else {
- $updateData = [
- 'nickname' => $data['nickname'] ? : time(),
- 'avatar' => $data['avatar'] ? : 'https://cdn.bjtdba.com/vod/image/2025-01-21/20250121180855764.png',
- 'update_time' => date('Y-m-d H:i:s')
- ];
- $unionUsersDao->update($unionUser->id, $updateData);
- }
- });
- } catch (\Throwable $e) {
- Log::info($e->getMessage());
- }
- }
- return app("json")->fail($result['msg']);
- }
- /**
- * 检验数据的真实性,并且获取解密后的明文.
- * @param $sessionKey string sessionKey
- * @param $encryptedData string 加密的用户数据
- * @param $iv string 与用户数据一同返回的初始向量
- * @param $data string 解密后的原文
- *
- * @return int 成功0,失败返回对应的错误码
- */
- public function decryptData($sessionKey,$encryptedData, $iv, &$data, $appid)
- {
- if (strlen($sessionKey) != 24) {
- return -41001;
- }
- $aesKey=base64_decode($sessionKey);
- if (strlen($iv) != 24) {
- return -41002;
- }
- $aesIV=base64_decode($iv);
- $aesCipher=base64_decode($encryptedData);
- $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
- $dataObj=json_decode( $result );
- if( $dataObj == NULL )
- {
- return -41003;
- }
- if( $dataObj->watermark->appid != $appid )
- {
- return -41003;
- }
- $data = $result;
- return 0;
- }
- }
|