DouYinController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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','refundPassNotify'];
  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. return $this->refundNotify();
  42. }
  43. } catch (\Exception $e) {
  44. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  45. }
  46. }
  47. /**
  48. * 手机号注册
  49. * @author liugc <466014217@qq.com>
  50. * @date 2025/5/20 13:39
  51. */
  52. public function register()
  53. {
  54. try {
  55. $params = $this->request->post();
  56. $this->validate($params,[
  57. "code" => "require",
  58. "mobile" => "require|mobile",
  59. ]);
  60. // 验证码验证
  61. $res = \app\workerapi\logic\LoginLogic::confirmMobile($params);
  62. if(!$res){
  63. throw new \Exception('验证码错误');
  64. }
  65. // 注册并登录
  66. $result = DouYinService::phoneLogin($params);
  67. return $this->data($result);
  68. } catch (\Exception $e) {
  69. return $this->fail($e->getMessage());
  70. }
  71. }
  72. /**
  73. * @notes 账号密码/手机号密码/手机号验证码登录
  74. * @return \think\response\Json
  75. * @author 段誉
  76. * @date 2022/9/16 10:42
  77. */
  78. public function account()
  79. {
  80. try {
  81. $params = (new LoginAccountValidate())->post()->goCheck();
  82. $result = LoginLogic::login($params);
  83. if (false === $result) {
  84. return $this->fail(LoginLogic::getError());
  85. }
  86. return $this->data($result);
  87. } catch (\Exception $e) {
  88. return $this->fail($e->getMessage());
  89. }
  90. }
  91. /**
  92. * @notes 退出登录
  93. * @return \think\response\Json
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. * @author 段誉
  98. * @date 2022/9/16 10:42
  99. */
  100. public function logout()
  101. {
  102. LoginLogic::logout($this->userInfo);
  103. return $this->success();
  104. }
  105. public function getOpenid()
  106. {
  107. $params = $this->request->post();
  108. $toData = [
  109. 'appid' => 'tt74fb0246ebf34b0601',
  110. 'secret' => "0c841cc324e8bea8a584d248556d6263fb2dadb2",
  111. 'code' => $params['code'],
  112. 'anonymous_code' => $params['anonymous_code']
  113. ];
  114. $res = DouYinService::toDyRequestUrl('https://developer.toutiao.com/api/apps/v2/jscode2session',$toData,['Content-Type' => 'application/json'],'errNoReturn',1);
  115. Log::info(json_encode($res));
  116. return $this->success('',$res);
  117. }
  118. // ******************************** 列表详情 _商品 _订单 _退款
  119. /*
  120. * 商品审核结果回调
  121. */
  122. public function goodsNotify()
  123. {
  124. try {
  125. $params = $this->request->post();
  126. Log::info('goodsNotify:'.formatLogData($params));
  127. DouYinService::goodsNotify($params);
  128. return json(["err_no"=>0,"err_tips"=>""], 200);
  129. } catch (\Exception $e) {
  130. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  131. }
  132. }
  133. /**
  134. * 所有商品
  135. * @return \think\response\Json
  136. * @author liugc <466014217@qq.com>
  137. * @date 2025/5/20 14:35
  138. */
  139. public function getAllGoods()
  140. {
  141. $params = $this->request->get();
  142. $params['platform_value'] = 6;
  143. return $this->dataLists((new GoodsLists())->setParams($params));
  144. }
  145. /**
  146. * 商品详情
  147. * @return \think\response\Json
  148. * @author liugc <466014217@qq.com>
  149. * @date 2025/5/20 14:35
  150. */
  151. public function getGoodsDetail()
  152. {
  153. $params = $this->request->get();
  154. $params['platform_value'] = 6;
  155. $result = GoodsLogic::detail($params['goods_category_id'],'category',$this->userId,$params);
  156. return $this->data($result);
  157. }
  158. /**
  159. * 订单列表
  160. * @return \think\response\Json
  161. * @author liugc <466014217@qq.com>
  162. * @date 2025/5/22 14:35
  163. */
  164. public function getOrderLists()
  165. {
  166. $params = $this->request->get();
  167. $params['user_id'] = $this->userId;
  168. return $this->dataLists((new DouyinOrderLists())->setParams($params));
  169. }
  170. public function getOrderDetail()
  171. {
  172. $params = $this->request->get();
  173. $params['user_id'] = $this->userId;
  174. $result = DouYinService::getOrderDetail($params);
  175. return $this->data($result);
  176. }
  177. /**
  178. * 退款订单列表
  179. * @return \think\response\Json
  180. * @author liugc <466014217@qq.com>
  181. * @date 2025/5/22 14:35
  182. */
  183. public function getRefundLists()
  184. {
  185. $params = $this->request->get();
  186. $params['user_id'] = $this->userId;
  187. return $this->dataLists((new DouyinRefundOrderLists())->setParams($params));
  188. }
  189. // ******************************** 订单业务
  190. /*public function createOrder()
  191. {
  192. try {
  193. $params = $this->request->post();
  194. $params['user_id'] = $this->userId;
  195. $params['user_info'] = $this->userInfo;
  196. $requestOrderData = DouYinService::createOrder($params);
  197. return $this->success('',$requestOrderData);
  198. } catch (\Exception $e) {
  199. return $this->fail($e->getMessage());
  200. }
  201. }*/
  202. /**
  203. * 拉起支付所需参数
  204. * @return \think\response\Json
  205. * @author liugc <466014217@qq.com>
  206. * @date 2025/5/22 14:35
  207. */
  208. public function requestOrderData()
  209. {
  210. try {
  211. $params = $this->request->post();
  212. $params['user_id'] = $this->userId;
  213. $params['user_info'] = $this->userInfo;
  214. $requestOrderData = DouYinService::getPluginCreateOrderData($params['goods_id']??'',$params['quantity']??1,$params['dy_order_id']??'',$params);
  215. Log::info('requestOrderData:'.formatLogData($requestOrderData));
  216. return $this->success('',$requestOrderData);
  217. } catch (\Exception $e) {
  218. return $this->fail($e->getMessage());
  219. }
  220. }
  221. public function submitOrderNotify()
  222. {
  223. try {
  224. $params = $this->request->post();
  225. Log::info('submitOrderNotify:'.formatLogData($params));
  226. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  227. if($params['type'] == 'pre_create_order'){
  228. $res = DouYinService::submitOrderNotify($msg);
  229. Log::info("submitOrderNotify:".formatLogData($res));
  230. if($res){
  231. return json(["err_no"=>0,"err_tips"=>"","data"=>$res], 200);
  232. }
  233. }
  234. return json(["err_no"=>1001,"err_tips"=>''], 200);
  235. } catch (\Exception $e) {
  236. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  237. }
  238. }
  239. /**
  240. * 支付回调
  241. * @return \think\response\Json
  242. * @author liugc <466014217@qq.com>
  243. * @date 2025/5/22 14:35
  244. * {"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"}
  245. */
  246. public function payNotify()
  247. {
  248. try {
  249. $params = $this->request->post();
  250. Log::info('payNotify:'.formatLogData($params));
  251. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  252. if($params['type'] == 'payment' && DouYinService::payNotify($msg)){
  253. return json(["err_no"=>0,"err_tips"=>""], 200);
  254. }
  255. return json(["err_no"=>1001,"err_tips"=>''], 200);
  256. } catch (\Exception $e) {
  257. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  258. }
  259. }
  260. public function payTailNotify()
  261. {
  262. try {
  263. $params = $this->request->post();
  264. Log::info('payTailNotify:'.formatLogData($params));
  265. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  266. if($params['type'] == 'payment' && DouYinService::payTailNotify($msg)){
  267. return json(["err_no"=>0,"err_tips"=>""], 200);
  268. }
  269. return json(["err_no"=>1001,"err_tips"=>''], 200);
  270. } catch (\Exception $e) {
  271. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  272. }
  273. }
  274. // ******************************** 订单预约/改约
  275. /**
  276. * 预约
  277. * @return \think\response\Json
  278. * @author liugc <466014217@qq.com>
  279. * @date 2025/5/22 14:35
  280. */
  281. /*public function reservation()
  282. {
  283. try {
  284. $params = $this->request->post(); // order_number
  285. Log::info('reservation:'.formatLogData($params));
  286. //DouYinService::reservation($params);
  287. return $this->success();
  288. } catch (\Exception $e) {
  289. return $this->fail($e->getMessage());
  290. }
  291. }*/
  292. /**
  293. * 修改预约
  294. * @return \think\response\Json
  295. * @author liugc <466014217@qq.com>
  296. * @date 2025/5/22 14:35
  297. */
  298. /*public function upReservation()
  299. {
  300. try {
  301. $params = $this->request->post(); // order_number appointment_time
  302. Log::info('upReservation:'.formatLogData($params));
  303. //DouYinService::upReservation($params);
  304. return $this->success();
  305. } catch (\Exception $e) {
  306. return $this->fail($e->getMessage());
  307. }
  308. }*/
  309. // ******************************** 订单 取消 退款 加价单,只能单独退,或者三单一起退(三单:先买单,预约单,加价单)
  310. /**
  311. * 取消订单
  312. * @return \think\response\Json
  313. * @author liugc <466014217@qq.com>
  314. * @date 2025/5/22 14:35
  315. */
  316. public function cancelOrder()
  317. {
  318. try {
  319. $params = $this->request->post(); // order_number
  320. Log::info('cancelOrder:'.formatLogData($params));
  321. DouYinService::cancelOrder($params);
  322. return $this->success();
  323. } catch (\Exception $e) {
  324. return $this->fail($e->getMessage());
  325. }
  326. }
  327. /**
  328. * 退款
  329. * @return \think\response\Json
  330. * @author liugc <466014217@qq.com>
  331. * @date 2025/5/22 14:35
  332. */
  333. /*public function refund()
  334. {
  335. try {
  336. $params = $this->request->post(); // order_number appointment_time
  337. $params['user_id'] = $this->userId;
  338. Log::info('refund:'.formatLogData($params));
  339. DouYinService::refund($params);
  340. return $this->success();
  341. } catch (\Exception $e) {
  342. return $this->fail($e->getMessage());
  343. }
  344. }*/
  345. /**
  346. * 抖音申请退款回调
  347. * @author liugc <466014217@qq.com>
  348. * @date 2025/5/22 14:35
  349. */
  350. public function refundNotify()
  351. {
  352. try {
  353. $params = $this->request->post();
  354. Log::info('refundNotify:'.formatLogData($params));
  355. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  356. if($params['type'] == 'pre_create_refund'){
  357. $res = DouYinService::refundNotify($msg);
  358. if($res){
  359. return json(["err_no"=>0,"err_tips"=>"success","data"=>$res], 200);
  360. }
  361. }
  362. return json(["err_no"=>1001,"err_tips"=>'申请失败'], 200);
  363. } catch (\Exception $e) {
  364. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  365. }
  366. }
  367. // 抖音退款成功回调
  368. public function refundPassNotify()
  369. {
  370. try {
  371. $params = $this->request->post();
  372. Log::info('refundPassNotify:'.formatLogData($params));
  373. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  374. if($params['type'] == 'refund' && DouYinService::refundPassNotify($msg)){
  375. return json(["err_no"=>0,"err_tips"=>"success"], 200);
  376. }
  377. return json(["err_no"=>1001,"err_tips"=>''], 200);
  378. } catch (\Exception $e) {
  379. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  380. }
  381. }
  382. }