Wechat.php 50 KB

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