123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services\user\member;
- use app\dao\user\SecurityCodeRecordDao;
- use app\dao\user\SecurityCategoryDao;
- use app\services\user\UserServices;
- use app\services\user\member\SecurityCategoryServices;
- use app\services\user\UserBillServices;
- use app\services\BaseServices;
- use crmeb\exceptions\AdminException;
- use crmeb\exceptions\ApiException;
- /**
- * Class MemberRightServices
- * @package app\services\user
- */
- class SecurityCodeRecordServices extends BaseServices
- {
- /**
- * MemberCardServices constructor.
- * @param MemberRightDao $memberCardDao
- */
- public function __construct(SecurityCodeRecordDao $securityCodeRecordDao)
- {
- $this->dao = $securityCodeRecordDao;
- }
- /**
- * @param array $where
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getSearchList(array $where = [])
- {
- [$page, $limit] = $this->getPageValue();
- $list = $this->dao->getSearchList($where, $page, $limit);
- $userServices = app()->make(UserServices::class);
- foreach ($list as &$item) {
- $item['price'] = $item['category']['price'] ?? '';
- $item['point'] = $item['category']['point'] ?? '';
- $item['category_name'] = $item['category']['name'] ?? '';
- $item['category_status'] = $item['category']['status'] ?? '';
- $item['scan_name'] = '-';
- $item['city_name'] = '-';
- $item['store_name'] = '-';
- $item['scan_phone'] = '-';
- if($item['scan_uid']){
- $userInfo = $userServices->getUserInfo((int)$item['scan_uid']);
- $item['scan_name'] = $userInfo['nickname']."-".$userInfo['phone'];
- $item['city_name'] = $userInfo['city_name'] ?? '';
- $item['store_name'] = $userInfo['store_name'] ?? '';
- $item['scan_phone'] = $userInfo['phone'] ?? '';
- }
- unset($item['category']);
- }
- $count = $this->dao->count($where);
- return compact('list', 'count');
- }
- /**
- * @param array $where
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getSearchAllList(array $where = [])
- {
- [$page, $limit] = $this->getPageValue();
- $list = $this->dao->getSearchList($where);
- $userServices = app()->make(UserServices::class);
- foreach ($list as &$item) {
- $item['price'] = $item['category']['price'] ?? '';
- $item['point'] = $item['category']['point'] ?? '';
- $item['category_name'] = $item['category']['name'] ?? '';
- $item['category_status'] = $item['category']['status'] ?? '';
- $item['scan_name'] = '-';
- $item['city_name'] = '-';
- $item['store_name'] = '-';
- $item['scan_phone'] = '-';
- if($item['scan_uid']){
- $userInfo = $userServices->getUserInfo((int)$item['scan_uid']);
- $item['scan_name'] = $userInfo['nickname']."-".$userInfo['phone'];
- $item['city_name'] = $userInfo['city_name'] ?? '';
- $item['store_name'] = $userInfo['store_name'] ?? '';
- $item['scan_phone'] = $userInfo['phone'] ?? '';
- }
- unset($item['category']);
- }
- $count = $this->dao->count($where);
- return compact('list', 'count');
- }
- /**获取会员卡api接口
- * @return mixed
- */
- public function getOneByUrl(array $where)
- {
- return $this->dao->getOneByUrl($where);
- }
-
- /**
- * 领取积分
- */
- public function receiveSecurityCodePoint(int $uid, array $data)
- {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- //检测用户是否存在
- $user = $userServices->getUserInfo($uid);
- if (!$user) {
- throw new ApiException(100026);
- }
- $codeRecord = $this->dao->getOne($data);
- if(!$codeRecord){
- throw new ApiException(600001);
- }
- if($codeRecord['status']==2){
- throw new ApiException(600003);
- }
- $categoryServices = app()->make(SecurityCategoryServices::class);
- $cateInfo = $categoryServices->getOne(['id' => $codeRecord['category_id']]);
-
- $codeRecord['status'] = 2;
- $codeRecord['scan_uid'] = $uid;
- $codeRecord['scan_time'] = time();
- $point = $cateInfo['point'];
-
- $data = [];
- $data['uid'] = $uid;
- $data['link_id'] = $codeRecord['id'];
- $data['title'] = "扫防伪码领积分";
- $data['number'] = $cateInfo['point'];
- $data['add_time'] = time();
- /** @var UserBillServices $userBill */
- $userBill = app()->make(UserBillServices::class);
- $data['mark'] = "扫防伪码领取了".$point."积分";
- //增加数据
- $this->transaction(function () use ($uid, $userBill, $codeRecord, $data, $user, $point) {
- //保存记录
- $this->dao->update($codeRecord['id'], ['status' => 2, 'scan_uid' => $uid, 'scan_time' => time()]);
- $data['balance'] = (int)$user['integral'] + (int)$point;
- $userBill->incomeIntegral($user['uid'], 'system_add', $data);
- //保存积分
- $user->integral = (int)$user->integral + (int)$point;
- if (!$user->save()) {
- throw new ApiException(400692);
- }
- });
- //检测会员等级
- try {
- //用户升级事件
- event('UserLevelListener', [$uid]);
- } catch (\Throwable $e) {
- Log::error('会员等级升级失败,失败原因:' . $e->getMessage());
- }
- return $point;
- }
- /**
- * 编辑保存
- * @param int $id
- * @param array $data
- */
- public function save(int $id, array $data)
- {
- if (!$data['category_id']) throw new AdminException(400630);
- if (!$data['security_code']) throw new AdminException(400630);
- if (!$data['wx_url']) throw new AdminException(400630);
- //TODO $save没有使用
- if ($id>0) {
- $data['update_time'] = time();
- return $this->dao->update($id, $data);
- } else {
- $data['add_time'] = time();
- $data['status'] = 1;
- return $this->dao->save($data);
- }
- }
- /**
- * 获取单条信息
- * @param array $where
- * @return array|bool|\think\Model|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getOne(array $where)
- {
- if (!$where) return false;
- return $this->dao->getOne($where);
- }
- /**
- * 查看某权益是否开启
- * @param $rightType
- * @return bool
- */
- public function getMemberRightStatus($rightType)
- {
- if (!$rightType) return false;
- $status = $this->dao->value(['right_type' => $rightType], 'status');
- if ($status) return true;
- return false;
- }
- public function delCode(int $id)
- {
- if ($this->getById($id)) {
- if (!$this->dao->delete($id)) {
- throw new AdminException(100008);
- }
- }
- return true;
- }
- public function getById($id)
- {
- return $this->dao->get($id);
- }
- }
|