UserMp.php 7.8 KB

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