StoreStaffController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\store;
  12. use app\Request;
  13. use app\services\order\OtherOrderServices;
  14. use app\services\order\store\BranchOrderServices;
  15. use app\services\store\SystemStoreStaffServices;
  16. use app\services\system\attachment\SystemAttachmentServices;
  17. use app\services\user\UserCardServices;
  18. use app\services\user\UserRechargeServices;
  19. use app\services\user\UserServices;
  20. use app\services\user\UserSpreadServices;
  21. use app\services\wechat\WechatCardServices;
  22. use crmeb\services\CacheService;
  23. use crmeb\services\UploadService;
  24. use crmeb\services\UtilService;
  25. use crmeb\services\wechat\MiniProgram;
  26. use crmeb\services\wechat\OfficialAccount;
  27. use GuzzleHttp\Psr7\Utils;
  28. use think\exception\ValidateException;
  29. /**
  30. * 店员
  31. * Class StoreStaffController
  32. * @package app\controller\api\store
  33. */
  34. class StoreStaffController
  35. {
  36. /**
  37. * @var SystemStoreStaffServices
  38. */
  39. protected $services;
  40. /**
  41. * @var int
  42. */
  43. protected $uid;
  44. /**
  45. * 门店店员信息
  46. * @var array
  47. */
  48. protected $staffInfo;
  49. /**
  50. * 门店id
  51. * @var int|mixed
  52. */
  53. protected $store_id;
  54. /**
  55. * 门店店员ID
  56. * @var int|mixed
  57. */
  58. protected $staff_id;
  59. /**
  60. * 构造方法
  61. * StoreStaffController constructor.
  62. * @param SystemStoreStaffServices $services
  63. */
  64. public function __construct(SystemStoreStaffServices $services, Request $request)
  65. {
  66. $this->services = $services;
  67. $this->uid = (int)$request->uid();
  68. $this->staffInfo = $services->getStaffInfoByUid($this->uid)->toArray();
  69. $this->store_id = (int)$this->staffInfo['store_id'] ?? 0;
  70. $this->staff_id = (int)$this->staffInfo['id'] ?? 0;
  71. }
  72. /**
  73. * 获取店员信息
  74. * @return mixed
  75. */
  76. public function info()
  77. {
  78. return app('json')->success($this->staffInfo);
  79. }
  80. /**
  81. * 店员|门店数据统计
  82. * @param Request $request
  83. * @return mixed
  84. */
  85. public function statistics(Request $request)
  86. {
  87. [$is_manager, $time] = $request->getMore([
  88. ['is_manager', 0],
  89. ['data', '', '', 'time'],
  90. ], true);
  91. if (!$is_manager || !$this->staffInfo['is_manager']) {
  92. $is_manager = 0;
  93. }
  94. $store_id = $this->store_id;
  95. $staff_id = $is_manager ? 0 : $this->staff_id;
  96. $data = $this->services->getStoreData($this->uid, $store_id, $staff_id, $time);
  97. return app('json')->successful($data);
  98. }
  99. /**
  100. * 增长率
  101. * @param $left
  102. * @param $right
  103. * @return int|string
  104. */
  105. public function growth($nowValue, $lastValue)
  106. {
  107. if ($lastValue == 0 && $nowValue == 0) return [0, 0, 0];
  108. if ($lastValue == 0) return [$nowValue, round(bcmul(bcdiv($nowValue, 1, 4), 100, 2), 2), 1];
  109. if ($nowValue == 0) return [$lastValue, round(bcmul(bcdiv($lastValue, 1, 4), 100, 2), 2), 0];
  110. $differ = bcsub($nowValue, $lastValue, 2);
  111. $increase_time_status = (float)$differ >= 0 ? 1 : 0;
  112. return [$differ, bcmul(bcdiv($differ, $lastValue, 4), 100, 2), $increase_time_status];
  113. }
  114. /**
  115. * 店员|门店统计详情页列表
  116. * @param $type
  117. * @param Request $request
  118. * @return mixed
  119. */
  120. public function data($type, Request $request)
  121. {
  122. [$is_manager, $start, $stop] = $request->getMore([
  123. ['is_manager', 0],
  124. ['start', strtotime(date('Y-m'))],
  125. ['stop', time()]
  126. ], true);
  127. if (!$start || !$stop) {
  128. return app('json')->fail('请重新选择时间');
  129. }
  130. if (!$is_manager || !$this->staffInfo['is_manager']) {
  131. $is_manager = 0;
  132. }
  133. $staff_id = $is_manager ? 0 : $this->staff_id;
  134. [$start, $stop, $front_start] = getFrontTime((int)$start, (int)$stop);
  135. $front_stop = (int)bcsub((string)$start, '1');
  136. $data = $list = [];
  137. switch ($type) {
  138. case 1:
  139. case 2:
  140. case 3:
  141. case 4:
  142. case 5:
  143. /** @var BranchOrderServices $order */
  144. $order = app()->make(BranchOrderServices::class);
  145. [$data, $list] = $order->time($this->store_id, $staff_id, $type, [$start, $stop, $front_start, $front_stop]);
  146. break;
  147. case 6://付费会员
  148. /** @var OtherOrderServices $otherOrder */
  149. $otherOrder = app()->make(OtherOrderServices::class);
  150. [$data, $list] = $otherOrder->time($this->store_id, $staff_id, [$start, $stop, $front_start, $front_stop]);
  151. break;
  152. case 7://充值
  153. /** @var UserRechargeServices $userRecharge */
  154. $userRecharge = app()->make(UserRechargeServices::class);
  155. [$data, $list] = $userRecharge->time($this->store_id, $staff_id, [$start, $stop, $front_start, $front_stop]);
  156. break;
  157. case 8://推广用户
  158. /** @var UserSpreadServices $userSpread */
  159. $userSpread = app()->make(UserSpreadServices::class);
  160. [$data, $list] = $userSpread->time($this->store_id, $staff_id, [$start, $stop, $front_start, $front_stop]);
  161. break;
  162. case 9://激活会员卡
  163. /** @var UserCardServices $userCard */
  164. $userCard = app()->make(UserCardServices::class);
  165. [$data, $list] = $userCard->time($this->store_id, $staff_id, [$start, $stop, $front_start, $front_stop]);
  166. break;
  167. default:
  168. return app('json')->fail('没有此类型');
  169. }
  170. $now = $front = $differ = $growth = $increase_time_status = 0;
  171. if ($data) {
  172. [$now, $front] = $data;
  173. [$differ, $growth, $increase_time_status] = $this->growth($now, $front);
  174. }
  175. $data = compact('now', 'front', 'differ', 'growth', 'increase_time_status');
  176. return app('json')->success(compact('data', 'list'));
  177. }
  178. /**
  179. * 商品分享二维码 推广员
  180. * @param Request $request
  181. * @param $id
  182. * @return mixed
  183. */
  184. public function code(Request $request, WechatCardServices $cardServices)
  185. {
  186. $wechatCard = $cardServices->get(['card_type' => 'member_card', 'status' => 1, 'is_del' => 0]);
  187. $uid = (int)$request->uid();
  188. if ($wechatCard && !$request->isApp()) {
  189. $key = $wechatCard['card_id'] . '_cart_' . $uid;
  190. $data = CacheService::get($key);
  191. if (!$data) {
  192. $result = OfficialAccount::getCardQRCode($wechatCard['card_id'], $uid);
  193. $data = [
  194. 'url' => $result['url'] ?? '',
  195. 'show_qrcode_url' => $result['show_qrcode_url'] ?? ''
  196. ];
  197. CacheService::set($key, $data, 1500);
  198. }
  199. } else {
  200. $site_url = sys_config('site_url');
  201. $valueData = 'spread=' . $uid . '&spid=' . $uid;
  202. $url = $site_url ? $site_url . '?'.$valueData : '';
  203. if (request()->isRoutine()) {
  204. try {
  205. //小程序
  206. $name = $uid . '_store_share_routine.jpg';
  207. /** @var SystemAttachmentServices $systemAttachmentServices */
  208. $systemAttachmentServices = app()->make(SystemAttachmentServices::class);
  209. $imageInfo = $systemAttachmentServices->getInfo(['name' => $name]);
  210. if (!$imageInfo) {
  211. $res = MiniProgram::appCodeUnlimit($valueData, 'pages/index/index', 280);
  212. if (!$res) throw new ValidateException('二维码生成失败');
  213. $uploadType = (int)sys_config('upload_type', 1);
  214. $upload = UploadService::init($uploadType);
  215. $res = (string)Utils::streamFor($res);
  216. $res = $upload->to('routine/store/code')->validate()->stream($res, $name);
  217. if ($res === false) {
  218. throw new ValidateException($upload->getError());
  219. }
  220. $imageInfo = $upload->getUploadInfo();
  221. $imageInfo['image_type'] = $uploadType;
  222. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($site_url . $imageInfo['dir']);
  223. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  224. if (!$remoteImage['status']) throw new ValidateException($remoteImage['msg']);
  225. $systemAttachmentServices->save([
  226. 'name' => $imageInfo['name'],
  227. 'att_dir' => $imageInfo['dir'],
  228. 'satt_dir' => $imageInfo['thumb_path'],
  229. 'att_size' => $imageInfo['size'],
  230. 'att_type' => $imageInfo['type'],
  231. 'image_type' => $imageInfo['image_type'],
  232. 'module_type' => 2,
  233. 'time' => time(),
  234. 'pid' => 1,
  235. 'type' => 1
  236. ]);
  237. $url = $imageInfo['dir'];
  238. } else {
  239. $url = $imageInfo['att_dir'];
  240. }
  241. if ($imageInfo['image_type'] == 1)
  242. $url = $site_url . $url;
  243. } catch (\Throwable $e) {
  244. }
  245. }
  246. $data = [
  247. 'url' => $url,
  248. 'show_qrcode_url' => $url
  249. ];
  250. }
  251. return app('json')->success($data);
  252. }
  253. }