// +---------------------------------------------------------------------- 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); } }