1
0

DouYinController.php 13 KB

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