Wechat.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\WechatPlan;
  4. use app\common\model\UserRelation;
  5. use app\common\controller\Api;
  6. use app\common\library\Auth;
  7. use liuniu\WechatService;
  8. use app\common\model\User;
  9. use Overtrue\Socialite\AuthorizeFailedException;
  10. use think\Exception;
  11. use think\Hook;
  12. use think\Request;
  13. class Wechat extends Api
  14. {
  15. protected $noNeedLogin = ['*'];
  16. /**
  17. * 微信公众号服务
  18. * @return \think\Response
  19. */
  20. public function serve()
  21. {
  22. ob_clean();
  23. return WechatService::serve();
  24. }
  25. /**
  26. * 支付异步回调
  27. */
  28. public function notify()
  29. {
  30. ob_clean();
  31. WechatService::handleNotify(input('cid', 0));
  32. }
  33. /**
  34. * 公众号权限配置信息获取
  35. * @param Request $request
  36. * @return mixed
  37. */
  38. public function config(Request $request)
  39. {
  40. return app('json')->success(json_decode(WechatService::jsSdk($request->get('url')), true));
  41. }
  42. /**
  43. * 公众号授权登陆
  44. * @param Request $request
  45. * @return mixed
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. * @throws \think\exception\DbException
  49. */
  50. public function auth(Request $request)
  51. {
  52. $spreadId = intval($request->param('spread'));
  53. $login_type = $request->param('login_type', 1);
  54. @file_put_contents("auth.txt", json_encode(input()));
  55. try {
  56. $wechatInfo = WechatService::oauthService($this->cid)->user()->getOriginal();
  57. } catch (\Exception $e) {
  58. $this->error(['message' => $e->getMessage(), 'line' => $e->getLine()]);
  59. }
  60. @file_put_contents("auth.txt", "\r\n" . json_encode($wechatInfo), 8);
  61. try {
  62. if (!isset($wechatInfo['nickname'])) {
  63. try {
  64. $wechatInfo = WechatService::getUserInfo($this->cid, $wechatInfo['openid']);
  65. } catch (\Exception $e) {
  66. $this->error(['message' => $e->getMessage(), 'line' => $e->getLine()]);
  67. }
  68. if (!$wechatInfo['subscribe'] && !isset($wechatInfo['nickname']))
  69. exit(WechatService::oauthService($this->cid)->scopes(['snsapi_userinfo'])
  70. ->redirect($this->request->url(true))->send());
  71. if (isset($wechatInfo['tagid_list']))
  72. $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
  73. } else {
  74. if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  75. if (!UserRelation::where(['openid' => $wechatInfo['openid']])->find())
  76. $wechatInfo['subscribe'] = 0;
  77. }
  78. $openid = $wechatInfo['openid'];
  79. $wechatInfo['cid'] = $this->cid;
  80. $params = [$openid, $wechatInfo, $spreadId, $login_type];
  81. @file_put_contents("auth.txt", "\r\n" . json_encode($params), 8);
  82. Hook::exec("\\app\admin\\behavior\\User", "WechatOauth", $params);
  83. $user = User::where('id', UserRelation::openidToUid($openid, 'openid'))->find();
  84. if (!$user)
  85. $this->error('获取用户失败');
  86. $this->auth->direct($user['id']);
  87. // 设置推广关系
  88. User::setSpread(intval($spreadId), $user->id);
  89. return $this->success('登录成功', $this->auth->getUserinfo());
  90. } catch (Exception $e) {
  91. @file_put_contents("error.txt", $e->getFile() . '-', $e->getLine(), '-' . $e->getMessage());
  92. }
  93. }
  94. // /**
  95. // * 扣款服务
  96. // * @param Request $request
  97. // * @return mixed
  98. // * @throws \think\db\exception\DataNotFoundException
  99. // * @throws \think\db\exception\ModelNotFoundException
  100. // * @throws \think\exception\DbException
  101. // */
  102. // public function signing(Request $request)
  103. // {
  104. // $peice = intval($request->param('peice'));
  105. // $uid = intval($request->param('uid'));
  106. // $plan_id = WechatPlan::where('price',$peice)->value('plan_id');
  107. // $login_type = $request->param('login_type', 1);
  108. // @file_put_contents("auth.txt", json_encode(input()));
  109. // try {
  110. //
  111. //// $notify_url=Request::instance()->domain() . "/api/wechat/notify/" . $cid; //回调接口
  112. // $app_id='wx5681205d1ef4d9d3';
  113. // $mch_id ='';
  114. // $sub_mch_id ='';
  115. // $contract_code =$this->generateRandomString(6);
  116. // $notify_url=Request::instance()->domain() . "/api/wechat/notify/" . 12; //回调接口 $cid 企业id
  117. //// $num = time() + mt_rand(10, 999999) . '' . substr($msec, 2, 3);//生成随机数
  118. // $request_serial=$this->generateUniqueSerialNumber();
  119. // $contract_display_account='咸宁红十字';
  120. // $timestamp=time();
  121. // $version=1.0;
  122. // $array = array(
  123. // 'appid' => $app_id,
  124. // 'mch_id' => $mch_id,
  125. // 'plan_id' => $plan_id,
  126. // 'sub_mch_id' => $sub_mch_id,
  127. // 'contract_code' => $contract_code,
  128. // 'notify_url' => $notify_url,
  129. // 'contract_display_account' => $contract_display_account,
  130. // 'request_serial' => $request_serial,
  131. // 'timestamp' => $timestamp,
  132. // 'version' => $version,
  133. //
  134. // );
  135. // $arr=ksort($array);
  136. // $sign=$this->md5_sign($arr,'');
  137. // $url=$this->OfficialAccountSigning($app_id,$mch_id,$plan_id,$sub_mch_id,$contract_code,$request_serial,$contract_display_account,$notify_url,$version,$sign,$timestamp);
  138. // return $url;
  139. // } catch (Exception $e) {
  140. // @file_put_contents("error.txt", $e->getFile() . '-', $e->getLine(), '-' . $e->getMessage());
  141. // }
  142. // }
  143. //
  144. // public function generateRandomString($length) {
  145. // $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  146. // $charactersLength = strlen($characters);
  147. // $randomString = '';
  148. // for ($i = 0; $i < $length; $i++) {
  149. // $randomString .= $characters[rand(0, $charactersLength - 1)];
  150. // }
  151. // return $randomString;
  152. // }
  153. //// 生成唯一序列号
  154. // public function generateUniqueSerialNumber() {
  155. // // 使用uniqid生成一个唯一ID,并去除前缀的'0'
  156. // $uniqueId = ltrim(uniqid('', true), '0');
  157. //
  158. // // 确保生成的ID不以0开头且长度不超过19位(int64的最大长度)
  159. // while (strlen($uniqueId) > 19 || substr($uniqueId, 0, 1) === '0') {
  160. // $uniqueId = ltrim(uniqid('', true), '0');
  161. // }
  162. //
  163. // // 将生成的ID转换为纯数字
  164. // $serialNumber = preg_replace('/\D/', '', $uniqueId);
  165. //
  166. // // 确保生成的序列号不以0开头
  167. // if (substr($serialNumber, 0, 1) === '0') {
  168. // $serialNumber = generateUniqueSerialNumber(); // 递归调用以重新生成
  169. // }
  170. //
  171. // return $serialNumber;
  172. // }
  173. //
  174. //
  175. // public function curl_post($url = '', $name = array(), $timeout = 100)
  176. // {
  177. // // var_dump($url);die();
  178. // $ch = curl_init();
  179. // curl_setopt($ch, CURLOPT_URL, $url);
  180. // curl_setopt($ch, CURLOPT_HEADER, false); //是否显示头部
  181. // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//是否直接输出到屏幕
  182. // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  183. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  184. // curl_setopt($ch, CURLOPT_POST, true); //是否以post方式
  185. // //设置post数据
  186. // $post_data = json_encode($name);
  187. // // curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
  188. // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  189. // $content = curl_exec($ch);
  190. // // var_dump($content);die();
  191. // curl_close($ch);
  192. // $content = json_decode($content, true);
  193. // return $content;
  194. //
  195. // }
  196. // public function md5_sign($data, $key){
  197. // $stringSignTemp=$data."&key=$key"; //注:key为商户平台设置的密钥key
  198. //
  199. // $sign=MD5($stringSignTemp); //MD5加密
  200. // $sign=strtoupper($sign); //大写
  201. // return $sign;
  202. // }
  203. //
  204. // /**
  205. // * 公众号签约
  206. // * see:https://pay.weixin.qq.com/wiki/doc/api/wxpay_v2/papay/chapter5_1.shtml
  207. // * @param array $data
  208. // *
  209. // * @return array
  210. // * @throws AuthorizeFailedException
  211. // */
  212. // public function OfficialAccountSigning(string $appid,string $mch_id, string $plan_id,string $sub_mch_id, string $contract_code, string $request_serial, string $contract_display_account, string $notify_url, string $version, string $sign,string $timestamp): array
  213. // {
  214. //
  215. // $url = 'https://api.mch.weixin.qq.com/papay/partner/entrustweb';
  216. // $info = [
  217. // 'appid' => $appid, //应用ID
  218. // 'mch_id' => $mch_id, //商户号
  219. // 'plan_id' => $plan_id, //子商户号
  220. // 'sub_mch_id' => $sub_mch_id, //模板id
  221. // 'contract_code' => $contract_code, //签约协议号
  222. // 'request_serial' => $request_serial, //请求序列号
  223. // 'contract_display_account' => $contract_display_account, //用户账户展示名称
  224. // 'notify_url' => $notify_url, //回调通知url
  225. // 'version' => $version, //版本号 固定值1.0
  226. // 'sign' => $sign, //签名
  227. // 'timestamp' => $timestamp
  228. // ];
  229. // $response = $this->curl_post($url, $info);
  230. // return $response;
  231. // }
  232. public function test() {
  233. return '123456';
  234. }
  235. }