Spread.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\controller;
  12. use app\wap\model\user\SignPoster;
  13. use app\wap\model\user\SmsCode;
  14. use app\wap\model\special\SpecialSubject;
  15. use app\wap\model\store\StoreOrder;
  16. use app\wap\model\store\StorePink;
  17. use app\wap\model\user\User;
  18. use app\wap\model\user\UserBill;
  19. use app\wap\model\user\UserExtract;
  20. use app\wap\model\user\WechatUser;
  21. use service\CanvasService;
  22. use service\GroupDataService;
  23. use service\JsonService;
  24. use service\SystemConfigService;
  25. use service\UtilService;
  26. use service\WechatService;
  27. use think\Db;
  28. use think\response\Json;
  29. use think\Session;
  30. use app\wap\model\special\Special;
  31. use app\wap\model\merchant\Merchant;
  32. use think\Url;
  33. /**推广控制器
  34. * Class Spread
  35. * @package app\wap\controller
  36. */
  37. class Spread extends AuthController
  38. {
  39. /**推广佣金
  40. * @return mixed
  41. * @throws \think\Exception
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. * @throws \think\exception\DbException
  45. */
  46. public function spread()
  47. {
  48. $data['income'] = UserBill::whereTime('add_time', 'today')->where('uid', $this->uid)->where('category', 'now_money')
  49. ->where('type', 'in', ['brokerage'])->sum('number');
  50. $return = UserBill::whereTime('add_time', 'today')->where('uid', $this->uid)->where('category', 'now_money')
  51. ->where('type', 'in', ['brokerage_return'])->sum('number');
  52. $data['income'] = bcsub($data['income'], $return, 2);
  53. $uids = User::where('spread_uid', $this->uid)->column('uid');
  54. $orderIds = StoreOrder::whereTime('add_time', 'today')->where('uid', 'in', $uids)->where(['refund_status' => 0, 'paid' => 1])->where('pay_price', '>', 0)->field('order_id,pink_id')->select();
  55. $orderids = count($orderIds) ? $orderIds->toArray() : [];
  56. $order_count = 0;
  57. foreach ($orderids as $item) {
  58. if ($item['pink_id']) {
  59. $res = StorePink::where(['id' => $item['pink_id'], 'is_refund' => 0, 'status' => 3, 'order_id' => $item['order_id']])->count();
  60. if ($res) $order_count++;
  61. } else {
  62. $order_count++;
  63. }
  64. }
  65. $data['order_count'] = $order_count;
  66. $data['spread_count'] = User::whereTime('spread_time', 'today')->where('spread_uid', $this->uid)->count();
  67. $this->assign('data', $data);
  68. return $this->fetch();
  69. }
  70. /**
  71. * 提现页面
  72. * @return mixed
  73. */
  74. public function withdraw($mer_id = 0)
  75. {
  76. if (Session::has('form__token__')) Session::delete('form__token__');
  77. $token = md5(time() . $this->uid . $this->request->ip());
  78. Session::set('form__token__', $token);
  79. if ($mer_id) {
  80. $now_money = Merchant::where(['uid' => $this->uid])->value('now_money');
  81. $this->assign('brokerage_price', (float)$now_money);
  82. $this->assign('extract_min_money', (float)SystemConfigService::get('extract_price'));
  83. } else {
  84. $this->assign('brokerage_price', $this->userInfo['brokerage_price']);
  85. $this->assign('extract_min_money', (float)SystemConfigService::get('extract_min_money'));
  86. }
  87. $this->assign('token', $token);
  88. $this->assign('extract_bank', json_encode(GroupDataService::getData('extract_bank') ?: []));
  89. return $this->fetch();
  90. }
  91. /**
  92. * 保存提现信息发起企业付款到个人
  93. * @praem $number int 提现金额
  94. * @return json
  95. * */
  96. public function save_withdraw($token = '')
  97. {
  98. if (!$token) return JsonService::fail('token不能为空');
  99. if (!Session::has('form__token__')) return JsonService::fail('请刷新页面后重试!');
  100. if (Session::get('form__token__') != $token) return JsonService::fail('token验证失败,非法操作');
  101. Session::delete('form__token__');
  102. $extractInfo = UtilService::postMore([
  103. ['alipay_code', ''],
  104. ['extract_type', ''],
  105. ['business', 0],
  106. ['money', 0],
  107. ['name', ''],
  108. ['bankname', ''],
  109. ['cardnum', ''],
  110. ['weixin', ''],
  111. ]);
  112. if ($extractInfo['business']) {
  113. $res = UserExtract::userMerExtract($this->userInfo, $extractInfo);
  114. } else {
  115. $res = UserExtract::userExtract($this->userInfo, $extractInfo);
  116. }
  117. if ($res) {
  118. return JsonService::successful('申请成功');
  119. } else {
  120. return JsonService::fail(UserExtract::getErrorInfo());
  121. }
  122. }
  123. /**
  124. * 专题推广
  125. *
  126. * */
  127. public function special()
  128. {
  129. return $this->fetch();
  130. }
  131. /**
  132. * 获取年级列表
  133. *
  134. * */
  135. public function get_grade_list()
  136. {
  137. return JsonService::successful(SpecialSubject::wapSpecialCategoryAll(1));
  138. }
  139. /**
  140. * 获取每个年级下的专题并分页
  141. * */
  142. public function getSpecialSpread()
  143. {
  144. $where = UtilService::getMore([
  145. ['limit', 10],
  146. ['page', 1],
  147. ['grade_id', 0],
  148. ]);
  149. $is_member = isset($this->userInfo['level']) ? $this->userInfo['level'] : 0;
  150. return JsonService::successful(Special::getSpecialSpread($where, $is_member));
  151. }
  152. /**
  153. * 专题推广二维码
  154. * */
  155. public function poster_special($special_id = 0)
  156. {
  157. if (!$special_id) $this->failed('缺少专题id无法查看海报', Url::build('spread/special'));
  158. $special = Special::getSpecialInfo($special_id);
  159. if ($special === false) $this->failed(Special::getErrorInfo(), Url::build('spread/special'));
  160. if (!$special['poster_image']) $this->failed('您查看的海报不存在', Url::build('spread/special'));
  161. if ($special['is_light']) {
  162. $url = SystemConfigService::get('site_url') . Url::build('special/single_details') . '?id=' . $special['id'] . '&link_pay_uid=' . $this->uid . '&link_pay=1&spread_uid=' . $this->uid . '#link_pay';
  163. } else {
  164. $url = SystemConfigService::get('site_url') . Url::build('special/details') . '?id=' . $special['id'] . '&link_pay_uid=' . $this->uid . '&link_pay=1&spread_uid=' . $this->uid . '#link_pay';
  165. }
  166. $this->assign([
  167. 'url' => $url,
  168. 'poster_image' => $special['poster_image']
  169. ]);
  170. return $this->fetch();
  171. }
  172. /**
  173. * 我的推广人
  174. *
  175. * */
  176. public function my_promoter()
  177. {
  178. $uids = User::where('spread_uid', $this->uid)->group('uid')->column('uid');
  179. $data['one_spread_count'] = count($uids);
  180. if ($data['one_spread_count']) {
  181. $data['order_count'] = UserBill::where(['u.paid' => 1, 'u.is_del' => 0])->where('a.category', 'now_money')->where('a.type', 'in', ['brokerage'])->group('u.id')->where('a.uid', 'in', $uids)->join("__STORE_ORDER__ u", 'u.id=a.link_id')->alias('a')->count();
  182. } else {
  183. $data['order_count'] = 0;
  184. }
  185. $isPromoter = 0;
  186. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;
  187. if ($storeBrokerageStatu == 1) {
  188. if ($this->userInfo['is_promoter']) {
  189. $isPromoter = 1;
  190. }
  191. } else {
  192. $isPromoter = 1;
  193. }
  194. $this->assign('isPromoter', $isPromoter);
  195. $this->assign('data', $data);
  196. return $this->fetch();
  197. }
  198. /**
  199. * 佣金明细
  200. * @return mixed
  201. */
  202. public function commission()
  203. {
  204. $uids = User::where('spread_uid', $this->uid)->column('uid');
  205. $data['spread_two'] = 0;
  206. $data['spread_one'] = 0;
  207. if ($uids) {
  208. $data['spread_one'] = UserBill::where(['a.uid' => $this->uid, 'a.type' => 'brokerage', 'a.category' => 'now_money'])->alias('a')
  209. ->join('store_order o', 'o.id = a.link_id')
  210. ->whereIn('o.uid', $uids)->where('o.refund_status', 'eq', 0)
  211. ->where('a.link_id', 'neq', 0)->sum('a.number');
  212. $uids1 = User::where('spread_uid', 'in', $uids)->group('uid')->column('uid');
  213. if ($uids1) {
  214. $data['spread_two'] = UserBill::where(['a.uid' => $this->uid, 'a.type' => 'brokerage', 'a.category' => 'now_money'])->alias('a')
  215. ->join('store_order o', 'o.id = a.link_id')
  216. ->whereIn('o.uid', $uids1)->where('o.refund_status', 'eq', 0)
  217. ->where('a.link_id', 'neq', 0)->sum('a.number');
  218. }
  219. }
  220. $data['sum_spread'] = bcadd($data['spread_one'], $data['spread_two'], 2);
  221. $this->assign('data', $data);
  222. return $this->fetch();
  223. }
  224. /**
  225. * 推广明细
  226. * @param $type int 明细类型
  227. * @return html
  228. * */
  229. public function spread_detail($type = 0)
  230. {
  231. $this->assign('type', $type);
  232. return $this->fetch();
  233. }
  234. public function get_spread_list()
  235. {
  236. $where = UtilService::getMore([
  237. ['page', 1],
  238. ['limit', 10],
  239. ['type', 0]
  240. ]);
  241. return JsonService::successful(UserBill::getSpreadList($where, $this->uid));
  242. }
  243. public function get_withdrawal_list()
  244. {
  245. $where = UtilService::getMore([
  246. ['page', 1],
  247. ['limit', 10]
  248. ]);
  249. return JsonService::successful(UserBill::get_user_withdrawal_list($where, $this->uid));
  250. }
  251. /**
  252. * 推广海报
  253. *
  254. * */
  255. public function poster_spread()
  256. {
  257. $spread_poster_url = SystemConfigService::get('spread_poster_url');
  258. if (!$spread_poster_url) return $this->failed('海报不存在', Url::build('spread/special'));
  259. $url = SystemConfigService::get('site_url') . Url::build('Spread/become_promoter', ['spread_uid' => $this->uid]);
  260. $this->assign([
  261. 'url' => $url,
  262. 'spread_poster_url' => $spread_poster_url
  263. ]);
  264. return $this->fetch();
  265. }
  266. /**
  267. * 绑定推广人
  268. * @param int $pread_uid
  269. * @return json
  270. * */
  271. public function save_promoter($spread_uid = 0)
  272. {
  273. if (!$spread_uid) return JsonService::fail('缺少推广人UID');
  274. list($phone, $code) = UtilService::postMore([
  275. ['phone', ''],
  276. ['code', ''],
  277. ], $this->request, true);
  278. if (!$phone || !$code) return JsonService::fail('请输入登录账号');
  279. if (!$code) return JsonService::fail('请输入验证码');
  280. $code = md5('is_phone_code' . $code);
  281. if (!SmsCode::CheckCode($phone, $code)) return JsonService::fail('验证码验证失败');
  282. SmsCode::setCodeInvalid($phone, $code);
  283. $spreadUserInfo = User::getUserData($spread_uid);
  284. if (!$spreadUserInfo) return JsonService::fail('您想要绑定的上级不存在!');
  285. if ($this->userInfo['is_promoter']) return JsonService::fail('您已经成为推广人,无法绑定!');
  286. if ($spreadUserInfo['spread_uid'] == $this->uid) return JsonService::fail('您绑定的上级的推广人不能是您自己!');
  287. if ($this->uid == $spread_uid) return JsonService::fail('您不能绑定自己!');
  288. $data = ['phone' => $phone];
  289. $data = User::manageSpread($spread_uid, $data, $spreadUserInfo['is_promoter']);
  290. if ($data === false) return JsonService::fail(User::getErrorInfo());
  291. if (User::edit($data, $this->uid, 'uid'))
  292. return JsonService::successful('恭喜您,加入成功!');
  293. else
  294. return JsonService::fail('很抱歉加入失败!');
  295. }
  296. /**
  297. * 新增推广人注册
  298. *
  299. * */
  300. public function become_promoter($spread_uid = 0)
  301. {
  302. if (!$spread_uid) $this->failed('缺少推广人uid');
  303. $this->assign('spread_uid', $spread_uid);
  304. $this->assign('promoter_content', SystemConfigService::get('promoter_content'));
  305. $this->assign('home_logo', SystemConfigService::get('home_logo'));
  306. return $this->fetch();
  307. }
  308. /**
  309. * 推广人列表获取
  310. *
  311. * */
  312. public function spread_list()
  313. {
  314. $where = UtilService::getMore([
  315. ['page', 1],
  316. ['limit', 10],
  317. ['search', '']
  318. ]);
  319. return JsonService::successful(User::getSpreadList($where, $this->uid));
  320. }
  321. /**
  322. * 移除当前用下的推广人
  323. * @param int $uid 需要移除的用户id
  324. * */
  325. public function remove_spread($uid = 0)
  326. {
  327. if (!$uid) return JsonService::fail('缺少用户id');
  328. $res = User::where('uid', $uid)->update(['spread_uid' => 0, 'valid_time' => 0]);
  329. if ($res)
  330. return JsonService::successful('移除成功');
  331. else
  332. return JsonService::fail('移除失败');
  333. }
  334. }