DouYinController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. $params = $this->request->post();
  125. // {"operate_type":"CREATE","product_id":"7527118921439119395","reason":"","status":"PASS"}
  126. Log::info('goodsNotify:'.formatLogData($params));
  127. return $this->success();
  128. }
  129. /**
  130. * 所有商品
  131. * @return \think\response\Json
  132. * @author liugc <466014217@qq.com>
  133. * @date 2025/5/20 14:35
  134. */
  135. public function getAllGoods()
  136. {
  137. $params = $this->request->get();
  138. $params['platform_value'] = 6;
  139. return $this->dataLists((new GoodsLists())->setParams($params));
  140. }
  141. /**
  142. * 商品详情
  143. * @return \think\response\Json
  144. * @author liugc <466014217@qq.com>
  145. * @date 2025/5/20 14:35
  146. */
  147. public function getGoodsDetail()
  148. {
  149. $params = $this->request->get();
  150. $params['platform_value'] = 6;
  151. $result = GoodsLogic::detail($params['goods_category_id'],'category',$this->userId,$params);
  152. return $this->data($result);
  153. }
  154. /**
  155. * 订单列表
  156. * @return \think\response\Json
  157. * @author liugc <466014217@qq.com>
  158. * @date 2025/5/22 14:35
  159. */
  160. public function getOrderLists()
  161. {
  162. $params = $this->request->get();
  163. $params['user_id'] = $this->userId;
  164. return $this->dataLists((new DouyinOrderLists())->setParams($params));
  165. }
  166. public function getOrderDetail()
  167. {
  168. $params = $this->request->get();
  169. $params['user_id'] = $this->userId;
  170. $result = DouYinService::getOrderDetail($params);
  171. return $this->data($result);
  172. }
  173. /**
  174. * 退款订单列表
  175. * @return \think\response\Json
  176. * @author liugc <466014217@qq.com>
  177. * @date 2025/5/22 14:35
  178. */
  179. public function getRefundLists()
  180. {
  181. $params = $this->request->get();
  182. $params['user_id'] = $this->userId;
  183. return $this->dataLists((new DouyinRefundOrderLists())->setParams($params));
  184. }
  185. // ******************************** 订单业务
  186. /*public function createOrder()
  187. {
  188. try {
  189. $params = $this->request->post();
  190. $params['user_id'] = $this->userId;
  191. $params['user_info'] = $this->userInfo;
  192. $requestOrderData = DouYinService::createOrder($params);
  193. return $this->success('',$requestOrderData);
  194. } catch (\Exception $e) {
  195. return $this->fail($e->getMessage());
  196. }
  197. }*/
  198. /**
  199. * 拉起支付所需参数
  200. * @return \think\response\Json
  201. * @author liugc <466014217@qq.com>
  202. * @date 2025/5/22 14:35
  203. */
  204. public function requestOrderData()
  205. {
  206. try {
  207. $params = $this->request->post();
  208. $params['user_id'] = $this->userId;
  209. $params['user_info'] = $this->userInfo;
  210. $requestOrderData = DouYinService::getPluginCreateOrderData($params['goods_id']??'',$params['quantity']??1,$params['dy_order_id']??'',$params);
  211. Log::info('requestOrderData:'.formatLogData($requestOrderData));
  212. return $this->success('',$requestOrderData);
  213. } catch (\Exception $e) {
  214. return $this->fail($e->getMessage());
  215. }
  216. }
  217. public function submitOrderNotify()
  218. {
  219. try {
  220. $params = $this->request->post();
  221. Log::info('submitOrderNotify:'.formatLogData($params));
  222. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  223. if($params['type'] == 'pre_create_order') $res = DouYinService::submitOrderNotify($msg);
  224. if($params['type'] == 'pre_create_refund') $res = DouYinService::refundOrderNotify($msg);
  225. Log::info("submitOrderNotify:".formatLogData($res));
  226. if($res){
  227. return json(["err_no"=>0,"err_tips"=>"","data"=>$res], 200);
  228. }
  229. } catch (\Exception $e) {
  230. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  231. }
  232. }
  233. /**
  234. * 支付回调
  235. * @return \think\response\Json
  236. * @author liugc <466014217@qq.com>
  237. * @date 2025/5/22 14:35
  238. * {"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"}
  239. */
  240. public function payNotify()
  241. {
  242. try {
  243. $params = $this->request->post();
  244. Log::info('payNotify:'.formatLogData($params));
  245. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  246. if($params['type'] == 'payment' && DouYinService::payNotify($msg)){
  247. return json(["err_no"=>0,"err_tips"=>""], 200);
  248. }
  249. if($params['type'] == 'refund'){
  250. return json(["err_no"=>0,"err_tips"=>""], 200);
  251. }
  252. } catch (\Exception $e) {
  253. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  254. }
  255. }
  256. public function payTailNotify()
  257. {
  258. try {
  259. $params = $this->request->post();
  260. Log::info('payTailNotify:'.formatLogData($params));
  261. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  262. if($params['type'] == 'payment' && DouYinService::payTailNotify($msg)){
  263. return json(["err_no"=>0,"err_tips"=>""], 200);
  264. }
  265. if($params['type'] == 'refund'){
  266. return json(["err_no"=>0,"err_tips"=>""], 200);
  267. }
  268. } catch (\Exception $e) {
  269. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  270. }
  271. }
  272. // ******************************** 订单预约/改约
  273. /**
  274. * 预约
  275. * @return \think\response\Json
  276. * @author liugc <466014217@qq.com>
  277. * @date 2025/5/22 14:35
  278. */
  279. /*public function reservation()
  280. {
  281. try {
  282. $params = $this->request->post(); // order_number
  283. Log::info('reservation:'.formatLogData($params));
  284. //DouYinService::reservation($params);
  285. return $this->success();
  286. } catch (\Exception $e) {
  287. return $this->fail($e->getMessage());
  288. }
  289. }*/
  290. /**
  291. * 修改预约
  292. * @return \think\response\Json
  293. * @author liugc <466014217@qq.com>
  294. * @date 2025/5/22 14:35
  295. */
  296. /*public function upReservation()
  297. {
  298. try {
  299. $params = $this->request->post(); // order_number appointment_time
  300. Log::info('upReservation:'.formatLogData($params));
  301. //DouYinService::upReservation($params);
  302. return $this->success();
  303. } catch (\Exception $e) {
  304. return $this->fail($e->getMessage());
  305. }
  306. }*/
  307. // ******************************** 订单 取消 退款 加价单,只能单独退,或者三单一起退(三单:先买单,预约单,加价单)
  308. /**
  309. * 取消订单
  310. * @return \think\response\Json
  311. * @author liugc <466014217@qq.com>
  312. * @date 2025/5/22 14:35
  313. */
  314. public function cancelOrder()
  315. {
  316. try {
  317. $params = $this->request->post(); // order_number
  318. Log::info('cancelOrder:'.formatLogData($params));
  319. DouYinService::cancelOrder($params);
  320. return $this->success();
  321. } catch (\Exception $e) {
  322. return $this->fail($e->getMessage());
  323. }
  324. }
  325. /**
  326. * 退款
  327. * @return \think\response\Json
  328. * @author liugc <466014217@qq.com>
  329. * @date 2025/5/22 14:35
  330. */
  331. /*public function refund()
  332. {
  333. try {
  334. $params = $this->request->post(); // order_number appointment_time
  335. $params['user_id'] = $this->userId;
  336. Log::info('refund:'.formatLogData($params));
  337. DouYinService::refund($params);
  338. return $this->success();
  339. } catch (\Exception $e) {
  340. return $this->fail($e->getMessage());
  341. }
  342. }*/
  343. /**
  344. * 抖音申请退款回调
  345. * @author liugc <466014217@qq.com>
  346. * @date 2025/5/22 14:35
  347. */
  348. public function refundNotify()
  349. {
  350. try {
  351. $params = $this->request->post();
  352. Log::info('refundNotify:'.formatLogData($params));
  353. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  354. $res = DouYinService::refundNotify($msg);
  355. if($res){
  356. return json(["err_no"=>0,"err_tips"=>"success","data"=>$res], 200);
  357. }else{
  358. throw new \Exception('申请失败');
  359. }
  360. } catch (\Exception $e) {
  361. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  362. }
  363. }
  364. // 抖音退款成功回调
  365. public function refundPassNotify()
  366. {
  367. try {
  368. $params = $this->request->post();
  369. Log::info('refundPassNotify:'.formatLogData($params));
  370. $msg = is_array($params['msg'])?$params['msg']:json_decode($params['msg'],true);
  371. if(DouYinService::refundPassNotify($msg)){
  372. return json(["err_no"=>0,"err_tips"=>"success"], 200);
  373. }
  374. } catch (\Exception $e) {
  375. return json(["err_no"=>1001,"err_tips"=>$e->getMessage()], 200);
  376. }
  377. }
  378. }