Manage.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php namespace Seller\Lib;
  2. use Dever;
  3. use Dever\Helper\Str;
  4. use Manage\Lib\Auth;
  5. class Manage extends Auth
  6. {
  7. # 获取商户列表
  8. public function getInfo()
  9. {
  10. return Dever::db('info', 'seller')->select(array('status' => 1));
  11. }
  12. # 为商户生成appkey
  13. public function createAppKey($data)
  14. {
  15. $info = Dever::db('info', 'seller')->find($data['id']);
  16. if (!$info['appkey']) {
  17. $update['appkey'] = 'app' . Str::rand(15, 0);
  18. $update['appsecret'] = strtoupper(Str::rand(32));
  19. $check = Dever::db('info', 'seller')->find(array('appkey' => $update['appkey']));
  20. if ($check) {
  21. return $this->createAppKey($data);
  22. }
  23. Dever::db('info', 'seller')->update($data['id'], $update);
  24. }
  25. # 写到redis中
  26. $info = Dever::db('info', 'seller')->find($data['id']);
  27. Dever::cache('seller_' . $info['appkey'], $info);
  28. }
  29. # 订单手动充值
  30. public function createOrder($data)
  31. {
  32. if (!$data['seller_id']) {
  33. Dever::error('未选择商户');
  34. }
  35. if (!$data['data']) {
  36. Dever::error('未输入充值数据');
  37. }
  38. $goods = explode(',', $data['goods']);
  39. $goods_id = end($goods);
  40. $sku_id = $data['sku_id'];
  41. $seller_id = $data['seller_id'];
  42. $seller = Dever::db('info', 'seller')->find($seller_id);
  43. if (!$seller) {
  44. Dever::error('商户不存在');
  45. }
  46. if ($seller && $seller['status'] == 2) {
  47. Dever::error('商户已封禁');
  48. }
  49. if (!$goods_id) {
  50. Dever::error('未选择商品');
  51. }
  52. if (!$sku_id) {
  53. Dever::error('未选择规格');
  54. }
  55. $goods = Dever::db('info', 'goods')->find($goods_id);
  56. $sku = Dever::db('info_sku', 'goods')->find($sku_id);
  57. if (!$goods) {
  58. Dever::error('商品不存在');
  59. }
  60. if (!$sku) {
  61. Dever::error('规格不存在');
  62. }
  63. if ($sku['info_id'] != $goods['id']) {
  64. Dever::error('商品['.$goods['name'].']下无此规格');
  65. }
  66. $param = explode("\r\n", $data['data']);
  67. $class = Dever::load('order', 'seller');
  68. foreach ($param as $k => $v) {
  69. $order = Str::order('PAY') . '_' . $k;
  70. $class->add($seller, $goods, $sku, $v, $order);
  71. }
  72. return $data;
  73. }
  74. # 修改订单状态
  75. public function editOrder($id, $data)
  76. {
  77. if ($data['status'] <= 10) {
  78. Dever::error('请选择订单状态');
  79. }
  80. $newid = array();
  81. $id = explode(',', $id);
  82. foreach ($id as $k => $v) {
  83. $info = Dever::db('order', 'seller')->find($v);
  84. if ($info['status'] < 10) {
  85. $newid[] = $v;
  86. if (!$info['order_num']) {
  87. $update['order_num'] = Dever::load('order', 'seller')->createOrder();
  88. Dever::db('order', 'seller')->update($v, $update);
  89. }
  90. }
  91. }
  92. if (!$newid) {
  93. Dever::error('请选择未完成订单');
  94. }
  95. $newid = implode(',', $newid);
  96. $data['finish'] = 1;
  97. $data['finish_date'] = time();
  98. return array($newid, $data);
  99. }
  100. # 将订单插入到订单日志中
  101. public function addOrderLog($id, $data)
  102. {
  103. $id = explode(',', $id);
  104. foreach ($id as $k => $v) {
  105. $info = Dever::db('order', 'seller')->find($v);
  106. if ($info['status'] >= 10) {
  107. if ($info['status'] == 11) {
  108. # 失败,余额加回来
  109. Dever::load('info', 'seller')->inc($info['seller_id'], $info['price']);
  110. Dever::load('info', 'seller')->log($info['seller_id'], $info['id'], $info['price'], 4);
  111. }
  112. Dever::db('order_log', 'seller')->insert($info);
  113. }
  114. }
  115. }
  116. # 后台充值、提现
  117. public function operCash($data)
  118. {
  119. if ($data['type'] == 1) {
  120. Dever::load('info', 'seller')->inc($data['seller_id'], $data['cash']);
  121. Dever::db('info', 'seller')->update($id, array('p_cash' => array('+', $cash)));
  122. } elseif ($data['type'] == 2) {
  123. Dever::load('info', 'seller')->dec($data['seller_id'], $data['cash']);
  124. $data['cash'] = -1*$data['cash'];
  125. }
  126. return $data;
  127. }
  128. # 展示订单信息
  129. public function showOrder($order_id)
  130. {
  131. $result = '';
  132. if ($order_id) {
  133. $order = Dever::db('order', 'seller')->find($order_id);
  134. if ($order) {
  135. $result = $order['order_num'];
  136. }
  137. }
  138. return $result;
  139. }
  140. # 展示订单金额
  141. public function showOrderCash($cate_id, $goods_id, $sku_id, $cash, $price)
  142. {
  143. $goods = Dever::db('info', 'goods')->find($goods_id);
  144. $result = $goods['name'] . ' / ' . $cash . ' / ' . $price;
  145. return $result;
  146. }
  147. # 展示订单状态
  148. public function showOrderStatus($status)
  149. {
  150. $value = Dever::db('order', 'seller')->config['struct']['status']['value'][$status];
  151. if ($status < 10) {
  152. return $value;
  153. } elseif ($status == 10) {
  154. return '<span style="color:blue">'.$value.'</span>';
  155. } elseif ($status == 11) {
  156. return '<span style="color:red">'.$value.'</span>';
  157. }
  158. }
  159. # 展示订单统计信息
  160. public function showOrderStat($where)
  161. {
  162. $db = Dever::db('order', 'seller');
  163. $total = $db->count($where);
  164. $zongjine = $db->find($where, array('col' => 'sum(cash) as cash'));
  165. $where['status'] = 10;
  166. $yes = $db->count($where);
  167. if ($total <= 0) {
  168. $lv = '0%';
  169. } else {
  170. $lv = round($yes/$total*100, 2) . '%';
  171. }
  172. $czongjine = $db->find($where, array('col' => 'sum(cash) as cash'));
  173. $szongjine = $db->find($where, array('col' => 'sum(price) as cash'));
  174. $where['status'] = array('<', 10);
  175. $chu = $db->count($where);
  176. $where['status'] = 11;
  177. $shibai = $db->count($where);
  178. return array
  179. (
  180. array
  181. (
  182. # 一共24
  183. 'span' => '3',
  184. 'name' => '总订单数',
  185. 'value' => $total,
  186. ),
  187. array
  188. (
  189. 'span' => '3',
  190. 'name' => '成功订单数',
  191. 'value' => $yes,
  192. ),
  193. array
  194. (
  195. 'span' => '3',
  196. 'name' => '成功率',
  197. 'value' => $lv,
  198. ),
  199. array
  200. (
  201. 'span' => '3',
  202. 'name' => '处理中订单数',
  203. 'value' => $chu,
  204. ),
  205. array
  206. (
  207. 'span' => '3',
  208. 'name' => '失败订单数',
  209. 'value' => $shibai,
  210. ),
  211. array
  212. (
  213. 'span' => '3',
  214. 'name' => '总金额',
  215. 'value' => $zongjine['cash'] ?? 0,
  216. ),
  217. array
  218. (
  219. 'span' => '3',
  220. 'name' => '成功金额',
  221. 'value' => $czongjine['cash'] ?? 0,
  222. ),
  223. array
  224. (
  225. 'span' => '3',
  226. 'name' => '实际成功金额',
  227. 'value' => $szongjine['cash'] ?? 0,
  228. ),
  229. );
  230. }
  231. # 展示订单扩展信息
  232. public function showOrderExpand($data)
  233. {
  234. $seller = Dever::db('info', 'seller')->find($data['seller_id']);
  235. $channel = array();
  236. if ($data['channel_id']) {
  237. $channel = Dever::db('info', 'channel')->find($data['channel_id']);
  238. }
  239. $value = $this->showOrderStatus($data['status']);
  240. $goods = $this->showOrderCash($data['cate_id'], $data['goods_id'], $data['sku_id'], $data['cash'], $data['price']);
  241. $result['type'] = 'desc';
  242. $result['content'][] = array
  243. (
  244. 'title' => '商户发起请求',
  245. 'border' => true,
  246. 'direction' => 'horizontal',
  247. 'size' => 'small',
  248. 'column' => 1,
  249. 'content' => array
  250. (
  251. array('商户名称', $seller['name']),
  252. array('商户订单号', $data['seller_order_num']),
  253. array('商品', $goods),
  254. array('商户请求参数', $data['seller_request']),
  255. array('请求时间', date('Y-m-d H:i', $data['cdate'])),
  256. ),
  257. );
  258. $result['content'][] = array
  259. (
  260. 'title' => '系统下单',
  261. 'border' => true,
  262. 'direction' => 'horizontal',//'vertical' | 'horizontal'
  263. 'size' => 'small',//'' | 'large' | 'default' | 'small'
  264. 'column' => 1,
  265. 'content' => array
  266. (
  267. array('系统订单ID', $data['id']),
  268. array('系统订单号', $data['order_num']),
  269. array('订单状态', $value),
  270. array('系统发起请求', $data['channel_request']),
  271. array('请求时间', $data['channel_order_date'] ? date('Y-m-d H:i', $data['channel_order_date']) : '-'),
  272. ),
  273. );
  274. if ($channel) {
  275. $result['content'][] = array
  276. (
  277. 'title' => '渠道响应信息',
  278. 'border' => true,
  279. 'direction' => 'horizontal',
  280. 'size' => 'small',
  281. 'column' => 1,
  282. 'content' => array
  283. (
  284. array('渠道名称', $channel['name']),
  285. array('渠道订单号', $data['channel_order_num']),
  286. array('渠道响应信息', $data['channel_response']),
  287. ),
  288. );
  289. }
  290. if ($data['channel_callback']) {
  291. $result['content'][] = array
  292. (
  293. 'title' => '渠道回调信息',
  294. 'border' => true,
  295. 'direction' => 'horizontal',
  296. 'size' => 'small',
  297. 'column' => 1,
  298. 'content' => array
  299. (
  300. array('渠道回调信息', $data['channel_callback']),
  301. array('回调时间', date('Y-m-d H:i', $data['channel_callback_date'])),
  302. array('官方流水号', $data['official_order_num']),
  303. array('官方充值结果', $data['official_msg']),
  304. ),
  305. );
  306. }
  307. $result['content'][] = array
  308. (
  309. 'title' => '系统向商户发起回调',
  310. 'border' => true,
  311. 'direction' => 'horizontal',
  312. 'size' => 'small',
  313. 'column' => 1,
  314. 'content' => array
  315. (
  316. array('商户响应信息', $data['seller_callback']),
  317. array('响应时间', $data['seller_callback_date'] ? date('Y-m-d H:i', $data['seller_callback_date']) : '-'),
  318. array('响应次数', $data['seller_callback_num']),
  319. ),
  320. );
  321. return $result;
  322. }
  323. }