Wechat.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\Company;
  4. use app\admin\model\WechatPlan;
  5. use app\admin\model\WechatPlanRecord;
  6. use app\common\model\UserRelation;
  7. use app\common\controller\Api;
  8. use app\common\library\Auth;
  9. use liuniu\WechatService;
  10. use app\common\model\User;
  11. use Overtrue\Socialite\AuthorizeFailedException;
  12. use think\Exception;
  13. use think\Hook;
  14. use think\Request;
  15. class Wechat extends Api
  16. {
  17. protected $noNeedLogin = ['*'];
  18. /**
  19. * 微信公众号服务
  20. * @return \think\Response
  21. */
  22. public function serve()
  23. {
  24. ob_clean();
  25. return WechatService::serve();
  26. }
  27. /**
  28. * 支付异步回调
  29. */
  30. public function notify()
  31. {
  32. ob_clean();
  33. WechatService::handleNotify(input('cid', 0));
  34. }
  35. /**
  36. * 公众号权限配置信息获取
  37. * @param Request $request
  38. * @return mixed
  39. */
  40. public function config(Request $request)
  41. {
  42. return app('json')->success(json_decode(WechatService::jsSdk($request->get('url')), true));
  43. }
  44. /**
  45. * 公众号授权登陆
  46. * @param Request $request
  47. * @return mixed
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. public function auth(Request $request)
  53. {
  54. $spreadId = intval($request->param('spread'));
  55. $login_type = $request->param('login_type', 1);
  56. @file_put_contents("auth.txt", json_encode(input()));
  57. try {
  58. $wechatInfo = WechatService::oauthService($this->cid)->user()->getOriginal();
  59. } catch (\Exception $e) {
  60. $this->error(['message' => $e->getMessage(), 'line' => $e->getLine()]);
  61. }
  62. @file_put_contents("auth.txt", "\r\n" . json_encode($wechatInfo), 8);
  63. try {
  64. if (!isset($wechatInfo['nickname'])) {
  65. try {
  66. $wechatInfo = WechatService::getUserInfo($this->cid, $wechatInfo['openid']);
  67. } catch (\Exception $e) {
  68. $this->error(['message' => $e->getMessage(), 'line' => $e->getLine()]);
  69. }
  70. if (!$wechatInfo['subscribe'] && !isset($wechatInfo['nickname']))
  71. exit(WechatService::oauthService($this->cid)->scopes(['snsapi_userinfo'])
  72. ->redirect($this->request->url(true))->send());
  73. if (isset($wechatInfo['tagid_list']))
  74. $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
  75. } else {
  76. if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  77. if (!UserRelation::where(['openid' => $wechatInfo['openid']])->find())
  78. $wechatInfo['subscribe'] = 0;
  79. }
  80. $openid = $wechatInfo['openid'];
  81. $wechatInfo['cid'] = $this->cid;
  82. $params = [$openid, $wechatInfo, $spreadId, $login_type];
  83. @file_put_contents("auth.txt", "\r\n" . json_encode($params), 8);
  84. Hook::exec("\\app\admin\\behavior\\User", "WechatOauth", $params);
  85. $user = User::where('id', UserRelation::openidToUid($openid, 'openid'))->find();
  86. if (!$user)
  87. $this->error('获取用户失败');
  88. $this->auth->direct($user['id']);
  89. // 设置推广关系
  90. User::setSpread(intval($spreadId), $user->id);
  91. return $this->success('登录成功', $this->auth->getUserinfo());
  92. } catch (Exception $e) {
  93. @file_put_contents("error.txt", $e->getFile() . '-', $e->getLine(), '-' . $e->getMessage());
  94. }
  95. }
  96. //
  97. // /**
  98. // * 扣款服务
  99. // * @param Request $request
  100. // * @return mixed
  101. // * @throws \think\db\exception\DataNotFoundException
  102. // * @throws \think\db\exception\ModelNotFoundException
  103. // * @throws \think\exception\DbException
  104. // */
  105. // public function signing(Request $request)
  106. // {
  107. // $price = intval($request->param('price'));
  108. // $uid = intval($request->param('uid'));
  109. // $cid = intval($request->param('cid'));
  110. // $plan_id = WechatPlan::where('price', $price)->value('plan_id');
  111. // $p_id = WechatPlan::where('price', $price)->value('id');
  112. //// var_dump($plan_id);
  113. //// var_dump($p_id);die();
  114. //// $plan_id = 11;
  115. //// $p_id = 12;
  116. // try {
  117. //// $notify_url=Request::instance()->domain() . "/api/wechat/notify/" . $cid; //回调接口
  118. //// $app_id = 'wx5681205d1ef4d9d3';
  119. // $app_id = Company::where('id', $cid)->value('wechat_appid');
  120. //// $mch_id = '1623907696';
  121. // $mch_id = Company::where('id', $cid)->value('pay_weixin_mchid');
  122. // $key = Company::where('id', $cid)->value('pay_weixin_key');
  123. // @file_put_contents("quanju.txt", $key . "-key\r\n", 8);
  124. // $contract_code = $this->generateRandomString(12);
  125. // $notify_url = Request::instance()->domain() . "/api/wechat/notify/" . $cid; //回调接口 $cid 企业id
  126. //// $num = time() + mt_rand(10, 999999) . '' . substr($msec, 2, 3);//生成随机数
  127. //// $request_serial=$this->generateUniqueSerialNumber();
  128. // $request_serial = WechatPlanRecord::where('is_signing', 0)->order('request_serial desc')->value('request_serial');
  129. // if (empty($request_serial)) {
  130. // $request_serial = 100000;
  131. // } else {
  132. // $request_serial = $request_serial + 1;
  133. // }
  134. // $contract_display_account = User::where('id', $uid)->value('nickname');
  135. // if (empty($contract_display_account)){
  136. // $this->error('用户不存在!');
  137. // }
  138. // $timestamp = time();
  139. // $version = '1.0';
  140. // $array = array(
  141. // 'appid' => $app_id,
  142. // 'mch_id' => $mch_id,
  143. // 'plan_id' => $plan_id,
  144. //// 'sub_mch_id' => $sub_mch_id,
  145. // 'contract_code' => $contract_code,
  146. // 'notify_url' => $notify_url,
  147. // 'contract_display_account' => $contract_display_account,
  148. // 'request_serial' => $request_serial,
  149. // 'timestamp' => $timestamp,
  150. // 'version' => $version,
  151. //
  152. // );
  153. //
  154. //
  155. // ksort($array);
  156. // $xml = '';
  157. // foreach ($array as $key => $value) {
  158. // $xml = $xml . $key . '=' . $value . '&';
  159. // }
  160. // $xml = substr($xml, 0, -1);
  161. // @file_put_contents("quanju.txt", $xml . "-签约内容\r\n", 8);
  162. // $sign = $this->md5_sign($xml, $key);
  163. // @file_put_contents("quanju.txt", $sign . "-签名\r\n", 8);
  164. // $url = 'https://api.mch.weixin.qq.com/papay/entrustweb?' . $xml . '&sign=' . $sign;
  165. // @file_put_contents("quanju.txt", $url . "-链接\r\n", 8);
  166. //// var_dump($url);
  167. // $response = $this->curl_get($url);
  168. //// $url = $this->OfficialAccountSigning($app_id, $mch_id, $plan_id, $contract_code, $request_serial, $contract_display_account, $notify_url, $version, $sign, $timestamp);
  169. // @file_put_contents("quanju.txt", $response . "-返回链接\r\n", 8);
  170. //
  171. // WechatPlanRecord::create([
  172. // 'uid' => $uid,
  173. // 'plan_id' => $plan_id,
  174. // 'price' => $price,
  175. // 'is_signing' => 0,
  176. // 'request_serial' => $request_serial,
  177. // 'contract_code' => $contract_code,
  178. // 'contract_display_account' => $contract_display_account,
  179. // 'createtime' => time(),
  180. // 'cid' => 12,
  181. // ]);
  182. //// die();
  183. // $this->success('获取成功', $response);
  184. //// return $response;
  185. // } catch (Exception $e) {
  186. //// var_dump($e->getMessage());
  187. // @file_put_contents("quanju.txt", $e->getMessage() . "-报错信息\r\n", 8);
  188. // @file_put_contents("error.txt", $e->getFile() . '-', $e->getLine(), '-' . $e->getMessage());
  189. // }
  190. // }
  191. //
  192. // /**
  193. // * 解约扣款服务
  194. // * @param Request $request
  195. // * @return mixed
  196. // * @throws \think\db\exception\DataNotFoundException
  197. // * @throws \think\db\exception\ModelNotFoundException
  198. // * @throws \think\exception\DbException
  199. // */
  200. // public function delete_signing(Request $request)
  201. // {
  202. // $record_id = intval($request->param('id'));
  203. // $uid = intval($request->param('uid'));
  204. // $cid = intval($request->param('uid'));
  205. // try {
  206. //// $notify_url=Request::instance()->domain() . "/api/wechat/notify/" . $cid; //回调接口
  207. // $app_id = Company::where('id', $cid)->value('wechat_appid');
  208. // $mch_id = Company::where('id', $cid)->value('pay_weixin_mchid');
  209. // $contract_code = WechatPlanRecord::where('id', $record_id)->value('contract_code');
  210. // $plan_id = WechatPlanRecord::where('id', $record_id)->value('plan_id');
  211. // $contract_termination_remark = '解约备注';
  212. // $version = '1.0';
  213. // $array = array(
  214. // 'appid' => $app_id,
  215. // 'mch_id' => $mch_id,
  216. // 'plan_id' => $plan_id,
  217. // 'contract_code' => $contract_code,
  218. // 'contract_termination_remark' => $contract_termination_remark,
  219. // 'version' => $version
  220. // );
  221. //
  222. //
  223. // ksort($array);
  224. // $xml = '';
  225. // foreach ($array as $key => $value) {
  226. // $xml = $xml . $key . '=' . $value . '&';
  227. // }
  228. // $xml = substr($xml, 0, -1);
  229. // @file_put_contents("quanju.txt", json_encode($xml) . "-签约内容\r\n", 8);
  230. // $sign = $this->md5_sign($xml, '192006250b4c09247ec02edce69f6a2d');
  231. // @file_put_contents("quanju.txt", $sign . "-签名\r\n", 8);
  232. // $url = 'https://api.mch.weixin.qq.com/papay/deletecontract?' . $xml . '&sign=' . $sign;
  233. // @file_put_contents("quanju.txt", $url . "-链接\r\n", 8);
  234. // var_dump($url);
  235. //// $response = $this->curl_get($url);
  236. //// $url=$this->OfficialAccountSigning($app_id,$mch_id,$plan_id,$contract_code,$request_serial,$contract_display_account,$notify_url,$version,$sign,$timestamp);
  237. //// @file_put_contents("quanju.txt", $response.'返回链接');
  238. //
  239. // WechatPlanRecord::where('id', $record_id)->update(['is_signing' => 1]);
  240. // die();
  241. // return 1;
  242. // } catch (Exception $e) {
  243. // @file_put_contents("error.txt", $e->getFile() . '-', $e->getLine(), '-' . $e->getMessage());
  244. // }
  245. // }
  246. //
  247. // public function generateRandomString($length)
  248. // {
  249. // $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  250. // $charactersLength = strlen($characters);
  251. // $randomString = '';
  252. // for ($i = 0; $i < $length; $i++) {
  253. // $randomString .= $characters[rand(0, $charactersLength - 1)];
  254. // }
  255. // return $randomString;
  256. // }
  257. //
  258. //// 生成唯一序列号
  259. // public function generateUniqueSerialNumber()
  260. // {
  261. // // 使用uniqid生成一个唯一ID,并去除前缀的'0'
  262. // $uniqueId = ltrim(uniqid('', true), '0');
  263. //
  264. // // 确保生成的ID不以0开头且长度不超过19位(int64的最大长度)
  265. // while (strlen($uniqueId) > 19 || substr($uniqueId, 0, 1) === '0') {
  266. // $uniqueId = ltrim(uniqid('', true), '0');
  267. // }
  268. //
  269. // // 将生成的ID转换为纯数字
  270. // $serialNumber = preg_replace('/\D/', '', $uniqueId);
  271. //
  272. // // 确保生成的序列号不以0开头
  273. // if (substr($serialNumber, 0, 1) === '0') {
  274. // $serialNumber = generateUniqueSerialNumber(); // 递归调用以重新生成
  275. // }
  276. //
  277. // return $serialNumber;
  278. // }
  279. //
  280. //
  281. // public function curl_post($url = '', $name = array(), $timeout = 100)
  282. // {
  283. // // var_dump($url);die();
  284. // $ch = curl_init();
  285. // curl_setopt($ch, CURLOPT_URL, $url);
  286. // curl_setopt($ch, CURLOPT_HEADER, false); //是否显示头部
  287. // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//是否直接输出到屏幕
  288. // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  289. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  290. // curl_setopt($ch, CURLOPT_POST, true); //是否以post方式
  291. // //设置post数据
  292. // $post_data = json_encode($name);
  293. // // curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
  294. // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  295. // $content = curl_exec($ch);
  296. // // var_dump($content);die();
  297. // curl_close($ch);
  298. // $content = json_decode($content, true);
  299. // return $content;
  300. //
  301. // }
  302. //
  303. // public function curl_get(string $url, $timeout = 100)
  304. // {
  305. //// if (!empty($name)) {
  306. //// $data = '&';
  307. //// foreach ($name as $k => $v) {
  308. //// $data = $data . $k . '=' . $v.'&';
  309. //// }
  310. //// $url = $url . $data;
  311. //// }
  312. //// $url = substr($url,0,-1);
  313. // $ch = curl_init();
  314. // curl_setopt($ch, CURLOPT_URL, $url);
  315. // curl_setopt($ch, CURLOPT_HEADER, false); //是否显示头部
  316. // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//是否直接输出到屏幕
  317. // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  318. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  319. //
  320. // $content = curl_exec($ch);
  321. // curl_close($ch);
  322. //// $content = json_decode($content, true);
  323. // // $content = (array)$content;
  324. // var_dump($content);die();
  325. // return $content;
  326. // }
  327. //
  328. // public function md5_sign($data, $key)
  329. // {
  330. // $stringSignTemp = $data . "&key=$key"; //注:key为商户平台设置的密钥key
  331. //
  332. // $sign = MD5($stringSignTemp); //MD5加密
  333. // $sign = strtoupper($sign); //大写
  334. // return $sign;
  335. // }
  336. //
  337. // /**
  338. // * 公众号签约
  339. // * see:https://pay.weixin.qq.com/wiki/doc/api/wxpay_v2/papay/chapter3_1.shtml
  340. // * @param array $data
  341. // *
  342. // * @return array
  343. // * @throws AuthorizeFailedException
  344. // */
  345. // public function OfficialAccountSigning(string $appid, string $mch_id, string $plan_id, string $contract_code, string $request_serial, string $contract_display_account, string $notify_url, string $version, string $sign, string $timestamp): array
  346. // {
  347. //
  348. // $url = 'https://api.mch.weixin.qq.com/papay/entrustweb';
  349. // $info = [
  350. // 'appid' => $appid, //应用ID
  351. // 'mch_id' => $mch_id, //商户号
  352. // 'plan_id' => $plan_id, //模板id
  353. //// 'sub_mch_id' => $sub_mch_id, //子商户号
  354. // 'contract_code' => $contract_code, //签约协议号
  355. // 'request_serial' => $request_serial, //请求序列号
  356. // 'contract_display_account' => $contract_display_account, //用户账户展示名称
  357. // 'notify_url' => $notify_url, //回调通知url
  358. // 'version' => $version, //版本号 固定值1.0
  359. // 'sign' => $sign, //签名
  360. // 'timestamp' => $timestamp
  361. // ];
  362. // $response = $this->curl_get($url, $info);
  363. // return $response;
  364. // }
  365. //
  366. // public function test()
  367. // {
  368. // return '123456';
  369. // }
  370. public function signing_plan()
  371. {
  372. $this->success('获取成功', WechatPlan::where('cid', 12)->select());
  373. }
  374. // 用户签约列表
  375. public function user_signing(Request $request)
  376. {
  377. $uid = intval($request->param('uid'));
  378. $this->success('获取成功', WechatPlanRecord::where('uid', $uid)->where('is_signing', 0)->select());
  379. }
  380. }