SystemNotificationServices.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. declare (strict_types=1);
  12. namespace app\services\message;
  13. use app\dao\system\SystemNotificationDao;
  14. use app\services\BaseServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\FormBuilder as Form;
  17. use think\facade\Route as Url;
  18. /**
  19. * 消息管理类
  20. * Class SystemNotificationServices
  21. * @package app\services\system
  22. * @method value($where, $value) 条件获取某个字段的值
  23. */
  24. class SystemNotificationServices extends BaseServices
  25. {
  26. protected $messageData = [
  27. //短信验证码
  28. 'verify_code' => [
  29. ['label' => '验证码', 'value' => 'code'],
  30. ['label' => '有效时间', 'value' => 'time'],
  31. ],
  32. //用户登录
  33. 'login_success' => [
  34. ['label' => '用户昵称', 'value' => 'nickname'],
  35. ['label' => '用户电话', 'value' => 'phone'],
  36. ['label' => '上次登录时间', 'value' => 'last_time'],
  37. ['label' => '用户余额', 'value' => 'now_money'],
  38. ['label' => '用户佣金', 'value' => 'brokerage_price'],
  39. ['label' => '用户积分', 'value' => 'integral'],
  40. ['label' => '用户经验', 'value' => 'exp'],
  41. ['label' => '登录时间', 'value' => 'time'],
  42. ],
  43. //用户绑定关系
  44. 'spread_success' => [
  45. ['label' => '用户昵称', 'value' => 'nickname'],
  46. ['label' => '绑定时间', 'value' => 'time'],
  47. ],
  48. //未支付订单修改金额
  49. 'price_change_price' => [
  50. ['label' => '订单order_id', 'value' => 'order_id'],
  51. ['label' => '订单原金额', 'value' => 'pay_price'],
  52. ['label' => '修改后金额', 'value' => 'change_price'],
  53. ],
  54. //订单支付成功
  55. 'order_pay_success' => [
  56. ['label' => '用户uid', 'value' => 'uid'],
  57. ['label' => '订单order_id', 'value' => 'order_id'],
  58. ['label' => '用户名称', 'value' => 'real_name'],
  59. ['label' => '用户电话', 'value' => 'user_phone'],
  60. ['label' => '用户地址', 'value' => 'user_address'],
  61. ['label' => '商品总数', 'value' => 'total_num'],
  62. ['label' => '支付金额', 'value' => 'pay_price'],
  63. ['label' => '支付邮费', 'value' => 'pay_postage'],
  64. ['label' => '积分抵扣金额', 'value' => 'deduction_price'],
  65. ['label' => '优惠券抵扣金额', 'value' => 'coupon_price'],
  66. ['label' => '支付类型', 'value' => 'pay_type'],
  67. ['label' => '商品名称', 'value' => 'storeName'],
  68. ['label' => '下单时间', 'value' => 'time'],
  69. ],
  70. //订单快递发货
  71. 'order_express_success' => [
  72. ['label' => '用户uid', 'value' => 'uid'],
  73. ['label' => '订单order_id', 'value' => 'order_id'],
  74. ['label' => '用户名称', 'value' => 'real_name'],
  75. ['label' => '用户电话', 'value' => 'user_phone'],
  76. ['label' => '用户地址', 'value' => 'user_address'],
  77. ['label' => '商品总数', 'value' => 'total_num'],
  78. ['label' => '支付金额', 'value' => 'pay_price'],
  79. ['label' => '支付邮费', 'value' => 'pay_postage'],
  80. ['label' => '积分抵扣金额', 'value' => 'deduction_price'],
  81. ['label' => '优惠券抵扣金额', 'value' => 'coupon_price'],
  82. ['label' => '支付类型', 'value' => 'pay_type'],
  83. ['label' => '商品名称', 'value' => 'storeName'],
  84. ['label' => '快递公司', 'value' => 'delivery_name'],
  85. ['label' => '快递单号', 'value' => 'delivery_id'],
  86. ['label' => '发货时间', 'value' => 'time'],
  87. ],
  88. //订单配送员送货
  89. 'order_send_success' => [
  90. ['label' => '用户uid', 'value' => 'uid'],
  91. ['label' => '订单order_id', 'value' => 'order_id'],
  92. ['label' => '用户名称', 'value' => 'real_name'],
  93. ['label' => '用户电话', 'value' => 'user_phone'],
  94. ['label' => '用户地址', 'value' => 'user_address'],
  95. ['label' => '商品总数', 'value' => 'total_num'],
  96. ['label' => '支付金额', 'value' => 'pay_price'],
  97. ['label' => '支付邮费', 'value' => 'pay_postage'],
  98. ['label' => '积分抵扣金额', 'value' => 'deduction_price'],
  99. ['label' => '优惠券抵扣金额', 'value' => 'coupon_price'],
  100. ['label' => '支付类型', 'value' => 'pay_type'],
  101. ['label' => '商品名称', 'value' => 'storeName'],
  102. ['label' => '配送员姓名', 'value' => 'delivery_name'],
  103. ['label' => '配送员电话', 'value' => 'delivery_id'],
  104. ['label' => '送货时间', 'value' => 'time'],
  105. ],
  106. //订单收货
  107. 'order_take' => [
  108. ['label' => '用户uid', 'value' => 'uid'],
  109. ['label' => '订单order_id', 'value' => 'order_id'],
  110. ['label' => '用户名称', 'value' => 'real_name'],
  111. ['label' => '用户电话', 'value' => 'user_phone'],
  112. ['label' => '用户地址', 'value' => 'user_address'],
  113. ['label' => '商品总数', 'value' => 'total_num'],
  114. ['label' => '支付金额', 'value' => 'pay_price'],
  115. ['label' => '支付邮费', 'value' => 'pay_postage'],
  116. ['label' => '积分抵扣金额', 'value' => 'deduction_price'],
  117. ['label' => '优惠券抵扣金额', 'value' => 'coupon_price'],
  118. ['label' => '支付类型', 'value' => 'pay_type'],
  119. ['label' => '商品名称', 'value' => 'storeTitle'],
  120. ['label' => '配送员姓名', 'value' => 'delivery_name'],
  121. ['label' => '配送员电话', 'value' => 'delivery_id'],
  122. ['label' => '签收时间', 'value' => 'time'],
  123. ],
  124. //订单发起退款
  125. 'order_initiated_refund' => [
  126. ['label' => '用户uid', 'value' => 'uid'],
  127. ['label' => '订单order_id', 'value' => 'order_id'],
  128. ['label' => '用户名称', 'value' => 'real_name'],
  129. ['label' => '用户电话', 'value' => 'user_phone'],
  130. ['label' => '用户地址', 'value' => 'user_address'],
  131. ['label' => '商品总数', 'value' => 'total_num'],
  132. ['label' => '支付金额', 'value' => 'pay_price'],
  133. ['label' => '支付邮费', 'value' => 'pay_postage'],
  134. ['label' => '积分抵扣金额', 'value' => 'deduction_price'],
  135. ['label' => '优惠券抵扣金额', 'value' => 'coupon_price'],
  136. ['label' => '支付类型', 'value' => 'pay_type'],
  137. ],
  138. //订单成功退款
  139. 'order_refund_success' => [
  140. ['label' => '用户uid', 'value' => 'uid'],
  141. ['label' => '订单order_id', 'value' => 'order_id'],
  142. ['label' => '用户名称', 'value' => 'real_name'],
  143. ['label' => '用户电话', 'value' => 'user_phone'],
  144. ['label' => '用户地址', 'value' => 'user_address'],
  145. ['label' => '商品总数', 'value' => 'total_num'],
  146. ['label' => '支付金额', 'value' => 'pay_price'],
  147. ['label' => '支付邮费', 'value' => 'pay_postage'],
  148. ['label' => '积分抵扣金额', 'value' => 'deduction_price'],
  149. ['label' => '优惠券抵扣金额', 'value' => 'coupon_price'],
  150. ['label' => '支付类型', 'value' => 'pay_type'],
  151. ['label' => '退款理由类型', 'value' => 'refund_reason_wap'],
  152. ['label' => '退款理由', 'value' => 'refund_reason_wap_explain'],
  153. ['label' => '实际退款金额', 'value' => 'refund_price'],
  154. ],
  155. //订单拒绝退款
  156. 'order_refund_fail' => [
  157. ['label' => '用户uid', 'value' => 'uid'],
  158. ['label' => '退款金额', 'value' => 'refund_price'],
  159. ['label' => '拒绝退款理由', 'value' => 'refuse_reason'],
  160. ['label' => '拒绝时间', 'value' => 'time'],
  161. ],
  162. //用户充值
  163. 'recharge_success' => [
  164. ['label' => '用户uid', 'value' => 'uid'],
  165. ['label' => '用户昵称', 'value' => 'nickname'],
  166. ['label' => '用户电话', 'value' => 'phone'],
  167. ['label' => '充值金额', 'value' => 'price'],
  168. ['label' => '赠送金额', 'value' => 'give_price'],
  169. ['label' => '充值后用户余额', 'value' => 'now_money'],
  170. ['label' => '充值时间', 'value' => 'time'],
  171. ],
  172. //用户充值退款
  173. 'recharge_refund' => [
  174. ['label' => '用户uid', 'value' => 'uid'],
  175. ['label' => '用户昵称', 'value' => 'nickname'],
  176. ['label' => '用户电话', 'value' => 'phone'],
  177. ['label' => '退款金额', 'value' => 'price'],
  178. ['label' => '退款后用户余额', 'value' => 'now_money'],
  179. ['label' => '退款时间', 'value' => 'time'],
  180. ],
  181. //用户提现通过
  182. 'extract_success' => [
  183. ['label' => '用户uid', 'value' => 'uid'],
  184. ['label' => '用户昵称', 'value' => 'nickname'],
  185. ['label' => '用户电话', 'value' => 'phone'],
  186. ['label' => '提现金额', 'value' => 'price'],
  187. ['label' => '提现时间', 'value' => 'time'],
  188. ],
  189. //用户提现失败
  190. 'extract_fail' => [
  191. ['label' => '用户uid', 'value' => 'uid'],
  192. ['label' => '用户昵称', 'value' => 'nickname'],
  193. ['label' => '失败理由', 'value' => 'message'],
  194. ['label' => '提现金额', 'value' => 'price'],
  195. ['label' => '失败时间', 'value' => 'time'],
  196. ],
  197. //佣金到账
  198. 'brokerage_received' => [
  199. ['label' => '用户uid', 'value' => 'uid'],
  200. ['label' => '用户电话', 'value' => 'phone'],
  201. ['label' => '到账金额', 'value' => 'brokeragePrice'],
  202. ['label' => '商品名称', 'value' => 'goodsName'],
  203. ['label' => '商品金额', 'value' => 'goodsPrice'],
  204. ['label' => '到账时间', 'value' => 'time'],
  205. ],
  206. //积分到账
  207. 'point_received' => [
  208. ['label' => '用户uid', 'value' => 'uid'],
  209. ['label' => '用户电话', 'value' => 'phone'],
  210. ['label' => '积分数量', 'value' => 'give_integral'],
  211. ['label' => '商品名称', 'value' => 'storeTitle'],
  212. ['label' => '积分总数', 'value' => 'integral'],
  213. ['label' => '到账时间', 'value' => 'time'],
  214. ],
  215. ];
  216. /**
  217. * SystemNotificationServices constructor.
  218. * @param SystemNotificationDao $dao
  219. */
  220. public function __construct(SystemNotificationDao $dao)
  221. {
  222. $this->dao = $dao;
  223. }
  224. /**
  225. * 单个配置
  226. * @param int $where
  227. * @return array
  228. * @throws \think\db\exception\DataNotFoundException
  229. * @throws \think\db\exception\DbException
  230. * @throws \think\db\exception\ModelNotFoundException
  231. */
  232. public function getOneNotce(array $where)
  233. {
  234. return $this->dao->getOne($where);
  235. }
  236. /**
  237. * 后台获取列表
  238. * @param array $where
  239. * @return array
  240. * @throws \think\db\exception\DataNotFoundException
  241. * @throws \think\db\exception\DbException
  242. * @throws \think\db\exception\ModelNotFoundException
  243. */
  244. public function getNotList(array $where)
  245. {
  246. return $this->dao->getList($where);
  247. }
  248. /**
  249. * 添加自定义消息表单
  250. * @return array
  251. * @throws \FormBuilder\Exception\FormBuilderException
  252. * @author wuhaotian
  253. * @email 442384644@qq.com
  254. * @date 2024/2/19
  255. */
  256. public function getNotForm($id = 0)
  257. {
  258. if ($id) {
  259. $info = $this->dao->get($id);
  260. if ($info) $info = $info->toArray();
  261. } else {
  262. $info = [];
  263. }
  264. $data = [
  265. ['value' => 'login_success', 'label' => '用户登录成功场景'],
  266. ['value' => 'spread_success', 'label' => '绑定推广关系成功场景'],
  267. ['value' => 'price_change_price', 'label' => '未支付订单修改价格场景'],
  268. ['value' => 'order_pay_success', 'label' => '订单支付成功场景'],
  269. ['value' => 'order_express_success', 'label' => '订单快递发货成功场景'],
  270. ['value' => 'order_send_success', 'label' => '订单配送员开始送货场景'],
  271. ['value' => 'order_take', 'label' => '订单成功收货场景'],
  272. ['value' => 'order_initiated_refund', 'label' => '订单发起退款场景'],
  273. ['value' => 'order_refund_success', 'label' => '订单退款成功场景'],
  274. ['value' => 'order_refund_fail', 'label' => '订单退款失败场景'],
  275. ['value' => 'recharge_success', 'label' => '充值成功场景'],
  276. ['value' => 'recharge_refund', 'label' => '充值退款场景'],
  277. ['value' => 'extract_success', 'label' => '提现成功场景'],
  278. ['value' => 'extract_fail', 'label' => '提现失败场景'],
  279. ['value' => 'brokerage_received', 'label' => '佣金到账场景'],
  280. ['value' => 'point_received', 'label' => '积分到账场景'],
  281. ];
  282. $field = [];
  283. $field[] = Form::select('custom_trigger', '触发位置', $info['custom_trigger'] ?? '')->options($data);
  284. $field[] = Form::input('name', '名称', $info['name'] ?? '')->placeholder('请填写消息名称,例:支付成功消息');
  285. $field[] = Form::input('mark', '标识', $info['mark'] ?? '')->placeholder('请填写消息标识,使用英文和下划线,例:order_pay_success');
  286. return create_form('添加消息', $field, Url::buildUrl('/setting/notification/not_form_save/' . $id), 'POST');
  287. }
  288. /**
  289. * 保存自定义消息
  290. * @param $id
  291. * @param $data
  292. * @return bool
  293. * @author wuhaotian
  294. * @email 442384644@qq.com
  295. * @date 2024/2/20
  296. */
  297. public function notFormSave($id, $data)
  298. {
  299. if ($id) {
  300. $data['title'] = $data['name'];
  301. $res = $this->dao->update($id, $data);
  302. } else {
  303. $data['type'] = 3;
  304. $data['title'] = $data['name'];
  305. $data['is_system'] = $data['is_wechat'] = $data['is_routine'] = $data['is_sms'] = $data['is_ent_wechat'] = 2;
  306. $data['add_time'] = time();
  307. $res = $this->dao->save($data);
  308. }
  309. if ($res) return true;
  310. throw new AdminException(100006);
  311. }
  312. /**
  313. * 获取单条数据
  314. * @param array $where
  315. * @return array
  316. * @throws \think\db\exception\DataNotFoundException
  317. * @throws \think\db\exception\DbException
  318. * @throws \think\db\exception\ModelNotFoundException
  319. */
  320. public function getNotInfo(array $where)
  321. {
  322. $type = $where['type'];
  323. unset($where['type']);
  324. $info = $this->dao->getOne($where);
  325. if (!$info) return [];
  326. $info = $info->toArray();
  327. switch ($type) {
  328. case 'is_system':
  329. $info['content'] = $info['system_text'] ?? '';
  330. break;
  331. case 'is_sms':
  332. $info['content'] = $info['sms_text'];
  333. break;
  334. case 'is_wechat':
  335. $info['tempkey'] = $info['wechat_tempkey'] ?? '';
  336. $info['tempid'] = $info['wechat_tempid'] ?? '';
  337. $info['content'] = $info['wechat_content'] ?? '';
  338. $info['key_list'] = json_decode($info['wechat_data'], true) ?? [];
  339. break;
  340. case 'is_routine':
  341. $info['tempkey'] = $info['routine_tempkey'] ?? '';
  342. $info['tempid'] = $info['routine_tempid'] ?? '';
  343. $info['content'] = $info['routine_content'] ?? '';
  344. $info['key_list'] = json_decode($info['routine_data'], true) ?? [];
  345. break;
  346. case 'is_ent_wechat':
  347. $info['content'] = $info['ent_wechat_text'];
  348. break;
  349. }
  350. if ($info['type'] == 3) {
  351. $info['custom_variable'] = $this->messageData[$info['custom_trigger']];
  352. if (in_array($type, ['is_system', 'is_sms', 'is_ent_wechat'])) {
  353. foreach ($info['custom_variable'] as &$item) {
  354. $item['value'] = '{' . $item['value'] . '}';
  355. }
  356. }
  357. }
  358. return $info;
  359. }
  360. /**
  361. * 保存数据
  362. * @param array $data
  363. * @return bool|\crmeb\basic\BaseModel|null
  364. * @throws \think\db\exception\DataNotFoundException
  365. * @throws \think\db\exception\DbException
  366. * @throws \think\db\exception\ModelNotFoundException
  367. */
  368. public function saveData(array $data)
  369. {
  370. $type = $data['type'];
  371. $id = $data['id'];
  372. $info = $this->dao->get($id);
  373. if (!$info) {
  374. throw new AdminException(100026);
  375. }
  376. $res = null;
  377. switch ($type) {
  378. case 'is_system':
  379. $update = [];
  380. $update['name'] = $data['name'];
  381. $update['title'] = $data['title'];
  382. $update['is_system'] = $data['is_system'];
  383. $update['is_app'] = $data['is_app'];
  384. $update['system_title'] = $data['system_title'];
  385. $update['system_text'] = $data['system_text'];
  386. $res = $this->dao->update((int)$id, $update);
  387. break;
  388. case 'is_sms':
  389. $update = [];
  390. $update['name'] = $data['name'];
  391. $update['title'] = $data['title'];
  392. $update['is_sms'] = $data['is_sms'];
  393. $update['sms_id'] = $data['sms_id'];
  394. $update['sms_text'] = $data['sms_text'];
  395. $res = $this->dao->update((int)$id, $update);
  396. break;
  397. case 'is_wechat':
  398. $update['is_wechat'] = $data['is_wechat'];
  399. $update['wechat_tempid'] = $data['tempid'];
  400. $update['wechat_tempkey'] = $data['tempkey'];
  401. $update['wechat_content'] = $data['content'];
  402. $update['wechat_link'] = $data['wechat_link'];
  403. $update['wechat_to_routine'] = $data['wechat_to_routine'];
  404. $update['wechat_data'] = json_encode($data['key_list']);
  405. $res = $this->dao->update((int)$id, $update);
  406. break;
  407. case 'is_routine':
  408. $update['is_routine'] = $data['is_routine'];
  409. $update['routine_tempid'] = $data['tempid'];
  410. $update['routine_tempkey'] = $data['tempkey'];
  411. $update['routine_content'] = $data['content'];
  412. $update['routine_data'] = json_encode($data['key_list']);
  413. $update['routine_link'] = $data['routine_link'];
  414. $res = $this->dao->update((int)$id, $update);
  415. break;
  416. case 'is_ent_wechat':
  417. $update['name'] = $data['name'];
  418. $update['title'] = $data['title'];
  419. $update['is_ent_wechat'] = $data['is_ent_wechat'];
  420. $update['ent_wechat_text'] = $data['ent_wechat_text'];
  421. $update['url'] = $data['url'];
  422. $res = $this->dao->update((int)$id, $update);
  423. break;
  424. }
  425. return $res;
  426. }
  427. /**
  428. * 获取tempid
  429. * @param $type
  430. * @return array
  431. * @author: 吴汐
  432. * @email: 442384644@qq.com
  433. * @date: 2023/8/16
  434. */
  435. public function getTempId($type)
  436. {
  437. return $this->dao->getTempId($type);
  438. }
  439. /**
  440. * 获取tempkey
  441. * @param $type
  442. * @return array
  443. * @throws \think\db\exception\DataNotFoundException
  444. * @throws \think\db\exception\DbException
  445. * @throws \think\db\exception\ModelNotFoundException
  446. * @author: 吴汐
  447. * @email: 442384644@qq.com
  448. * @date: 2023/8/16
  449. */
  450. public function getTempKey($type)
  451. {
  452. return $this->dao->getTempKey($type);
  453. }
  454. }