UserCardServices.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserCardDao;
  15. use app\services\store\SystemStoreStaffServices;
  16. use app\services\wechat\WechatCardServices;
  17. use app\services\wechat\WechatUserServices;
  18. use crmeb\services\wechat\OfficialAccount;
  19. /**
  20. * 用户领取卡券
  21. * Class UserCardServices
  22. * @package app\services\user
  23. * @mixin UserCardDao
  24. */
  25. class UserCardServices extends BaseServices
  26. {
  27. /**
  28. * 激活会员卡微信字段对应eb_user字段
  29. * @var string[]
  30. */
  31. protected $userField = [
  32. 'USER_FORM_INFO_FLAG_MOBILE' => 'phone',
  33. 'USER_FORM_INFO_FLAG_BIRTHDAY' => 'birthday'
  34. ];
  35. /**
  36. * UserCardServices constructor.
  37. * @param UserCardDao $dao
  38. */
  39. public function __construct(UserCardDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * 用户领取微信会员卡事件
  45. * @param $message
  46. * @return bool
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function userGetCard($message)
  52. {
  53. if (isset($message['CardId'])) {
  54. $card_id = $message['CardId'];
  55. $openid = $message['FromUserName'];
  56. /** @var WechatCardServices $wechatCardServices */
  57. $wechatCardServices = app()->make(WechatCardServices::class);
  58. $card = $wechatCardServices->getOne(['card_id' => $card_id]);
  59. if ($card) {
  60. $staffInfo = [];
  61. $uid = (int)$message['OuterId'];
  62. if ($uid) {
  63. try {
  64. /** @var SystemStoreStaffServices $staffServices */
  65. $staffServices = app()->make(SystemStoreStaffServices::class);
  66. $staffInfo = $staffServices->getStaffInfoByUid($uid);
  67. } catch (\Throwable $e) {
  68. $staffInfo = [];
  69. }
  70. }
  71. $data = [
  72. 'openid' => $openid,
  73. 'card_id' => $card_id,
  74. 'code' => $message['UserCardCode'],
  75. 'wechat_card_id' => $card['id'],
  76. 'staff_id' => $staffInfo['id'] ?? 0,
  77. 'store_id' => $staffInfo['store_id'] ?? 0,
  78. 'spread_uid' => $uid,
  79. 'add_time' => time()
  80. ];
  81. $userCard = $this->dao->getOne(['openid' => $openid, 'card_id' => $card_id, 'is_del' => 0]);
  82. if ($userCard) {
  83. $this->dao->update($userCard['id'], $data);
  84. } else {
  85. $this->dao->save($data);
  86. }
  87. }
  88. }
  89. return true;
  90. }
  91. /**
  92. * 激活会员事件
  93. * @param $message
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public function userSubmitCard($message)
  99. {
  100. if (isset($message['CardId'])) {
  101. $card_id = $message['CardId'];
  102. $openid = $message['FromUserName'];
  103. /** @var WechatCardServices $wechatCardServices */
  104. $wechatCardServices = app()->make(WechatCardServices::class);
  105. $card = $wechatCardServices->getOne(['card_id' => $card_id]);
  106. if ($card) {
  107. $userCard = $this->dao->getOne(['openid' => $openid, 'card_id' => $card_id, 'is_del' => 0]);
  108. if ($userCard && !$userCard['is_submit']) {
  109. $this->transaction(function () use ($openid, $userCard, $card_id) {
  110. $data = $this->getWechatCardInfo($card_id, $userCard['code']);
  111. /** @var WechatUserServices $wechatUserSerives */
  112. $wechatUserSerives = app()->make(WechatUserServices::class);
  113. $userInfo = $wechatUserSerives->saveUser($openid, $userCard['spread_uid'], $data['phone']);
  114. $this->dao->update($userCard['id'], ['uid' => $userInfo['uid'] ?? 0, 'is_submit' => 1, 'submit_time' => time()]);
  115. });
  116. }
  117. }
  118. }
  119. return true;
  120. }
  121. /**
  122. * 获取用户激活会员卡填写信息
  123. * @param string $card_id
  124. * @param string $code
  125. * @return array
  126. */
  127. public function getWechatCardInfo(string $card_id, string $code)
  128. {
  129. //获取用户激活填写字段信息
  130. $cart = OfficialAccount::getMemberCardUser($card_id, $code);
  131. $formList = $cart['user_info']['common_field_list'] ?? [];
  132. $userField = $this->userField;
  133. $fields = array_keys($userField);
  134. $data = [];
  135. foreach ($formList as $item) {
  136. if (in_array($item['name'], $fields)) {
  137. $data[$userField[$item['name']]] = $item['value'];
  138. }
  139. }
  140. return $data;
  141. }
  142. /**
  143. * 用户删除微信会员卡事件
  144. * @param $message
  145. * @return bool
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\DbException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. */
  150. public function userDelCard($message)
  151. {
  152. if (isset($message['CardId'])) {
  153. $card_id = $message['CardId'];
  154. $openid = $message['FromUserName'];
  155. /** @var WechatCardServices $wechatCardServices */
  156. $wechatCardServices = app()->make(WechatCardServices::class);
  157. $card = $wechatCardServices->getOne(['card_id' => $card_id]);
  158. if ($card) {
  159. $userCard = $this->dao->getOne(['openid' => $openid, 'card_id' => $card_id, 'is_del' => 0]);
  160. if ($userCard) {
  161. $this->dao->update($userCard['id'], ['is_del' => 1, 'del_time' => time()]);
  162. }
  163. }
  164. }
  165. return true;
  166. }
  167. /**
  168. * 门店推广统计详情列表
  169. * @param int $store_id
  170. * @param int $staff_id
  171. * @param array $time
  172. * @return array|array[]
  173. */
  174. public function time(int $store_id, int $staff_id, array $time = [])
  175. {
  176. if (!$time) {
  177. return [[], []];
  178. }
  179. [$start, $stop, $front, $front_stop] = $time;
  180. $where = ['store_id' => $store_id, 'is_submit' => 1];
  181. if ($staff_id) {
  182. $where['staff_id'] = $staff_id;
  183. }
  184. $frontPrice = $this->dao->count($where + ['time' => [$front, $front_stop]]);
  185. $nowPrice = $this->dao->count($where + ['time' => [$start, $stop]]);
  186. [$page, $limit] = $this->getPageValue();
  187. $list = $this->dao->getList($where + ['time' => [$start, $stop]], '*', ['user'], $page, $limit);
  188. foreach ($list as &$item) {
  189. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  190. }
  191. return [[$nowPrice, $frontPrice], $list];
  192. }
  193. /**
  194. * 获取会员卡列表
  195. * @param int $uid
  196. * @param UserServices $userServices
  197. * @return array
  198. * @throws \think\db\exception\DataNotFoundException
  199. * @throws \think\db\exception\DbException
  200. * @throws \think\db\exception\ModelNotFoundException
  201. */
  202. public function getCardList(array $where, string $field = '*', array $with = ['user'])
  203. {
  204. [$page, $limit] = $this->getPageValue();
  205. $list = $this->dao->getList($where, $field, $with, $page, $limit);
  206. $count = $this->dao->count($where);
  207. foreach ($list as &$item) {
  208. $item['submit_time'] = $item['submit_time'] ? date('Y-m-d H:i:s', $item['submit_time']) : '';
  209. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  210. }
  211. return compact('list', 'count');
  212. }
  213. }