Wechat.php 52 KB

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