UserMp.php 7.6 KB

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