Wechat.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\ApiController;
  4. use app\api\model\taxi\UserPaymentOrder;
  5. use app\api\model\user\MotorAgent;
  6. use app\common\model\OrderPaylog;
  7. use EasyWeChat\Factory;
  8. use EasyWeChat\Kernel\Exceptions\Exception;
  9. use EasyWeChat\Payment\Kernel\Exceptions\InvalidSignException;
  10. use think\Db;
  11. use think\exception\DbException;
  12. use think\exception\PDOException;
  13. use think\facade\Cache;
  14. use think\facade\Log;
  15. date_default_timezone_set("Asia/Shanghai");
  16. class Wechat extends ApiController
  17. {
  18. private $config = [
  19. // 'app_id' => 'wx7a48de22423333c3',
  20. // 'secret' => '5306eb0f85490248e2f39e51dbbd6de3',
  21. 'app_id' => 'wx088555482d9bae2b',
  22. 'secret' => 'b8a3a0da52df81472af4ec1fbc4b571c',
  23. // 'response_type' => 'array',
  24. 'log' => [
  25. 'level' => 'debug',
  26. 'file' => __DIR__ . '/wechat_debug.log',
  27. ],
  28. ];
  29. private $tpl1 = [
  30. 'template_id' => 'bhkIzzwYpjjXJJ9lPdfXIsUYRrfSz52aJel1n74AA7A', // 所需下发的订阅模板id
  31. 'touser' => 'o11PJ5QDWJnIa1kPKsvvStXj243U',
  32. 'data' => [
  33. ]
  34. ];
  35. private $tpl2 =[
  36. 'template_id' => '_3OK4582YpA9s5y_6D_dwewqxqEdsd9JNrhvBFQqcrI', // 所需下发的订阅模板id
  37. 'touser' => 'o11PJ5QDWJnIa1kPKsvvStXj243U', // 接收者(用户)的 openid
  38. 'page' => '/pages/index/index', // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  39. 'data' => [ // 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
  40. 'thing1' => [
  41. 'value' => '拼团返现奖励金',
  42. ],
  43. 'thing2' => [
  44. 'value' => "快打开小程序,立即抢红包!",
  45. ]
  46. ]
  47. ];
  48. private $tpl3 = [
  49. 'template_id' => 'v2nNL6LKvYr0pyGsTBw5r9Zaa7lS63Hm5AjvJq-tKWc', // 所需下发的订阅模板id
  50. 'touser' => 'o11PJ5QDWJnIa1kPKsvvStXj243U',
  51. 'data' => [
  52. ]
  53. ];
  54. protected $wechat;
  55. /**
  56. *
  57. * @author 许祖兴 < zuxing.xu@lettered.cn>
  58. * @date 2020/7/1 14:46
  59. */
  60. public function initialize()
  61. {
  62. // 加载配置
  63. $wechat = sys_config('','wechat');
  64. //var_dump($wechat);
  65. $this->wechat = Factory::miniProgram([
  66. 'app_id' => $wechat['mini_appid'],
  67. 'secret' => $wechat['mni_secret_key'],
  68. 'response_type' => 'array',
  69. 'log' => [
  70. 'level' => 'debug',
  71. 'file' => app()->getRuntimePath() . 'log/'.date('Ym').'/wechat_debug.log',
  72. ],
  73. ]);
  74. }
  75. /**
  76. * 获取微信session
  77. *
  78. * @author 许祖兴 < zuxing.xu@lettered.cn>
  79. * @date 2020/7/6 16:27
  80. *
  81. * @return \think\response\Json
  82. */
  83. public function getSession()
  84. {
  85. // 接收数据
  86. $param = $this->request->param();
  87. // 内置验证
  88. $valid = $this->validate($param, [
  89. 'code' => 'require'
  90. ]);
  91. // 错误
  92. if (true !== $valid){
  93. return $this->ApiJson(-1,$valid);
  94. }
  95. $session = $this->wechat->auth->session($param['code']);
  96. if (isset($session['errcode'])){
  97. return $this->ApiJson(-1,'获取不到用户OpenId!');
  98. }
  99. // 这里存在问题,建议修改 20210525,cache存在唯一,多个用户登录会有问题
  100. // 写入缓存
  101. Cache::set('wx_session',$session);
  102. return $this->ApiJson(0,'OK!', $session);
  103. }
  104. /**
  105. * 前置验证
  106. *
  107. * @author 许祖兴 < zuxing.xu@lettered.cn>
  108. * @date 2020/7/8 10:55
  109. *
  110. * @return array|\think\response\Json
  111. */
  112. private function preValidData()
  113. {
  114. // 接收数据
  115. $param = $this->request->param();
  116. // 内置验证
  117. $valid = $this->validate($param, [
  118. 'encryptedData' => 'require',
  119. 'iv' => 'require',
  120. ]);
  121. // 错误
  122. if (true !== $valid){
  123. return $this->ApiJson(-1,$valid);
  124. }
  125. return $param;
  126. }
  127. /**
  128. * 登录解密用户数据
  129. *
  130. * @author 许祖兴 < zuxing.xu@lettered.cn>
  131. * @date 2020/7/6 16:27
  132. *
  133. * @return \think\response\Json
  134. */
  135. public function login()
  136. {
  137. // 验证
  138. $param = $this->preValidData();
  139. // 读取用户session
  140. $session = Cache::get('wx_session');
  141. // app()->log(enjson([$param,$session,$parent]),'debug');
  142. // die;
  143. try {
  144. // 解密用户信息
  145. $decryptedData = $this->wechat->encryptor->decryptData($session['session_key'], $param['iv'], $param['encryptedData']);
  146. app()->log('decryptedData:' . enjson($decryptedData));
  147. // 查用户
  148. $model = model("common/Users");
  149. if (!$user = $model->where(['open_id' => $session['openid']])->find()) {
  150. // 创建新用户
  151. $model::create([
  152. 'card_id' => make_mcard_id(),
  153. 'open_id' => $session['openid'],
  154. 'avatar_url' => $decryptedData['avatarUrl'],
  155. 'nickname' => $decryptedData['nickName'],
  156. 'gender' => $decryptedData['gender']
  157. ], true);
  158. // 再查完整记录
  159. $user = $model->where(['open_id' => $session['openid']])->find();
  160. // 存在邀请码
  161. if ($param['inviteCode'] !== "" && $param['inviteCode'] !== "undefined" ){
  162. // 查找上级
  163. $parent = model('common/UsersInvite')->getBy(['code' => $param['inviteCode']]);
  164. if($parent) {
  165. // 写入关系
  166. model('common/UsersInviteRelation')->storeBy([
  167. 'form_id' => $parent['user_id'],
  168. 'invite_id' => $user['id']
  169. ]);
  170. // 推荐奖励
  171. $user_spread_property_reward = sys_config('user_spread_property_reward','user');
  172. model('common/Users')->changeProperty(
  173. $parent['user_id'],
  174. $user_spread_property_reward,
  175. "推荐奖励,资产【" . round($user_spread_property_reward, 3) .'】',
  176. true
  177. );
  178. // 更新上级字段
  179. $user->updateBy($user['id'],[
  180. 'parent_id' => $parent['user_id']
  181. ]);
  182. }
  183. }
  184. }
  185. // 用户状态
  186. if($user['status'] == 0){
  187. return $this->ApiJson(-1, '账号冻结,请联系客服');
  188. }
  189. // 登录
  190. $token = $this->auth->guard('user')->attempt($user);
  191. return $this->ApiJson(0, 'OK!', ['token' => 'Bearer ' . $token]);
  192. } catch (\Exception $e) {
  193. return $this->ApiJson(-1, $e->getMessage());
  194. }
  195. }
  196. /**
  197. * 解密手机号
  198. *
  199. * @author 许祖兴 < zuxing.xu@lettered.cn>
  200. * @date 2020/7/6 16:28
  201. *
  202. * @return \think\response\Json
  203. */
  204. public function mobile()
  205. {
  206. // 验证
  207. $param = $this->preValidData();
  208. // 读取用户session
  209. $session = Cache::get('wx_session');
  210. try {
  211. // 解密用户信息
  212. $decryptedData = $this->wechat->encryptor->decryptData($session['session_key'], $param['iv'], $param['encryptedData']);
  213. // 更新用户数据
  214. model("common/Users")->update(['mobile' => $decryptedData['phoneNumber']], ['open_id' => $session['openid']]);
  215. return $this->ApiJson(0, 'OK!', $decryptedData);
  216. } catch (\Exception $e) {
  217. return $this->ApiJson(-1, $e->getMessage());
  218. }
  219. }
  220. /**
  221. * 统一支付
  222. *
  223. * @author 许祖兴 < zuxing.xu@lettered.cn>
  224. * @date 2020/7/7 10:27
  225. *
  226. * @return \think\response\Json
  227. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  228. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  229. * @throws \GuzzleHttp\Exception\GuzzleException
  230. * @throws \Lettered\Support\Exceptions\FailedException
  231. */
  232. public function payment()
  233. {
  234. // 查
  235. $param = $this->request->param();
  236. $param['payType'] = input('payType', 'wechat');
  237. // 数据校验
  238. $valid = $this->validate($param, [
  239. 'trade_no|支付单号' => 'require',
  240. 'payType|支付方式' => 'require'
  241. ]);
  242. // 错误
  243. if (true !== $valid){
  244. return $this->ApiJson(-1,$valid);
  245. }
  246. // 数据检查
  247. $paylog = model('common/OrderPaylog')->getBy(['out_trade_no' => $param['trade_no']]);
  248. if (!$paylog){
  249. return $this->ApiJson(-1,'本次交易不存在');
  250. }
  251. if ($paylog['is_pay'] == 1){
  252. return $this->ApiJson(-1,'本次交易已处理,请勿重复操作');
  253. }
  254. // 选中支付方式
  255. switch ($param['payType']){
  256. case 'alipay':
  257. case 'wechat':
  258. return $this->thirdPayment($param['payType'], $paylog);
  259. break;
  260. case 'property':
  261. default:
  262. // 默认余额支付
  263. // 1. 支付订单
  264. // 2. 扣减余额
  265. return $this->buyOnUserSelf($param['payType'],$paylog);
  266. break;
  267. }
  268. }
  269. /***
  270. *
  271. * @author 许祖兴 < zuxing.xu@lettered.cn>
  272. * @date 2020/7/15 11:00
  273. *
  274. * @return \think\response\Json
  275. * @throws \Lettered\Support\Exceptions\FailedException
  276. */
  277. public function repay()
  278. {
  279. $param = $this->request->param();
  280. // 数据校验
  281. $valid = $this->validate($param, [
  282. 'order_id|订单号' => 'require',
  283. ]);
  284. // 错误
  285. if (true !== $valid){
  286. return $this->ApiJson(-1,$valid);
  287. }
  288. // 订单信息
  289. $order = model('common/GoodsOrder')
  290. ->getBy(['id' => $param['order_id'],'user_id' => $this->auth->user()['id']]);
  291. // 数据检查
  292. $paylog = model('common/OrderPaylog')->getBy(['order_idx' => $param['order_id'],'ascription' => 'goods']);
  293. if (!$paylog){
  294. return $this->ApiJson(-1,'本次交易不存在');
  295. }
  296. if ($paylog['is_pay'] == 1){
  297. return $this->ApiJson(-1,'本次交易已处理,请勿重复操作');
  298. }
  299. // 是否有过记录
  300. if (!$paylog){
  301. // 二次创建子支付
  302. $trade_no = get_order_no();
  303. model('common/OrderPaylog')->storeBy([
  304. 'order_idx' =>$param['order_id'],
  305. 'out_trade_no' => $trade_no,
  306. 'total_price' => $order['total_price'],
  307. 'ascription' => 'goods' // 归属订单
  308. ]);
  309. return $this->ApiJson(0,'获取支付信息成功', $trade_no);
  310. }
  311. // 再次支付
  312. return $this->ApiJson(0,'获取支付信息成功',$paylog['out_trade_no']);
  313. }
  314. public function refund()
  315. {
  316. // 查
  317. $param = $this->request->param();
  318. $param['payType'] = input('payType', 'wechat');
  319. // 数据校验
  320. $valid = $this->validate($param, [
  321. 'trade_no|支付单号' => 'require',
  322. 'payType|支付方式' => 'require'
  323. ]);
  324. // 错误
  325. if (true !== $valid){
  326. return $this->ApiJson(-1,$valid);
  327. }
  328. // 数据检查
  329. $paylog = model('common/OrderPaylog')->getBy(['out_trade_no' => $param['trade_no']]);
  330. if (!$paylog){
  331. return $this->ApiJson(-1,'本次交易不存在');
  332. }
  333. if ($paylog['is_pay'] != 1){
  334. return $this->ApiJson(-1,'本次交易未支付');
  335. }
  336. return $this->thirdRefund('wechat', $paylog);
  337. }
  338. /**
  339. * 支付通知
  340. *
  341. * @author 许祖兴 < zuxing.xu@lettered.cn>
  342. * @date 2020/7/7 9:56
  343. *
  344. * @throws \EasyWeChat\Kernel\Exceptions\Exception
  345. */
  346. public function paymentNotify()
  347. {
  348. // 加载配置
  349. $wechat = sys_config('', 'wechat');
  350. // 发起支付
  351. $config = [
  352. 'app_id' => $wechat['mini_appid'],
  353. 'mch_id' => $wechat['pay_mch_id'],
  354. 'key' => $wechat['pay_secret_key'],
  355. ];
  356. app()->log("微信异步:" . enjson($config),'debug');
  357. // 创建应用实例
  358. $payment = Factory::payment($config);
  359. Db::startTrans();
  360. try {
  361. // 响应处理
  362. $response = $payment->handlePaidNotify(function ($message, $fail) {
  363. app()->log("微信异步:" . enjson($message),'debug');
  364. // 获取支付订单
  365. $order = model('common/OrderPaylog')->where(['out_trade_no' => $message['out_trade_no']])->lock(true)->find();
  366. // 订单状态
  367. if (!$order || $order->is_pay == 1) {
  368. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  369. }
  370. if ($message['return_code'] === 'SUCCESS' && $message['result_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  371. Log::debug(enjson($message));
  372. // 支付成功
  373. $pay_price = sprintf("%.2f", round($message['total_fee'] / 100, 2));
  374. // 1. 支付订单状态is_pay =1
  375. model('common/OrderPaylog')->updateBy($order['id'], [
  376. 'pay_price' => $pay_price,
  377. 'is_pay' => 1,
  378. ]);
  379. // TODO 下面的写法我自己都看累,应该提出对应方法好处理写 =_=
  380. $thing5 = '感谢您的支持,我们将致力于更好的服务!';
  381. // 2. 对归属订单状态处理
  382. foreach (str2arr($order['order_idx']) as $orderId) {
  383. // 所属订单
  384. //TODO 等待优化
  385. $goodsOrder = [];
  386. // 模板消息标题
  387. $tplTitle = "";
  388. switch ($order['ascription']) {
  389. // 田地订单
  390. case "farmland":
  391. $tplTitle = "人人接 - 田地服务";
  392. $goodsOrder = model('common/FarmlandOrder')->getBy(['id' => $orderId]);
  393. model('common/FarmlandOrder')->updateBy($orderId, ['status' => 2,'pay_type' => 'wechat']);
  394. // todo 停止本次招标
  395. model('common/FarmlandBlock')->updateBy($goodsOrder['block_id'], [
  396. 'status' => 2 // 1 可用 2 服务中
  397. ]);
  398. push_socket_data('farmland',[
  399. 'id' => $orderId,
  400. 'msg' => '有新的农田订单等待处理,点击前往!'
  401. ]);
  402. break;
  403. // 摩的订单
  404. case "motor":
  405. $tplTitle = "人人接 - 摩的服务";
  406. $goodsOrder = model('common/TaxiOrder')->getBy(['id' => $orderId]);
  407. model('common/TaxiOrder')->updateBy($orderId, ['status' => 2]);
  408. push_socket_data('motor',[
  409. 'id' => $orderId,
  410. 'msg' => '有新的摩的订单等待处理,点击前往!'
  411. ]);
  412. // 订阅消息
  413. $thing5 = '电话请保持通畅,师傅正在赶往路上,请稍候';
  414. // 订单结算给摩的代理
  415. model('\app\api\model\taxi\Award')->send($goodsOrder);
  416. break;
  417. // 技能订单
  418. case "skill":
  419. $tplTitle = "人人接 - 技能服务";
  420. $goodsOrder = model('common/SkillOrder')->getBy(['id' => $orderId]);
  421. model('common/SkillOrder')->updateBy($orderId, ['status' => 2]);
  422. push_socket_data('skill',[
  423. 'id' => $orderId,
  424. 'msg' => '有新的技能订单等待处理,点击前往!'
  425. ]);
  426. break;
  427. // 配送订单
  428. case "mission":
  429. $tplTitle = "人人接 - 配送服务";
  430. $goodsOrder = model('common/MissionOrder')->getBy(['id' => $orderId]);
  431. model('common/MissionOrder')->updateBy($orderId, ['status' => 2]);
  432. push_socket_data('mission',[
  433. 'id' => $orderId,
  434. 'msg' => '有新的配送订单等待处理,点击前往!'
  435. ]);
  436. break;
  437. // 救援订单
  438. case "rescue":
  439. $tplTitle = "人人接 - 救援服务";
  440. $goodsOrder = model('common/RescueOrder')->getBy(['id' => $orderId]);
  441. model('common/RescueOrder')->updateBy($orderId, ['status' => 2]);
  442. push_socket_data('rescue',[
  443. 'id' => $orderId,
  444. 'msg' => '有新的救援订单等待处理,点击前往!'
  445. ]);
  446. break;
  447. // 商品订单
  448. case "goods":
  449. $tplTitle = "人人接 - 商品购买";
  450. // 产品订单
  451. $goodsOrder = model('common/GoodsOrder')->where(['id' => $orderId])->lock(true)->find();
  452. // 已经支付过了
  453. if ($goodsOrder['status'] !== 1){
  454. return $fail('已经支付过了');
  455. }
  456. // 获取当前支付订单是否为拼团
  457. // todo
  458. if ($goodsOrder && $goodsOrder['is_pin']) {
  459. $this->dealWithPinOrder($pay_price, $goodsOrder);
  460. } else {
  461. // 0: 已关闭 1:待支付, 2:待发货,3:待收货 , 4: 已完成 , 5: 待开团 , 6: 未中团
  462. // 数据更新
  463. model('common/GoodsOrder')->updateBy($orderId, [
  464. 'pay_price' => $pay_price,
  465. 'paid_at' => time(),
  466. 'status' => 2
  467. ]);
  468. }
  469. $thing5 = '顾客您好您的订单已出库小哥预计十分钟送达';
  470. push_socket_data('goods',[
  471. 'id' => $orderId,
  472. 'msg' => '有新的商品订单等待处理,点击前往!'
  473. ]);
  474. break;
  475. case "motor_agent":
  476. $UserPaymentOrder = new UserPaymentOrder();
  477. $goodsOrder = $UserPaymentOrder->with('agent,user')
  478. ->field('id,user_id,order_no,status')
  479. ->find($orderId);
  480. // 更新订单信息
  481. $goodsOrder->status = 2;
  482. $goodsOrder->save();
  483. // 更新摩的代理
  484. $goodsOrder->agent->status = 40;
  485. $goodsOrder->agent->save();
  486. // 更新用户信息
  487. $goodsOrder->user->is_motor_agent = 2;
  488. $goodsOrder->user->save();
  489. break;
  490. case "motor_driver":
  491. $UserPaymentOrder = new UserPaymentOrder();
  492. $goodsOrder = $UserPaymentOrder->with('driver')
  493. ->field('id,user_id,order_no,status')
  494. ->find($orderId);
  495. // 更新订单信息
  496. $goodsOrder->status = 2;
  497. $goodsOrder->save();
  498. // 更新摩的代理
  499. $goodsOrder->driver->status = 1;
  500. $goodsOrder->driver->save();
  501. }
  502. // 获取用户
  503. $user = model('common/Users')->getBy(['id' => $goodsOrder['user_id']]);
  504. // 用户消费金额处理
  505. // 写入本次消费后总金额
  506. $user_consume_total = $user['consume'] + $pay_price;
  507. model('common/Users')->updateBy($goodsOrder['user_id'], [
  508. 'consume' => sprintf('%.2f', $user_consume_total)
  509. ]);
  510. // 3.查找用户消费获得资产奖励
  511. if ($order['ascription'] == 'goods'){
  512. // 消费总额包含 商品、接单、技能、摩的
  513. // 获取资产设置
  514. $user_property_reward = sys_config('user_property_reward', 'user');
  515. if ($user_property_reward != ''){
  516. foreach (str2arr($user_property_reward, ',') as $key => $reward) {
  517. list($select, $amount) = str2arr($reward, ":");
  518. if ($amount == 0) continue;
  519. // 当前总消费达到奖励
  520. if ($user_consume_total >= $select && $key == $user['property_count']) {
  521. //TODO 达到消费 -- 反奖励
  522. model('common/Users')->changeProperty(
  523. $goodsOrder['user_id'],
  524. sprintf("%.2f", round($amount, 2)),
  525. "消费满 {$select},平台奖励资产【" . sprintf("%.2f", round($amount, 2)) . '】',
  526. true
  527. );
  528. // 增加领取次数
  529. model('common/Users')->where(['id' => $goodsOrder['user_id']])
  530. ->setInc('property_count');
  531. }
  532. }
  533. }
  534. }
  535. // 模板消息
  536. $this->tpl1['touser'] = $user['open_id'];
  537. $this->tpl1['data'] = [
  538. 'character_string1' => [
  539. 'value' => $goodsOrder['order_no'],
  540. ],
  541. 'thing3' => [
  542. 'value' => $tplTitle,
  543. ],
  544. 'amount4' => [
  545. 'value' => $pay_price,
  546. ],
  547. 'date2' => [
  548. 'value' => date("Y/m/d H:i:s"),
  549. ],
  550. 'thing7' => [
  551. 'value' => $thing5,
  552. ]
  553. ];
  554. $this->wechat->subscribe_message->send($this->tpl1);
  555. }
  556. return true;
  557. } else {
  558. return $fail('支付失败,请稍后再通知我');
  559. }
  560. });
  561. Db::commit();
  562. return $response->send(); // Laravel 里请使用:return $response;
  563. } catch (Exception $e) {
  564. Log::error('支付错误:' . $e->getMessage());
  565. Db::rollback();
  566. return $this->ApiJson(-1, "支付返回时错误!" . $e->getMessage());
  567. }
  568. }
  569. /**
  570. * 获取小程序分享码
  571. *
  572. * @author 许祖兴 < zuxing.xu@lettered.cn>
  573. * @date 2020/6/26 18:11
  574. *
  575. * @return \think\response\Json
  576. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  577. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  578. * @throws \Lettered\Support\Exceptions\FailedException
  579. * @throws \think\db\exception\DataNotFoundException
  580. * @throws \think\db\exception\ModelNotFoundException
  581. * @throws \think\exception\DbException
  582. */
  583. public function getShareSpread()
  584. {
  585. // 获取用户信息
  586. $user = $this->auth->user();
  587. // 获取邀请信息
  588. $spread = model('common/UsersInvite')
  589. ->where(['user_id' => $user['id']])->find();
  590. // 存在性判断
  591. if(!$spread){
  592. // 邀请码就选择用户ID
  593. $response = $this->wechat->app_code->getUnlimit('spd=' . $user['card_id'], [
  594. // 'scene' => [
  595. // 'spd' => $user['card_id']
  596. // ],
  597. 'page' => 'pages/index/index',
  598. 'width' => 600,
  599. 'line_color' => [
  600. 'r' => 236,
  601. 'g' => 108,
  602. 'b' => 68,
  603. ]
  604. ]);
  605. // 检查
  606. if(is_array($response) && $response['errcode'] == '41030'){
  607. return $this->ApiJson(-1,'当前体验版无法生成二维码,请上线后重试~');
  608. }
  609. // 保存二维码
  610. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  611. $filename = $response->saveAs($this->app->getRootPath() . 'public/uploads/spread', md5(time()).'.png');
  612. $qrImg = get_annex_url('/spread/'. $filename);
  613. //TODO 二维码写入数据库
  614. model('common/UsersInvite')->storeBy([
  615. 'user_id' => $user['id'],
  616. 'code' => $user['card_id'],
  617. 'qr_img' => $qrImg
  618. ]);
  619. return $this->ApiJson(0,'OK!', [
  620. 'inviteCode' => $user['card_id'],
  621. 'qrcodeImg' => $qrImg
  622. ]);
  623. }
  624. return $this->ApiJson(-1,'生成邀请失败,请稍后重试~');
  625. }
  626. return $this->ApiJson(0,'OK!', [
  627. 'inviteCode' => $spread['code'],
  628. 'qrcodeImg' => $spread['qr_img']
  629. ]);
  630. }
  631. private function thirdPayment($channel, $paylog)
  632. {
  633. // 目前就微信支付
  634. // 加载配置
  635. $wechat = sys_config('',$channel);
  636. // 发起支付
  637. $config = [
  638. // 前面的appid什么的也得保留哦
  639. 'app_id' => $wechat['mini_appid'],
  640. 'mch_id' => $wechat['pay_mch_id'],
  641. 'key' => $wechat['pay_secret_key'],
  642. // 'notify_url' => 'https://api.gxrrj.cn/api/v1/wechat/notify',
  643. 'notify_url' => site_url().'/api/v1/wechat/notify',
  644. // 'notify_url' => 'http://ncnjmz.natappfree.cc/api/v1/wechat/notify',
  645. // 'sandbox' => true
  646. ];
  647. // 创建应用实例
  648. $payment = Factory::payment($config);
  649. $jssdk = $payment->jssdk;
  650. // 服务名称
  651. $serve = "";
  652. switch ($paylog['ascription']){
  653. case 'farmland':
  654. $serve = "田地服务";
  655. break;
  656. case 'motor':
  657. $serve = "摩的服务";
  658. break;
  659. case 'skill':
  660. $serve = "技能服务";
  661. break;
  662. case 'mission':
  663. $serve = "配送服务";
  664. break;
  665. case 'goods':
  666. $serve = "商品购买";
  667. break;
  668. }
  669. // 统一下单
  670. $result = $payment->order->unify([
  671. 'attach' => $paylog['ascription'], // 附加数据,区分订单所属
  672. 'body' => '人人接 - ' . $serve,
  673. 'out_trade_no' => $paylog['out_trade_no'],
  674. 'total_fee' => round($paylog['total_price'] * 100),
  675. 'trade_type' => 'JSAPI',
  676. 'openid' => $this->auth->user()['open_id']
  677. ]);
  678. app()->log( '提交支付' . enjson($result),'debug');
  679. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  680. return $this->ApiJson(0,'',$jssdk->sdkConfig($result['prepay_id']));
  681. }
  682. return $this->ApiJson(-1,isset($result['return_msg'])? $result['return_msg'] : '支付异常,请稍后重试...');
  683. }
  684. private function thirdRefund($channel, $paylog)
  685. {
  686. // 1.查找对应是否有退款订单
  687. // 2.创建退款单记录
  688. // 3.请求微信退款
  689. // 参数分别为:微信订单号、商户退款单号、订单金额、退款金额、其他参数
  690. //$app->refund->byTransactionId(string $transactionId, string $refundNumber, int $totalFee, int $refundFee, array $config = []);
  691. // Example:
  692. $result = $app->refund->byTransactionId($paylog['out_trade_no'], 'refund-no-xxx', 10000, 10000, [
  693. // 可在此处传入其他参数,详细参数见微信支付文档
  694. 'refund_desc' => '商品已售完',
  695. ]);
  696. }
  697. /**
  698. * 余额支付
  699. *
  700. * @author 许祖兴 < zuxing.xu@lettered.cn>
  701. * @date 2020/8/6 16:39
  702. *
  703. * @param $payType
  704. * @param $paylog
  705. * @return \think\response\Json
  706. * @throws \Lettered\Support\Exceptions\FailedException
  707. * @throws \think\Exception
  708. * @throws \think\db\exception\DataNotFoundException
  709. * @throws \think\db\exception\ModelNotFoundException
  710. * @throws \think\exception\DbException
  711. * @throws \think\exception\PDOException
  712. */
  713. private function buyOnUserSelf($payType = 'balance', $paylog)
  714. {
  715. // 检查用户余额
  716. $user = $this->auth->user();
  717. if ($user[$payType] < $paylog['total_price']) {
  718. return $this->ApiJson(-1, '支付失败,账户余额不足!');
  719. }
  720. // 支付金额
  721. $pay_price = sprintf("%.2f", round($paylog['total_price'], 2));
  722. Db::startTrans();
  723. try{
  724. // 支付成功 支付订单状态is_pay =1
  725. model('common/OrderPaylog')->updateBy($paylog['id'], [
  726. 'pay_price' => $pay_price,
  727. 'pay_type' => $payType, // 默认支付方式
  728. 'is_pay' => 1,
  729. ]);
  730. $orderId = $paylog['order_idx'];
  731. // 模板消息标题
  732. $tplTitle = "";
  733. $thing7 = '感谢您的支持,我们将致力于更好的服务!';
  734. $goodsOrder = 1;
  735. switch($paylog['ascription']){
  736. // 田地订单
  737. case "farmland":
  738. $goodsOrder = model('common/FarmlandOrder')->getBy(['id' => $orderId]);
  739. $tplTitle = "人人接 - 田地服务";
  740. model('common/FarmlandOrder')->updateBy($orderId, ['status' => 2,'pay_type' => $payType]);
  741. // todo 停止本次招标
  742. model('common/FarmlandBlock')->updateBy($goodsOrder['block_id'], [
  743. 'status' => 2 // 1 可用 2 服务中
  744. ]);
  745. push_socket_data('farmland',[
  746. 'id' => $orderId,
  747. 'msg' => '有新的农田订单等待处理,点击前往!'
  748. ]);
  749. break;
  750. // 摩的订单
  751. case "motor":
  752. $goodsOrder = model('common/TaxiOrder')->getBy(['id' => $orderId]);
  753. $tplTitle = "人人接 - 摩的服务";
  754. model('common/TaxiOrder')->updateBy($orderId, ['status' => 2]);
  755. push_socket_data('motor',[
  756. 'id' => $orderId,
  757. 'msg' => '有新的摩的订单等待处理,点击前往!'
  758. ]);
  759. // 订阅消息
  760. // $thing7 = '电话请保持通畅,师傅正在赶往路上,请稍候';
  761. // 订单结算给摩的代理
  762. //model('\app\api\model\taxi\Award')->send($goodsOrder);
  763. break;
  764. // 技能订单
  765. case "skill":
  766. $tplTitle = "人人接 - 技能服务";
  767. $goodsOrder = model('common/SkillOrder')->getBy(['id' => $orderId]);
  768. model('common/SkillOrder')->updateBy($orderId, ['status' => 2]);
  769. push_socket_data('skill',[
  770. 'id' => $orderId,
  771. 'msg' => '有新的技能订单等待处理,点击前往!'
  772. ]);
  773. break;
  774. // 配送订单
  775. case "mission":
  776. $tplTitle = "人人接 - 配送服务";
  777. $goodsOrder = model('common/MissionOrder')->getBy(['id' => $orderId]);
  778. model('common/MissionOrder')->updateBy($orderId, ['status' => 2]);
  779. push_socket_data('mission',[
  780. 'id' => $orderId,
  781. 'msg' => '有新的配送订单等待处理,点击前往!'
  782. ]);
  783. break;
  784. // 救援订单
  785. case "rescue":
  786. $tplTitle = "人人接 - 救援服务";
  787. $goodsOrder = model('common/RescueOrder')->getBy(['id' => $orderId]);
  788. model('common/RescueOrder')->updateBy($orderId, ['status' => 2]);
  789. push_socket_data('rescue',[
  790. 'id' => $orderId,
  791. 'msg' => '有新的救援订单等待处理,点击前往!'
  792. ]);
  793. break;
  794. // 商品订单
  795. case "goods":
  796. // 获取订单产品
  797. $goods = model('common/GoodsOrderDetail')->where(['order_id' => $paylog['order_idx']])->find();
  798. $tplTitle = $goods['title'];
  799. // 产品订单
  800. $goodsOrder = model('common/GoodsOrder')->where(['id' => $paylog['order_idx']])->lock(true)->find();
  801. // 获取当前支付订单是否为拼团
  802. if ($goodsOrder && $goodsOrder['is_pin']) {
  803. $this->dealWithPinOrder($pay_price, $goodsOrder);
  804. } else {
  805. // 0: 已关闭 1:待支付, 2:待发货,3:待收货 , 4: 已完成 , 5: 待开团 , 6: 未中团
  806. // 数据更新
  807. model('common/GoodsOrder')->updateBy($goodsOrder['id'], [
  808. 'pay_price' => $pay_price,
  809. 'paid_at' => time(),
  810. 'status' => 2
  811. ]);
  812. }
  813. push_socket_data('goods',[
  814. 'id' => $goodsOrder['id'],
  815. 'msg' => '有新的商品订单等待处理,点击前往!'
  816. ]);
  817. break;
  818. }
  819. //$user = model('common/Users')->getBy(['id' => $goodsOrder['user_id']]);
  820. // 扣减账户余额
  821. if($payType == 'balance'){
  822. model('common/Users')->changeBalance(
  823. $user['id'],
  824. $pay_price,
  825. "支付成功,支付金额【" . $pay_price . "】"
  826. );
  827. }else if($payType == 'property'){
  828. model('common/Users')->changeProperty(
  829. $user['id'],
  830. $pay_price,
  831. "消费成功,消费资产【{$pay_price}】"
  832. );
  833. }
  834. // 查找用户消费获得资产奖励 - 资产消费无法获得资产
  835. if ($paylog['ascription'] == 'goods' && $payType != 'property'){
  836. // 用户消费金额处理
  837. // 写入本次消费后总金额
  838. $user_consume_total = $user['consume'] + $pay_price;
  839. model('common/Users')->updateBy($user['id'], [
  840. 'consume' => sprintf('%.2f', $user_consume_total)
  841. ]);
  842. // 消费总额包含 商品、接单、技能、摩的
  843. // 获取资产设置
  844. $user_property_reward = sys_config('user_property_reward', 'user');
  845. if ($user_property_reward != ''){
  846. foreach (str2arr($user_property_reward, ',') as $key => $reward) {
  847. list($select, $amount) = str2arr($reward, ":");
  848. if ($amount == 0) continue;
  849. // 当前总消费达到奖励
  850. if ($user_consume_total >= $select && $key == $user['property_count']) {
  851. //TODO 达到消费 -- 反奖励
  852. model('common/Users')->changeProperty(
  853. $goodsOrder['user_id'],
  854. sprintf("%.2f", round($amount, 2)),
  855. "消费满 {$select},平台奖励资产【" . sprintf("%.2f", round($amount, 2)) . '】',
  856. true
  857. );
  858. // 增加领取次数
  859. model('common/Users')->where(['id' => $goodsOrder['user_id']])
  860. ->setInc('property_count');
  861. }
  862. }
  863. }
  864. }
  865. // 模板消息
  866. $this->tpl1['touser'] = $user['open_id'];
  867. $this->tpl1['data'] = [
  868. 'character_string1' => [
  869. 'value' => $goodsOrder['order_no'],
  870. ],
  871. 'thing3' => [
  872. 'value' => $tplTitle,
  873. ],
  874. 'amount4' => [
  875. 'value' => $pay_price,
  876. ],
  877. 'date2' => [
  878. 'value' => date("Y/m/d H:i:s"),
  879. ],
  880. 'thing7' => [
  881. 'value' => $thing7,
  882. ]
  883. ];
  884. // $this->wechat->subscribe_message->send($this->tpl1);
  885. Db::commit();
  886. return $this->ApiJson(0, '支付成功');
  887. }catch(\Exception $e){
  888. Log::error('支付错误:' . $e->getMessage().$e->getTraceAsString());
  889. Db::rollback();
  890. return $this->ApiJson(-1, $e->getMessage());
  891. }
  892. }
  893. private function dealWithPinOrder($pay_price, $goodsOrder)
  894. {
  895. // 拼单详情
  896. $gorder_detail = model('common/GoodsOrderDetail')->getBy(['order_id' => $goodsOrder['id']]);
  897. // 查拼单产品
  898. $goods = model('common/Goods')->where(['id' => $gorder_detail['goods_id']])->lock(true)->find();
  899. // 支付成功 -- 0 为创建拼团
  900. if ($goodsOrder['group_id'] == 0) {
  901. $joinId = model('common/GoodsOrderGroup')->storeBy([
  902. 'user_id' => $goodsOrder['user_id'],
  903. 'goods_id' => $goods['id'],
  904. 'pin_sum' => $goods['pin_sum'],
  905. 'pin_number' => $goods['pin_number'],
  906. // 超时时间
  907. 'expired_at' => time() + (sys_config('pin_expired_at', 'store') * 3600) // 24 * 3600
  908. ]);
  909. // 数据更新
  910. model('common/GoodsOrder')->updateBy($goodsOrder['id'], [
  911. 'group_id' => $joinId, //创建的时候追加拼团id
  912. 'pay_price' => $pay_price,
  913. 'paid_at' => time(),
  914. 'status' => 5
  915. ]);
  916. } else { // 参加拼团 -- 增加人数
  917. $groupOrder = model('common/GoodsOrderGroup')->where(['id' => $goodsOrder['group_id']])->lock(true)->find();
  918. // 1. 是否已经拼满了,或者已经拼单成功了 -- 拼单满了就直接拼单失败,直接返回金额到余额,关闭订单开启限时红白奖励
  919. if (($groupOrder['pin_sum'] == $groupOrder['now_number'] ) || $groupOrder['status'] == 1) {
  920. // 关闭我的订单 -- 这里应该直接关闭订单直接返款,没有红包
  921. // 先返款
  922. model('common/Users')->changeBalance(
  923. $goodsOrder['user_id'],
  924. $pay_price,
  925. "参团失败,退还支付金额【" . $pay_price . '】',
  926. true
  927. );
  928. // 单产品SKU库存回增
  929. model('common/GoodsSku')
  930. ->where(['param' => enjson(str2arr($gorder_detail['spec']))])
  931. ->setInc('stock', $gorder_detail['count']);
  932. // 产品总库存更新
  933. model('common/Goods')
  934. ->where(['id' => $gorder_detail['goods_id']])
  935. ->setInc('stock', $gorder_detail['count']);
  936. // 销量减
  937. model('common/Goods')
  938. ->where(['id' => $gorder_detail['goods_id']])
  939. ->setDec('sell_count', $gorder_detail['count']);
  940. // 再更新 -- 关闭订单
  941. model('common/GoodsOrder')->updateBy($goodsOrder['id'], [
  942. 'pay_price' => $pay_price,
  943. 'paid_at' => time(),
  944. 'status' => 0
  945. ]);
  946. } else {
  947. // pin_sum 拼团人数
  948. // pin_number 获得人数
  949. // now_number 当前人数
  950. // 2. 加入拼单成功,检查我是不是最后一个,最后一个直接开团,获得者从随机获得
  951. $gog = model('common/GoodsOrderGroup')->where(['id' => $goodsOrder['group_id']]);
  952. $gog->setInc('now_number', $gorder_detail['count']);
  953. // 当前人数检查 -- 达到拼团人数--开团
  954. if ($groupOrder['pin_sum'] == $gog->value("now_number")) {
  955. // 当前拼单成团 - 进行逻辑发放
  956. // 查找当前拼团下的所有订单
  957. $cgo = model('common/GoodsOrder')
  958. ->where(['group_id' => $groupOrder['id']])
  959. ->select();
  960. // 利润 count * ( total_price - instock )
  961. // 总利润
  962. $cgo_total = 0;
  963. // 子单
  964. foreach ($cgo as $item) {
  965. // 子单明细
  966. $its = model('common/GoodsOrderDetail')->where(['order_id' => $item['id']])->select();
  967. foreach ($its as $itsv) {
  968. // 产品
  969. $goin = model('common/GoodsSku')->where(['goods_id' => $itsv['goods_id'],'param' => enjson(str2arr($itsv['spec'],','))])->value('instock_price');
  970. $cgo_total += sprintf("%.2f", round($itsv['total_price'] - $goin, 2));
  971. }
  972. }
  973. // todo 返现生成如果是按百分比和整数混合
  974. $pin_rebate_amount = sys_config('pin_rebate_amount', 'store');
  975. $pin_rebate_expired_at = sys_config('pin_rebate_expired_at', 'store');
  976. // 20200730 客户说按利润比得数据
  977. // 利润 * 比例 = 要发放的钱
  978. $rebate = sprintf("%.2f", round($cgo_total * (float)$pin_rebate_amount / 100, 2));
  979. $num = count($cgo);// 分成10个红包,支持10人随机领取
  980. $min = 0.01 * $rebate;//每个人最少能收到0.01元
  981. $rArr = [];
  982. for ($i = 1; $i < $num; $i++) {
  983. $safe_total = ($rebate - ($num - $i) * $min) / ($num - $i);//随机安全上限
  984. $money = mt_rand($min * 100, $safe_total * 100) / 100;
  985. $rebate -= $money;
  986. $rArr[$i] = $money;
  987. }
  988. $rArr[$num] = $rebate;
  989. //TODO 暂时脑子瓦特,想不到什么好办法
  990. // 0: 已关闭 1:待支付, 2:待发货,3:待收货 , 4: 已完成 , 5: 待开团 , 6: 未中团
  991. $in_num = array_rand($cgo->toArray(), $groupOrder['pin_number']);
  992. foreach ($cgo->toArray() as $key => $val) {
  993. if (in_array($key, (array)$in_num)) {
  994. // 这里是拼中的 -- 修改订单状态 (拼团成功,等待发货)
  995. model('common/GoodsOrder')->updateBy($val['id'], [
  996. 'status' => 2
  997. ]);
  998. } else {
  999. // 这里是拼不中-- 修改订单状态 (未中团,返款加限时红包,等待红包过期才关闭订单)
  1000. // 先返款
  1001. model('common/Users')->changeBalance(
  1002. $val['user_id'],
  1003. $pay_price,
  1004. "参团失败,退还支付金额【" . $pay_price . '】',
  1005. true
  1006. );
  1007. // 获取用户信息
  1008. $user = model('common/Users')->findBy($val['user_id']);
  1009. // 订阅消息
  1010. $this->tpl2['touser'] = $user['open_id'];
  1011. $this->wechat->subscribe_message->send($this->tpl2);
  1012. // 获取订单产品
  1013. $order_detail = model('common/GoodsOrderDetail')->getAll(['order_id' => $val['id']]);
  1014. // 单产品SKU库存回增
  1015. foreach ($order_detail as $goods) {
  1016. // 单产品SKU库存回增
  1017. model('common/GoodsSku')
  1018. ->where(['param' => enjson(str2arr($goods['spec']))])
  1019. ->setInc('stock', $goods['count']);
  1020. // 产品总库存更新
  1021. model('common/Goods')
  1022. ->where(['id' => $goods['goods_id']])
  1023. ->setInc('stock', $goods['count']);
  1024. }
  1025. // 再更新
  1026. model('common/GoodsOrder')->updateBy($val['id'], [
  1027. 'pin_rebate' => $rArr[$key + 1], //返现生成 -- 读取数据库配置
  1028. 'pin_rebate_expired' => time() + ($pin_rebate_expired_at * 3600), // x 3600 数据库配置
  1029. 'status' => 6
  1030. ]);
  1031. }
  1032. }
  1033. // 数据更新
  1034. model('common/GoodsOrder')->updateBy($goodsOrder['id'], [
  1035. 'pay_price' => $pay_price,
  1036. 'paid_at' => time()
  1037. ]);
  1038. // 拼团完成
  1039. model('common/GoodsOrderGroup')->updateBy(['id' => $goodsOrder['group_id']], [
  1040. 'success_at' => time(), // 拼单成功时间
  1041. 'status' => 1 // 拼单状态完成
  1042. ]);
  1043. } else {
  1044. // 数据更新
  1045. model('common/GoodsOrder')->updateBy($goodsOrder['id'], [
  1046. 'pay_price' => $pay_price,
  1047. 'paid_at' => time(),
  1048. 'status' => 5
  1049. ]);
  1050. }
  1051. }
  1052. }
  1053. }
  1054. }