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