Agent.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace app\allwin\controller\home;
  3. use app\common\controller\Official;
  4. use app\allwin\model\AllwinConfig;
  5. use app\allwin\model\Bank;
  6. use app\allwin\model\BankBill;
  7. use app\allwin\model\MchId;
  8. use app\allwin\model\AllwinStore;
  9. use app\allwin\model\StoreBill;
  10. use app\allwin\model\StoreStats;
  11. use app\allwin\model\AllwinUser;
  12. use app\allwin\model\Worker;
  13. use app\allwin\model\WorkerStoreOrder;
  14. use app\common\model\SystemUser;
  15. use app\common\facade\WechatPay;
  16. use app\common\facade\WechatMp;
  17. use app\common\facade\Upload;
  18. use think\facade\Request;
  19. use think\helper\Time;
  20. class agent extends Official{
  21. /**
  22. * @return \think\response\View
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. * @throws \think\exception\DbException
  26. * 首页
  27. */
  28. public function index(){
  29. $view['worker'] = Worker::where(['member_miniapp_id' => $this->member_miniapp_id])->where(['uid' => $this->user->id])->find();
  30. $view['title'] = '合伙人';
  31. $view['weconfig'] = WechatMp::jsApiList($this->member_miniapp_id);
  32. $view['workerOnStore'] = WorkerStoreOrder::where(['member_miniapp_id' => $this->member_miniapp_id, 'uid' => $this->user->id, 'pay_state' => 0])->where(['uid' => $this->user->id])->find();
  33. $view['member_miniapp_id'] = $this->member_miniapp_id;
  34. $view['setting'] = model('AllwinConfig')->getConfig($this->member_miniapp_id);
  35. $allWinConfig = AllwinConfig::where(['member_miniapp_id' => $this->member_miniapp_id])->find();
  36. $view['sla'] = json_decode($allWinConfig->sla);
  37. $this->view->engine->layout(false);
  38. return view()->assign($view);
  39. }
  40. /**
  41. * @return \think\response\Json
  42. * @throws \think\exception\DbException
  43. * tabar1数据
  44. */
  45. public function myStore(){
  46. $worker = Worker::where(['member_miniapp_id' => $this->member_miniapp_id])->where(['uid' => $this->user->id])->find();
  47. if ($worker) {
  48. $list = AllwinStore::where(['member_miniapp_id' => $this->member_miniapp_id])->whereIn('id', $worker->store_ids)->order('create_time desc')->paginate(10, false)->toArray();
  49. if (count($list['data']) == 0) {
  50. return enjson(204);
  51. }
  52. return enjson(200, '', $list);
  53. } else {
  54. return enjson(204);
  55. }
  56. }
  57. /**
  58. * @return \think\response\Json
  59. * @throws \think\exception\DbException
  60. * tabar3全部帐单
  61. */
  62. public function allBill(int $type = 1){
  63. $condition[] = ['member_miniapp_id', '=', $this->member_miniapp_id];
  64. if ($type) {
  65. switch ($type) {
  66. case 2:
  67. list($start, $end) = Time::yesterday();
  68. break;
  69. case 3:
  70. list($start, $end) = $this->beforeYesterday(2);
  71. break;
  72. default:
  73. list($start, $end) = Time::today();
  74. break;
  75. }
  76. $condition[] = ['update_time', '>=', $start];
  77. $condition[] = ['update_time', '<=', $end];
  78. }
  79. $list = BankBill::where($condition)->where(['user_id' => $this->user->id])->order('update_time desc')->paginate(10, false)->toArray();
  80. if (count($list['data']) == 0) {
  81. return enjson(204);
  82. }
  83. return enjson(200, '', $list);
  84. }
  85. /**
  86. * @return \think\response\Json
  87. * tabar3账单总金额
  88. */
  89. public function allBillSum(){
  90. $worker = Worker::where(['member_miniapp_id' => $this->member_miniapp_id])->where(['uid' => $this->user->id])->find();
  91. $data = [
  92. 'worker' => $worker,
  93. 'user' => SystemUser::where(['member_miniapp_id' => $this->member_miniapp_id, 'id' => $this->user->id])->find(),
  94. 'storeNum' => AllwinStore::where(['member_miniapp_id' => $this->member_miniapp_id])->where(['is_lock' => 0])->whereIn('id', $worker->store_ids)->count(),
  95. 'sum' => BankBill::where(['member_miniapp_id' => $this->member_miniapp_id])->where(['user_id' => $this->user->id])->sum('money'),
  96. 'vipNum' => AllwinUser::where('store_id', '>', 0)->whereIn('store_id', $worker->store_ids)->count(),
  97. 'bank' => Bank::where(['member_miniapp_id' => $this->member_miniapp_id, 'user_id' => $this->user->id])->find(),
  98. ];
  99. return enjson(200, '', $data);
  100. }
  101. /**
  102. * @param int $id
  103. * @return \think\response\Json
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. * @throws \think\exception\DbException
  107. * tabar1修改时所需
  108. */
  109. public function getDataById(int $id = 0){
  110. $store = AllwinStore::where(['member_miniapp_id' => $this->member_miniapp_id, 'id' => $id])->find();
  111. $data = [
  112. 'store' => $store,
  113. 'user' => SystemUser::where(['member_miniapp_id' => $this->member_miniapp_id, 'id' => $store->manage_uid])->find()
  114. ];
  115. return enjson(200, '', $data);
  116. }
  117. /**
  118. * @param $id
  119. * @return \think\response\Json
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. * @throws \think\exception\DbException
  123. * tabar1单个店铺帐单总计
  124. */
  125. public function getStoreBill(int $id = 0){
  126. $storeStats = StoreStats::where(['member_miniapp_id' => $this->member_miniapp_id, 'store_id' => $id])->find();
  127. return enjson(200, '', $storeStats);
  128. }
  129. /**
  130. * @param $id
  131. * @param $type
  132. * @return \think\response\Json
  133. * @throws \think\exception\DbException
  134. * tabar1单个店铺帐单列表
  135. */
  136. public function storeBillList(int $id = 0,int $type = 1){
  137. $condition[] = ['member_miniapp_id', '=', $this->member_miniapp_id];
  138. if ($type) {
  139. switch ($type) {
  140. case 2:
  141. list($start, $end) = Time::yesterday();
  142. break;
  143. case 3:
  144. list($start, $end) = $this->beforeYesterday(2);
  145. break;
  146. default:
  147. list($start, $end) = Time::today();
  148. break;
  149. }
  150. $condition[] = ['update_time', '>=', $start];
  151. $condition[] = ['update_time', '<=', $end];
  152. }
  153. $list = StoreBill::where($condition)->where(['store_id' => $id])->order('update_time desc')->paginate(10, false)->toArray();
  154. if (count($list['data']) == 0) {
  155. return enjson(204);
  156. }
  157. return enjson(200, '', $list);
  158. }
  159. /**
  160. * @param string $name
  161. * @return \think\response\Json
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\ModelNotFoundException
  164. * @throws \think\exception\DbException
  165. * tabar2获取用户数据
  166. */
  167. public function getUser($key = ''){
  168. $user = SystemUser::where(['member_miniapp_id' => $this->member_miniapp_id])->where('nickname|phone_uid', 'like','%' . $key . '%','or')->find();
  169. if ($user) {
  170. $store = AllwinStore::where(['member_miniapp_id' => $this->member_miniapp_id, 'manage_uid' => $user->id])->find();
  171. if ($store) {
  172. return enjson(0, '该店长已绑定其他店铺');
  173. } else {
  174. return enjson(200, '', $user);
  175. }
  176. } else {
  177. return enjson(0, '该用户不存在');
  178. }
  179. }
  180. /**
  181. * @return \think\response\Json
  182. * 上传图片
  183. */
  184. public function upImg(){
  185. if (request()->isPost()) {
  186. $rel = Upload::index();
  187. if ($rel['error'] == 0) {
  188. return json(['code' => 200, 'msg' => 'success', 'data' => $rel['url']]);
  189. } else {
  190. return json(['code' => 204, 'msg' => 'error']);
  191. }
  192. }
  193. }
  194. /**
  195. * @return \think\response\Json
  196. * 修改保存
  197. */
  198. public function edit(){
  199. $worker = Worker::where(['member_miniapp_id' => $this->member_miniapp_id])->where(['uid' => $this->user->id])->find();
  200. $data = [
  201. 'member_miniapp_id' => $this->member_miniapp_id,
  202. 'uid' => $this->user->id,
  203. 'id' => input('post.id/d'),
  204. 'manage_uid' => input('post.manage_uid/d'),
  205. 'charges' => input('post.charges/d'),
  206. 'work_time' => input('post.work_time/s'),
  207. 'name' => input('post.name/s'),
  208. 'address' => input('post.address/s'),
  209. 'telphone' => input('post.telphone/s'),
  210. 'tags' => input('post.tags/s'),
  211. 'img' => input('post.img/s')
  212. ];
  213. if ($data['id'] > 0) {
  214. $store = AllwinStore::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $data['id']])->find();
  215. if($store){
  216. $workerStoreOrder = WorkerStoreOrder::where(['member_miniapp_id' => $this->member_miniapp_id, 'store_id' => $store->id])->find();
  217. if($workerStoreOrder){
  218. if(isset($workerStoreOrder->pay_state) && $workerStoreOrder->pay_state != 0 && $store->is_lock == 1){
  219. return enjson(0, '该数据禁止修改');
  220. }
  221. }else{
  222. return enjson(0, '数据访问错误');
  223. }
  224. }else{
  225. return enjson(0, '数据访问错误');
  226. }
  227. }
  228. if ($data['manage_uid'] == 0) {
  229. return enjson(0, '好店店长不存在');
  230. }
  231. $validate = $this->validate($data, 'Store.wechatedit');
  232. if (true !== $validate) {
  233. return enjson(0, $validate);
  234. }
  235. if ($data['charges'] > 0 && $data['charges'] <= 30) {
  236. return enjson(0, '抱歉~交易服务费禁止小于30‰');
  237. }
  238. $store_id = AllwinStore::wechatEdit($data);
  239. $workerStoreOrder = WorkerStoreOrder::where(['member_miniapp_id' => $this->member_miniapp_id, 'store_id' => $store_id])->find();
  240. if(!empty($workerStoreOrder) && $workerStoreOrder->pay_state == 1){
  241. return enjson(200,'操作成功',$store_id);
  242. }
  243. $setting = model('AllwinConfig')->getConfig($this->member_miniapp_id);
  244. $order_no = $this->user->invite_code . order_no();
  245. $data['store_id'] = $store_id;
  246. $data['order_no'] = $order_no;
  247. $data['uid'] = $this->user->id;
  248. $data['total_price'] = $setting->workers_on_store;
  249. $data['price'] = $setting->workers_discount >0 ? $setting->workers_discount/100 * $setting->workers_on_store : $setting->workers_on_store;
  250. $data['member_miniapp_id'] = $this->member_miniapp_id;
  251. if(!empty($setting) && isset($setting->workers_on_store) && $setting->workers_on_store > 0){
  252. $data['pay_state'] = 0;
  253. $result = WorkerStoreOrder::createOrder($data);
  254. $unorder['miniapp_id'] = $this->member_miniapp_id;
  255. $unorder['name'] = '开通好店费用';
  256. $unorder['order_no'] = $order_no;
  257. $unorder['notify_url'] = api(3,'allwin/Partnershipnotify/onStoreNotify',$this->member_miniapp_id);;
  258. $unorder['total_fee'] = $data['price'] * 100;
  259. if ($setting->is_psp == 1) {
  260. $mid = MchId::getMch(0, $this->member_miniapp_id);
  261. $unorder['mchid'] = $mid->mchid;
  262. }
  263. $unorder['openid'] = $this->user->official_uid;
  264. $ispay = WechatPay::orderPay($unorder,true);
  265. if ($ispay['code'] == 0) {
  266. return json(['code' => 0, 'return_code' => 'FAIL', 'msg' => $ispay['msg']]);
  267. }
  268. return enjson(206,'操作成功', $ispay['data']);
  269. }else{
  270. $data['pay_state'] = 2;
  271. $result = WorkerStoreOrder::createOrder($data);
  272. return enjson(200,'操作成功',$store_id);
  273. }
  274. }
  275. /**
  276. * @param int $day
  277. * @return array
  278. * 获取前天时间
  279. */
  280. public function beforeYesterday(int $day)
  281. {
  282. $yesterday = date('d') - $day;
  283. return [
  284. mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
  285. mktime(23, 59, 59, date('m'), $yesterday, date('Y'))
  286. ];
  287. }
  288. /**
  289. * @param $uid
  290. * @return \think\response\Json
  291. * 下单
  292. */
  293. public function pay(){
  294. $order_no = $this->user->invite_code . order_no();
  295. $data['id'] = Request::param('id/d');
  296. $data['order_no'] = $order_no;
  297. $data['uid'] = $this->user->id;
  298. $data['member_miniapp_id'] = $this->member_miniapp_id;
  299. $unorder['miniapp_id'] = $this->member_miniapp_id;
  300. $unorder['name'] = '开通合伙人费用';
  301. $unorder['order_no'] = $order_no;
  302. $unorder['notify_url'] = api(4,'allwin/Partnershipnotify/workerNotify',$this->member_miniapp_id);
  303. $setting = model('AllwinConfig')->getConfig($this->member_miniapp_id);
  304. $unorder['total_fee'] = $setting->workers_on_amount ? $setting->workers_on_amount * 100 : 100;
  305. $data['price'] = $unorder['total_fee']/100;
  306. $result = Worker::createOrder($data);
  307. if ($setting->is_psp == 1) {
  308. $mid = MchId::getMch(0, $this->member_miniapp_id);
  309. $unorder['mchid'] = $mid->mchid;
  310. }
  311. $unorder['openid'] = $this->user->official_uid;
  312. $ispay = WechatPay::orderPay($unorder,true);
  313. if ($ispay['code'] == 0) {
  314. return json(['code' => 0, 'return_code' => 'FAIL', 'msg' => $ispay['msg']]);
  315. }
  316. return json(['code' => 200, 'msg' => '操作成功', 'data' => $ispay['data']]);
  317. }
  318. }