SecurityCodeRecordServices.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\user\member;
  12. use app\dao\user\SecurityCodeRecordDao;
  13. use app\dao\user\SecurityCategoryDao;
  14. use app\services\user\UserServices;
  15. use app\services\user\member\SecurityCategoryServices;
  16. use app\services\user\UserBillServices;
  17. use app\services\BaseServices;
  18. use crmeb\exceptions\AdminException;
  19. use crmeb\exceptions\ApiException;
  20. /**
  21. * Class MemberRightServices
  22. * @package app\services\user
  23. */
  24. class SecurityCodeRecordServices extends BaseServices
  25. {
  26. /**
  27. * MemberCardServices constructor.
  28. * @param MemberRightDao $memberCardDao
  29. */
  30. public function __construct(SecurityCodeRecordDao $securityCodeRecordDao)
  31. {
  32. $this->dao = $securityCodeRecordDao;
  33. }
  34. /**
  35. * @param array $where
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function getSearchList(array $where = [])
  42. {
  43. [$page, $limit] = $this->getPageValue();
  44. $list = $this->dao->getSearchList($where, $page, $limit);
  45. $userServices = app()->make(UserServices::class);
  46. foreach ($list as &$item) {
  47. $item['price'] = $item['category']['price'] ?? '';
  48. $item['point'] = $item['category']['point'] ?? '';
  49. $item['category_name'] = $item['category']['name'] ?? '';
  50. $item['category_status'] = $item['category']['status'] ?? '';
  51. $item['scan_name'] = '-';
  52. $item['city_name'] = '-';
  53. $item['store_name'] = '-';
  54. $item['scan_phone'] = '-';
  55. if($item['scan_uid']){
  56. $userInfo = $userServices->getUserInfo((int)$item['scan_uid']);
  57. $item['scan_name'] = $userInfo['nickname']."-".$userInfo['phone'];
  58. $item['city_name'] = $userInfo['city_name'] ?? '';
  59. $item['store_name'] = $userInfo['store_name'] ?? '';
  60. $item['scan_phone'] = $userInfo['phone'] ?? '';
  61. }
  62. unset($item['category']);
  63. }
  64. $count = $this->dao->count($where);
  65. return compact('list', 'count');
  66. }
  67. /**
  68. * @param array $where
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function getSearchAllList(array $where = [])
  75. {
  76. [$page, $limit] = $this->getPageValue();
  77. $list = $this->dao->getSearchList($where);
  78. $userServices = app()->make(UserServices::class);
  79. foreach ($list as &$item) {
  80. $item['price'] = $item['category']['price'] ?? '';
  81. $item['point'] = $item['category']['point'] ?? '';
  82. $item['category_name'] = $item['category']['name'] ?? '';
  83. $item['category_status'] = $item['category']['status'] ?? '';
  84. $item['scan_name'] = '-';
  85. $item['city_name'] = '-';
  86. $item['store_name'] = '-';
  87. $item['scan_phone'] = '-';
  88. if($item['scan_uid']){
  89. $userInfo = $userServices->getUserInfo((int)$item['scan_uid']);
  90. $item['scan_name'] = $userInfo['nickname']."-".$userInfo['phone'];
  91. $item['city_name'] = $userInfo['city_name'] ?? '';
  92. $item['store_name'] = $userInfo['store_name'] ?? '';
  93. $item['scan_phone'] = $userInfo['phone'] ?? '';
  94. }
  95. unset($item['category']);
  96. }
  97. $count = $this->dao->count($where);
  98. return compact('list', 'count');
  99. }
  100. /**获取会员卡api接口
  101. * @return mixed
  102. */
  103. public function getOneByUrl(array $where)
  104. {
  105. return $this->dao->getOneByUrl($where);
  106. }
  107. /**
  108. * 领取积分
  109. */
  110. public function receiveSecurityCodePoint(int $uid, array $data)
  111. {
  112. /** @var UserServices $userServices */
  113. $userServices = app()->make(UserServices::class);
  114. //检测用户是否存在
  115. $user = $userServices->getUserInfo($uid);
  116. if (!$user) {
  117. throw new ApiException(100026);
  118. }
  119. $codeRecord = $this->dao->getOne($data);
  120. if(!$codeRecord){
  121. throw new ApiException(600001);
  122. }
  123. if($codeRecord['status']==2){
  124. throw new ApiException(600003);
  125. }
  126. $categoryServices = app()->make(SecurityCategoryServices::class);
  127. $cateInfo = $categoryServices->getOne(['id' => $codeRecord['category_id']]);
  128. $codeRecord['status'] = 2;
  129. $codeRecord['scan_uid'] = $uid;
  130. $codeRecord['scan_time'] = time();
  131. $point = $cateInfo['point'];
  132. $data = [];
  133. $data['uid'] = $uid;
  134. $data['link_id'] = $codeRecord['id'];
  135. $data['title'] = "扫防伪码领积分";
  136. $data['number'] = $cateInfo['point'];
  137. $data['add_time'] = time();
  138. /** @var UserBillServices $userBill */
  139. $userBill = app()->make(UserBillServices::class);
  140. $data['mark'] = "扫防伪码领取了".$point."积分";
  141. //增加数据
  142. $this->transaction(function () use ($uid, $userBill, $codeRecord, $data, $user, $point) {
  143. //保存记录
  144. $this->dao->update($codeRecord['id'], ['status' => 2, 'scan_uid' => $uid, 'scan_time' => time()]);
  145. $data['balance'] = (int)$user['integral'] + (int)$point;
  146. $userBill->incomeIntegral($user['uid'], 'system_add', $data);
  147. //保存积分
  148. $user->integral = (int)$user->integral + (int)$point;
  149. if (!$user->save()) {
  150. throw new ApiException(400692);
  151. }
  152. });
  153. //检测会员等级
  154. try {
  155. //用户升级事件
  156. event('UserLevelListener', [$uid]);
  157. } catch (\Throwable $e) {
  158. Log::error('会员等级升级失败,失败原因:' . $e->getMessage());
  159. }
  160. return $point;
  161. }
  162. /**
  163. * 编辑保存
  164. * @param int $id
  165. * @param array $data
  166. */
  167. public function save(int $id, array $data)
  168. {
  169. if (!$data['category_id']) throw new AdminException(400630);
  170. if (!$data['security_code']) throw new AdminException(400630);
  171. if (!$data['wx_url']) throw new AdminException(400630);
  172. //TODO $save没有使用
  173. if ($id>0) {
  174. $data['update_time'] = time();
  175. return $this->dao->update($id, $data);
  176. } else {
  177. $data['add_time'] = time();
  178. $data['status'] = 1;
  179. return $this->dao->save($data);
  180. }
  181. }
  182. /**
  183. * 获取单条信息
  184. * @param array $where
  185. * @return array|bool|\think\Model|null
  186. * @throws \think\db\exception\DataNotFoundException
  187. * @throws \think\db\exception\DbException
  188. * @throws \think\db\exception\ModelNotFoundException
  189. */
  190. public function getOne(array $where)
  191. {
  192. if (!$where) return false;
  193. return $this->dao->getOne($where);
  194. }
  195. /**
  196. * 查看某权益是否开启
  197. * @param $rightType
  198. * @return bool
  199. */
  200. public function getMemberRightStatus($rightType)
  201. {
  202. if (!$rightType) return false;
  203. $status = $this->dao->value(['right_type' => $rightType], 'status');
  204. if ($status) return true;
  205. return false;
  206. }
  207. public function delCode(int $id)
  208. {
  209. if ($this->getById($id)) {
  210. if (!$this->dao->delete($id)) {
  211. throw new AdminException(100008);
  212. }
  213. }
  214. return true;
  215. }
  216. public function getById($id)
  217. {
  218. return $this->dao->get($id);
  219. }
  220. }