DouYinController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use app\adminapi\lists\external\DouyinOrderLists;
  16. use app\adminapi\lists\external\DouyinRefundOrderLists;
  17. use app\api\lists\GoodsLists;
  18. use app\api\logic\GoodsLogic;
  19. use app\api\service\DouYinService;
  20. use app\common\enum\LoginEnum;
  21. use app\common\enum\user\UserTerminalEnum;
  22. use app\common\model\user\User;
  23. use app\api\validate\{LoginAccountValidate, RegisterValidate, WebScanLoginValidate, WechatLoginValidate};
  24. use app\api\logic\LoginLogic;
  25. use think\facade\Log;
  26. /**
  27. * 抖音 - tmp
  28. * Class DouYinController
  29. * @package app\api\controller
  30. */
  31. class DouYinController extends BaseApiController
  32. {
  33. public array $notNeedLogin = ['testNotify','getClientToken','getOpenid','register','account','goodsNotify','getAllGoods','getGoodsDetail','submitOrderNotify','payNotify','payTailNotify'];
  34. public function testNotify()
  35. {
  36. try {
  37. $params = $this->request->post();
  38. Log::info('testNotify'.formatLogData($params));
  39. //"type": "pre_create_refund"
  40. if($params['type'] == 'pre_create_refund'){
  41. $this->refundNotify();
  42. }
  43. return json(["err_no"=>0,"err_tips"=>""], 200);
  44. } catch (\Exception $e) {
  45. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  46. }
  47. }
  48. /**
  49. * 手机号注册
  50. * @author liugc <466014217@qq.com>
  51. * @date 2025/5/20 13:39
  52. */
  53. public function register()
  54. {
  55. try {
  56. $params = $this->request->post();
  57. $this->validate($params,[
  58. "code" => "require",
  59. "mobile" => "require|mobile",
  60. ]);
  61. // 验证码验证
  62. $res = \app\workerapi\logic\LoginLogic::confirmMobile($params);
  63. if(!$res){
  64. throw new \Exception('验证码错误');
  65. }
  66. // 注册并登录
  67. $result = DouYinService::phoneLogin($params);
  68. return $this->data($result);
  69. } catch (\Exception $e) {
  70. return $this->fail($e->getMessage());
  71. }
  72. }
  73. /**
  74. * @notes 账号密码/手机号密码/手机号验证码登录
  75. * @return \think\response\Json
  76. * @author 段誉
  77. * @date 2022/9/16 10:42
  78. */
  79. public function account()
  80. {
  81. try {
  82. $params = (new LoginAccountValidate())->post()->goCheck();
  83. $result = LoginLogic::login($params);
  84. if (false === $result) {
  85. return $this->fail(LoginLogic::getError());
  86. }
  87. return $this->data($result);
  88. } catch (\Exception $e) {
  89. return $this->fail($e->getMessage());
  90. }
  91. }
  92. /**
  93. * @notes 退出登录
  94. * @return \think\response\Json
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @author 段誉
  99. * @date 2022/9/16 10:42
  100. */
  101. public function logout()
  102. {
  103. LoginLogic::logout($this->userInfo);
  104. return $this->success();
  105. }
  106. public function getOpenid()
  107. {
  108. $params = $this->request->post();
  109. $toData = [
  110. 'appid' => 'tt74fb0246ebf34b0601',
  111. 'secret' => "0c841cc324e8bea8a584d248556d6263fb2dadb2",
  112. 'code' => $params['code'],
  113. 'anonymous_code' => $params['anonymous_code']
  114. ];
  115. $res = DouYinService::toDyRequestUrl('https://developer.toutiao.com/api/apps/v2/jscode2session',$toData,['Content-Type' => 'application/json'],'errNoReturn',1);
  116. Log::info(json_encode($res));
  117. return $this->success('',$res);
  118. }
  119. // ******************************** 列表详情 _商品 _订单 _退款
  120. /*
  121. * 商品审核结果回调
  122. */
  123. public function goodsNotify()
  124. {
  125. $params = $this->request->post();
  126. // {"operate_type":"CREATE","product_id":"7527118921439119395","reason":"","status":"PASS"}
  127. Log::info('goodsNotify:'.formatLogData($params));
  128. return $this->success();
  129. }
  130. /**
  131. * 所有商品
  132. * @return \think\response\Json
  133. * @author liugc <466014217@qq.com>
  134. * @date 2025/5/20 14:35
  135. */
  136. public function getAllGoods()
  137. {
  138. $params = $this->request->get();
  139. $params['platform_value'] = 6;
  140. return $this->dataLists((new GoodsLists())->setParams($params));
  141. }
  142. /**
  143. * 商品详情
  144. * @return \think\response\Json
  145. * @author liugc <466014217@qq.com>
  146. * @date 2025/5/20 14:35
  147. */
  148. public function getGoodsDetail()
  149. {
  150. $params = $this->request->get();
  151. $params['platform_value'] = 6;
  152. $result = GoodsLogic::detail($params['goods_category_id'],'category',$this->userId,$params);
  153. return $this->data($result);
  154. }
  155. /**
  156. * 订单列表
  157. * @return \think\response\Json
  158. * @author liugc <466014217@qq.com>
  159. * @date 2025/5/22 14:35
  160. */
  161. public function getOrderLists()
  162. {
  163. $params = $this->request->get();
  164. $params['user_id'] = $this->userId;
  165. return $this->dataLists((new DouyinOrderLists())->setParams($params));
  166. }
  167. public function getOrderDetail()
  168. {
  169. $params = $this->request->get();
  170. $params['user_id'] = $this->userId;
  171. $result = DouYinService::getOrderDetail($params);
  172. return $this->data($result);
  173. }
  174. /**
  175. * 退款订单列表
  176. * @return \think\response\Json
  177. * @author liugc <466014217@qq.com>
  178. * @date 2025/5/22 14:35
  179. */
  180. public function getRefundLists()
  181. {
  182. $params = $this->request->get();
  183. $params['user_id'] = $this->userId;
  184. return $this->dataLists((new DouyinRefundOrderLists())->setParams($params));
  185. }
  186. // ******************************** 订单业务
  187. public function createOrder()
  188. {
  189. try {
  190. $params = $this->request->post();
  191. $params['user_id'] = $this->userId;
  192. $params['user_info'] = $this->userInfo;
  193. $requestOrderData = DouYinService::createOrder($params);
  194. return $this->success('',$requestOrderData);
  195. } catch (\Exception $e) {
  196. return $this->fail($e->getMessage());
  197. }
  198. }
  199. /**
  200. * 拉起支付所需参数
  201. * @return \think\response\Json
  202. * @author liugc <466014217@qq.com>
  203. * @date 2025/5/22 14:35
  204. */
  205. public function requestOrderData()
  206. {
  207. try {
  208. $params = $this->request->post();
  209. $params['user_id'] = $this->userId;
  210. $params['user_info'] = $this->userInfo;
  211. $requestOrderData = DouYinService::getPluginCreateOrderData($params['goods_id']??'',$params['quantity']??1,$params['dy_order_id']??'',$params);
  212. Log::info('requestOrderData:'.formatLogData($requestOrderData));
  213. return $this->success('',$requestOrderData);
  214. } catch (\Exception $e) {
  215. return $this->fail($e->getMessage());
  216. }
  217. }
  218. public function submitOrderNotify()
  219. {
  220. try {
  221. $params = $this->request->post();
  222. Log::info('submitOrderNotify:'.formatLogData($params));
  223. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  224. if($params['type'] == 'pre_create_order') $res = DouYinService::submitOrderNotify($msg);
  225. if($params['type'] == 'pre_create_refund') $res = DouYinService::refundOrderNotify($msg);
  226. Log::info("submitOrderNotify:".formatLogData($res));
  227. if($res){
  228. return json(["err_no"=>0,"err_tips"=>"","data"=>$res], 200);
  229. }
  230. } catch (\Exception $e) {
  231. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  232. }
  233. }
  234. /**
  235. * 支付回调
  236. * @return \think\response\Json
  237. * @author liugc <466014217@qq.com>
  238. * @date 2025/5/22 14:35
  239. * {"version":"2.0","msg":"{\"app_id\":\"tt74fb0246ebf34b0601\",\"status\":\"CANCEL\",\"order_id\":\"1086630633686742888\",\"cp_extra\":\"{\\\"outShopId\\\":6,\\\"skuId\\\":\\\"7526760812149835817\\\",\\\"quantity\\\":\\\"1\\\",\\\"user_id\\\":29,\\\"douyinOrderId\\\":0}\",\"message\":\"TIME_OUT\",\"event_time\":1752545313000,\"out_order_no\":\"202507151003336089\",\"total_amount\":15000,\"discount_amount\":0,\"item_id\":\"0\",\"delivery_type\":0,\"order_source\":\"\"}","type":"payment"}
  240. */
  241. public function payNotify()
  242. {
  243. try {
  244. $params = $this->request->post();
  245. Log::info('payNotify:'.formatLogData($params));
  246. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  247. if($params['type'] == 'payment' && DouYinService::payNotify($msg)){
  248. return json(["err_no"=>0,"err_tips"=>""], 200);
  249. }
  250. if($params['type'] == 'refund'){
  251. return json(["err_no"=>0,"err_tips"=>""], 200);
  252. }
  253. } catch (\Exception $e) {
  254. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  255. }
  256. }
  257. public function payTailNotify()
  258. {
  259. try {
  260. $params = $this->request->post();
  261. Log::info('payTailNotify:'.formatLogData($params));
  262. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  263. if($params['type'] == 'payment' && DouYinService::payTailNotify($msg)){
  264. return json(["err_no"=>0,"err_tips"=>""], 200);
  265. }
  266. if($params['type'] == 'refund'){
  267. return json(["err_no"=>0,"err_tips"=>""], 200);
  268. }
  269. } catch (\Exception $e) {
  270. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  271. }
  272. }
  273. // ******************************** 订单预约/改约
  274. /**
  275. * 预约
  276. * @return \think\response\Json
  277. * @author liugc <466014217@qq.com>
  278. * @date 2025/5/22 14:35
  279. */
  280. public function reservation()
  281. {
  282. try {
  283. $params = $this->request->post(); // order_number
  284. Log::info('reservation:'.formatLogData($params));
  285. //DouYinService::reservation($params);
  286. return $this->success();
  287. } catch (\Exception $e) {
  288. return $this->fail($e->getMessage());
  289. }
  290. }
  291. /**
  292. * 修改预约
  293. * @return \think\response\Json
  294. * @author liugc <466014217@qq.com>
  295. * @date 2025/5/22 14:35
  296. */
  297. public function upReservation()
  298. {
  299. try {
  300. $params = $this->request->post(); // order_number appointment_time
  301. Log::info('upReservation:'.formatLogData($params));
  302. //DouYinService::upReservation($params);
  303. return $this->success();
  304. } catch (\Exception $e) {
  305. return $this->fail($e->getMessage());
  306. }
  307. }
  308. // ******************************** 订单 取消 退款
  309. /**
  310. * 取消订单
  311. * @return \think\response\Json
  312. * @author liugc <466014217@qq.com>
  313. * @date 2025/5/22 14:35
  314. */
  315. public function cancelOrder()
  316. {
  317. try {
  318. $params = $this->request->post(); // order_number
  319. Log::info('cancelOrder:'.formatLogData($params));
  320. DouYinService::cancelOrder($params);
  321. return $this->success();
  322. } catch (\Exception $e) {
  323. return $this->fail($e->getMessage());
  324. }
  325. }
  326. /**
  327. * 退款
  328. * @return \think\response\Json
  329. * @author liugc <466014217@qq.com>
  330. * @date 2025/5/22 14:35
  331. */
  332. /*public function refund()
  333. {
  334. try {
  335. $params = $this->request->post(); // order_number appointment_time
  336. $params['user_id'] = $this->userId;
  337. Log::info('refund:'.formatLogData($params));
  338. DouYinService::refund($params);
  339. return $this->success();
  340. } catch (\Exception $e) {
  341. return $this->fail($e->getMessage());
  342. }
  343. }*/
  344. /**
  345. * 申请退款回调
  346. * @author liugc <466014217@qq.com>
  347. * @date 2025/5/22 14:35
  348. */
  349. public function refundNotify()
  350. {
  351. try {
  352. $params = $this->request->post();
  353. Log::info('refundNotify:'.formatLogData($params));
  354. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  355. $res = DouYinService::refundNotify($msg);
  356. if($res){
  357. return json(["err_no"=>0,"err_tips"=>"success","data"=>$res], 200);
  358. }else{
  359. throw new \Exception('申请失败');
  360. }
  361. } catch (\Exception $e) {
  362. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  363. }
  364. }
  365. // 抖音退款成功回调
  366. public function refundPassNotify()
  367. {
  368. try {
  369. $params = $this->request->post();
  370. Log::info('refundNotify:'.formatLogData($params));
  371. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  372. if(DouYinService::refundPassNotify($msg)){
  373. return json(["err_no"=>0,"err_tips"=>"success"], 200);
  374. }
  375. } catch (\Exception $e) {
  376. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  377. }
  378. }
  379. }