WechatDeliveryForm.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\models\Admin;
  4. use app\models\District;
  5. use app\models\intracityService\IndexController;
  6. use app\models\Md;
  7. use app\models\Store;
  8. use app\models\WechatIntracityBalanceLog;
  9. use app\models\WechatIntracityService;
  10. use app\utils\Wechat\WechatMini;
  11. use EasyWeChat\Kernel\BaseClient;
  12. use yii\base\Model;
  13. class WechatDeliveryForm extends Model
  14. {
  15. public $store_id;
  16. public $md_id = 0;
  17. public $order_pattern;//运力偏好
  18. public $service_trans_prefer;//优先使用的运力ID
  19. public $flow_type; //流水类型 1:充值流水, 2:消费流水,3:退款流水
  20. public $out_store_id;//自定义门店编号
  21. public $store_name;//门店名称
  22. public $province;//省/自治区/直辖市
  23. public $city;//地级市
  24. public $area;// 县/县级市/区
  25. public $house;//具体门牌号或详细地址
  26. public $lat;//门店所在地纬度
  27. public $lng;//门店所在地经度
  28. public $phone;//门店联系电话
  29. public $street;//街道
  30. public $amount;
  31. public $begin_time;
  32. public $end_time;
  33. public $params;//检测运费所用
  34. public function rules()
  35. {
  36. return [
  37. [['store_id', 'md_id', 'order_pattern', 'flow_type'], 'integer'],
  38. [['service_trans_prefer', 'province', 'city', 'area', 'house', 'lat', 'lng', 'phone', 'out_store_id', 'store_name', 'street', 'begin_time', 'end_time'], 'string'],
  39. [['amount'], 'number']
  40. ];
  41. }
  42. ///cgi-bin/express/intracity/apply
  43. //申请门店
  44. public function applyStore(&$service) {
  45. try {
  46. $store_id = $this->store_id;
  47. $service = WechatIntracityService::findOne(['md_id' => $this->md_id, 'is_delete' => 0, 'store_id' => $store_id]);
  48. if ($service) {
  49. return [
  50. 'code' => 0,
  51. 'msg' => "成功"
  52. ];
  53. }
  54. $form = new IndexController();
  55. //申请门店不需要加签
  56. $result = $form->getUrlData($this->store_id, 'cgi-bin/express/intracity/apply', [], 1);
  57. if (isset($result['errcode']) && $result['errcode'] === 0) {
  58. $service = new WechatIntracityService();
  59. $service->md_id = $this->md_id;
  60. $service->store_id = $this->store_id;
  61. $service->save();
  62. return [
  63. 'code' => 0,
  64. 'msg' => "成功",
  65. 'data' => $result
  66. ];
  67. }
  68. throw new \Exception('申请失败' . $result['errmsg'], 1);
  69. } catch (\Exception $e) {
  70. return [
  71. 'code' => $e->getCode(),
  72. 'msg' => $e->getMessage()
  73. ];
  74. }
  75. }
  76. //创建/修改门店
  77. public function createStore() {
  78. try {
  79. // $params = $this->params;
  80. $order_pattern = (int)$this->order_pattern;
  81. $service_trans_prefer = $this->service_trans_prefer;
  82. $province = $this->province;
  83. $city = $this->city;
  84. $area = $this->area;
  85. $house = $this->house;
  86. $lat = $this->lat;
  87. $lng = $this->lng;
  88. $street = $this->street;
  89. $md_id = $this->md_id;
  90. $store_id = $this->store_id;
  91. //开通门店权限
  92. $service = WechatIntracityService::findOne(['md_id' => $this->md_id, 'is_delete' => 0, 'store_id' => $store_id]);
  93. if (empty($service)) {
  94. $result = $this->applyStore($service);
  95. debug_log($result,'wx_store.log');
  96. if ($result['code'] !== 0) {
  97. return $result;
  98. }
  99. }
  100. $is_edit = 0;
  101. $out_store_id = "";
  102. if (!empty(trim($service->wx_store_id))) {
  103. $out_store_id = $service->out_store_id;
  104. $is_edit = 1;
  105. }
  106. if (empty($out_store_id)) {
  107. $out_store_id = sprintf("%04d", $store_id) . sprintf("%04d", $md_id);
  108. if ($this->out_store_id) {
  109. $out_store_id = $this->out_store_id;
  110. }
  111. }
  112. if ($md_id <= 0) {
  113. $store = Store::findOne($store_id);
  114. $store_name = $store->name ?: '';
  115. $admin = Admin::findOne(['type' => 'store', 'is_delete' => 0, 'type_id' => $store_id]);
  116. $phone = $admin->mobile ?: '';
  117. } else {
  118. $md = Md::findOne($md_id);
  119. $store_name = $md->name ?: '';
  120. $phone = $md->mobile ?: '';
  121. }
  122. if ($this->store_name) {
  123. $store_name = $this->store_name;
  124. }
  125. if ($this->phone) {
  126. $phone = $this->phone;
  127. }
  128. //检测
  129. if (empty(trim($out_store_id))) {
  130. throw new \Exception('需要填写自定义门店编号');
  131. }
  132. if (empty(trim($store_name))) {
  133. throw new \Exception('需要填写门店名称');
  134. }
  135. if (in_array($order_pattern, [1, 2])) {
  136. //1:价格优先,2:运力优先,
  137. if ($order_pattern === 2 && !$service_trans_prefer) {
  138. throw new \Exception('运力偏好为运力优先需要选择运力');
  139. }
  140. //运力ID只支持达达,顺丰同城
  141. if ($order_pattern === 2 && !in_array($service_trans_prefer, ['DADA', 'SFTC'])) {
  142. throw new \Exception('请选择运力且只能为达达、顺丰同城');
  143. }
  144. } else {
  145. throw new \Exception('需要选择运力偏好');
  146. }
  147. $province_name = District::findOne($province)->name;
  148. $city_name = District::findOne($city)->name;
  149. $area_name = District::findOne($area)->name;
  150. if (empty(trim($province_name)) || empty(trim($city_name)) || empty(trim($area_name)) || empty(trim($house))) {
  151. throw new \Exception('缺少省市区详细地址信息');
  152. }
  153. if (empty(trim($lat)) || empty(trim($lng))) {
  154. throw new \Exception('缺少经纬度信息');
  155. }
  156. if (empty(trim($phone))) {
  157. throw new \Exception('缺少门店联系电话');
  158. }
  159. if ($is_edit) {
  160. $data = [
  161. 'keys' => [
  162. 'out_store_id' => $out_store_id,
  163. ],
  164. 'content' => [
  165. 'store_name' => $store_name, //门店名称
  166. 'order_pattern' => $order_pattern, //运力偏好
  167. 'service_trans_prefer' => $service_trans_prefer, //优先使用的运力ID
  168. 'address_info' => [
  169. 'province' => $province_name,
  170. 'city' => $city_name,
  171. 'area' => $area_name,
  172. 'street' => $street,
  173. 'house' => $house,
  174. 'lat' => (float)$lat,
  175. 'lng' => (float)$lng,
  176. 'phone' => $phone
  177. ]
  178. ]
  179. ];
  180. $url = 'cgi-bin/express/intracity/updatestore';
  181. } else {
  182. $data = [
  183. 'out_store_id' => $out_store_id, //自定义门店编号
  184. 'store_name' => $store_name, //门店名称
  185. 'order_pattern' => $order_pattern, //运力偏好
  186. 'service_trans_prefer' => $service_trans_prefer, //优先使用的运力ID
  187. 'address_info' => [
  188. 'province' => $province_name,
  189. 'city' => $city_name,
  190. 'area' => $area_name,
  191. 'street' => $street,
  192. 'house' => $house,
  193. 'lat' => (float)$lat,
  194. 'lng' => (float)$lng,
  195. 'phone' => $phone
  196. ]
  197. ];
  198. $url = 'cgi-bin/express/intracity/createstore';
  199. }
  200. $form = new IndexController();
  201. $form->store_id = $store_id;
  202. $result = $form->getUrlData($store_id, $url, $data);
  203. if (isset($result['errcode']) && $result['errcode'] === 0) {
  204. if ($result['wx_store_id']) {
  205. $service->wx_store_id = $result['wx_store_id'];
  206. }
  207. $service->out_store_id = $out_store_id;
  208. $service->store_name = $store_name;
  209. $service->order_pattern = (int)$order_pattern;
  210. $service->service_trans_prefer = $service_trans_prefer;
  211. $service->address_info = json_encode([
  212. 'province' => $province,
  213. 'city' => $city,
  214. 'area' => $area,
  215. 'street' => $street,
  216. 'house' => $house,
  217. 'lat' => (float)$lat,
  218. 'lng' => (float)$lng,
  219. 'phone' => $phone
  220. ], JSON_UNESCAPED_UNICODE);
  221. if (!$service->save()) {
  222. throw new \Exception(json_encode($service->errors, JSON_UNESCAPED_UNICODE));
  223. }
  224. return [
  225. 'code' => 0,
  226. 'msg' => "成功",
  227. 'data' => array_merge([
  228. 'wx_store_id' => $result['wx_store_id']
  229. ], $data)
  230. ];
  231. }
  232. //偶尔会解密失败,所以直接查询,失败后让用户重试
  233. $result = $this->getStoreInfo();
  234. if ($result['code'] === 0) {
  235. return [
  236. 'code' => 0,
  237. 'msg' => '成功'
  238. ];
  239. }
  240. return $result;
  241. } catch (\Exception $e) {
  242. return [
  243. 'code' => $e->getCode() ?: 1,
  244. 'msg' => $e->getMessage(),
  245. 'line' => $e->getLine(),
  246. 'file' => $e->getFile()
  247. ];
  248. }
  249. }
  250. //获取
  251. public function getDeliveryStore() {
  252. try {
  253. $store_id = $this->store_id;
  254. $md_id = $this->md_id;
  255. $this->getStoreInfo();
  256. $service = WechatIntracityService::findOne(['md_id' => $this->md_id, 'is_delete' => 0, 'store_id' => $store_id]);
  257. $service = $service->attributes;
  258. $out_store_id = sprintf("%04d", $store_id) . sprintf("%04d", $md_id);
  259. $md = Md::findOne($md_id);
  260. if (empty($service)) {
  261. $service = [
  262. 'out_store_id' => $out_store_id, //自定义门店编号
  263. 'store_name' => $md->name ?: '', //门店名称
  264. 'order_pattern' => 1, //运力偏好
  265. 'service_trans_prefer' => 'DADA', //优先使用的运力ID
  266. 'address_info' => json_encode([
  267. 'province' => '',
  268. 'city' => '',
  269. 'area' => '',
  270. 'street' => '',
  271. 'house' => '',
  272. 'lat' => (float)0,
  273. 'lng' => (float)0,
  274. 'phone' => $md->mobile ?: ''
  275. ])
  276. ];
  277. }
  278. $service['order_pattern'] = (int)$service['order_pattern'];
  279. $address_info = json_decode($service['address_info'], true) ?: [];
  280. unset($service['address_info']);
  281. $service = array_merge($service, $address_info);
  282. return [
  283. 'code' => 0,
  284. 'msg' => '获取成功',
  285. 'data' => $service
  286. ];
  287. } catch (\Exception $e) {
  288. return [
  289. 'code' => 1,
  290. 'msg' => $e->getMessage()
  291. ];
  292. }
  293. }
  294. //调用接口获取信息
  295. public function getStoreInfo() {
  296. try {
  297. $store_id = $this->store_id;
  298. $md_id = $this->md_id;
  299. $service = WechatIntracityService::findOne(['md_id' => $md_id, 'is_delete' => 0, 'store_id' => $store_id]);
  300. if (!$service) {
  301. throw new \Exception('暂未申请同城配送权限');
  302. }
  303. $out_store_id = $service->out_store_id;
  304. if (!$out_store_id) {
  305. $out_store_id = sprintf("%04d", $store_id) . sprintf("%04d", $md_id);
  306. }
  307. $data = [
  308. 'out_store_id' => $out_store_id
  309. ];
  310. $form = new IndexController();
  311. $form->store_id = $store_id;
  312. $result = $form->getUrlData($store_id, 'cgi-bin/express/intracity/querystore', $data);
  313. if (isset($result['errcode']) && $result['errcode'] === 0) {
  314. //传递out_store_id结果只有一行
  315. $store_list = $result['store_list'][0];
  316. $service->out_store_id = $store_list['out_store_id'];
  317. $service->wx_store_id = $store_list['wx_store_id'];
  318. if ($service->address_info) {
  319. $address_info = json_decode($store_list['address_info'], true);
  320. $address_info['street'] = $store_list['address_info']['street'] ?: '';
  321. $address_info['house'] = $store_list['address_info']['house'] ?: '';
  322. $address_info['lat'] = $store_list['address_info']['lat'] ?: '';
  323. $address_info['lng'] = $store_list['address_info']['lng'] ?: '';
  324. $address_info['phone'] = $store_list['address_info']['phone'] ?: '';
  325. } else {
  326. $address_info = $store_list['address_info'];
  327. }
  328. $service->address_info = json_encode($address_info, JSON_UNESCAPED_UNICODE);
  329. $service->order_pattern = $store_list['order_pattern'];
  330. $service->service_trans_prefer = $store_list['service_trans_prefer'];
  331. if (!$service->save()) {
  332. throw new \Exception(json_encode($service->errors, JSON_UNESCAPED_UNICODE));
  333. }
  334. return [
  335. 'code' => 0,
  336. 'msg' => '查询成功',
  337. 'service' => $service
  338. ];
  339. }
  340. throw new \Exception($result['errmsg'], $result['errcode']);
  341. } catch (\Exception $e) {
  342. return [
  343. 'code' => 1,
  344. 'msg' => $e->getMessage()
  345. ];
  346. }
  347. }
  348. //门店充值 https://api.weixin.qq.com/cgi-bin/express/intracity/storecharge
  349. public function storeCharge() {
  350. try {
  351. $store_id = $this->store_id;
  352. $md_id = $this->md_id;
  353. $amount = $this->amount;
  354. $service_trans_prefer = $this->service_trans_prefer;
  355. $service = WechatIntracityService::findOne(['md_id' => $md_id, 'is_delete' => 0, 'store_id' => $store_id]);
  356. if (!$service) {
  357. throw new \Exception('暂未申请同城配送权限');
  358. }
  359. if (!$service->wx_store_id) {
  360. throw new \Exception('缺少关键参数');
  361. }
  362. if (empty(trim($service_trans_prefer))) {
  363. throw new \Exception('请选择运力');
  364. }
  365. if ($amount < 50) {
  366. throw new \Exception('请输入充值金额,50元起充');
  367. }
  368. $data = [
  369. 'wx_store_id' => $service->wx_store_id,
  370. 'service_trans_id' => $service_trans_prefer,
  371. 'amount' => $amount * 100
  372. ];
  373. $form = new IndexController();
  374. $form->store_id = $store_id;
  375. $result = $form->getUrlData($store_id, 'cgi-bin/express/intracity/storecharge', $data);
  376. if (isset($result['errcode']) && $result['errcode'] === 0) {
  377. $payurl = $result['payurl'];
  378. return [
  379. 'code' => 0,
  380. 'msg' => '查询成功',
  381. 'data' => [
  382. 'payurl' => $payurl
  383. ]
  384. ];
  385. }
  386. throw new \Exception($result['errmsg'], $result['errcode']);
  387. } catch (\Exception $e) {
  388. return [
  389. 'code' => 1,
  390. 'msg' => $e->getMessage()
  391. ];
  392. }
  393. }
  394. //门店退款 https://api.weixin.qq.com/cgi-bin/express/intracity/storerefund
  395. public function storeRefund() {
  396. try {
  397. $store_id = $this->store_id;
  398. $md_id = $this->md_id;
  399. $service_trans_prefer = $this->service_trans_prefer;
  400. $service = WechatIntracityService::findOne(['md_id' => $md_id, 'is_delete' => 0, 'store_id' => $store_id]);
  401. if (!$service) {
  402. throw new \Exception('暂未申请同城配送权限');
  403. }
  404. if (!$service->wx_store_id) {
  405. throw new \Exception('缺少关键参数');
  406. }
  407. if (empty(trim($service_trans_prefer))) {
  408. throw new \Exception('请选择运力');
  409. }
  410. $data = [
  411. 'wx_store_id' => $service->wx_store_id,
  412. 'service_trans_id' => $service_trans_prefer,
  413. ];
  414. $form = new IndexController();
  415. $form->store_id = $store_id;
  416. $result = $form->getUrlData($store_id, 'cgi-bin/express/intracity/storerefund', $data);
  417. if (isset($result['errcode']) && $result['errcode'] === 0) {
  418. $refund_amount = $result['refund_amount'];
  419. return [
  420. 'code' => 0,
  421. 'msg' => '退款成功,共计退款' . ($refund_amount / 100),
  422. 'data' => [
  423. 'refund_amount' => ($refund_amount / 100)
  424. ]
  425. ];
  426. }
  427. throw new \Exception($result['errmsg'], $result['errcode']);
  428. } catch (\Exception $e) {
  429. return [
  430. 'code' => 1,
  431. 'msg' => $e->getMessage()
  432. ];
  433. }
  434. }
  435. //门店余额查询 https://api.weixin.qq.com/cgi-bin/express/intracity/balancequery
  436. public function balanceQuery() {
  437. try {
  438. $store_id = $this->store_id;
  439. $md_id = $this->md_id;
  440. $service_trans_prefer = $this->service_trans_prefer;
  441. $service = WechatIntracityService::findOne(['md_id' => $md_id, 'is_delete' => 0, 'store_id' => $store_id]);
  442. if (!$service) {
  443. throw new \Exception('暂未申请同城配送权限');
  444. }
  445. if (!$service->wx_store_id) {
  446. throw new \Exception('缺少关键参数');
  447. }
  448. $data = [
  449. 'wx_store_id' => $service->wx_store_id,
  450. 'service_trans_id' => $service_trans_prefer,
  451. ];
  452. $form = new IndexController();
  453. $form->store_id = $store_id;
  454. $result = $form->getUrlData($store_id, 'cgi-bin/express/intracity/balancequery', $data);
  455. if (isset($result['errcode']) && $result['errcode'] === 0) {
  456. $balance_arr = [];
  457. if ($result['balance_detail']) {
  458. foreach ($result['balance_detail'] as $index => $item) {
  459. $balance_arr[$index]['service_trans_name'] = $item['service_trans_name'];
  460. $balance_arr[$index]['service_trans_id'] = $item['service_trans_id'];
  461. $balance_arr[$index]['balance'] = ($item['balance'] / 100);
  462. }
  463. }
  464. $service->balance = json_encode($balance_arr, JSON_UNESCAPED_UNICODE);
  465. if (!$service->save()) {
  466. throw new \Exception(json_encode($service->errors, JSON_UNESCAPED_UNICODE));
  467. }
  468. return [
  469. 'code' => 0,
  470. 'msg' => '查询成功',
  471. 'data' => [
  472. 'balance_list' => $balance_arr
  473. ]
  474. ];
  475. }
  476. throw new \Exception($result['errmsg'], $result['errcode']);
  477. } catch (\Exception $e) {
  478. return [
  479. 'code' => 1,
  480. 'msg' => $e->getMessage()
  481. ];
  482. }
  483. }
  484. //资金流水查询 https://api.weixin.qq.com/cgi-bin/express/intracity/queryflow
  485. public function queryFlow() {
  486. try {
  487. $store_id = $this->store_id;
  488. $md_id = $this->md_id;
  489. $service_trans_prefer = $this->service_trans_prefer;
  490. $flow_type = $this->flow_type;
  491. $service = WechatIntracityService::findOne(['md_id' => $md_id, 'is_delete' => 0, 'store_id' => $store_id]);
  492. if (!$service) {
  493. throw new \Exception('暂未申请同城配送权限');
  494. }
  495. if (empty($service->wx_store_id)) {
  496. throw new \Exception('缺少关键参数');
  497. }
  498. if (!in_array($flow_type, [1, 2, 3])) {
  499. throw new \Exception('流水类型错误');
  500. }
  501. // if (!in_array($service_trans_prefer, ['DADA', 'SFTC'])) {
  502. // throw new \Exception('运力类型错误');
  503. // }
  504. $data = [
  505. 'wx_store_id' => $service->wx_store_id,
  506. 'flow_type' => $flow_type,
  507. 'service_trans_id' => $service_trans_prefer,
  508. 'begin_time' => time() - 3600 * 24 * 3,
  509. 'end_time' => time()
  510. ];
  511. $form = new IndexController();
  512. $form->store_id = $store_id;
  513. $result = $form->getUrlData($store_id, 'cgi-bin/express/intracity/queryflow', $data);
  514. if (isset($result['errcode']) && $result['errcode'] === 0) {
  515. foreach ($result['flow_list'] as $item) {
  516. if (in_array($item['flow_type'], [1, 3])) {
  517. $where = [
  518. 'pay_order_id' => $item['pay_order_id']
  519. ];
  520. } else {
  521. $where = [
  522. 'wx_order_id' => $item['wx_order_id']
  523. ];
  524. }
  525. $balance_log = WechatIntracityBalanceLog::findOne($where);
  526. if (!$balance_log) {
  527. $balance_log = new WechatIntracityBalanceLog();
  528. $balance_log->md_id = $md_id;
  529. $balance_log->flow_type = $item['flow_type'];
  530. if (isset($item['pay_order_id'])) {
  531. $balance_log->pay_order_id = $item['pay_order_id'];
  532. }
  533. $balance_log->service_trans_prefer = $item['service_trans_id'];
  534. $balance_log->pay_amount = sprintf('%.2f', ($item['pay_amount'] / 100));
  535. $balance_log->pay_time = $item['pay_time'];
  536. $balance_log->pay_status = $item['pay_status'];
  537. $balance_log->create_time = $item['create_time'];
  538. if (isset($item['consume_deadline'])) {
  539. $balance_log->consume_deadline = $item['consume_deadline'];
  540. }
  541. if (isset($item['refund_time'])) {
  542. $balance_log->refund_time = $item['refund_time'];
  543. }
  544. if (isset($item['refund_amount'])) {
  545. $balance_log->refund_amount = sprintf('%.2f', ($item['refund_amount'] / 100));
  546. }
  547. if (isset($item['refund_status'])) {
  548. $balance_log->refund_status = $item['refund_status'];
  549. }
  550. if (isset($item['deduct_amount'])) {
  551. $balance_log->deduct_amount = sprintf('%.2f', ($item['deduct_amount'] / 100));
  552. }
  553. if (isset($item['bill_id'])) {
  554. $balance_log->bill_id = $item['bill_id'];
  555. }
  556. if (isset($item['delivery_finished_time'])) {
  557. $balance_log->delivery_finished_time = $item['delivery_finished_time'];
  558. }
  559. if (isset($item['wx_order_id'])) {
  560. $balance_log->wx_order_id = $item['wx_order_id'];
  561. }
  562. if (isset($item['openid'])) {
  563. $balance_log->openid = $item['openid'];
  564. }
  565. if (isset($item['delivery_status'])) {
  566. $balance_log->delivery_status = $item['delivery_status'];
  567. }
  568. if (!$balance_log->save()) {
  569. throw new \Exception(json_encode($balance_log->errors, JSON_UNESCAPED_UNICODE));
  570. }
  571. }
  572. }
  573. if ($flow_type < 3) {
  574. $this->flow_type += 1;
  575. return $this->queryFlow();
  576. } else {
  577. return [
  578. 'code' => 0,
  579. 'msg' => '请求成功'
  580. ];
  581. }
  582. }
  583. throw new \Exception($result['errmsg'], $result['errcode']);
  584. } catch (\Exception $e) {
  585. return [
  586. 'code' => 1,
  587. 'msg' => $e->getMessage()
  588. ];
  589. }
  590. }
  591. public function getStoreFundsList() {
  592. $store_id = $this->store_id;
  593. $md_id = $this->md_id;
  594. //获取余额信息
  595. $balance_result = $this->balanceQuery();
  596. $balance_list = [];
  597. if ($balance_result['code'] === 0) {
  598. $balance_list = $balance_result['data']['balance_list'];
  599. }
  600. return [
  601. 'code' => 0,
  602. 'msg' => '获取成功',
  603. 'data' => [
  604. 'balance_list' => $balance_list
  605. ]
  606. ];
  607. }
  608. public function getBalanceLog() {
  609. $store_id = $this->store_id;
  610. $md_id = $this->md_id;
  611. $service_trans_prefer = $this->service_trans_prefer;
  612. $flow_type = $this->flow_type;
  613. $begin_time = $this->begin_time;
  614. $end_time = $this->end_time;
  615. $query = WechatIntracityBalanceLog::find()->where(['md_id' => $md_id]);
  616. if (in_array($flow_type, [1, 2, 3])) {
  617. $query->andWhere(['flow_type' => $flow_type]);
  618. }
  619. if (in_array($service_trans_prefer, ['DADA', 'SFTC'])) {
  620. $query->andWhere(['service_trans_prefer' => $service_trans_prefer]);
  621. }
  622. if (!empty($begin_time)) {
  623. $start_time = strtotime($begin_time);
  624. $query->andWhere(['>=', 'create_time', $begin_time]);
  625. }
  626. if (!empty($end_time)) {
  627. $end_time = strtotime($end_time);
  628. $query->andWhere(['<=', 'create_time', $end_time]);
  629. }
  630. $query->orderBy('create_time');
  631. $pagination = pagination_make($query);
  632. foreach ($pagination['list'] as &$item) {
  633. $item['flow_type'] = (int)$item['flow_type'];
  634. $item['pay_time'] = $item['pay_time'] > 0 ? date('Y-m-d H:i:s', $item['pay_time']) : "";
  635. $item['create_time'] = $item['create_time'] > 0 ? date('Y-m-d H:i:s', $item['create_time']) : "";
  636. $item['consume_deadline'] = $item['consume_deadline'] > 0 ? date('Y-m-d H:i:s', $item['consume_deadline']) : "";
  637. $item['refund_time'] = $item['refund_time'] > 0 ? date('Y-m-d H:i:s', $item['refund_time']) : "";
  638. $item['delivery_finished_time'] = $item['delivery_finished_time'] > 0 ? date('Y-m-d H:i:s', $item['delivery_finished_time']) : "";
  639. $item['delivery_status'] = (int)$item['delivery_status'];
  640. }
  641. return [
  642. 'code' => 0,
  643. 'msg' => 'success',
  644. 'data' => [
  645. 'data' => $pagination['list'],
  646. 'pageNo' => $pagination['pageNo'],
  647. 'totalCount' => $pagination['totalCount'],
  648. ]
  649. ];
  650. }
  651. }