CardController.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. <?php
  2. namespace addons\BusinessCard\api\modules\v1\controllers;
  3. use addons\BusinessCard\common\enums\CardEnums;
  4. use addons\BusinessCard\common\logic\Statistics;
  5. use addons\BusinessCard\common\logic\WechatNotice;
  6. use addons\BusinessCard\common\models\BusinessCard;
  7. use addons\BusinessCard\common\models\BusinessCardCustomerLog;
  8. use addons\BusinessCard\common\models\BusinessCardGoodsStatistics;
  9. use addons\BusinessCard\common\models\BusinessCardPosition;
  10. use addons\BusinessCard\common\models\BusinessCardStatistics;
  11. use addons\BusinessCard\common\models\BusinessCardTag;
  12. use addons\BusinessCard\common\models\BusinessCardUser;
  13. use addons\BusinessCard\common\models\BusinessCardUserTag;
  14. use addons\BusinessCard\common\services\TagService;
  15. use api\controllers\OnAuthController;
  16. use common\enums\RabbitMqEnum;
  17. use common\enums\StatusEnum;
  18. use common\models\common\PosterCode;
  19. use common\models\user\User;
  20. use Exception;
  21. use Yii;
  22. use yii\helpers\Json;
  23. /**
  24. * 名片控制器
  25. */
  26. class CardController extends OnAuthController
  27. {
  28. public $modelClass = '';
  29. public $card = '';
  30. protected $authOptional = ['card-list', 'config', 'card-info-by-share'];
  31. /**
  32. * 提示创建名片
  33. */
  34. public function beforeAction($action) {
  35. parent::beforeAction($action);
  36. $white_list = [
  37. 'browsing-history',
  38. 'to-create-card',
  39. 'create-card',
  40. 'card-list',
  41. 'card-info',
  42. 'valid-card-token',
  43. 'like',
  44. 'config',
  45. 'is-created',
  46. 'card-info-by-share'
  47. ];
  48. if (in_array($action->id, $white_list)) return $action;
  49. $card = BusinessCard::getOne([['=', 'user_id', $this->user_id], ['=', 'status', StatusEnum::ENABLED]], true);
  50. $setting = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic');
  51. if (empty($card)) {
  52. Yii::$app->controller->asJson(['code' => 3, 'msg' => '请先创建名片', 'data' => ['is_card_token' => $setting['is_card_token'] ?? 0, 'add_command_show_title' => $setting['add_command_show_title'] ?? '添加命令']])->send();
  53. return;
  54. }
  55. $this->card = $card;
  56. return $action;
  57. }
  58. /**
  59. * 进入创建名片
  60. *
  61. * @author hpei
  62. * @return array
  63. */
  64. public function actionToCreateCard() {
  65. if (Yii::$app->request->isGet) {
  66. $data = [];
  67. $setting = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic');
  68. $data['images'] = !empty($setting['images']) ? json_decode($setting['images']) : [];
  69. $data['address'] = $setting['address'] ?? '';
  70. $data['company'] = $setting['company'] ?? '';
  71. $data['video_url'] = $setting['video_url'] ?? '';
  72. $data['video_cover_pic'] = $setting['video_cover_pic'] ?? '';
  73. $data['share_pic'] = $setting['share_pic'] ?? '';
  74. $data['lng'] = $setting['lng'] ?? '';
  75. $data['lat'] = $setting['lat'] ?? '';
  76. $data['department'] = BusinessCardPosition::departmentPosition($this->mall_id);
  77. $data['department_show_label'] = $setting['department_show_label'] ?? '部门';
  78. $data['company_show_label'] = $setting['company_show_label'] ?? '公司';
  79. $data['is_allow_edit_card_info'] = $setting['is_allow_edit_card_info'] ?? 0;
  80. $data['is_show_update_division'] = $setting['is_show_update_division'] ?? 0;
  81. $data['default_department_id'] = BusinessCardPosition::find()
  82. ->where(['mall_id' => $this->mall_id, 'is_default_position' => StatusEnum::ENABLED])
  83. ->andWhere(['>', 'parent_id', 0])
  84. ->select('id')
  85. ->scalar();
  86. $data['is_show_networking'] = $setting['is_show_networking'] ?? 0;
  87. $data['collect_wechat'] = !empty($setting['collect_wechat']) ? $setting['collect_wechat'] : 0;
  88. $data['collect_info'] = !empty($setting['collect_info']) ? $setting['collect_info']: 0;
  89. $data['is_required'] = $setting['is_required'] ?? 2;
  90. $data['supply_resources'] = [];
  91. $data['need_resources'] = [];
  92. $data['education'] = [];
  93. $data['industry'] = [];
  94. $data['identity_tags'] = [];
  95. $data['zodiac'] = CardEnums::getZodiac();
  96. $data['sign'] = $setting['sign'] ?? '';
  97. //获取商城标签数据
  98. $tag_info = BusinessCardTag::lists([
  99. 'where' => [
  100. ['mall_id' => $this->mall_id],
  101. ['status' => StatusEnum::ENABLED],
  102. ],
  103. 'select' => 'id as tid,name,type',
  104. 'order' => 'id asc',
  105. ]);
  106. if (!empty($tag_info)) {
  107. foreach ($tag_info as $key => $value) {
  108. switch ($value['type']) {
  109. case 1:
  110. $data['supply_resources'][] = $value;
  111. break;
  112. case 2:
  113. $data['need_resources'][] = $value;
  114. break;
  115. case 3:
  116. $data['education'][] = $value;
  117. break;
  118. case 4:
  119. $data['industry'][] = $value;
  120. break;
  121. case 5:
  122. $data['identity_tags'][] = $value;
  123. break;
  124. }
  125. }
  126. }
  127. if (BusinessCard::find()->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])->count()) {
  128. return $this->error('已存在卡片', $data);
  129. }
  130. return $this->success('操作成功', $data);
  131. }
  132. }
  133. /**
  134. * 创建名片&编辑名片
  135. * @author hpei
  136. * @param string $avatar_url 头像
  137. * @param int $mobile 电话
  138. * @param string $nickname 昵称
  139. * @param int $department_id 部门ID
  140. * @param int $position_id 职位ID
  141. * @param string $phone 座机
  142. * @param string $email 邮箱
  143. * @param string $company 公司
  144. * @param string $address 地址
  145. * @param string $sign 签名
  146. * @param string $video 视频
  147. * @param array $tags 标签
  148. * @param array $images 图文
  149. * @return array|mixed
  150. */
  151. public function actionCreateCard() {
  152. if (Yii::$app->request->isPost) {
  153. $params = Yii::$app->request->post();
  154. $res = Yii::$app->services->validate->validate($params, [
  155. [['mobile', 'nickname', 'department_id'], 'required'],
  156. [['avatar_url', 'phone', 'email', 'company', 'address', 'sign', 'video', 'tags', 'images', 'id', 'card_token',
  157. 'video_cover_pic', 'share_pic', 'lng', 'lat', 'wechat', 'phone_key_text', 'position_id', 'zodiac',
  158. 'wechat_code', 'province_id', 'city_id', 'supply_resources', 'need_resources', 'education', 'industry', 'identity_tags', 'delete_tag'], 'safe'],
  159. [['avatar_url', 'phone', 'email', 'company', 'address', 'sign', 'video', 'tags', 'images', 'video_cover_pic', 'active_city'], 'default', 'value' => ''],
  160. [['id', 'sex', 'marry_status', 'education_id'], 'default', 'value' => 0]
  161. ]);
  162. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  163. try {
  164. // 判断当前用户是否有申请过卡片 - 非编辑状态下才需要判断
  165. if (empty($params['id'])) {
  166. $card = BusinessCard::getOne([
  167. ['mall_id' => $this->mall_id],
  168. ['user_id' => $this->user_id],
  169. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  170. ]);
  171. if (!empty($card)) return $this->error('不能多次创建');
  172. }
  173. // 判断密令 - 编辑的时候不需要密令
  174. if (empty($params['id'])) {
  175. $card_token = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic.card_token');
  176. $is_card_token = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic.is_card_token');
  177. if (!empty($card_token) && (string)$params['card_token'] != $card_token && !empty($is_card_token)) throw new \Exception("名片密令错误");
  178. } else {
  179. /**
  180. * @var $cardUser BusinessCardUser
  181. */
  182. $cardUser = BusinessCardUser::getOne([
  183. ['mall_id' => $this->mall_id],
  184. ['user_id' => $this->user_id],
  185. ['status' => StatusEnum::ENABLED]
  186. ]);
  187. $isAllowEditCardInfo = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic.is_allow_edit_card_info');
  188. if (empty($isAllowEditCardInfo) && $cardUser->role_type == 3) { // 如果是员工 那么不能修改 图文信息
  189. unset($params['images'], $params['video_cover_pic'], $params['video'], $params['share_pic']);
  190. }
  191. }
  192. // 是否已经没有了职位,只有角色?
  193. $res1 = BusinessCardPosition::checkPosition(['mall_id' => $this->mall_id, 'id' => $params['department_id'], 'isParent' => true]);
  194. // $res2 = BusinessCardPosition::checkPosition(['mall_id' => $this->mall_id, 'id' => $params['position_id'], 'isParent' => false]);
  195. // || $res2 === false
  196. if ($res1 === false) {
  197. return $this->error('部门不存在');
  198. }
  199. $params['mall_id'] = $this->mall_id;
  200. $params['user_id'] = $this->user_id;
  201. //修改管理判断是否传了职位id过来没有传默认一个
  202. if (empty($params['position_id'])) {
  203. // 默认一个职位
  204. $params['position_id'] = BusinessCardPosition::find()->where(['mall_id' => $params['mall_id']])
  205. ->andWhere(['parent_id' => $params['department_id']])
  206. ->andWhere(['status' => StatusEnum::ENABLED])
  207. ->orderBy('id asc')->select('id')->scalar() ?? 0;
  208. // 判断部门下是否存在职位不存在提醒
  209. if (empty($params['position_id'])) {
  210. $department_name = BusinessCardPosition::find()->where(['id' => $params['department_id']])
  211. ->select('name')->scalar();
  212. return $this->error("请在部门 {$department_name} 下面添加至少一个职位");
  213. }
  214. }
  215. $setting = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic');
  216. if ($setting['is_show_networking'] == 1) {
  217. //开启收集微信二维码按钮,微信二维码必填
  218. if ($setting['collect_wechat'] == 1) {
  219. if (empty($params['wechat_code'])) return $this->error('请上传微信二维码');
  220. }
  221. if ($setting['collect_info'] == 1) {
  222. //开启了必填按钮,则需要校验数据是否为空
  223. if ($setting['is_required'] == 1) {
  224. if (empty($params['sex'])) return $this->error('请选择性别');
  225. if (empty($params['marry_status'])) return $this->error('请选择婚姻状态');
  226. if (empty($params['active_city'])) return $this->error('请选择活跃城市');
  227. if (empty($params['industry'])) return $this->error('请选择从事行业');
  228. if (empty($params['identity_tags'])) return $this->error('请选择身份标签');
  229. if (count($params['identity_tags']) > 1) return $this->error('最多只能选择1个身份标签');
  230. if (count($params['industry']) > 3) return $this->error('最多只能选择3个行业');
  231. }
  232. }
  233. }
  234. if (is_array($params['images'])) $params['images'] = implode(',', $params['images']);
  235. if (is_array($params['tags'])) $params['tags'] = implode(',', $params['tags']);
  236. if($params['id'] == 'undefined') $params['id'] = '';
  237. $res1 = BusinessCard::setData($params);
  238. if (!$res1) return $this->error(BusinessCard::getStaticError());
  239. $params['cid'] = $params['id'];
  240. if (empty($params['id'])) $params['cid'] = $res1->id;
  241. //添加用户标签
  242. $res3 = (new TagService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id]))->setUserTag($params);
  243. if(!$params['id']){
  244. $res2 = BusinessCardStatistics::setData(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'card_num' => 1]);
  245. }
  246. return $this->success('操作成功', $res1->id);
  247. } catch (\Exception $e) {
  248. return $this->error($e->getMessage());
  249. }
  250. }
  251. }
  252. /**
  253. * 名片预览
  254. * @param int $card_id
  255. * @param string $type show:预览 edit:编辑
  256. * @return array
  257. * @throws \Exception
  258. * @author hpei
  259. */
  260. public function actionCardInfo() {
  261. if (Yii::$app->request->isGet) {
  262. $params = Yii::$app->request->get();
  263. // 检查提交的参数
  264. $res = Yii::$app->services->validate->validate($params,[
  265. [['scene','card_id','share_id','type'], 'safe'],
  266. ]);
  267. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  268. $type = Yii::$app->request->get('type', 'show');
  269. $card_id = $params['card_id'] ?? null;
  270. $share_id = $params['share_id'] ?? null;
  271. // 检查提交的参数
  272. if(!empty($params['scene'])){
  273. $where[] = ['=', 'mall_id', $this->mall_id];
  274. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  275. $where[] = ['=', 'code', $params['scene']];
  276. $qrode = PosterCode::getOne($where,true);
  277. if($qrode){
  278. $card_id = $qrode['item_id'];
  279. $share_id = $qrode['user_id'];
  280. }
  281. }
  282. $setting = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic');
  283. if (!empty($card_id)) { // 如果 没有拿到分享人的 那么就拿 card_id的
  284. $where = [['=', 'id', $card_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
  285. $card_info = BusinessCard::getCardInfo($where);
  286. }
  287. if(!empty($share_id) && empty($card_info)){ // 先拿分享人的
  288. $where = [['=', 'user_id', $share_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
  289. $card_info = BusinessCard::getCardInfo($where);
  290. }
  291. if (empty($card_info)){ // 如果没有 那就拿当前用户的
  292. $where = [['=', 'user_id', $this->user_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
  293. $is_how_parent_card = boolval($setting['is_show_parent_card']); // 只有查看自己的名片的时候才需要
  294. $card_info = BusinessCard::getCardInfo($where);
  295. }
  296. // V3需求,如果自己没有名片则取出上级名片
  297. $is_parent_card = StatusEnum::DISABLED;
  298. if (empty($card_info) && !empty($is_how_parent_card) && $params['type'] != 'edit') { // 如果还没有 就拿上级的
  299. // 取之前的浏览过的名片
  300. $history_card_id = BusinessCardCustomerLog::find()->where(['mall_id' => $this->mall_id])
  301. ->andWhere(['user_id' => $this->user_id])
  302. ->andWhere(['log_type' => CardEnums::LOG_TYPE_CARD])
  303. ->andWhere(['status' => StatusEnum::ENABLED])
  304. ->orderBy('id desc')
  305. ->select('item_id')->scalar();
  306. if (!empty($history_card_id)) {
  307. $where = [['=', 'id', $history_card_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
  308. $card_info = BusinessCard::getCardInfo($where);
  309. }
  310. if (empty($card_info)) {
  311. // 取出上级的名片
  312. $parent_id = User::find()->where(['id' => $this->user_id])->select('parent_id')->scalar();
  313. if (!empty($parent_id)) {
  314. $where = [['=', 'user_id', $parent_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
  315. $card_info = BusinessCard::getCardInfo($where);
  316. }
  317. }
  318. if (!empty($card_info)) {
  319. $is_parent_card = StatusEnum::ENABLED;
  320. $params['type'] = 'show';
  321. $card_id = $card_info['id'];
  322. }
  323. }
  324. $add_command_show_title = $setting['add_command_show_title'] ?? '添加命令';
  325. //var_dump($card_info);
  326. if (empty($card_info)&&$share_id) {
  327. Yii::$app->controller->asJson(['code' => 3, 'msg' => '名片不存在,请先创建名片', 'data' => ['is_card_token' => $setting['is_card_token'] ?? 0, 'add_command_show_title' => $add_command_show_title]])->send();
  328. return;
  329. }else if(empty($card_info)&&!$card_id&&$params['type']!='edit'){
  330. Yii::$app->controller->asJson(['code' => 4, 'msg' => '请先创建名片', 'data' => ['is_card_token' => $setting['is_card_token'] ?? 0, 'add_command_show_title' => $add_command_show_title]])->send();
  331. return;
  332. }
  333. if (in_array($type, ['show', 'edit'])) {
  334. $card_info['goods_list'] = empty($setting) ? [] : json_decode($setting['goods_list'], true);
  335. $card_info['logo'] = empty($setting) ? '' : $setting['logo'];
  336. $card_info['style'] = $setting['style'] ?? '';
  337. $card_info['video_url'] = !empty($card_info['video']) ? $card_info['video'] : ($setting['video_url'] ?? '');;
  338. $card_info['video_cover_pic'] = !empty($card_info['video_cover_pic']) ? $card_info['video_cover_pic'] : ($setting['video_cover_pic'] ?? '');
  339. $card_info['share_title'] = $setting['share_title'] ?? '';
  340. $card_info['share_desc'] = $setting['share_desc'] ?? '';
  341. $card_info['share_pic'] = !empty($card_info['share_pic']) ? $card_info['share_pic'] : ($setting['share_pic'] ?? '');
  342. $card_info['list_card_show'] = !empty($setting['list_card_show']) ? Json::decode($setting['list_card_show']) : [];
  343. // $card_info['like_num'] = $card_info['like_num'];//靠谱数
  344. // $card_info['browse_num'] = $card_info['browse_num'];//浏览数
  345. $card_info['lately_browse'] = BusinessCardCustomerLog::latelyBrowse($card_id);//最近浏览
  346. $card_user = BusinessCardUser::getOne([['=', 'user_id', $this->user_id]]);
  347. $card_info['user_role'] = empty($card_user) ? 0 : $card_user->role_type;
  348. $where = [['=', 'status', StatusEnum::ENABLED], ['=', 'user_id', $this->user_id], ['=', 'log_type', CardEnums::LOG_TYPE_LIKE], ['=', 'item_id', $card_info['id']]];
  349. $card_info['is_like'] = BusinessCardCustomerLog::count(['where' => $where]) > 0;
  350. $card_info['is_show_alert'] = !empty($setting['is_show_alert']) ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  351. $card_info['is_enable_save_phone'] = !empty($setting['is_enable_save_phone']) ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  352. $card_info['is_hidden_popularity'] = !empty($setting['is_hidden_popularity']) ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  353. $card_info['is_show_mall_nav'] = !empty($setting['is_show_mall_nav']) ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  354. $card_info['is_show_update_division'] = !empty($setting['is_show_update_division']) ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  355. $card_info['is_parent_card'] = $is_parent_card;
  356. $card_info['parent_id'] = $parent_id ?? 0;
  357. $card_info['collect_wechat'] = !empty($setting['collect_wechat']) ? $setting['collect_wechat'] : 0;
  358. $card_info['collect_info'] = !empty($setting['collect_info']) ? $setting['collect_info'] : 0;
  359. $card_info['sign'] = !empty($card_info['sign']) ? $card_info['sign'] : $setting['sign'];
  360. if ($type == 'show') {
  361. if ($card_info['user_id'] != $this->user_id) {
  362. Statistics::handler(['item_id' => $card_id, 'user_id' => $this->user_id,
  363. 'mall_id' => $this->mall_id, 'log_type' => CardEnums::LOG_TYPE_CARD]);
  364. if ($this->user_id) {
  365. WechatNotice::handler(['to_user_id' => $card_info['user_id'], 'user_id' => $this->user_id,
  366. 'mall_id' => $this->mall_id, 'log_type' => CardEnums::LOG_TYPE_CARD]);
  367. }
  368. }
  369. if ($share_id) Statistics::handler(['item_id' => $card_id, 'user_id' => $share_id,
  370. 'mall_id' => $this->mall_id, 'log_type' => CardEnums::LOG_TYPE_SHARE]);
  371. if (empty($is_parent_card)) { // 如果为非上级名片,那么就需要绑定关系
  372. //绑定关系
  373. $pid = Yii::$app->request->get('pid', 0);
  374. $data = ['parent_id' => $pid, 'user_id' => $this->user_id, 'mall_id' => $this->mall_id,'card_id' =>$card_id];
  375. if ($pid != 0 && $pid == $card_info['user_id']){
  376. Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE,
  377. $data, Statistics::class, 'band_parent_handle');
  378. }
  379. }
  380. // if ($card_info['browse_num'] > 0) {
  381. // 需要返回流量数据
  382. $card_info['browse_list'] = BusinessCardCustomerLog::lists([
  383. 'where' => [
  384. ['mall_id' => $this->mall_id],
  385. ['item_id' => $card_info['id']],
  386. ['log_type' => CardEnums::LOG_TYPE_CARD],
  387. ['status' => StatusEnum::ENABLED],
  388. ],
  389. 'limit' => 5,
  390. 'group' => 'user_id',
  391. 'with' => ['user'],
  392. 'order' => 'id desc',
  393. 'select'=> 'id, user_id, created_at'
  394. ]);
  395. // }
  396. $card_info['config'] = [
  397. 'company_name_show_label' => $setting['company_name_show_label'] ?? '公司名称',
  398. 'company_address_show_label' => $setting['company_address_show_label'] ?? '公司地址',
  399. 'add_command_show_title' => $add_command_show_title,
  400. 'is_card_token' => $setting['is_card_token'] ?? 0,
  401. ];
  402. }
  403. $card_info['supply_resources'] = []; //提供的人脉
  404. $card_info['need_resources'] = [];//需要的人脉
  405. $card_info['education'] = [];//学历
  406. $card_info['industry'] = [];//从事的行业
  407. $card_info['identity_tags'] = [];//身份标签
  408. //获取用户标签
  409. $tag_info = BusinessCardUserTag::lists([
  410. 'where' => [
  411. ['mall_id' => $this->mall_id],
  412. ['user_id' => $card_info['user_id']],
  413. ['cid' => $card_info['id']],
  414. ['status' => StatusEnum::ENABLED],
  415. ],
  416. 'with' => ['tag'],
  417. ]);
  418. if (!empty($tag_info)) {
  419. foreach ($tag_info as $key => $value) {
  420. switch ($value['tag']['type']) {
  421. case 1:
  422. $value['name'] = $value['tag']['name'];
  423. unset($value['tag']);
  424. $card_info['supply_resources'][] = $value;
  425. break;
  426. case 2:
  427. $value['name'] = $value['tag']['name'];
  428. unset($value['tag']);
  429. $card_info['need_resources'][] = $value;
  430. break;
  431. case 3:
  432. $value['name'] = $value['tag']['name'];
  433. unset($value['tag']);
  434. $card_info['education'][] = $value;
  435. break;
  436. case 4:
  437. $value['name'] = $value['tag']['name'];
  438. unset($value['tag']);
  439. $card_info['industry'][] = $value;
  440. break;
  441. case 5:
  442. $value['name'] = $value['tag']['name'];
  443. unset($value['tag']);
  444. $card_info['identity_tags'][] = $value;
  445. break;
  446. }
  447. }
  448. }
  449. }
  450. // 不需要浏览名片列表
  451. // if ($card_info['user_id'] != $this->user_id) Statistics::handler(['item_id' => $card_id,'card_user_id' => $card_info['user_id'],
  452. // 'user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'log_type' => CardEnums::LOG_TYPE_INDEX]);
  453. return $this->success('操作成功', $card_info);
  454. }
  455. }
  456. /**
  457. * 名片预览
  458. * @param int $card_id
  459. * @param string $type show:预览 edit:编辑
  460. * @return array
  461. * @throws \Exception
  462. * @author hpei
  463. */
  464. public function actionCardInfoByShare() {
  465. if (Yii::$app->request->isGet) {
  466. $params = Yii::$app->request->get();
  467. // 检查提交的参数
  468. $res = Yii::$app->services->validate->validate($params,[
  469. [['scene','card_id','share_id','type'], 'safe'],
  470. ]);
  471. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  472. $type = Yii::$app->request->get('type', 'show');
  473. $card_id = $params['card_id'] ?? null;
  474. $share_id = $params['share_id'] ?? null;
  475. // 检查提交的参数
  476. if(!empty($params['scene'])){
  477. $where[] = ['=', 'mall_id', $this->mall_id];
  478. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  479. $where[] = ['=', 'code', $params['scene']];
  480. $qrode = PosterCode::getOne($where,true);
  481. if($qrode){
  482. $card_id = $qrode['item_id'];
  483. $share_id = $qrode['user_id'];
  484. }
  485. }
  486. $setting = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic');
  487. if (!empty($card_id)) { // 如果 没有拿到分享人的 那么就拿 card_id的
  488. $where = [['=', 'id', $card_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
  489. $card_info = BusinessCard::getCardInfo($where);
  490. }
  491. if(!empty($share_id) && empty($card_info)){ // 先拿分享人的
  492. $where = [['=', 'user_id', $share_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
  493. $card_info = BusinessCard::getCardInfo($where);
  494. }
  495. if (empty($card_info)){
  496. return $this->error('数据不存在');
  497. }
  498. $add_command_show_title = $setting['add_command_show_title'] ?? '添加命令';
  499. if (in_array($type, ['show'])) {
  500. $card_info['goods_list'] = empty($setting) ? [] : json_decode($setting['goods_list'], true);
  501. $card_info['logo'] = empty($setting) ? '' : $setting['logo'];
  502. $card_info['style'] = $setting['style'] ?? '';
  503. $card_info['video_url'] = !empty($card_info['video']) ? $card_info['video'] : ($setting['video_url'] ?? '');;
  504. $card_info['video_cover_pic'] = !empty($card_info['video_cover_pic']) ? $card_info['video_cover_pic'] : ($setting['video_cover_pic'] ?? '');
  505. $card_info['share_title'] = $setting['share_title'] ?? '';
  506. $card_info['share_desc'] = $setting['share_desc'] ?? '';
  507. $card_info['share_pic'] = !empty($card_info['share_pic']) ? $card_info['share_pic'] : ($setting['share_pic'] ?? '');
  508. $card_info['list_card_show'] = !empty($setting['list_card_show']) ? Json::decode($setting['list_card_show']) : [];
  509. // $card_info['like_num'] = $card_info['like_num'];//靠谱数
  510. // $card_info['browse_num'] = $card_info['browse_num'];//浏览数
  511. $card_info['lately_browse'] = BusinessCardCustomerLog::latelyBrowse($card_id);//最近浏览
  512. $card_info['user_role'] = 0;
  513. $card_info['is_like'] = false;
  514. $card_info['is_show_alert'] = !empty($setting['is_show_alert']) ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  515. $card_info['is_enable_save_phone'] = !empty($setting['is_enable_save_phone']) ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  516. $card_info['is_hidden_popularity'] = !empty($setting['is_hidden_popularity']) ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  517. $card_info['is_show_mall_nav'] = !empty($setting['is_show_mall_nav']) ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  518. $card_info['is_show_update_division'] = !empty($setting['is_show_update_division']) ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  519. $card_info['is_parent_card'] = 1;
  520. $card_info['parent_id'] = $share_id;
  521. if (empty($card_info['sign'])) $card_info['sign'] = $setting['sign'];
  522. if ($share_id) Statistics::handler(['item_id' => $card_id, 'user_id' => $share_id,
  523. 'mall_id' => $this->mall_id, 'log_type' => CardEnums::LOG_TYPE_SHARE]);
  524. // 需要返回流量数据
  525. $card_info['browse_list'] = BusinessCardCustomerLog::lists([
  526. 'where' => [
  527. ['mall_id' => $this->mall_id],
  528. ['item_id' => $card_info['id']],
  529. ['log_type' => CardEnums::LOG_TYPE_CARD],
  530. ['status' => StatusEnum::ENABLED],
  531. ],
  532. 'limit' => 5,
  533. 'group' => 'user_id',
  534. 'with' => ['user'],
  535. 'order' => 'id desc',
  536. 'select'=> 'id, user_id, created_at'
  537. ]);
  538. // }
  539. $card_info['config'] = [
  540. 'company_name_show_label' => $setting['company_name_show_label'] ?? '公司名称',
  541. 'company_address_show_label' => $setting['company_address_show_label'] ?? '公司地址',
  542. 'add_command_show_title' => $add_command_show_title,
  543. ];
  544. }
  545. return $this->success('操作成功', $card_info);
  546. }
  547. }
  548. /**
  549. * 海报接口
  550. *
  551. * @author hpei
  552. * @param string keyword
  553. * @return array|mixed
  554. */
  555. public function actionPoster() {
  556. if (Yii::$app->request->isGet) {
  557. $params = Yii::$app->request->get();
  558. $res = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic');
  559. $carduser = BusinessCard::getOne([['id'=>$params['id'],'status' =>StatusEnum::ENABLED]],true);
  560. $data['content'] = $res['content'];
  561. $list['name'] = $carduser['nickname'];
  562. $list['id'] = $carduser['id'];
  563. $list['head'] = $carduser['avatar_url'];
  564. $list['list'][] = $data;
  565. $list['qrcode_id'] = CardEnums::SHARE_TYPE;
  566. return $this->success('操作成功',$list);
  567. }
  568. }
  569. /**
  570. * 名片列表
  571. * @author hpei
  572. * @param string $keyword
  573. * @return array
  574. */
  575. public function actionCardList() {
  576. if (Yii::$app->request->isGet) {
  577. $params = Yii::$app->request->get();
  578. $where[] = ['=', 'mall_id', $this->mall_id];
  579. $where[] = ['<>','status', StatusEnum::DELETE];
  580. if (isset($params['department_id']) && !empty($params['department_id'])) {
  581. }
  582. //添加判断条件,管理层可以看自己部门的信息,非管理层只可以看自己推荐人的信息
  583. $carduser = BusinessCardUser::getOne([['mall_id' => $this->mall_id,'user_id' => $this->user_id,'status' =>StatusEnum::ENABLED]]);
  584. $card = BusinessCard::getOne([['mall_id' => $this->mall_id,'user_id' => $this->user_id,'status' =>StatusEnum::ENABLED]]);
  585. if($carduser['role_type'] == CardEnums::ROLE_TYPE_STAFF || !$carduser['role_type'] && $card){
  586. $where[] = ['parent_id' => $this->user_id];
  587. }else if($carduser['role_type'] == CardEnums::ROLE_TYPE_LEADER){
  588. if($carduser['department_id']){
  589. //分组搜索
  590. if (isset($params['department_id']) && !empty($params['department_id']) && $params['department_id'] !=-1) {
  591. $where[] = ['position_id' => $params['department_id']];
  592. }
  593. $where[] = ['department_id' => $carduser['department_id']];
  594. $position_params['where'][] = ['=', 'parent_id',$carduser['department_id']];
  595. }
  596. }else{
  597. //分组搜索
  598. if (isset($params['department_id']) && !empty($params['department_id']) && $params['department_id'] !=-1) {
  599. $where[] = ['department_id' => $params['department_id']];
  600. }
  601. $position_params['where'][] = ['=', 'parent_id', '0'];
  602. }
  603. $position_params['where'][] = ['=', 'mall_id', $this->mall_id];
  604. $position_params['where'][] = ['=', 'status' , StatusEnum::ENABLED];
  605. $position_params['order'] = 'sort desc';
  606. $position_data = BusinessCardPosition::lists($position_params,true);
  607. $position_data_all[0]['id'] = -1;
  608. $position_data_all[0]['name'] = "全部";
  609. $list = BusinessCard::getCardList($where,'share_num desc');
  610. $list['is_create'] = (bool)BusinessCard::count(['where' => [['=', 'user_id', $this->user_id], ['=', 'status', StatusEnum::ENABLED]]]);
  611. $list['detail_data']['user_role'] = empty($carduser) ? 0 : $carduser['role_type'];
  612. $list['position'] = array_merge($position_data_all,$position_data);
  613. return $this->success('操作成功', $list);
  614. }
  615. }
  616. /**
  617. * boss的优秀列表
  618. * @author ltm
  619. * @param string $keyword
  620. * @return array
  621. */
  622. public function actionBossGoods() {
  623. if (Yii::$app->request->isGet) {
  624. $params = Yii::$app->request->get();
  625. $carduser = BusinessCardUser::getOne([['mall_id' => $this->mall_id,'user_id' => $this->user_id,'status' =>StatusEnum::ENABLED]]);
  626. if($carduser['role_type'] == CardEnums::ROLE_TYPE_BOSS){//老板
  627. //分组搜索
  628. if (isset($params['department_id']) && !empty($params['department_id']) && $params['department_id'] !=-1) {
  629. $user_ids = BusinessCardUser::find()->select("user_id")->where(['mall_id' => $this->mall_id])
  630. ->andWhere(['=', 'department_id', $params['department_id']])->asArray()->column();
  631. $where[] = ['in', 'user_id', $user_ids];
  632. }
  633. $position_params['where'][] = ['=', 'parent_id', '0'];
  634. }else{
  635. if($carduser['role_type'] == CardEnums::ROLE_TYPE_STAFF || !$carduser['role_type']){
  636. $where[] = ['parent_id' => $this->user_id];
  637. }else if($carduser['role_type'] == CardEnums::ROLE_TYPE_LEADER){
  638. //分组搜索
  639. if (isset($params['department_id']) && !empty($params['department_id']) && $params['department_id'] !=-1) {
  640. $user_ids = BusinessCardUser::find()->select("user_id")->where(['mall_id' => $this->mall_id])
  641. ->andWhere(['=', 'department_id', $carduser['department_id']])
  642. ->andWhere(['=', 'position_id', $params['department_id']])
  643. ->asArray()->column();
  644. }else{
  645. $user_ids = BusinessCardUser::find()->select("user_id")->where(['mall_id' => $this->mall_id])
  646. ->andWhere(['=', 'department_id', $carduser['department_id']])->asArray()->column();
  647. }
  648. $where[] = ['in', 'user_id', $user_ids];
  649. $position_params['where'][] = ['=', 'parent_id',$carduser['department_id']];
  650. }
  651. }
  652. //员工和主管看到得信息
  653. $with = ['goods'];
  654. $where[] = ['=', 'mall_id', $this->mall_id];
  655. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  656. $order = 'total_views desc';
  657. $goodsparams = array('where' => $where,'with' => $with,'group'=>'goods_id','order'=>$order);
  658. $goodsparams['select'] = 'goods_id,max(total_views) as total_views,count(user_id) as total_visitors';
  659. $list = BusinessCardGoodsStatistics::listPage($goodsparams,false,true);
  660. $position_params['where'][] = ['=', 'mall_id', $this->mall_id];
  661. $position_params['where'][] = ['=', 'status' , StatusEnum::ENABLED];
  662. $position_params['order'] = 'sort desc';
  663. $position_data = BusinessCardPosition::lists($position_params,true);
  664. $position_data_all[0]['id'] = -1;
  665. $position_data_all[0]['name'] = "全部";
  666. $list['detail_data']['user_role'] = empty($carduser) ? 0 : $carduser['role_type'];
  667. $list['position'] = array_merge($position_data_all,$position_data);
  668. return $this->success('操作成功', $list);
  669. }
  670. }
  671. /**
  672. * 雷达优秀列表
  673. * @author ltm
  674. * @param string $keyword
  675. * @return array
  676. */
  677. public function actionSell() {
  678. if (Yii::$app->request->isGet) {
  679. $params = Yii::$app->request->get();
  680. $params['where'][] = ['mall_id' =>$this->mall_id];
  681. $params['where'][] = ['status' =>StatusEnum::ENABLED];
  682. //添加判断条件,管理层可以看自己部门的信息,非管理层只可以看自己推荐人的信息
  683. $carduser = BusinessCardUser::getOne([['mall_id' => $this->mall_id,'user_id' => $this->user_id,'status' =>StatusEnum::ENABLED]]);
  684. if($carduser['role_type'] == CardEnums::ROLE_TYPE_STAFF || !$carduser['role_type']){ // 员工
  685. $params['where'][] = ['parent_id' => $this->user_id];
  686. }else if($carduser['role_type'] == CardEnums::ROLE_TYPE_LEADER){ // 主管
  687. if($carduser['department_id']){
  688. //分组搜索
  689. if (isset($params['department_id']) && !empty($params['department_id']) && $params['department_id'] !=-1) {
  690. $params['where'][] = ['position_id' => $params['department_id']];
  691. }
  692. $params['where'][] = ['department_id' => $carduser['department_id']];
  693. $position_params['where'][] = ['=', 'parent_id',$carduser['department_id']];
  694. }else{
  695. return $this->error('请联系管理先设置部门再查看');
  696. }
  697. }else{ // 老板
  698. //分组搜索
  699. if (isset($params['department_id']) && !empty($params['department_id']) && $params['department_id'] !=-1) {
  700. $params['where'][] = ['department_id' => $params['department_id']];
  701. }
  702. $position_params['where'][] = ['=', 'parent_id', '0'];
  703. }
  704. $params['order'] = 'share_num desc';
  705. $params['limit'] = $params['limit'] ?? 5;
  706. $params['with'] = ['user', 'card' => function($query) {
  707. $query->where(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->select('user_id, browse_num');
  708. }];
  709. $list = BusinessCardUser::listPage($params);
  710. //分组搜索
  711. $position_params['where'][] = ['=', 'mall_id', $this->mall_id];
  712. $position_params['where'][] = ['=', 'status' , StatusEnum::ENABLED];
  713. $position_params['order'] = 'sort desc';
  714. $position_data = BusinessCardPosition::lists($position_params);
  715. $position_data_all[0]['id'] = -1;
  716. $position_data_all[0]['name'] = "全部";
  717. $list['detail_data']['user_role'] = empty($carduser) ? 0 : $carduser['role_type'];
  718. $list['position'] = array_merge($position_data_all,$position_data);
  719. return $this->success('操作成功', $list);
  720. }
  721. }
  722. /**
  723. * 获取部门列表
  724. * @author ltm
  725. * @param string $keyword
  726. * @return array
  727. */
  728. public function actionPosition() {
  729. if (Yii::$app->request->isGet) {
  730. //分组搜索
  731. $carduser = BusinessCardUser::getOne([['mall_id' => $this->mall_id,'user_id' => $this->user_id,'status' =>StatusEnum::ENABLED]]);
  732. $position_params['where'][] = ['=', 'mall_id', $this->mall_id];
  733. $position_params['where'][] = ['=', 'status' , StatusEnum::ENABLED];
  734. $position_params['order'] = 'sort desc';
  735. $position_data = BusinessCardPosition::lists($position_params,true);
  736. $position_data_all[0]['id'] = -1;
  737. $position_data_all[0]['name'] = "全部";
  738. $list['detail_data']['user_role'] = empty($carduser) ? 0 : $carduser['role_type'];
  739. $list['position'] = array_merge($position_data_all,$position_data);
  740. return $this->success('操作成功', $list);
  741. }
  742. }
  743. /**
  744. * 商品列表
  745. * @author ltm
  746. * @param string $keyword
  747. * @return array
  748. */
  749. public function actionGoods() {
  750. if (Yii::$app->request->isGet) {
  751. $params = Yii::$app->request->get();
  752. $where[] = ['parent_id' => $this->user_id];
  753. //员工和主管看到得信息
  754. $with = ['goods'];
  755. $where[] = ['=', 'mall_id', $this->mall_id];
  756. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  757. $order = 'total_views desc';
  758. $goodsparams = array('where' => $where,'with' => $with,'group'=>'goods_id','order'=>$order);
  759. $goodsparams['select'] = 'goods_id,max(total_views) as total_views,count(user_id) as total_visitors';
  760. $list = BusinessCardGoodsStatistics::listPage($goodsparams,false,true);
  761. $position_params['where'][] = ['=', 'mall_id', $this->mall_id];
  762. $position_params['where'][] = ['=', 'status' , StatusEnum::ENABLED];
  763. $position_params['order'] = 'sort desc';
  764. $position_data = BusinessCardPosition::lists($position_params,true);
  765. $position_data_all[0]['id'] = -1;
  766. $position_data_all[0]['name'] = "全部";
  767. $list['detail_data']['user_role'] = empty($carduser) ? 0 : $carduser['role_type'];
  768. $list['position'] = array_merge($position_data_all,$position_data);
  769. return $this->success('操作成功', $list);
  770. }
  771. }
  772. /**
  773. * 点赞&取消点赞
  774. * @author hpei
  775. * @param int $card_id
  776. * @return mixed
  777. */
  778. public function actionLike() {
  779. if (Yii::$app->request->isPost) {
  780. $card_id = Yii::$app->request->post('card_id', 0);
  781. $where = [['=', 'id', $card_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
  782. $card_info = BusinessCard::getCardInfo($where, false);
  783. if (empty($card_info)) return $this->error('卡片不存在');
  784. Statistics::handler(['item_id' => $card_id,'card_user_id' =>$card_info['user_id'], 'user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'log_type' => CardEnums::LOG_TYPE_LIKE]);
  785. //$res = Statistics::async_handle(['item_id' => $card_id,'card_user_id' =>$card_info['user_id'], 'user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'log_type' => CardEnums::LOG_TYPE_LIKE]);
  786. //var_dump($res);
  787. return $this->success('操作成功');
  788. }
  789. }
  790. /**
  791. * 浏览记录
  792. * @return mixed
  793. */
  794. public function actionBrowsingHistory()
  795. {
  796. if (Yii::$app->request->isGet) {
  797. $params = Yii::$app->request->get();
  798. // 检查提交的参数
  799. $res = Yii::$app->services->validate->validate($params,[
  800. [['keyword'], 'string'],
  801. ]);
  802. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  803. $where[] = ['=', 'mall_id', $this->mall_id];
  804. $where[] = ['<>','status', StatusEnum::DELETE];
  805. $position_params['where'][] = ['=', 'mall_id', $this->mall_id];
  806. $position_params['where'][] = ['=', 'status' , StatusEnum::ENABLED];
  807. $position_params['order'] = 'sort desc';
  808. $position_data = BusinessCardPosition::lists($position_params,true);
  809. $position_data_all[0]['id'] = -1;
  810. $position_data_all[0]['name'] = "全部";
  811. $cWhere = [
  812. ['mall_id' => $this->mall_id],
  813. ['user_id' => $this->user_id],
  814. ['status' => StatusEnum::ENABLED],
  815. ['log_type' => CardEnums::LOG_TYPE_CARD],
  816. ];
  817. if (!empty($params['keyword'])) {
  818. $cWhere[] = ['or', ['like', 'nickname', $params['keyword']],
  819. ['like', 'mobile', $params['keyword']],
  820. ['like', 'email', $params['keyword']]];
  821. }
  822. // 获取浏览过的数据
  823. $card_ids = BusinessCardCustomerLog::listPage([
  824. 'where' => $cWhere,
  825. 'order' => 'id desc',
  826. 'select'=> 'item_id',
  827. 'limit' => 10
  828. ]);
  829. $list = [];
  830. if (!empty($card_ids['list'])) {
  831. $where[] = ["id" => array_column($card_ids['list'], "item_id")];
  832. $list = BusinessCard::getCardList($where,'share_num desc');
  833. }
  834. $list['is_create'] = (bool)BusinessCard::count(['where' => [['=', 'user_id', $this->user_id], ['=', 'status', StatusEnum::ENABLED]]]);
  835. $list['detail_data']['user_role'] = empty($carduser) ? 0 : $carduser['role_type'];
  836. $list['position'] = array_merge($position_data_all,$position_data);
  837. return $this->success('操作成功', $list);
  838. }
  839. }
  840. /**
  841. * 我的页面数据
  842. * @return mixed|null
  843. */
  844. public function actionInfo()
  845. {
  846. $ret['card'] = BusinessCard::getCardInfo([
  847. // 'where' => [
  848. ['mall_id' => $this->mall_id],
  849. ['user_id' => $this->user_id],
  850. ['status' => StatusEnum::ENABLED],
  851. // ],
  852. // 'select'=> 'id, nickname, avatar_url, address, company, tags, images, department_id, position_id',
  853. ]);
  854. if (empty($ret['card'])) return $this->error('请先创建名片', [], 4);
  855. $today_browse_num = BusinessCardCustomerLog::find()
  856. ->where(['mall_id' => $this->mall_id])
  857. ->andWhere(['item_id' => $ret['card']['id']])
  858. ->andWhere(['log_type' => CardEnums::LOG_TYPE_CARD])
  859. ->andWhere(['>=', 'created_at', strtotime(date('Y-m-d'))])
  860. ->count();
  861. $share_num = BusinessCardCustomerLog::find()
  862. ->where(['mall_id' => $this->mall_id])
  863. ->where(['user_id' => $this->user_id])
  864. ->andWhere(['log_type' => CardEnums::LOG_TYPE_SHARE])
  865. ->count();
  866. // 数据
  867. $ret['data'] = [
  868. 'today_browse_num' => $today_browse_num,
  869. 'browse_num' => $ret['card']['browse_num'] + $today_browse_num, // 今天的量
  870. 'share_num' => $ret['card']['share_num'] + $share_num, // 被分享+本人分享
  871. ];
  872. return $this->success("获取成功", $ret);
  873. }
  874. /**
  875. * 最近访客记录
  876. * @return mixed|null
  877. */
  878. public function actionRecentVisitors()
  879. {
  880. $get = \Yii::$app->request->get();
  881. $id = BusinessCard::find()
  882. ->where(['mall_id' => $this->mall_id])
  883. ->andWhere(['status' => StatusEnum::ENABLED])
  884. ->andWhere(['user_id' => $this->user_id])
  885. ->select('id')
  886. ->scalar();
  887. $keyword = $get['keyword'];
  888. if (empty($id)) return $this->error('请先创建名片', [], 4);
  889. $where = [
  890. 'alias' => 'bc',
  891. 'join' =>[['LEFT JOIN', User::tableName() . ' u', "u.id = bc.user_id"]],
  892. 'where' => [
  893. ['bc.mall_id' => $this->mall_id],
  894. ['bc.log_type' => CardEnums::LOG_TYPE_CARD],
  895. ['bc.item_id' => $id],
  896. ['<>', 'bc.user_id', $this->user_id],
  897. ],
  898. 'order' => 'bc.id desc',
  899. 'select'=> 'bc.id, bc.log_type, bc.created_at, bc.user_id, u.avatar_url, u.mobile, u.nickname, u.username',
  900. // 'with' => ['user'],
  901. 'limit' => $get['limit'] ?? 10,
  902. 'group' => 'bc.user_id',
  903. // 'joinWith' =>['user' => function ($query) {
  904. // $query->from(['u' => User::tableName()]);
  905. // }]
  906. ];
  907. if (!empty($keyword)) {
  908. $where['where'][] = ['or',['like', 'u.nickname', $keyword], ['like', 'u.id', $keyword], ['like', 'u.mobile', $keyword]];
  909. }
  910. // print_r($where);exit;
  911. $ret = BusinessCardCustomerLog::listPage($where);
  912. if (!empty($ret['list'])) {
  913. foreach ($ret['list'] as &$item) {
  914. $item['created_at'] = date("Y/m/d", $item['created_at']);
  915. }
  916. }
  917. return $this->success('获取成功', $ret);
  918. }
  919. /**
  920. * 校验卡片密钥
  921. * @return mixed|void|null
  922. * @throws \Exception
  923. */
  924. public function actionValidCardToken()
  925. {
  926. $params = Yii::$app->request->post();
  927. // 检查提交的参数
  928. $res = Yii::$app->services->validate->validate($params,[
  929. [['card_token'], 'string'],
  930. [['card_token'], 'required'],
  931. ]);
  932. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  933. $card_token = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic.card_token');
  934. if (!empty($card_token) && (string)$params['card_token'] != $card_token) return $this->error('名片密令错误');
  935. return $this->success('校验通过');
  936. }
  937. public function actionConfig()
  938. {
  939. $config = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic');
  940. return $this->success('success', [
  941. 'is_not_forced_login' => $config['is_not_forced_login'] ?? 0,
  942. 'is_show_create_btn' => $config['is_show_create_btn'] ?? 0,
  943. ]);
  944. }
  945. public function actionIsCreated()
  946. {
  947. $where = [['=', 'user_id', $this->user_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
  948. $find = BusinessCard::find();
  949. BusinessCard::paramPack(compact('where'), $find);
  950. return $this->success('success', [
  951. 'is_exists' => $find->exists()
  952. ]);
  953. }
  954. //获取人脉配置数据
  955. public function actionGetNetworkingConfig()
  956. {
  957. $request = Yii::$app->request;
  958. try {
  959. if ($request->isGet) {
  960. $params = $request->get();
  961. $info = (new TagService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id]))->getNetWorkingConfig($params);
  962. return $this->success('获取成功', $info);
  963. }
  964. } catch (\Exception $e) {
  965. return $this->error($e->getMessage());
  966. }
  967. }
  968. //获取名片列表数据
  969. public function actionGetNetworkingList()
  970. {
  971. $request = Yii::$app->request;
  972. try {
  973. if ($request->isPost) {
  974. $post = $request->post();
  975. $res = Yii::$app->services->validate->validate($post, [
  976. [['name'], 'string'],
  977. [['level_status', 'distance', 'sex', 'marry_status', 'page', 'limit'], 'integer'],
  978. [['latitude', 'longitude'], 'number'],
  979. [['industry', 'identity_tags', 'zodiac'], 'safe'],
  980. ]);
  981. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  982. $info = (new TagService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id]))->getNetworkingList($post);
  983. return $this->success('获取成功', $info);
  984. }
  985. } catch (\Exception $e) {
  986. return $this->error($e->getMessage());
  987. }
  988. }
  989. public function actionDelUserTag()
  990. {
  991. $request = Yii::$app->request;
  992. try {
  993. if ($request->isPost) {
  994. $post = $request->post();
  995. $res = Yii::$app->services->validate->validate($post, [
  996. [['id'], 'integer']
  997. ]);
  998. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  999. $res = (new TagService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id]))->delUserTag($post);
  1000. return $this->success('操作成功');
  1001. }
  1002. } catch (\Exception $e) {
  1003. return $this->error($e->getMessage());
  1004. }
  1005. }
  1006. }