UserMp.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020/6/18
  7. *
  8. *
  9. */
  10. namespace app\controller\api\user;
  11. use app\common\dao\user\UnionUsersDao;
  12. use app\common\repositories\article\AgreementRepository;
  13. use app\common\repositories\merchant\order\OrderMerchantRepository;
  14. use app\common\repositories\user\UnionUsersRepository;
  15. use crmeb\basic\BaseController;
  16. use app\common\repositories\user\UserRepository;
  17. use crmeb\services\MiniProgramService;
  18. use think\App;
  19. use think\Exception;
  20. use think\facade\Cache;
  21. use think\facade\Db;
  22. use think\facade\Log;
  23. class UserMp extends BaseController
  24. {
  25. public $repository;
  26. private $appid;
  27. private $secret;
  28. public function __construct(App $app, UserRepository $repository)
  29. {
  30. parent::__construct($app);
  31. $this->repository = $repository;
  32. //点餐小程序
  33. // $this->appid = 'wx10669a2eca032216';
  34. // $this->secret = '0e71d5c636fdb9bf1c79c403db70f739';
  35. // $this->appid = 'wx7b4d857deb0447f3';
  36. // $this->secret = '643efbd7f3cd6398df1e7adb441afceb';
  37. $this->appid = 'wx9082e5ac2fdb513f';
  38. $this->secret = 'e334c41e1c4aa962f65d1882092fbda6';
  39. }
  40. public function getUserUnionid(){
  41. $data = $this->request->params(['code','source']);
  42. if(!$data['code'] || $data['code'] == '' || $data['code'] =='undefined') {
  43. return app('json')->fail("异常操作");
  44. }
  45. // $appid = 'wx7b4d857deb0447f3';
  46. // $secret = '643efbd7f3cd6398df1e7adb441afceb';
  47. $appid = 'wx9082e5ac2fdb513f';
  48. $secret = 'e334c41e1c4aa962f65d1882092fbda6';
  49. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$data['code']."&grant_type=authorization_code";
  50. $get = curlGet($url);
  51. if(!$get){
  52. return app('json')->fail($url);
  53. }
  54. $get['cache_key'] = md5(time() . $data['code']);
  55. try {
  56. Cache::set('mp_code_' . $get['cache_key'], $get['session_key'], 86400);
  57. }catch (\Exception $exception) {
  58. Log::error($exception->getMessage());
  59. }
  60. return app('json')->success($get);
  61. }
  62. public function getPhoneToBind(){
  63. $data = $this->request->params([
  64. 'cache_key',
  65. 'iv',
  66. 'encryptedData',
  67. 'third_user_id',
  68. 'source'
  69. ]);
  70. $session_key = Cache::get('mp_code_' . $data['cache_key']);
  71. $appid = $this -> appid;
  72. $secret = $this -> secret;
  73. if($data['source'] && $data['source'] == 'cxb'){
  74. $appid = env('WECHAT_MP.APPID');
  75. $secret = env('WECHAT_MP.SECRET');
  76. }
  77. //解密获取用户信息urldecode
  78. $returnCode = $this -> decryptData($session_key,$data['encryptedData'],$data['iv'],$userInfo,$appid);
  79. if($returnCode != 0){
  80. return app('json')->fail($returnCode);
  81. }
  82. $userInfo = json_decode($userInfo,true);
  83. if (!$userInfo || !isset($userInfo['purePhoneNumber'])) return app('json')->fail('手机号码获取失败');
  84. $phone = $userInfo['purePhoneNumber'];
  85. $repository = app()->make(UserRepository::class);
  86. $user = $repository->thirdBinding($phone, false,'MP',$data['third_user_id'],1,"http://". $this->request->host(true));//2222
  87. if($user){
  88. $userRepository = app()->make(UserRepository::class);
  89. $userInfo1 = $userRepository->getOne(['uid' => $user['uid']]);
  90. //绑定线下充值金额
  91. $unionUsersRepository = app()->make(UnionUsersRepository::class);
  92. $orderMerchantRepository = app()->make(OrderMerchantRepository::class);
  93. $notBind = $unionUsersRepository->getAllNotBind($data['third_user_id']);
  94. if ($notBind) {
  95. $orderMerchantRepository->doBindUser($userInfo1, 1);
  96. $userAccount = Db::name("UserAccount");
  97. $userAccount -> insert(['type'=>1,'uid'=>$user['uid'],'nickname'=>$notBind['nickname'],'avater'=>$notBind['avatar']]);
  98. }
  99. $user = $repository->accountByUser($userInfo['purePhoneNumber']);
  100. $tokenInfo = $repository->createToken($user);
  101. $repository->loginAfter($user);
  102. return app('json')->success($repository->returnToken($user, $tokenInfo));
  103. }else{
  104. return app('json')->fail("绑定失败");
  105. }
  106. }
  107. public function getUser(){
  108. $data = $this->request->params(['unionid','nickname','avatar']);
  109. if(!$data['unionid'] || $data['unionid'] == '' || $data['unionid'] =='undefined'){
  110. return app('json')->fail("异常操作");
  111. }
  112. $repository = app()->make(UserRepository::class);
  113. $result = $repository ->wechatCheck($data['unionid']);
  114. if(isset($result['code']) && $result['code'] == 200){
  115. //用户已注册并绑定了手机号码
  116. $user = $repository->accountByUser($result['user']['account']);
  117. $tokenInfo = $repository->createToken($user);
  118. $repository->loginAfter($user);
  119. return app('json')->success($repository->returnToken($user, $tokenInfo));
  120. }
  121. else{
  122. try {
  123. if($data['avatar'] && $data['avatar'] != ''){
  124. $data['avatar'] = str_replace('"', '', $data['avatar']);
  125. }
  126. Db::transaction(function () use ($data,$repository) {
  127. $unionData = [
  128. 'unionid' => $data['unionid'],
  129. 'nickname' => $data['nickname'] ? : time(),
  130. 'avatar' => $data['avatar'] ? : 'https://cdn.bjtdba.com/vod/image/2025-01-21/20250121180855764.png',
  131. 'is_lock' => 1,
  132. 'lock_time' => date('Y-m-d H:i:s'),
  133. 'balance' => 0,
  134. 'give_money' => 0,
  135. 'pension' => 0,
  136. 'type' => 1,
  137. ];
  138. $unionUsersDao = app()->make(UnionUsersDao::class);
  139. $unionUsersRepository = app()->make(UnionUsersRepository::class);
  140. $unionUser = $unionUsersRepository->getWhere(['unionid' => $data['unionid']]);
  141. if (!$unionUser) {
  142. $unionUsersDao->create($unionData);
  143. } else {
  144. $updateData = [
  145. 'nickname' => $data['nickname'] ? : time(),
  146. 'avatar' => $data['avatar'] ? : 'https://cdn.bjtdba.com/vod/image/2025-01-21/20250121180855764.png',
  147. 'update_time' => date('Y-m-d H:i:s')
  148. ];
  149. $unionUsersDao->update($unionUser->id, $updateData);
  150. }
  151. });
  152. } catch (\Throwable $e) {
  153. Log::info($e->getMessage());
  154. }
  155. }
  156. return app("json")->fail($result['msg']);
  157. }
  158. /**
  159. * 检验数据的真实性,并且获取解密后的明文.
  160. * @param $sessionKey string sessionKey
  161. * @param $encryptedData string 加密的用户数据
  162. * @param $iv string 与用户数据一同返回的初始向量
  163. * @param $data string 解密后的原文
  164. *
  165. * @return int 成功0,失败返回对应的错误码
  166. */
  167. public function decryptData($sessionKey,$encryptedData, $iv, &$data, $appid)
  168. {
  169. if (strlen($sessionKey) != 24) {
  170. return -41001;
  171. }
  172. $aesKey=base64_decode($sessionKey);
  173. if (strlen($iv) != 24) {
  174. return -41002;
  175. }
  176. $aesIV=base64_decode($iv);
  177. $aesCipher=base64_decode($encryptedData);
  178. $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  179. $dataObj=json_decode( $result );
  180. if( $dataObj == NULL )
  181. {
  182. return -41003;
  183. }
  184. if( $dataObj->watermark->appid != $appid )
  185. {
  186. return -41003;
  187. }
  188. $data = $result;
  189. return 0;
  190. }
  191. }